Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

+ ruby27.y: fix array pattern with tail source map #659

Merged
merged 1 commit into from Mar 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/AST_FORMAT.md
Expand Up @@ -2068,7 +2068,7 @@ so a single item match with comma gets interpreted as an array.
(array-pattern-with-tail
(match-var :foo))
"in foo,"
~~~ expression
~~~~ expression
~~~

### Matching using hash pattern
Expand Down
9 changes: 5 additions & 4 deletions lib/parser/builders/default.rb
Expand Up @@ -1318,7 +1318,7 @@ def array_pattern(lbrack_t, elements, rbrack_t)

trailing_comma = false

elements = elements.map do |element|
node_elements = elements.map do |element|
if element.type == :match_with_trailing_comma
trailing_comma = true
element.children.first
Expand All @@ -1329,12 +1329,13 @@ def array_pattern(lbrack_t, elements, rbrack_t)
end

node_type = trailing_comma ? :array_pattern_with_tail : :array_pattern
n(node_type, elements,

n(node_type, node_elements,
collection_map(lbrack_t, elements, rbrack_t))
end

def match_with_trailing_comma(match)
n(:match_with_trailing_comma, [ match ], nil)
def match_with_trailing_comma(match, comma_t)
n(:match_with_trailing_comma, [ match ], expr_map(match.loc.expression.join(loc(comma_t))))
end

def const_pattern(const, ldelim_t, pattern, rdelim_t)
Expand Down
6 changes: 3 additions & 3 deletions lib/parser/ruby27.y
Expand Up @@ -1784,7 +1784,7 @@ opt_block_args_tail:
# array patterns that end with comma
# like 1, 2,
# must be emitted as `array_pattern_with_tail`
item = @builder.match_with_trailing_comma(val[0])
item = @builder.match_with_trailing_comma(val[0], val[1])
result = @builder.array_pattern(nil, [ item ], nil)
}
| p_expr tCOMMA p_args
Expand Down Expand Up @@ -1934,15 +1934,15 @@ opt_block_args_tail:
# array patterns that end with comma
# like [1, 2,]
# must be emitted as `array_pattern_with_tail`
item = @builder.match_with_trailing_comma(val[0])
item = @builder.match_with_trailing_comma(val[0], val[1])
result = [ item ]
}
| p_args_head p_arg tCOMMA
{
# array patterns that end with comma
# like [1, 2,]
# must be emitted as `array_pattern_with_tail`
last_item = @builder.match_with_trailing_comma(val[1])
last_item = @builder.match_with_trailing_comma(val[1], val[2])
result = [ *val[0], last_item ]
}

Expand Down
4 changes: 2 additions & 2 deletions test/test_parser.rb
Expand Up @@ -8422,7 +8422,7 @@ def test_pattern_matching_implicit_array_match
nil,
s(:nil)),
%q{in x, then nil},
%q{ ~ expression (in_pattern.array_pattern_with_tail)}
%q{ ~~ expression (in_pattern.array_pattern_with_tail)}
)

assert_parses_pattern_match(
Expand Down Expand Up @@ -8468,7 +8468,7 @@ def test_pattern_matching_implicit_array_match
nil,
s(:nil)),
%q{in x, y, then nil},
%q{ ~~~~ expression (in_pattern.array_pattern_with_tail)}
%q{ ~~~~~ expression (in_pattern.array_pattern_with_tail)}
)

assert_parses_pattern_match(
Expand Down