summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKevin Newton <kddnewton@gmail.com>2024-03-05 23:10:28 -0500
committergit <svn-admin@ruby-lang.org>2024-03-06 16:42:54 +0000
commit712e841a4e4c968ed9984323b059fe7b8f825cff (patch)
tree15b7ef0d01f23b47f8e0ff2df745a2491512d3a5
parentc6299dda3dbe4dfb1caf78e1920c744dbcf334d5 (diff)
[ruby/prism] Better void stmt detection for comments in ripper translation
https://github.com/ruby/prism/commit/36bae94f06
-rw-r--r--lib/prism/translation/ripper.rb35
-rw-r--r--test/prism/ripper_test.rb7
2 files changed, 23 insertions, 19 deletions
diff --git a/lib/prism/translation/ripper.rb b/lib/prism/translation/ripper.rb
index e7abf01138..80c93a8bb1 100644
--- a/lib/prism/translation/ripper.rb
+++ b/lib/prism/translation/ripper.rb
@@ -385,7 +385,7 @@ module Prism
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)
@@ -399,7 +399,7 @@ module Prism
[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
@@ -421,7 +421,7 @@ module Prism
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)
@@ -461,7 +461,7 @@ module Prism
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)
@@ -1148,7 +1148,7 @@ module Prism
[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
@@ -1181,7 +1181,7 @@ module Prism
[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
@@ -1762,7 +1762,7 @@ module Prism
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)
@@ -2146,13 +2146,20 @@ module Prism
# /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
@@ -2676,8 +2683,8 @@ module Prism
##########################################################################
# 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
diff --git a/test/prism/ripper_test.rb b/test/prism/ripper_test.rb
index 2d0a2efe18..05a90daa10 100644
--- a/test/prism/ripper_test.rb
+++ b/test/prism/ripper_test.rb
@@ -10,13 +10,13 @@ module Prism
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
@@ -61,7 +61,6 @@ module Prism
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
@@ -106,7 +105,6 @@ module Prism
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
@@ -122,7 +120,6 @@ module Prism
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