Skip to content

Commit 8fa476d

Browse files
committed
Fix up body semicolon finding in ripper translation
1 parent 87538f1 commit 8fa476d

File tree

2 files changed

+12
-14
lines changed

2 files changed

+12
-14
lines changed

lib/prism/translation/ripper.rb

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -421,16 +421,18 @@ def visit_begin_node(node)
421421

422422
# Visit the body of a structure that can have either a set of statements
423423
# or statements wrapped in rescue/else/ensure.
424-
private def visit_body_node(node, location)
424+
private def visit_body_node(location, node)
425425
case node
426426
when nil
427427
bounds(location)
428428
on_bodystmt(visit_statements_node_body([nil]), nil, nil, nil)
429429
when StatementsNode
430-
body = visit(node)
430+
body = [*node.body]
431+
body.unshift(nil) if semicolon?(location, body[0].location)
432+
stmts = visit_statements_node_body(body)
431433

432-
bounds(node.location)
433-
on_bodystmt(body, nil, nil, nil)
434+
bounds(node.body.first.location)
435+
on_bodystmt(stmts, nil, nil, nil)
434436
when BeginNode
435437
visit_begin_node_clauses(node)
436438
else
@@ -472,7 +474,7 @@ def visit_block_node(node)
472474
bounds(node.body.location)
473475
braces ? stmts : on_bodystmt(stmts, nil, nil, nil)
474476
when BeginNode
475-
visit_body_node(node.body, node.location)
477+
visit_body_node(node.parameters&.location || node.opening_loc, node.body)
476478
else
477479
raise
478480
end
@@ -837,7 +839,7 @@ def visit_class_node(node)
837839
end
838840

839841
superclass = visit(node.superclass)
840-
bodystmt = visit_body_node(node.body, node.location)
842+
bodystmt = visit_body_node(node.superclass&.location || node.constant_path.location, node.body)
841843

842844
bounds(node.location)
843845
on_class(constant_path, superclass, bodystmt)
@@ -1114,7 +1116,7 @@ def visit_def_node(node)
11141116

11151117
bodystmt =
11161118
if node.equal_loc.nil?
1117-
visit_body_node(node.body, node.location)
1119+
visit_body_node(node.body&.location || node.end_keyword_loc, node.body)
11181120
else
11191121
body = visit(node.body.body.first)
11201122

@@ -1754,7 +1756,7 @@ def visit_lambda_node(node)
17541756
bounds(node.body.location)
17551757
braces ? stmts : on_bodystmt(stmts, nil, nil, nil)
17561758
when BeginNode
1757-
visit_body_node(node.body, node.location)
1759+
visit_body_node(node.opening_loc, node.body)
17581760
else
17591761
raise
17601762
end
@@ -1888,7 +1890,7 @@ def visit_module_node(node)
18881890
visit(node.constant_path)
18891891
end
18901892

1891-
bodystmt = visit_body_node(node.body, node.location)
1893+
bodystmt = visit_body_node(node.constant_path.location, node.body)
18921894

18931895
bounds(node.location)
18941896
on_module(constant_path, bodystmt)
@@ -2250,7 +2252,7 @@ def visit_self_node(node)
22502252
# ^^^^^^^^^^^^^^^^^^
22512253
def visit_singleton_class_node(node)
22522254
expression = visit(node.expression)
2253-
bodystmt = visit_body_node(node.body, node.location)
2255+
bodystmt = visit_body_node(node.body&.location || node.end_keyword_loc, node.body)
22542256

22552257
bounds(node.location)
22562258
on_sclass(expression, bodystmt)

test/prism/ripper_test.rb

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ class RipperTest < TestCase
1414
arrays.txt
1515
blocks.txt
1616
case.txt
17-
classes.txt
1817
command_method_call.txt
1918
constants.txt
2019
dos_endings.txt
@@ -93,7 +92,6 @@ class RipperTest < TestCase
9392
seattlerb/if_elsif.txt
9493
seattlerb/lambda_do_vs_brace.txt
9594
seattlerb/lasgn_middle_splat.txt
96-
seattlerb/magic_encoding_comment.txt
9795
seattlerb/masgn_anon_splat_arg.txt
9896
seattlerb/masgn_arg_colon_arg.txt
9997
seattlerb/masgn_arg_splat_arg.txt
@@ -190,7 +188,6 @@ class RipperTest < TestCase
190188
whitequark/bug_do_block_in_hash_brace.txt
191189
whitequark/case_cond_else.txt
192190
whitequark/case_expr_else.txt
193-
whitequark/class_definition_in_while_cond.txt
194191
whitequark/dedenting_heredoc.txt
195192
whitequark/dedenting_interpolating_heredoc_fake_line_continuation.txt
196193
whitequark/dedenting_non_interpolating_heredoc_line_continuation.txt
@@ -215,7 +212,6 @@ class RipperTest < TestCase
215212
whitequark/newline_in_hash_argument.txt
216213
whitequark/next_block.txt
217214
whitequark/numbered_args_after_27.txt
218-
whitequark/numparam_outside_block.txt
219215
whitequark/parser_bug_640.txt
220216
whitequark/parser_drops_truncated_parts_of_squiggly_heredoc.txt
221217
whitequark/parser_slash_slash_n_escaping_in_literals.txt

0 commit comments

Comments
 (0)