Skip to content

Commit

Permalink
Fix up body semicolon finding in ripper translation
Browse files Browse the repository at this point in the history
  • Loading branch information
kddnewton committed Mar 6, 2024
1 parent 87538f1 commit 8fa476d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 14 deletions.
22 changes: 12 additions & 10 deletions lib/prism/translation/ripper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -421,16 +421,18 @@ def visit_begin_node(node)

# Visit the body of a structure that can have either a set of statements
# or statements wrapped in rescue/else/ensure.
private def visit_body_node(node, location)
private def visit_body_node(location, node)
case node
when nil
bounds(location)
on_bodystmt(visit_statements_node_body([nil]), nil, nil, nil)
when StatementsNode
body = visit(node)
body = [*node.body]
body.unshift(nil) if semicolon?(location, body[0].location)
stmts = visit_statements_node_body(body)

bounds(node.location)
on_bodystmt(body, nil, nil, nil)
bounds(node.body.first.location)
on_bodystmt(stmts, nil, nil, nil)
when BeginNode
visit_begin_node_clauses(node)
else
Expand Down Expand Up @@ -472,7 +474,7 @@ def visit_block_node(node)
bounds(node.body.location)
braces ? stmts : on_bodystmt(stmts, nil, nil, nil)
when BeginNode
visit_body_node(node.body, node.location)
visit_body_node(node.parameters&.location || node.opening_loc, node.body)
else
raise
end
Expand Down Expand Up @@ -837,7 +839,7 @@ def visit_class_node(node)
end

superclass = visit(node.superclass)
bodystmt = visit_body_node(node.body, node.location)
bodystmt = visit_body_node(node.superclass&.location || node.constant_path.location, node.body)

bounds(node.location)
on_class(constant_path, superclass, bodystmt)
Expand Down Expand Up @@ -1114,7 +1116,7 @@ def visit_def_node(node)

bodystmt =
if node.equal_loc.nil?
visit_body_node(node.body, node.location)
visit_body_node(node.body&.location || node.end_keyword_loc, node.body)
else
body = visit(node.body.body.first)

Expand Down Expand Up @@ -1754,7 +1756,7 @@ def visit_lambda_node(node)
bounds(node.body.location)
braces ? stmts : on_bodystmt(stmts, nil, nil, nil)
when BeginNode
visit_body_node(node.body, node.location)
visit_body_node(node.opening_loc, node.body)
else
raise
end
Expand Down Expand Up @@ -1888,7 +1890,7 @@ def visit_module_node(node)
visit(node.constant_path)
end

bodystmt = visit_body_node(node.body, node.location)
bodystmt = visit_body_node(node.constant_path.location, node.body)

bounds(node.location)
on_module(constant_path, bodystmt)
Expand Down Expand Up @@ -2250,7 +2252,7 @@ def visit_self_node(node)
# ^^^^^^^^^^^^^^^^^^
def visit_singleton_class_node(node)
expression = visit(node.expression)
bodystmt = visit_body_node(node.body, node.location)
bodystmt = visit_body_node(node.body&.location || node.end_keyword_loc, node.body)

bounds(node.location)
on_sclass(expression, bodystmt)
Expand Down
4 changes: 0 additions & 4 deletions test/prism/ripper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ class RipperTest < TestCase
arrays.txt
blocks.txt
case.txt
classes.txt
command_method_call.txt
constants.txt
dos_endings.txt
Expand Down Expand Up @@ -93,7 +92,6 @@ class RipperTest < TestCase
seattlerb/if_elsif.txt
seattlerb/lambda_do_vs_brace.txt
seattlerb/lasgn_middle_splat.txt
seattlerb/magic_encoding_comment.txt
seattlerb/masgn_anon_splat_arg.txt
seattlerb/masgn_arg_colon_arg.txt
seattlerb/masgn_arg_splat_arg.txt
Expand Down Expand Up @@ -190,7 +188,6 @@ class RipperTest < TestCase
whitequark/bug_do_block_in_hash_brace.txt
whitequark/case_cond_else.txt
whitequark/case_expr_else.txt
whitequark/class_definition_in_while_cond.txt
whitequark/dedenting_heredoc.txt
whitequark/dedenting_interpolating_heredoc_fake_line_continuation.txt
whitequark/dedenting_non_interpolating_heredoc_line_continuation.txt
Expand All @@ -215,7 +212,6 @@ class RipperTest < TestCase
whitequark/newline_in_hash_argument.txt
whitequark/next_block.txt
whitequark/numbered_args_after_27.txt
whitequark/numparam_outside_block.txt
whitequark/parser_bug_640.txt
whitequark/parser_drops_truncated_parts_of_squiggly_heredoc.txt
whitequark/parser_slash_slash_n_escaping_in_literals.txt
Expand Down

0 comments on commit 8fa476d

Please sign in to comment.