Skip to content

Commit

Permalink
Handle Ruby 2.7 syntax (prettier#458)
Browse files Browse the repository at this point in the history
  • Loading branch information
kddnewton authored Jan 3, 2020
1 parent 43c601f commit 6ab852a
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 13 deletions.
16 changes: 16 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,22 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) a

## [Unreleased]

### Added

- Support for the `nokw_param` node for specifying when methods should no accept keywords, as in:

```ruby
def foo(**nil); end
```
- Support for the `args_forward` node for forwarding all types of arguments, as in:
```ruby
def foo(...)
bar(...)
end
```

## [0.17.0] - 2019-12-12

### Added
Expand Down
7 changes: 7 additions & 0 deletions src/nodes/args.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ module.exports = {
return "";
}

// Here we can skip the entire rest of the method by just checking if it's
// an args_forward node, as we're guaranteed that there are no other arg
// nodes.
if (path.getValue().body[0].type === "args_forward") {
return "(...)";
}

const { addTrailingCommas } = opts;
const { args, heredocs } = makeArgs(path, opts, print, 0);

Expand Down
5 changes: 2 additions & 3 deletions src/nodes/blocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const {
removeLines,
softline
} = require("../prettier");
const { empty, first, hasAncestor } = require("../utils");
const { empty, hasAncestor } = require("../utils");

const printBlock = (path, opts, print) => {
const [variables, statements] = path.getValue().body;
Expand Down Expand Up @@ -76,6 +76,5 @@ module.exports = {
},
brace_block: printBlock,
do_block: printBlock,
excessed_comma: empty,
number_arg: first
excessed_comma: empty
};
4 changes: 3 additions & 1 deletion src/nodes/params.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const { concat, group, join, line } = require("../prettier");
const { literal } = require("../utils");

const printGenericRestParam = symbol => (path, opts, print) =>
path.getValue().body[0]
Expand Down Expand Up @@ -53,7 +54,7 @@ const printParams = (path, opts, print) => {
}

if (kwargRest) {
parts.push(path.call(print, "body", 5));
parts.push(kwargRest === "nil" ? "**nil" : path.call(print, "body", 5));
}

if (block) {
Expand All @@ -79,6 +80,7 @@ const paramError = () => {
};

module.exports = {
args_forward: literal("..."),
kwrest_param: printGenericRestParam("**"),
rest_param: printGenericRestParam("*"),
params: printParams,
Expand Down
1 change: 1 addition & 0 deletions src/ripper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ def find_scanner_event(type, body = :any)
assoc_splat: [:@op, '**'],
arg_paren: :@lparen,
args_add_star: [:@op, '*'],
args_forward: [:@op, '...'],
begin: [:@kw, 'begin'],
blockarg: [:@op, '&'],
brace_block: :@lbrace,
Expand Down
4 changes: 0 additions & 4 deletions test/js/blocks.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,6 @@ describe("blocks", () => {

test("does not split up args inside pipes", () =>
expect(`loop do |${long} = 1, a${long} = 2|\nend`).toMatchFormat());

if (process.env.RUBY_VERSION >= "2.7") {
test("number args", () => expect("loop { @1 * 2 }").toMatchFormat());
}
});

describe("to_proc transform", () => {
Expand Down
19 changes: 14 additions & 5 deletions test/js/method.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,20 @@ describe("method", () => {
return expect(content).toMatchFormat();
});

if (process.env.RUBY_VERSION >= "2.7") {
test("nokw_param", () => expect("def foo(**nil); end").toMatchFormat());

test("args_forward", () => {
const content = ruby(`
def foo(...)
bar(...)
end
`);

return expect(content).toMatchFormat();
});
}

test("breaking", () =>
expect(`def foo(${long}:, a${long}:); end`).toChangeFormat(
ruby(`
Expand Down Expand Up @@ -171,11 +185,6 @@ describe("method", () => {
expect("Foo::foo").toChangeFormat("Foo.foo"));

test("lonely operator", () => expect("foo&.foo").toMatchFormat());

if (process.env.RUBY_VERSION >= "2.7") {
test("method reference operator", () =>
expect("foo.:foo").toMatchFormat());
}
});

describe("breaking", () => {
Expand Down
14 changes: 14 additions & 0 deletions test/rb/metadata_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -734,6 +734,20 @@ def test_zsuper
end

if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new('2.7')
def test_args_forward
content = <<~RUBY
def foo(...)
bar(...)
end
RUBY

assert_node_metadata(
:args_forward,
parse(content).dig(:body, 1, :body, 0, :body, 2),
char_start: 8, char_end: 11
)
end

def test_aryptn
content = <<~RUBY
case foo
Expand Down

0 comments on commit 6ab852a

Please sign in to comment.