Skip to content

Commit

Permalink
[ruby/prism] Consolidate semicolon checking in ripper translation
Browse files Browse the repository at this point in the history
  • Loading branch information
kddnewton authored and matzbot committed Mar 6, 2024
1 parent de61074 commit 31ef2f4
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 11 deletions.
18 changes: 12 additions & 6 deletions lib/prism/translation/ripper.rb
Expand Up @@ -392,7 +392,7 @@ def visit_begin_node(node)
on_stmts_add(on_stmts_new, on_void_stmt)
else
body = node.statements.body
body.unshift(nil) if source.byteslice(node.begin_keyword_loc.end_offset...node.statements.body[0].location.start_offset).include?(";")
body.unshift(nil) if semicolon?(node.begin_keyword_loc, node.statements.body[0].location)

bounds(node.statements.location)
visit_statements_node_body(body)
Expand All @@ -406,7 +406,7 @@ def visit_begin_node(node)
[nil]
else
body = else_clause_node.statements.body
body.unshift(nil) if source.byteslice(else_clause_node.else_keyword_loc.end_offset...else_clause_node.statements.body[0].location.start_offset).include?(";")
body.unshift(nil) if semicolon?(else_clause_node.else_keyword_loc, else_clause_node.statements.body[0].location)
body
end

Expand Down Expand Up @@ -466,7 +466,7 @@ def visit_block_node(node)
braces ? stmts : on_bodystmt(stmts, nil, nil, nil)
when StatementsNode
stmts = node.body.body
stmts.unshift(nil) if source.byteslice((node.parameters&.location || node.opening_loc).end_offset...node.body.location.start_offset).include?(";")
stmts.unshift(nil) if semicolon?(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,8 @@ def visit_else_node(node)
[nil]
else
body = node.statements.body
body.unshift(nil) if source.byteslice(node.else_keyword_loc.end_offset...node.statements.body[0].location.start_offset).include?(";")
body.unshift(nil) if semicolon?(node.else_keyword_loc, node.statements.body[0].location)
body
end

bounds(node.location)
Expand Down Expand Up @@ -1180,7 +1181,7 @@ def visit_ensure_node(node)
[nil]
else
body = node.statements.body
body.unshift(nil) if source.byteslice(node.ensure_keyword_loc.end_offset...body[0].location.start_offset).include?(";")
body.unshift(nil) if semicolon?(node.ensure_keyword_loc, body[0].location)
body
end

Expand Down Expand Up @@ -1740,7 +1741,7 @@ def visit_lambda_node(node)
braces ? stmts : on_bodystmt(stmts, nil, nil, nil)
when StatementsNode
stmts = node.body.body
stmts.unshift(nil) if source.byteslice((node.parameters&.location || node.opening_loc).end_offset...node.body.location.start_offset).include?(";")
stmts.unshift(nil) if semicolon?(node.parameters&.location || node.opening_loc, node.body.location)
stmts = visit_statements_node_body(stmts)

bounds(node.body.location)
Expand Down Expand Up @@ -2521,6 +2522,11 @@ def result
# Helpers
##########################################################################

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

# Visit the string content of a particular node. This method is used to
# split into the various token types.
def visit_token(token)
Expand Down
5 changes: 0 additions & 5 deletions test/prism/ripper_test.rb
Expand Up @@ -60,7 +60,6 @@ class RipperTest < TestCase
seattlerb/call_args_assoc_trailing_comma.txt
seattlerb/call_args_command.txt
seattlerb/call_array_lambda_block_call.txt
seattlerb/call_assoc_new_if_multiline.txt
seattlerb/call_assoc_trailing_comma.txt
seattlerb/call_block_arg_named.txt
seattlerb/call_colon_parens.txt
Expand Down Expand Up @@ -132,8 +131,6 @@ class RipperTest < TestCase
seattlerb/mlhs_mid_anonsplat.txt
seattlerb/mlhs_mid_splat.txt
seattlerb/module_comments.txt
seattlerb/parse_if_not_canonical.txt
seattlerb/parse_if_not_noncanonical.txt
seattlerb/parse_line_dstr_escaped_newline.txt
seattlerb/parse_line_dstr_soft_newline.txt
seattlerb/parse_line_evstr_after_break.txt
Expand Down Expand Up @@ -243,7 +240,6 @@ class RipperTest < TestCase
whitequark/forwarded_kwrestarg_with_additional_kwarg.txt
whitequark/forwarded_restarg.txt
whitequark/hash_label_end.txt
whitequark/if_else.txt
whitequark/if_elsif.txt
whitequark/kwbegin_compstmt.txt
whitequark/kwoptarg_with_kwrestarg_and_forwarded_args.txt
Expand Down Expand Up @@ -280,7 +276,6 @@ class RipperTest < TestCase
whitequark/ternary.txt
whitequark/ternary_ambiguous_symbol.txt
whitequark/trailing_forward_arg.txt
whitequark/unless_else.txt
whitequark/yield.txt
xstring.txt
yield.txt
Expand Down

0 comments on commit 31ef2f4

Please sign in to comment.