Skip to content

Commit

Permalink
[ruby/prism] Better void stmt detection for comments in ripper transl…
Browse files Browse the repository at this point in the history
  • Loading branch information
kddnewton authored and matzbot committed Mar 6, 2024
1 parent c6299dd commit 712e841
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 19 deletions.
35 changes: 21 additions & 14 deletions lib/prism/translation/ripper.rb
Expand Up @@ -385,7 +385,7 @@ def visit_begin_node(node)
on_stmts_add(on_stmts_new, on_void_stmt)
else
body = node.statements.body
body.unshift(nil) if semicolon?(location, node.statements.body[0].location)
body.unshift(nil) if void_stmt?(location, node.statements.body[0].location)

bounds(node.statements.location)
visit_statements_node_body(body)
Expand All @@ -399,7 +399,7 @@ def visit_begin_node(node)
[nil]
else
body = else_clause_node.statements.body
body.unshift(nil) if semicolon?(else_clause_node.else_keyword_loc, else_clause_node.statements.body[0].location)
body.unshift(nil) if void_stmt?(else_clause_node.else_keyword_loc, else_clause_node.statements.body[0].location)
body
end

Expand All @@ -421,7 +421,7 @@ def visit_begin_node(node)
on_bodystmt(visit_statements_node_body([nil]), nil, nil, nil)
when StatementsNode
body = [*node.body]
body.unshift(nil) if semicolon?(location, body[0].location)
body.unshift(nil) if void_stmt?(location, body[0].location)
stmts = visit_statements_node_body(body)

bounds(node.body.first.location)
Expand Down Expand Up @@ -461,7 +461,7 @@ def visit_block_node(node)
braces ? stmts : on_bodystmt(stmts, nil, nil, nil)
when StatementsNode
stmts = node.body.body
stmts.unshift(nil) if semicolon?(node.parameters&.location || node.opening_loc, node.body.location)
stmts.unshift(nil) if void_stmt?(node.parameters&.location || node.opening_loc, node.body.location)
stmts = visit_statements_node_body(stmts)

bounds(node.body.location)
Expand Down Expand Up @@ -1148,7 +1148,7 @@ def visit_else_node(node)
[nil]
else
body = node.statements.body
body.unshift(nil) if semicolon?(node.else_keyword_loc, node.statements.body[0].location)
body.unshift(nil) if void_stmt?(node.else_keyword_loc, node.statements.body[0].location)
body
end

Expand Down Expand Up @@ -1181,7 +1181,7 @@ def visit_ensure_node(node)
[nil]
else
body = node.statements.body
body.unshift(nil) if semicolon?(node.ensure_keyword_loc, body[0].location)
body.unshift(nil) if void_stmt?(node.ensure_keyword_loc, body[0].location)
body
end

Expand Down Expand Up @@ -1762,7 +1762,7 @@ def visit_lambda_node(node)
braces ? stmts : on_bodystmt(stmts, nil, nil, nil)
when StatementsNode
stmts = node.body.body
stmts.unshift(nil) if semicolon?(node.parameters&.location || node.opening_loc, node.body.location)
stmts.unshift(nil) if void_stmt?(node.parameters&.location || node.opening_loc, node.body.location)
stmts = visit_statements_node_body(stmts)

bounds(node.body.location)
Expand Down Expand Up @@ -2146,13 +2146,20 @@ def visit_redo_node(node)
# /foo/
# ^^^^^
def visit_regular_expression_node(node)
bounds(node.content_loc)
tstring_content = on_tstring_content(node.content)
if node.content.empty?
bounds(node.closing_loc)
closing = on_regexp_end(node.closing)

bounds(node.closing_loc)
closing = on_regexp_end(node.closing)
on_regexp_literal(on_regexp_new, closing)
else
bounds(node.content_loc)
tstring_content = on_tstring_content(node.content)

on_regexp_literal(on_regexp_add(on_regexp_new, tstring_content), closing)
bounds(node.closing_loc)
closing = on_regexp_end(node.closing)

on_regexp_literal(on_regexp_add(on_regexp_new, tstring_content), closing)
end
end

# def foo(bar:); end
Expand Down Expand Up @@ -2676,8 +2683,8 @@ def result
##########################################################################

# Returns true if there is a semicolon between the two locations.
def semicolon?(left, right)
source.byteslice(left.end_offset...right.start_offset).include?(";")
def void_stmt?(left, right)
source.byteslice(left.end_offset...right.start_offset).match?(/[;#]/)
end

# Visit the string content of a particular node. This method is used to
Expand Down
7 changes: 2 additions & 5 deletions test/prism/ripper_test.rb
Expand Up @@ -10,13 +10,13 @@ class RipperTest < TestCase
base = File.join(__dir__, "fixtures")
relatives = ENV["FOCUS"] ? [ENV["FOCUS"]] : Dir["**/*.txt", base: base]

failures = %w[
incorrect = %w[
whitequark/break_block.txt
whitequark/next_block.txt
whitequark/return_block.txt
]

skips = failures | %w[
skips = incorrect | %w[
arrays.txt
blocks.txt
case.txt
Expand Down Expand Up @@ -61,7 +61,6 @@ class RipperTest < TestCase
seattlerb/call_block_arg_named.txt
seattlerb/call_trailing_comma.txt
seattlerb/case_in.txt
seattlerb/class_comments.txt
seattlerb/defn_oneliner_eq2.txt
seattlerb/defs_oneliner_eq2.txt
seattlerb/difficult3_.txt
Expand Down Expand Up @@ -106,7 +105,6 @@ class RipperTest < TestCase
seattlerb/mlhs_front_splat.txt
seattlerb/mlhs_mid_anonsplat.txt
seattlerb/mlhs_mid_splat.txt
seattlerb/module_comments.txt
seattlerb/parse_line_dstr_escaped_newline.txt
seattlerb/parse_line_dstr_soft_newline.txt
seattlerb/parse_line_evstr_after_break.txt
Expand All @@ -122,7 +120,6 @@ class RipperTest < TestCase
seattlerb/yield_call_assocs.txt
single_method_call_with_bang.txt
spanning_heredoc.txt
spanning_heredoc_newlines.txt
strings.txt
symbols.txt
ternary_operator.txt
Expand Down

0 comments on commit 712e841

Please sign in to comment.