Skip to content

Commit

Permalink
Merge pull request #2288 from ruby/handle-implicit-rest-in-array-pattern
Browse files Browse the repository at this point in the history
Handle implicit rest in array pattern for parser gem
  • Loading branch information
kddnewton authored Jan 28, 2024
2 parents d6d73d7 + d3722d6 commit 6e44d64
Showing 1 changed file with 20 additions and 8 deletions.
28 changes: 20 additions & 8 deletions lib/prism/translation/parser/compiler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,14 @@ def visit_array_node(node)
# foo => [bar]
# ^^^^^
def visit_array_pattern_node(node)
elements = [*node.requireds]
elements << node.rest if !node.rest.nil? && !node.rest.is_a?(ImplicitRestNode)
elements.concat(node.posts)

if node.constant
builder.const_pattern(visit(node.constant), token(node.opening_loc), builder.array_pattern(nil, visit_all([*node.requireds, *node.rest, *node.posts]), nil), token(node.closing_loc))
builder.const_pattern(visit(node.constant), token(node.opening_loc), builder.array_pattern(nil, visit_all(elements), nil), token(node.closing_loc))
else
builder.array_pattern(token(node.opening_loc), visit_all([*node.requireds, *node.rest, *node.posts]), token(node.closing_loc))
builder.array_pattern(token(node.opening_loc), visit_all(elements), token(node.closing_loc))
end
end

Expand Down Expand Up @@ -603,10 +607,14 @@ def visit_false_node(node)
# foo => [*, bar, *]
# ^^^^^^^^^^^
def visit_find_pattern_node(node)
elements = [*node.requireds]
elements << node.rest if !node.rest.nil? && !node.rest.is_a?(ImplicitRestNode)
elements.concat(node.posts)

if node.constant
builder.const_pattern(visit(node.constant), token(node.opening_loc), builder.find_pattern(nil, visit_all([node.left, *node.requireds, node.right]), nil), token(node.closing_loc))
builder.const_pattern(visit(node.constant), token(node.opening_loc), builder.find_pattern(nil, visit_all(elements), nil), token(node.closing_loc))
else
builder.find_pattern(token(node.opening_loc), visit_all([node.left, *node.requireds, node.right]), token(node.closing_loc))
builder.find_pattern(token(node.opening_loc), visit_all(elements), token(node.closing_loc))
end
end

Expand Down Expand Up @@ -1098,24 +1106,28 @@ def visit_module_node(node)
# foo, bar = baz
# ^^^^^^^^
def visit_multi_target_node(node)
node = node.copy(rest: nil) if node.rest.is_a?(ImplicitRestNode)
elements = [*node.lefts]
elements << node.rest if !node.rest.nil? && !node.rest.is_a?(ImplicitRestNode)
elements.concat(node.rights)

builder.multi_lhs(
token(node.lparen_loc),
visit_all([*node.lefts, *node.rest, *node.rights]),
visit_all(elements),
token(node.rparen_loc)
)
end

# foo, bar = baz
# ^^^^^^^^^^^^^^
def visit_multi_write_node(node)
node = node.copy(rest: nil) if node.rest.is_a?(ImplicitRestNode)
elements = [*node.lefts]
elements << node.rest if !node.rest.nil? && !node.rest.is_a?(ImplicitRestNode)
elements.concat(node.rights)

builder.multi_assign(
builder.multi_lhs(
token(node.lparen_loc),
visit_all([*node.lefts, *node.rest, *node.rights]),
visit_all(elements),
token(node.rparen_loc)
),
token(node.operator_loc),
Expand Down

0 comments on commit 6e44d64

Please sign in to comment.