Skip to content

Commit

Permalink
Correctly detect void stmts in classes and modules in ripper translation
Browse files Browse the repository at this point in the history
  • Loading branch information
kddnewton committed Mar 6, 2024
1 parent b607e3a commit 1729e8a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 27 deletions.
31 changes: 16 additions & 15 deletions lib/prism/translation/ripper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -395,20 +395,20 @@ def visit_back_reference_read_node(node)
# begin end
# ^^^^^^^^^
def visit_begin_node(node)
clauses = visit_begin_node_clauses(node.begin_keyword_loc, node)
clauses = visit_begin_node_clauses(node.begin_keyword_loc, node, false)

bounds(node.location)
on_begin(clauses)
end

# Visit the clauses of a begin node to form an on_bodystmt call.
private def visit_begin_node_clauses(location, node)
private def visit_begin_node_clauses(location, node, allow_newline)
statements =
if node.statements.nil?
on_stmts_add(on_stmts_new, on_void_stmt)
else
body = node.statements.body
body.unshift(nil) if void_stmt?(location, node.statements.body[0].location)
body.unshift(nil) if void_stmt?(location, node.statements.body[0].location, allow_newline)

bounds(node.statements.location)
visit_statements_node_body(body)
Expand All @@ -422,7 +422,7 @@ def visit_begin_node(node)
[nil]
else
body = else_clause_node.statements.body
body.unshift(nil) if void_stmt?(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, allow_newline)
body
end

Expand All @@ -437,20 +437,20 @@ def visit_begin_node(node)

# Visit the body of a structure that can have either a set of statements
# or statements wrapped in rescue/else/ensure.
private def visit_body_node(location, node)
private def visit_body_node(location, node, allow_newline = false)
case node
when nil
bounds(location)
on_bodystmt(visit_statements_node_body([nil]), nil, nil, nil)
when StatementsNode
body = [*node.body]
body.unshift(nil) if void_stmt?(location, body[0].location)
body.unshift(nil) if void_stmt?(location, body[0].location, allow_newline)
stmts = visit_statements_node_body(body)

bounds(node.body.first.location)
on_bodystmt(stmts, nil, nil, nil)
when BeginNode
visit_begin_node_clauses(location, node)
visit_begin_node_clauses(location, node, allow_newline)
else
raise
end
Expand Down Expand Up @@ -484,7 +484,7 @@ def visit_block_node(node)
braces ? stmts : on_bodystmt(stmts, nil, nil, nil)
when StatementsNode
stmts = node.body.body
stmts.unshift(nil) if void_stmt?(node.parameters&.location || node.opening_loc, node.body.location)
stmts.unshift(nil) if void_stmt?(node.parameters&.location || node.opening_loc, node.body.location, false)
stmts = visit_statements_node_body(stmts)

bounds(node.body.location)
Expand Down Expand Up @@ -879,7 +879,7 @@ def visit_class_node(node)
end

superclass = visit(node.superclass)
bodystmt = visit_body_node(node.superclass&.location || node.constant_path.location, node.body)
bodystmt = visit_body_node(node.superclass&.location || node.constant_path.location, node.body, node.superclass.nil?)

bounds(node.location)
on_class(constant_path, superclass, bodystmt)
Expand Down Expand Up @@ -1190,7 +1190,7 @@ def visit_else_node(node)
[nil]
else
body = node.statements.body
body.unshift(nil) if void_stmt?(node.else_keyword_loc, node.statements.body[0].location)
body.unshift(nil) if void_stmt?(node.else_keyword_loc, node.statements.body[0].location, false)
body
end

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

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

bounds(node.body.location)
Expand Down Expand Up @@ -1979,7 +1979,7 @@ def visit_module_node(node)
visit(node.constant_path)
end

bodystmt = visit_body_node(node.constant_path.location, node.body)
bodystmt = visit_body_node(node.constant_path.location, node.body, true)

bounds(node.location)
on_module(constant_path, bodystmt)
Expand Down Expand Up @@ -2778,8 +2778,9 @@ def trailing_comma?(left, right)
end

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

# Visit the string content of a particular node. This method is used to
Expand Down
15 changes: 3 additions & 12 deletions test/prism/ripper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,36 +46,27 @@ class RipperTest < TestCase
seattlerb/heredoc_squiggly_visually_blank_lines.txt
spanning_heredoc.txt
tilde_heredocs.txt
unparser/corpus/semantic/dstr.txt
whitequark/dedenting_heredoc.txt
whitequark/dedenting_interpolating_heredoc_fake_line_continuation.txt
whitequark/dedenting_non_interpolating_heredoc_line_continuation.txt
whitequark/parser_bug_640.txt
whitequark/parser_drops_truncated_parts_of_squiggly_heredoc.txt
whitequark/parser_slash_slash_n_escaping_in_literals.txt
whitequark/slash_newline_in_heredocs.txt
]

skips = incorrect | heredocs | %w[
if.txt
modules.txt
rescue.txt
seattlerb/TestRubyParserShared.txt
seattlerb/block_call_dot_op2_brace_block.txt
seattlerb/block_command_operation_colon.txt
seattlerb/block_command_operation_dot.txt
seattlerb/defn_oneliner_eq2.txt
seattlerb/defs_oneliner_eq2.txt
seattlerb/if_elsif.txt
unparser/corpus/literal/block.txt
unparser/corpus/literal/class.txt
unparser/corpus/literal/if.txt
unparser/corpus/literal/kwbegin.txt
unparser/corpus/literal/module.txt
unparser/corpus/literal/send.txt
unparser/corpus/literal/while.txt
unparser/corpus/semantic/dstr.txt
unparser/corpus/semantic/while.txt
whitequark/if_elsif.txt
whitequark/parser_bug_640.txt
whitequark/parser_slash_slash_n_escaping_in_literals.txt
whitequark/send_block_chain_cmd.txt
]

Expand Down

0 comments on commit 1729e8a

Please sign in to comment.