Skip to content

Commit c474147

Browse files
committed
Fix error with prism when method given no arguments
such as: p = Proc.new This is now highlighted, and it wasn't before.
1 parent a75ddf7 commit c474147

File tree

2 files changed

+29
-5
lines changed

2 files changed

+29
-5
lines changed

lib/error_highlight/base.rb

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,12 @@ def spot
181181
when :name
182182
spot_call_for_name
183183
when :args
184-
spot_call_for_args
184+
_nd_recv, _mid, nd_args = @node.children
185+
if nd_args
186+
spot_call_for_args(nd_args)
187+
else
188+
spot_call_for_name
189+
end
185190
end
186191

187192
when :ATTRASGN
@@ -244,7 +249,11 @@ def spot
244249
when :name
245250
prism_spot_call_for_name
246251
when :args
247-
prism_spot_call_for_args
252+
if @node.arguments.nil?
253+
prism_spot_call_for_name
254+
else
255+
prism_spot_call_for_args
256+
end
248257
end
249258

250259
when :local_variable_operator_write_node
@@ -356,8 +365,7 @@ def spot_call_for_name
356365
# ^^
357366
# x += 1
358367
# ^
359-
def spot_call_for_args
360-
_nd_recv, _mid, nd_args = @node.children
368+
def spot_call_for_args(nd_args)
361369
if nd_args && nd_args.first_lineno == nd_args.last_lineno
362370
fetch_line(nd_args.first_lineno)
363371
@beg_column = nd_args.first_column

test/test_error_highlight.rb

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ def assert_error_message(klass, expected_msg, &blk)
6363
ONE_RECV_MESSAGE = "1:Integer"
6464
end
6565

66+
def using_prism?
67+
RubyVM::InstructionSequence.compile("").to_a[4][:parser] == :prism
68+
end
69+
6670
def test_CALL_noarg_1
6771
assert_error_message(NoMethodError, <<~END) do
6872
undefined method `foo' for #{ NIL_RECV_MESSAGE }
@@ -197,6 +201,18 @@ def test_CALL_arg_6
197201
end
198202
end
199203

204+
def test_call_node_no_arguments
205+
assert_error_message(ArgumentError, <<~END) do
206+
tried to create Proc object without a block (ArgumentError)
207+
208+
Proc.new
209+
^^^^
210+
END
211+
212+
Proc.new
213+
end
214+
end
215+
200216
def test_QCALL_1
201217
assert_error_message(NoMethodError, <<~END) do
202218
undefined method `foo' for #{ ONE_RECV_MESSAGE }
@@ -1339,7 +1355,7 @@ def test_spot_with_node
13391355
# We can't revisit instruction sequences to find node ids if the prism
13401356
# compiler was used instead of the parse.y compiler. In that case, we'll
13411357
# omit some tests.
1342-
omit if RubyVM::InstructionSequence.compile("").to_a[4][:parser] == :prism
1358+
omit if using_prism?
13431359

13441360
begin
13451361
raise_name_error

0 commit comments

Comments
 (0)