Skip to content

Commit

Permalink
+ ruby27.y: fix array pattern with tail source map (#659)
Browse files Browse the repository at this point in the history
This change takes trailing comman into account for array_pattern_with_tail expression location
  • Loading branch information
palkan committed Mar 20, 2020
1 parent e5c421b commit 5610352
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 10 deletions.
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

0 comments on commit 5610352

Please sign in to comment.