summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lib/prism/translation/ripper.rb31
-rw-r--r--test/prism/ripper_test.rb15
2 files changed, 19 insertions, 27 deletions
diff --git a/lib/prism/translation/ripper.rb b/lib/prism/translation/ripper.rb
index 1abb52fe05..0965c0536b 100644
--- a/lib/prism/translation/ripper.rb
+++ b/lib/prism/translation/ripper.rb
@@ -395,20 +395,20 @@ module Prism
# 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)
@@ -422,7 +422,7 @@ module Prism
[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
@@ -437,20 +437,20 @@ module Prism
# 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
@@ -484,7 +484,7 @@ module Prism
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)
@@ -879,7 +879,7 @@ module Prism
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)
@@ -1190,7 +1190,7 @@ module Prism
[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
@@ -1229,7 +1229,7 @@ module Prism
[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
@@ -1839,7 +1839,7 @@ module Prism
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)
@@ -1979,7 +1979,7 @@ module Prism
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)
@@ -2778,8 +2778,9 @@ module Prism
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
diff --git a/test/prism/ripper_test.rb b/test/prism/ripper_test.rb
index c6f030ba3c..d9114d57df 100644
--- a/test/prism/ripper_test.rb
+++ b/test/prism/ripper_test.rb
@@ -46,36 +46,27 @@ module Prism
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
]