Skip to content

Commit 36bae94

Browse files
committed
Better void stmt detection for comments in ripper translation
1 parent 117c741 commit 36bae94

File tree

2 files changed

+23
-19
lines changed

2 files changed

+23
-19
lines changed

lib/prism/translation/ripper.rb

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,7 @@ def visit_begin_node(node)
385385
on_stmts_add(on_stmts_new, on_void_stmt)
386386
else
387387
body = node.statements.body
388-
body.unshift(nil) if semicolon?(location, node.statements.body[0].location)
388+
body.unshift(nil) if void_stmt?(location, node.statements.body[0].location)
389389

390390
bounds(node.statements.location)
391391
visit_statements_node_body(body)
@@ -399,7 +399,7 @@ def visit_begin_node(node)
399399
[nil]
400400
else
401401
body = else_clause_node.statements.body
402-
body.unshift(nil) if semicolon?(else_clause_node.else_keyword_loc, else_clause_node.statements.body[0].location)
402+
body.unshift(nil) if void_stmt?(else_clause_node.else_keyword_loc, else_clause_node.statements.body[0].location)
403403
body
404404
end
405405

@@ -421,7 +421,7 @@ def visit_begin_node(node)
421421
on_bodystmt(visit_statements_node_body([nil]), nil, nil, nil)
422422
when StatementsNode
423423
body = [*node.body]
424-
body.unshift(nil) if semicolon?(location, body[0].location)
424+
body.unshift(nil) if void_stmt?(location, body[0].location)
425425
stmts = visit_statements_node_body(body)
426426

427427
bounds(node.body.first.location)
@@ -461,7 +461,7 @@ def visit_block_node(node)
461461
braces ? stmts : on_bodystmt(stmts, nil, nil, nil)
462462
when StatementsNode
463463
stmts = node.body.body
464-
stmts.unshift(nil) if semicolon?(node.parameters&.location || node.opening_loc, node.body.location)
464+
stmts.unshift(nil) if void_stmt?(node.parameters&.location || node.opening_loc, node.body.location)
465465
stmts = visit_statements_node_body(stmts)
466466

467467
bounds(node.body.location)
@@ -1148,7 +1148,7 @@ def visit_else_node(node)
11481148
[nil]
11491149
else
11501150
body = node.statements.body
1151-
body.unshift(nil) if semicolon?(node.else_keyword_loc, node.statements.body[0].location)
1151+
body.unshift(nil) if void_stmt?(node.else_keyword_loc, node.statements.body[0].location)
11521152
body
11531153
end
11541154

@@ -1181,7 +1181,7 @@ def visit_ensure_node(node)
11811181
[nil]
11821182
else
11831183
body = node.statements.body
1184-
body.unshift(nil) if semicolon?(node.ensure_keyword_loc, body[0].location)
1184+
body.unshift(nil) if void_stmt?(node.ensure_keyword_loc, body[0].location)
11851185
body
11861186
end
11871187

@@ -1762,7 +1762,7 @@ def visit_lambda_node(node)
17621762
braces ? stmts : on_bodystmt(stmts, nil, nil, nil)
17631763
when StatementsNode
17641764
stmts = node.body.body
1765-
stmts.unshift(nil) if semicolon?(node.parameters&.location || node.opening_loc, node.body.location)
1765+
stmts.unshift(nil) if void_stmt?(node.parameters&.location || node.opening_loc, node.body.location)
17661766
stmts = visit_statements_node_body(stmts)
17671767

17681768
bounds(node.body.location)
@@ -2146,13 +2146,20 @@ def visit_redo_node(node)
21462146
# /foo/
21472147
# ^^^^^
21482148
def visit_regular_expression_node(node)
2149-
bounds(node.content_loc)
2150-
tstring_content = on_tstring_content(node.content)
2149+
if node.content.empty?
2150+
bounds(node.closing_loc)
2151+
closing = on_regexp_end(node.closing)
21512152

2152-
bounds(node.closing_loc)
2153-
closing = on_regexp_end(node.closing)
2153+
on_regexp_literal(on_regexp_new, closing)
2154+
else
2155+
bounds(node.content_loc)
2156+
tstring_content = on_tstring_content(node.content)
21542157

2155-
on_regexp_literal(on_regexp_add(on_regexp_new, tstring_content), closing)
2158+
bounds(node.closing_loc)
2159+
closing = on_regexp_end(node.closing)
2160+
2161+
on_regexp_literal(on_regexp_add(on_regexp_new, tstring_content), closing)
2162+
end
21562163
end
21572164

21582165
# def foo(bar:); end
@@ -2676,8 +2683,8 @@ def result
26762683
##########################################################################
26772684

26782685
# Returns true if there is a semicolon between the two locations.
2679-
def semicolon?(left, right)
2680-
source.byteslice(left.end_offset...right.start_offset).include?(";")
2686+
def void_stmt?(left, right)
2687+
source.byteslice(left.end_offset...right.start_offset).match?(/[;#]/)
26812688
end
26822689

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

test/prism/ripper_test.rb

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ class RipperTest < TestCase
1010
base = File.join(__dir__, "fixtures")
1111
relatives = ENV["FOCUS"] ? [ENV["FOCUS"]] : Dir["**/*.txt", base: base]
1212

13-
failures = %w[
13+
incorrect = %w[
1414
whitequark/break_block.txt
1515
whitequark/next_block.txt
1616
whitequark/return_block.txt
1717
]
1818

19-
skips = failures | %w[
19+
skips = incorrect | %w[
2020
arrays.txt
2121
blocks.txt
2222
case.txt
@@ -61,7 +61,6 @@ class RipperTest < TestCase
6161
seattlerb/call_block_arg_named.txt
6262
seattlerb/call_trailing_comma.txt
6363
seattlerb/case_in.txt
64-
seattlerb/class_comments.txt
6564
seattlerb/defn_oneliner_eq2.txt
6665
seattlerb/defs_oneliner_eq2.txt
6766
seattlerb/difficult3_.txt
@@ -106,7 +105,6 @@ class RipperTest < TestCase
106105
seattlerb/mlhs_front_splat.txt
107106
seattlerb/mlhs_mid_anonsplat.txt
108107
seattlerb/mlhs_mid_splat.txt
109-
seattlerb/module_comments.txt
110108
seattlerb/parse_line_dstr_escaped_newline.txt
111109
seattlerb/parse_line_dstr_soft_newline.txt
112110
seattlerb/parse_line_evstr_after_break.txt
@@ -122,7 +120,6 @@ class RipperTest < TestCase
122120
seattlerb/yield_call_assocs.txt
123121
single_method_call_with_bang.txt
124122
spanning_heredoc.txt
125-
spanning_heredoc_newlines.txt
126123
strings.txt
127124
symbols.txt
128125
ternary_operator.txt

0 commit comments

Comments
 (0)