Skip to content

Commit 52c4fa7

Browse files
committed
Also handle BasicObject as a return value
We should touch these as little as possible and just pass them along
1 parent bed4271 commit 52c4fa7

2 files changed

Lines changed: 37 additions & 32 deletions

File tree

lib/prism/translation/ripper.rb

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -832,7 +832,7 @@ def visit_array_pattern_node(node)
832832
# foo(bar)
833833
# ^^^
834834
def visit_arguments_node(node)
835-
arguments, _ = visit_call_node_arguments(node, nil, false)
835+
arguments, _, _ = visit_call_node_arguments(node, nil, false)
836836
arguments
837837
end
838838

@@ -1042,16 +1042,16 @@ def visit_call_node(node)
10421042
case node.name
10431043
when :[]
10441044
receiver = visit(node.receiver)
1045-
arguments, block = visit_call_node_arguments(node.arguments, node.block, trailing_comma?(node.arguments&.location || node.location, node.closing_loc))
1045+
arguments, block, has_ripper_block = visit_call_node_arguments(node.arguments, node.block, trailing_comma?(node.arguments&.location || node.location, node.closing_loc))
10461046

10471047
bounds(node.location)
10481048
call = on_aref(receiver, arguments)
10491049

1050-
if block.nil?
1051-
call
1052-
else
1050+
if has_ripper_block
10531051
bounds(node.location)
10541052
on_method_add_block(call, block)
1053+
else
1054+
call
10551055
end
10561056
when :[]=
10571057
receiver = visit(node.receiver)
@@ -1110,7 +1110,7 @@ def visit_call_node(node)
11101110
if node.variable_call?
11111111
on_vcall(message)
11121112
else
1113-
arguments, block = visit_call_node_arguments(node.arguments, node.block, trailing_comma?(node.arguments&.location || node.location, node.closing_loc || node.location))
1113+
arguments, block, has_ripper_block = visit_call_node_arguments(node.arguments, node.block, trailing_comma?(node.arguments&.location || node.location, node.closing_loc || node.location))
11141114
call =
11151115
if node.opening_loc.nil? && get_arguments_and_block(node.arguments, node.block).first.any?
11161116
bounds(node.location)
@@ -1123,11 +1123,11 @@ def visit_call_node(node)
11231123
on_method_add_arg(on_fcall(message), on_args_new)
11241124
end
11251125

1126-
if block.nil?
1127-
call
1128-
else
1126+
if has_ripper_block
11291127
bounds(node.block.location)
11301128
on_method_add_block(call, block)
1129+
else
1130+
call
11311131
end
11321132
end
11331133
end
@@ -1151,7 +1151,7 @@ def visit_call_node(node)
11511151
bounds(node.location)
11521152
on_assign(on_field(receiver, call_operator, message), value)
11531153
else
1154-
arguments, block = visit_call_node_arguments(node.arguments, node.block, trailing_comma?(node.arguments&.location || node.location, node.closing_loc || node.location))
1154+
arguments, block, has_ripper_block = visit_call_node_arguments(node.arguments, node.block, trailing_comma?(node.arguments&.location || node.location, node.closing_loc || node.location))
11551155
call =
11561156
if node.opening_loc.nil?
11571157
bounds(node.location)
@@ -1169,11 +1169,11 @@ def visit_call_node(node)
11691169
on_method_add_arg(on_call(receiver, call_operator, message), arguments)
11701170
end
11711171

1172-
if block.nil?
1173-
call
1174-
else
1172+
if has_ripper_block
11751173
bounds(node.block.location)
11761174
on_method_add_block(call, block)
1175+
else
1176+
call
11771177
end
11781178
end
11791179
end
@@ -1211,7 +1211,8 @@ def visit_call_node(node)
12111211
on_args_add_block(args, false)
12121212
end
12131213
end,
1214-
visit(block)
1214+
visit(block),
1215+
block != nil,
12151216
]
12161217
end
12171218

@@ -1648,10 +1649,10 @@ def visit_def_node(node)
16481649
end
16491650

16501651
bounds(node.location)
1651-
if receiver.nil?
1652-
on_def(name, parameters, bodystmt)
1653-
else
1652+
if receiver
16541653
on_defs(receiver, operator, name, parameters, bodystmt)
1654+
else
1655+
on_def(name, parameters, bodystmt)
16551656
end
16561657
end
16571658

@@ -2049,7 +2050,7 @@ def visit_in_node(node)
20492050
# ^^^^^^^^^^^^^^^
20502051
def visit_index_operator_write_node(node)
20512052
receiver = visit(node.receiver)
2052-
arguments, _ = visit_call_node_arguments(node.arguments, node.block, trailing_comma?(node.arguments&.location || node.location, node.closing_loc))
2053+
arguments, _, _ = visit_call_node_arguments(node.arguments, node.block, trailing_comma?(node.arguments&.location || node.location, node.closing_loc))
20532054

20542055
bounds(node.location)
20552056
target = on_aref_field(receiver, arguments)
@@ -2066,7 +2067,7 @@ def visit_index_operator_write_node(node)
20662067
# ^^^^^^^^^^^^^^^^
20672068
def visit_index_and_write_node(node)
20682069
receiver = visit(node.receiver)
2069-
arguments, _ = visit_call_node_arguments(node.arguments, node.block, trailing_comma?(node.arguments&.location || node.location, node.closing_loc))
2070+
arguments, _, _ = visit_call_node_arguments(node.arguments, node.block, trailing_comma?(node.arguments&.location || node.location, node.closing_loc))
20702071

20712072
bounds(node.location)
20722073
target = on_aref_field(receiver, arguments)
@@ -2083,7 +2084,7 @@ def visit_index_and_write_node(node)
20832084
# ^^^^^^^^^^^^^^^^
20842085
def visit_index_or_write_node(node)
20852086
receiver = visit(node.receiver)
2086-
arguments, _ = visit_call_node_arguments(node.arguments, node.block, trailing_comma?(node.arguments&.location || node.location, node.closing_loc))
2087+
arguments, _, _ = visit_call_node_arguments(node.arguments, node.block, trailing_comma?(node.arguments&.location || node.location, node.closing_loc))
20872088

20882089
bounds(node.location)
20892090
target = on_aref_field(receiver, arguments)
@@ -2100,7 +2101,7 @@ def visit_index_or_write_node(node)
21002101
# ^^^^^^^^
21012102
def visit_index_target_node(node)
21022103
receiver = visit(node.receiver)
2103-
arguments, _ = visit_call_node_arguments(node.arguments, node.block, trailing_comma?(node.arguments&.location || node.location, node.closing_loc))
2104+
arguments, _, _ = visit_call_node_arguments(node.arguments, node.block, trailing_comma?(node.arguments&.location || node.location, node.closing_loc))
21042105

21052106
bounds(node.location)
21062107
on_aref_field(receiver, arguments)
@@ -3130,7 +3131,7 @@ def visit_string_node(node)
31303131
# super(foo)
31313132
# ^^^^^^^^^^
31323133
def visit_super_node(node)
3133-
arguments, block = visit_call_node_arguments(node.arguments, node.block, trailing_comma?(node.arguments&.location || node.location, node.rparen_loc || node.location))
3134+
arguments, block, has_ripper_block = visit_call_node_arguments(node.arguments, node.block, trailing_comma?(node.arguments&.location || node.location, node.rparen_loc || node.location))
31343135

31353136
if !node.lparen_loc.nil?
31363137
bounds(node.lparen_loc)
@@ -3140,11 +3141,11 @@ def visit_super_node(node)
31403141
bounds(node.location)
31413142
call = on_super(arguments)
31423143

3143-
if block.nil?
3144-
call
3145-
else
3144+
if has_ripper_block
31463145
bounds(node.block.location)
31473146
on_method_add_block(call, block)
3147+
else
3148+
call
31483149
end
31493150
end
31503151

test/prism/ruby/ripper_test.rb

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -111,10 +111,18 @@ class PrismEvents < Translation::Ripper
111111
end
112112

113113
class ObjectEvents < Translation::Ripper
114+
OBJECT = BasicObject.new
114115
Prism::Translation::Ripper::PARSER_EVENTS.each do |event|
115-
define_method(:"on_#{event}") do |*args|
116-
Object.new
117-
end
116+
define_method(:"on_#{event}") { |*args| OBJECT }
117+
end
118+
end
119+
120+
Fixture.each_for_current_ruby(except: incorrect) do |fixture|
121+
define_method("#{fixture.test_name}_events") do
122+
source = fixture.read
123+
# Similar to test/ripper/assert_parse_files.rb in CRuby
124+
object_events = ObjectEvents.new(source)
125+
assert_nothing_raised { object_events.parse }
118126
end
119127
end
120128

@@ -160,10 +168,6 @@ def test_internals
160168

161169
def assert_ripper_sexp_raw(source)
162170
assert_equal Ripper.sexp_raw(source), Prism::Translation::Ripper.sexp_raw(source)
163-
164-
# Similar to test/ripper/assert_parse_files.rb in CRuby
165-
object_events = ObjectEvents.new(source)
166-
assert_nothing_raised { object_events.parse }
167171
end
168172

169173
def assert_ripper_lex(source)

0 commit comments

Comments
 (0)