Skip to content

Commit 934b98a

Browse files
authored
Merge pull request #4032 from Earlopain/ripper-events-test
Better ripper event tests / handful of compatibility fixes
2 parents 0fda21c + fb5303f commit 934b98a

2 files changed

Lines changed: 57 additions & 18 deletions

File tree

lib/prism/translation/ripper.rb

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -513,7 +513,12 @@ def parse
513513
bounds(location)
514514

515515
if comment.is_a?(InlineComment)
516-
on_comment(comment.slice)
516+
# Inline comments always contain a newline if the line itself contains it
517+
if result.source.source.bytesize > comment.location.end_offset
518+
on_comment("#{comment.slice}\n")
519+
else
520+
on_comment(comment.slice)
521+
end
517522
else
518523
offset = location.start_offset
519524
lines = comment.slice.lines
@@ -1577,7 +1582,6 @@ def visit_constant_path_write_node(node)
15771582
# ^^^^^^^^^^^^^^^
15781583
def visit_constant_path_operator_write_node(node)
15791584
target = visit_constant_path_write_node_target(node.target)
1580-
value = visit(node.value)
15811585

15821586
bounds(node.binary_operator_loc)
15831587
operator = on_op("#{node.binary_operator}=")
@@ -1591,7 +1595,6 @@ def visit_constant_path_operator_write_node(node)
15911595
# ^^^^^^^^^^^^^^^^
15921596
def visit_constant_path_and_write_node(node)
15931597
target = visit_constant_path_write_node_target(node.target)
1594-
value = visit(node.value)
15951598

15961599
bounds(node.operator_loc)
15971600
operator = on_op("&&=")
@@ -1605,7 +1608,6 @@ def visit_constant_path_and_write_node(node)
16051608
# ^^^^^^^^^^^^^^^^
16061609
def visit_constant_path_or_write_node(node)
16071610
target = visit_constant_path_write_node_target(node.target)
1608-
value = visit(node.value)
16091611

16101612
bounds(node.operator_loc)
16111613
operator = on_op("||=")
@@ -2357,6 +2359,8 @@ def visit_lambda_node(node)
23572359
visit(node.parameters.parameters)
23582360
end
23592361

2362+
visit_all(node.parameters.locals)
2363+
23602364
if node.parameters.opening_loc.nil?
23612365
params
23622366
else

test/prism/ruby/ripper_test.rb

Lines changed: 49 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,41 @@ class RipperTest < TestCase
8282
"whitequark/procarg0.txt",
8383
]
8484

85+
omitted_scan = [
86+
"dos_endings.txt",
87+
"heredocs_with_fake_newlines.txt",
88+
"rescue_modifier.txt",
89+
"seattlerb/block_call_dot_op2_brace_block.txt",
90+
"seattlerb/block_command_operation_colon.txt",
91+
"seattlerb/block_command_operation_dot.txt",
92+
"seattlerb/case_in.txt",
93+
"seattlerb/heredoc__backslash_dos_format.txt",
94+
"seattlerb/heredoc_backslash_nl.txt",
95+
"seattlerb/heredoc_nested.txt",
96+
"seattlerb/heredoc_squiggly_blank_line_plus_interpolation.txt",
97+
"seattlerb/heredoc_squiggly_empty.txt",
98+
"seattlerb/masgn_command_call.txt",
99+
"seattlerb/messy_op_asgn_lineno.txt",
100+
"seattlerb/op_asgn_primary_colon_const_command_call.txt",
101+
"seattlerb/parse_pattern_076.txt",
102+
"tilde_heredocs.txt",
103+
"unparser/corpus/literal/assignment.txt",
104+
"unparser/corpus/literal/pattern.txt",
105+
"unparser/corpus/semantic/dstr.txt",
106+
"variables.txt",
107+
"whitequark/dedenting_heredoc.txt",
108+
"whitequark/masgn_nested.txt",
109+
"whitequark/numparam_ruby_bug_19025.txt",
110+
"whitequark/op_asgn_cmd.txt",
111+
"whitequark/parser_drops_truncated_parts_of_squiggly_heredoc.txt",
112+
"whitequark/parser_slash_slash_n_escaping_in_literals.txt",
113+
"whitequark/pattern_matching_nil_pattern.txt",
114+
"whitequark/ruby_bug_12402.txt",
115+
"whitequark/ruby_bug_18878.txt",
116+
"whitequark/send_block_chain_cmd.txt",
117+
"whitequark/slash_newline_in_heredocs.txt",
118+
]
119+
85120
Fixture.each_for_current_ruby(except: incorrect | omitted_sexp_raw) do |fixture|
86121
define_method("#{fixture.test_name}_sexp_raw") { assert_ripper_sexp_raw(fixture.read) }
87122
end
@@ -100,6 +135,9 @@ def test_lex_ignored_missing_heredoc_end
100135
end
101136
end
102137

138+
UNSUPPORTED_EVENTS = %i[backtick comma heredoc_beg heredoc_end ignored_nl kw label_end lbrace lbracket lparen nl op rbrace rbracket rparen semicolon sp symbeg tstring_beg tstring_end words_sep ignored_sp]
139+
SUPPORTED_EVENTS = Translation::Ripper::EVENTS - UNSUPPORTED_EVENTS
140+
103141
module Events
104142
attr_reader :events
105143

@@ -108,9 +146,9 @@ def initialize(...)
108146
@events = []
109147
end
110148

111-
Prism::Translation::Ripper::PARSER_EVENTS.each do |event|
149+
SUPPORTED_EVENTS.each do |event|
112150
define_method(:"on_#{event}") do |*args|
113-
@events << [event, *args]
151+
@events << [event, *args.map(&:to_s)]
114152
super(*args)
115153
end
116154
end
@@ -126,28 +164,25 @@ class PrismEvents < Translation::Ripper
126164

127165
class ObjectEvents < Translation::Ripper
128166
OBJECT = BasicObject.new
129-
Prism::Translation::Ripper::PARSER_EVENTS.each do |event|
167+
SUPPORTED_EVENTS.each do |event|
130168
define_method(:"on_#{event}") { |*args| OBJECT }
131169
end
132170
end
133171

134-
Fixture.each_for_current_ruby(except: incorrect) do |fixture|
172+
Fixture.each_for_current_ruby(except: incorrect | omitted_scan) do |fixture|
135173
define_method("#{fixture.test_name}_events") do
136174
source = fixture.read
137175
# Similar to test/ripper/assert_parse_files.rb in CRuby
138176
object_events = ObjectEvents.new(source)
139177
assert_nothing_raised { object_events.parse }
140-
end
141-
end
142178

143-
def test_events
144-
source = "1 rescue 2"
145-
ripper = RipperEvents.new(source)
146-
prism = PrismEvents.new(source)
147-
ripper.parse
148-
prism.parse
149-
# This makes sure that the content is the same. Ordering is not correct for now.
150-
assert_equal(ripper.events.sort, prism.events.sort)
179+
ripper = RipperEvents.new(source)
180+
prism = PrismEvents.new(source)
181+
ripper.parse
182+
prism.parse
183+
# This makes sure that the content is the same. Ordering is not correct for now.
184+
assert_equal(ripper.events.sort, prism.events.sort)
185+
end
151186
end
152187

153188
def test_lexer

0 commit comments

Comments
 (0)