Skip to content

Commit

Permalink
Merge pull request prettier#1224 from prettier/plugins
Browse files Browse the repository at this point in the history
Support passing rubyPlugins options
  • Loading branch information
kddnewton authored May 12, 2022
2 parents 8b7800e + 7bed720 commit 618f609
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 24 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ jobs:
ruby-version: ${{ matrix.ruby }}
- uses: actions/setup-node@v2
with:
node-version: 12.x
node-version: 16.x
cache: yarn
- run: yarn install --frozen-lockfile
- run: yarn test
Expand All @@ -46,7 +46,7 @@ jobs:
ruby-version: "3.0"
- uses: actions/setup-node@v2
with:
node-version: 12.x
node-version: 16.x
cache: yarn
- run: yarn install --frozen-lockfile
- run: yarn checkFormat
Expand Down Expand Up @@ -76,7 +76,7 @@ jobs:
- run: gem install syntax_tree syntax_tree-haml syntax_tree-rbs
- uses: actions/setup-node@v2
with:
node-version: 12.x
node-version: 16.x
cache: yarn
- run: yarn install --frozen-lockfile
- run: yarn pack
Expand Down Expand Up @@ -108,7 +108,7 @@ jobs:
ruby-version: "3.1"
- uses: actions/setup-node@v2
with:
node-version: 12.x
node-version: 16.x
cache: yarn
- run: yarn install --frozen-lockfile
- run: gem build -o prettier.gem
Expand Down
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,11 +134,12 @@ The `prettier` executable is now installed and ready for use:

Below are the options (from [`src/plugin.js`](src/plugin.js)) that `@prettier/plugin-ruby` currently supports:

| API Option | CLI Option | Default | Description |
| --------------- | ------------------ | :-----: | --------------------------------------------------------------------------------------------------- |
| `printWidth` | `--print-width` | `80` | Same as in Prettier ([see prettier docs](https://prettier.io/docs/en/options.html#print-width)). |
| `requirePragma` | `--require-pragma` | `false` | Same as in Prettier ([see prettier docs](https://prettier.io/docs/en/options.html#require-pragma)). |
| `tabWidth` | `--tab-width` | `2` | Same as in Prettier ([see prettier docs](https://prettier.io/docs/en/options.html#tab-width)). |
| API Option | CLI Option | Default | Description |
| --------------- | ------------------ | :-----: | --------------------------------------------------------------------------------------------------------------------------- |
| `printWidth` | `--print-width` | `80` | Same as in Prettier ([see prettier docs](https://prettier.io/docs/en/options.html#print-width)). |
| `requirePragma` | `--require-pragma` | `false` | Same as in Prettier ([see prettier docs](https://prettier.io/docs/en/options.html#require-pragma)). |
| `rubyPlugins` | `--ruby-plugins` | `""` | The comma-separated list of plugins to require. See [Syntax Tree](https://github.com/ruby-syntax-tree/syntax_tree#plugins). |
| `tabWidth` | `--tab-width` | `2` | Same as in Prettier ([see prettier docs](https://prettier.io/docs/en/options.html#tab-width)). |

Any of these can be added to your existing [prettier configuration
file](https://prettier.io/docs/en/configuration.html). For example:
Expand Down
20 changes: 12 additions & 8 deletions src/parseSync.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ function getInfoFilepath() {
// server with that filepath as an argument, then spawn another process that
// will read that information in order to enable us to connect to it in the
// spawnSync function.
function spawnServer() {
function spawnServer(opts) {
const tempDir = mkdtempSync(path.join(os.tmpdir(), "prettier-plugin-ruby-"));
const filepath = getInfoFilepath();

Expand Down Expand Up @@ -114,11 +114,15 @@ function spawnServer() {
};
}

const server = spawn("ruby", [serverRbPath, filepath], {
env: Object.assign({}, process.env, { LANG: getLang() }),
detached: true,
stdio: "inherit"
});
const server = spawn(
"ruby",
[serverRbPath, `--plugins=${opts.rubyPlugins}`, filepath],
{
env: Object.assign({}, process.env, { LANG: getLang() }),
detached: true,
stdio: "inherit"
}
);

server.unref();
process.on("exit", () => {
Expand Down Expand Up @@ -171,9 +175,9 @@ function runningInPnPZip() {
// like it) here since Prettier requires the results of `parse` to be
// synchronous and Node.js does not offer a mechanism for synchronous socket
// requests.
function parseSync(parser, source) {
function parseSync(parser, source, opts) {
if (!parserArgs) {
parserArgs = spawnServer();
parserArgs = spawnServer(opts);
}

const response = spawnSync(parserArgs.cmd, parserArgs.args, {
Expand Down
21 changes: 15 additions & 6 deletions src/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ const plugin = {
],
parsers: {
ruby: {
parse(text) {
return parseSync("ruby", text);
parse(text, _parsers, opts) {
return parseSync("ruby", text, opts);
},
astFormat: "ruby",
hasPragma(text) {
Expand All @@ -96,8 +96,8 @@ const plugin = {
}
},
rbs: {
parse(text) {
return parseSync("rbs", text);
parse(text, _parsers, opts) {
return parseSync("rbs", text, opts);
},
astFormat: "rbs",
hasPragma(text) {
Expand All @@ -111,8 +111,8 @@ const plugin = {
}
},
haml: {
parse(text) {
return parseSync("haml", text);
parse(text, _parsers, opts) {
return parseSync("haml", text, opts);
},
astFormat: "haml",
hasPragma(text) {
Expand Down Expand Up @@ -152,6 +152,15 @@ const plugin = {
}
}
},
options: {
rubyPlugins: {
type: "string",
category: "Ruby",
default: "",
description: "The comma-separated list of plugins to require",
since: "3.1.0"
}
},
defaultOptions: {
printWidth: 80,
tabWidth: 2
Expand Down
5 changes: 5 additions & 0 deletions src/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@
require "syntax_tree/haml"
require "syntax_tree/rbs"

# First, require all of the plugins that the user specified.
ARGV.shift[/^--plugins=(.*)$/, 1]
.split(",")
.each { |plugin| require "syntax_tree/#{plugin}" }

# Make sure we trap these signals to be sure we get the quit command coming from
# the parent node process
quit = false
Expand Down
2 changes: 1 addition & 1 deletion test/js/globalSetup.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ function globalSetup() {
const filepath = getInfoFilepath();
const server = spawn(
"ruby",
[path.join(__dirname, "../../src/server.rb"), filepath],
[path.join(__dirname, "../../src/server.rb"), "--plugins=", filepath],
{
env: Object.assign({}, process.env, { LANG: getLang() }),
detached: true,
Expand Down

0 comments on commit 618f609

Please sign in to comment.