Skip to content

Commit 1729e8a

Browse files
committed
Correctly detect void stmts in classes and modules in ripper translation
1 parent b607e3a commit 1729e8a

File tree

2 files changed

+19
-27
lines changed

2 files changed

+19
-27
lines changed

lib/prism/translation/ripper.rb

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -395,20 +395,20 @@ def visit_back_reference_read_node(node)
395395
# begin end
396396
# ^^^^^^^^^
397397
def visit_begin_node(node)
398-
clauses = visit_begin_node_clauses(node.begin_keyword_loc, node)
398+
clauses = visit_begin_node_clauses(node.begin_keyword_loc, node, false)
399399

400400
bounds(node.location)
401401
on_begin(clauses)
402402
end
403403

404404
# Visit the clauses of a begin node to form an on_bodystmt call.
405-
private def visit_begin_node_clauses(location, node)
405+
private def visit_begin_node_clauses(location, node, allow_newline)
406406
statements =
407407
if node.statements.nil?
408408
on_stmts_add(on_stmts_new, on_void_stmt)
409409
else
410410
body = node.statements.body
411-
body.unshift(nil) if void_stmt?(location, node.statements.body[0].location)
411+
body.unshift(nil) if void_stmt?(location, node.statements.body[0].location, allow_newline)
412412

413413
bounds(node.statements.location)
414414
visit_statements_node_body(body)
@@ -422,7 +422,7 @@ def visit_begin_node(node)
422422
[nil]
423423
else
424424
body = else_clause_node.statements.body
425-
body.unshift(nil) if void_stmt?(else_clause_node.else_keyword_loc, else_clause_node.statements.body[0].location)
425+
body.unshift(nil) if void_stmt?(else_clause_node.else_keyword_loc, else_clause_node.statements.body[0].location, allow_newline)
426426
body
427427
end
428428

@@ -437,20 +437,20 @@ def visit_begin_node(node)
437437

438438
# Visit the body of a structure that can have either a set of statements
439439
# or statements wrapped in rescue/else/ensure.
440-
private def visit_body_node(location, node)
440+
private def visit_body_node(location, node, allow_newline = false)
441441
case node
442442
when nil
443443
bounds(location)
444444
on_bodystmt(visit_statements_node_body([nil]), nil, nil, nil)
445445
when StatementsNode
446446
body = [*node.body]
447-
body.unshift(nil) if void_stmt?(location, body[0].location)
447+
body.unshift(nil) if void_stmt?(location, body[0].location, allow_newline)
448448
stmts = visit_statements_node_body(body)
449449

450450
bounds(node.body.first.location)
451451
on_bodystmt(stmts, nil, nil, nil)
452452
when BeginNode
453-
visit_begin_node_clauses(location, node)
453+
visit_begin_node_clauses(location, node, allow_newline)
454454
else
455455
raise
456456
end
@@ -484,7 +484,7 @@ def visit_block_node(node)
484484
braces ? stmts : on_bodystmt(stmts, nil, nil, nil)
485485
when StatementsNode
486486
stmts = node.body.body
487-
stmts.unshift(nil) if void_stmt?(node.parameters&.location || node.opening_loc, node.body.location)
487+
stmts.unshift(nil) if void_stmt?(node.parameters&.location || node.opening_loc, node.body.location, false)
488488
stmts = visit_statements_node_body(stmts)
489489

490490
bounds(node.body.location)
@@ -879,7 +879,7 @@ def visit_class_node(node)
879879
end
880880

881881
superclass = visit(node.superclass)
882-
bodystmt = visit_body_node(node.superclass&.location || node.constant_path.location, node.body)
882+
bodystmt = visit_body_node(node.superclass&.location || node.constant_path.location, node.body, node.superclass.nil?)
883883

884884
bounds(node.location)
885885
on_class(constant_path, superclass, bodystmt)
@@ -1190,7 +1190,7 @@ def visit_else_node(node)
11901190
[nil]
11911191
else
11921192
body = node.statements.body
1193-
body.unshift(nil) if void_stmt?(node.else_keyword_loc, node.statements.body[0].location)
1193+
body.unshift(nil) if void_stmt?(node.else_keyword_loc, node.statements.body[0].location, false)
11941194
body
11951195
end
11961196

@@ -1229,7 +1229,7 @@ def visit_ensure_node(node)
12291229
[nil]
12301230
else
12311231
body = node.statements.body
1232-
body.unshift(nil) if void_stmt?(node.ensure_keyword_loc, body[0].location)
1232+
body.unshift(nil) if void_stmt?(node.ensure_keyword_loc, body[0].location, false)
12331233
body
12341234
end
12351235

@@ -1839,7 +1839,7 @@ def visit_lambda_node(node)
18391839
braces ? stmts : on_bodystmt(stmts, nil, nil, nil)
18401840
when StatementsNode
18411841
stmts = node.body.body
1842-
stmts.unshift(nil) if void_stmt?(node.parameters&.location || node.opening_loc, node.body.location)
1842+
stmts.unshift(nil) if void_stmt?(node.parameters&.location || node.opening_loc, node.body.location, false)
18431843
stmts = visit_statements_node_body(stmts)
18441844

18451845
bounds(node.body.location)
@@ -1979,7 +1979,7 @@ def visit_module_node(node)
19791979
visit(node.constant_path)
19801980
end
19811981

1982-
bodystmt = visit_body_node(node.constant_path.location, node.body)
1982+
bodystmt = visit_body_node(node.constant_path.location, node.body, true)
19831983

19841984
bounds(node.location)
19851985
on_module(constant_path, bodystmt)
@@ -2778,8 +2778,9 @@ def trailing_comma?(left, right)
27782778
end
27792779

27802780
# Returns true if there is a semicolon between the two locations.
2781-
def void_stmt?(left, right)
2782-
source.byteslice(left.end_offset...right.start_offset).match?(/[;#]/)
2781+
def void_stmt?(left, right, allow_newline)
2782+
pattern = allow_newline ? /[;#\n]/ : /[;#]/
2783+
source.byteslice(left.end_offset...right.start_offset).match?(pattern)
27832784
end
27842785

27852786
# Visit the string content of a particular node. This method is used to

test/prism/ripper_test.rb

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -46,36 +46,27 @@ class RipperTest < TestCase
4646
seattlerb/heredoc_squiggly_visually_blank_lines.txt
4747
spanning_heredoc.txt
4848
tilde_heredocs.txt
49+
unparser/corpus/semantic/dstr.txt
4950
whitequark/dedenting_heredoc.txt
5051
whitequark/dedenting_interpolating_heredoc_fake_line_continuation.txt
5152
whitequark/dedenting_non_interpolating_heredoc_line_continuation.txt
53+
whitequark/parser_bug_640.txt
5254
whitequark/parser_drops_truncated_parts_of_squiggly_heredoc.txt
55+
whitequark/parser_slash_slash_n_escaping_in_literals.txt
5356
whitequark/slash_newline_in_heredocs.txt
5457
]
5558

5659
skips = incorrect | heredocs | %w[
5760
if.txt
58-
modules.txt
5961
rescue.txt
6062
seattlerb/TestRubyParserShared.txt
6163
seattlerb/block_call_dot_op2_brace_block.txt
6264
seattlerb/block_command_operation_colon.txt
6365
seattlerb/block_command_operation_dot.txt
64-
seattlerb/defn_oneliner_eq2.txt
65-
seattlerb/defs_oneliner_eq2.txt
6666
seattlerb/if_elsif.txt
6767
unparser/corpus/literal/block.txt
68-
unparser/corpus/literal/class.txt
69-
unparser/corpus/literal/if.txt
7068
unparser/corpus/literal/kwbegin.txt
71-
unparser/corpus/literal/module.txt
72-
unparser/corpus/literal/send.txt
73-
unparser/corpus/literal/while.txt
74-
unparser/corpus/semantic/dstr.txt
75-
unparser/corpus/semantic/while.txt
7669
whitequark/if_elsif.txt
77-
whitequark/parser_bug_640.txt
78-
whitequark/parser_slash_slash_n_escaping_in_literals.txt
7970
whitequark/send_block_chain_cmd.txt
8071
]
8172

0 commit comments

Comments
 (0)