Skip to content

Commit b79d679

Browse files
pockemame
authored andcommitted
Keep it work if paren exists after receiver
1 parent 25ef7db commit b79d679

File tree

2 files changed

+171
-11
lines changed

2 files changed

+171
-11
lines changed

lib/error_highlight/base.rb

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ def spot_call_for_name
148148
nd_recv, mid, nd_args = @node.children
149149
lineno = nd_recv.last_lineno
150150
lines = @fetch[lineno, @node.last_lineno]
151-
if mid == :[] && lines.match(/\G\s*(\[(?:\s*\])?)/, nd_recv.last_column)
151+
if mid == :[] && lines.match(/\G[\s)]*(\[(?:\s*\])?)/, nd_recv.last_column)
152152
@beg_column = $~.begin(1)
153153
@snippet = lines[/.*\n/]
154154
@beg_lineno = @end_lineno = lineno
@@ -157,11 +157,11 @@ def spot_call_for_name
157157
@end_column = $~.end(0)
158158
end
159159
else
160-
if lines.match(/\G\s*?\[\s*\]/, nd_recv.last_column)
160+
if lines.match(/\G[\s)]*?\[\s*\]/, nd_recv.last_column)
161161
@end_column = $~.end(0)
162162
end
163163
end
164-
elsif lines.match(/\G\s*?(\&?\.)(\s*?)(#{ Regexp.quote(mid) }).*\n/, nd_recv.last_column)
164+
elsif lines.match(/\G[\s)]*?(\&?\.)(\s*?)(#{ Regexp.quote(mid) }).*\n/, nd_recv.last_column)
165165
lines = $` + $&
166166
@beg_column = $~.begin($2.include?("\n") ? 3 : 1)
167167
@end_column = $~.end(3)
@@ -207,16 +207,16 @@ def spot_attrasgn_for_name
207207
nd_recv, mid, nd_args = @node.children
208208
*nd_args, _nd_last_arg, _nil = nd_args.children
209209
fetch_line(nd_recv.last_lineno)
210-
if mid == :[]= && @snippet.match(/\G\s*(\[)/, nd_recv.last_column)
210+
if mid == :[]= && @snippet.match(/\G[\s)]*(\[)/, nd_recv.last_column)
211211
@beg_column = $~.begin(1)
212212
args_last_column = $~.end(0)
213213
if nd_args.last && nd_recv.last_lineno == nd_args.last.last_lineno
214214
args_last_column = nd_args.last.last_column
215215
end
216-
if @snippet.match(/\s*\]\s*=/, args_last_column)
216+
if @snippet.match(/[\s)]*\]\s*=/, args_last_column)
217217
@end_column = $~.end(0)
218218
end
219-
elsif @snippet.match(/\G\s*(\.\s*#{ Regexp.quote(mid.to_s.sub(/=\z/, "")) }\s*=)/, nd_recv.last_column)
219+
elsif @snippet.match(/\G[\s)]*(\.\s*#{ Regexp.quote(mid.to_s.sub(/=\z/, "")) }\s*=)/, nd_recv.last_column)
220220
@beg_column = $~.begin(1)
221221
@end_column = $~.end(1)
222222
end
@@ -232,7 +232,7 @@ def spot_attrasgn_for_name
232232
def spot_attrasgn_for_args
233233
nd_recv, mid, nd_args = @node.children
234234
fetch_line(nd_recv.last_lineno)
235-
if mid == :[]= && @snippet.match(/\G\s*\[/, nd_recv.last_column)
235+
if mid == :[]= && @snippet.match(/\G[\s)]*\[/, nd_recv.last_column)
236236
@beg_column = $~.end(0)
237237
if nd_recv.last_lineno == nd_args.last_lineno
238238
@end_column = nd_args.last_column
@@ -254,7 +254,7 @@ def spot_opcall_for_name
254254
fetch_line(nd_recv.last_lineno)
255255
if nd_arg
256256
# binary operator
257-
if @snippet.match(/\G\s*(#{ Regexp.quote(op) })/, nd_recv.last_column)
257+
if @snippet.match(/\G[\s)]*(#{ Regexp.quote(op) })/, nd_recv.last_column)
258258
@beg_column = $~.begin(1)
259259
@end_column = $~.end(1)
260260
end
@@ -330,7 +330,7 @@ def spot_vcall
330330
def spot_op_asgn1_for_name
331331
nd_recv, op, nd_args, _nd_rhs = @node.children
332332
fetch_line(nd_recv.last_lineno)
333-
if @snippet.match(/\G\s*(\[)/, nd_recv.last_column)
333+
if @snippet.match(/\G[\s)]*(\[)/, nd_recv.last_column)
334334
bracket_beg_column = $~.begin(1)
335335
args_last_column = $~.end(0)
336336
if nd_args && nd_recv.last_lineno == nd_args.last_lineno
@@ -377,7 +377,7 @@ def spot_op_asgn1_for_args
377377
def spot_op_asgn2_for_name
378378
nd_recv, _qcall, attr, op, _nd_rhs = @node.children
379379
fetch_line(nd_recv.last_lineno)
380-
if @snippet.match(/\G\s*(\.)\s*#{ Regexp.quote(attr) }()\s*(#{ Regexp.quote(op) })(=)/, nd_recv.last_column)
380+
if @snippet.match(/\G[\s)]*(\.)\s*#{ Regexp.quote(attr) }()\s*(#{ Regexp.quote(op) })(=)/, nd_recv.last_column)
381381
case @name
382382
when attr
383383
@beg_column = $~.begin(1)

test/test_error_highlight.rb

Lines changed: 161 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,18 @@ def test_CALL_noarg_3
6666
end
6767
end
6868

69+
def test_CALL_noarg_4
70+
assert_error_message(NoMethodError, <<~END) do
71+
undefined method `foo' for nil:NilClass
72+
73+
(nil).foo + 1
74+
^^^^
75+
END
76+
77+
(nil).foo + 1
78+
end
79+
end
80+
6981
def test_CALL_arg_1
7082
assert_error_message(NoMethodError, <<~END) do
7183
undefined method `foo' for nil:NilClass
@@ -222,6 +234,18 @@ def test_CALL_aref_4
222234
end
223235
end
224236

237+
def test_CALL_aref_5
238+
assert_error_message(NoMethodError, <<~END) do
239+
undefined method `[]' for nil:NilClass
240+
241+
(nil)[ ]
242+
^^^
243+
END
244+
245+
(nil)[ ]
246+
end
247+
end
248+
225249
def test_CALL_aset
226250
assert_error_message(NoMethodError, <<~END) do
227251
undefined method `[]=' for nil:NilClass
@@ -313,6 +337,30 @@ def test_ATTRASGN_3
313337
end
314338
end
315339

340+
def test_ATTRASGN_4
341+
assert_error_message(NoMethodError, <<~END) do
342+
undefined method `[]=' for nil:NilClass
343+
344+
(nil)[0] = 42
345+
^^^^^
346+
END
347+
348+
(nil)[0] = 42
349+
end
350+
end
351+
352+
def test_ATTRASGN_5
353+
assert_error_message(NoMethodError, <<~END) do
354+
undefined method `foo=' for nil:NilClass
355+
356+
(nil).foo = 42
357+
^^^^^^
358+
END
359+
360+
(nil).foo = 42
361+
end
362+
end
363+
316364
def test_OPCALL_binary_1
317365
assert_error_message(NoMethodError, <<~END) do
318366
undefined method `+' for nil:NilClass
@@ -338,7 +386,19 @@ def test_OPCALL_binary_2
338386
end
339387
end
340388

341-
def test_OPCALL_unary
389+
def test_OPCALL_binary_3
390+
assert_error_message(NoMethodError, <<~END) do
391+
undefined method `+' for nil:NilClass
392+
393+
(nil) + 42
394+
^
395+
END
396+
397+
(nil) + 42
398+
end
399+
end
400+
401+
def test_OPCALL_unary_1
342402
assert_error_message(NoMethodError, <<~END) do
343403
undefined method `+@' for nil:NilClass
344404
@@ -350,6 +410,18 @@ def test_OPCALL_unary
350410
end
351411
end
352412

413+
def test_OPCALL_unary_2
414+
assert_error_message(NoMethodError, <<~END) do
415+
undefined method `+@' for nil:NilClass
416+
417+
+(nil)
418+
^
419+
END
420+
421+
+(nil)
422+
end
423+
end
424+
353425
def test_FCALL_1
354426
assert_error_message(NoMethodError, <<~END) do
355427
undefined method `foo' for nil:NilClass
@@ -429,6 +501,20 @@ def test_OP_ASGN1_aref_3
429501
end
430502
end
431503

504+
def test_OP_ASGN1_aref_4
505+
v = nil
506+
507+
assert_error_message(NoMethodError, <<~END) do
508+
undefined method `[]' for nil:NilClass
509+
510+
(v)[0] += 42
511+
^^^
512+
END
513+
514+
(v)[0] += 42
515+
end
516+
end
517+
432518
def test_OP_ASGN1_op_1
433519
v = Object.new
434520
def v.[](x); nil; end
@@ -475,6 +561,21 @@ def v.[](x); nil; end
475561
end
476562
end
477563

564+
def test_OP_ASGN1_op_4
565+
v = Object.new
566+
def v.[](x); nil; end
567+
568+
assert_error_message(NoMethodError, <<~END) do
569+
undefined method `+' for nil:NilClass
570+
571+
(v)[0] += 42
572+
^
573+
END
574+
575+
(v)[0] += 42
576+
end
577+
end
578+
478579
def test_OP_ASGN1_aset_1
479580
v = Object.new
480581
def v.[](x); 1; end
@@ -521,6 +622,21 @@ def v.[](x); 1; end
521622
end
522623
end
523624

625+
def test_OP_ASGN1_aset_4
626+
v = Object.new
627+
def v.[](x); 1; end
628+
629+
assert_error_message(NoMethodError, <<~END) do
630+
undefined method `[]=' for #{ v.inspect }
631+
632+
(v)[0] += 42
633+
^^^^^^
634+
END
635+
636+
(v)[0] += 42
637+
end
638+
end
639+
524640
def test_OP_ASGN2_read_1
525641
v = nil
526642

@@ -550,6 +666,20 @@ def test_OP_ASGN2_read_2
550666
end
551667
end
552668

669+
def test_OP_ASGN2_read_3
670+
v = nil
671+
672+
assert_error_message(NoMethodError, <<~END) do
673+
undefined method `foo' for nil:NilClass
674+
675+
(v).foo += 42
676+
^^^^
677+
END
678+
679+
(v).foo += 42
680+
end
681+
end
682+
553683
def test_OP_ASGN2_op_1
554684
v = Object.new
555685
def v.foo; nil; end
@@ -581,6 +711,21 @@ def v.foo; nil; end
581711
end
582712
end
583713

714+
def test_OP_ASGN2_op_3
715+
v = Object.new
716+
def v.foo; nil; end
717+
718+
assert_error_message(NoMethodError, <<~END) do
719+
undefined method `+' for nil:NilClass
720+
721+
(v).foo += 42
722+
^
723+
END
724+
725+
(v).foo += 42
726+
end
727+
end
728+
584729
def test_OP_ASGN2_write_1
585730
v = Object.new
586731
def v.foo; 1; end
@@ -612,6 +757,21 @@ def v.foo; 1; end
612757
end
613758
end
614759

760+
def test_OP_ASGN2_write_3
761+
v = Object.new
762+
def v.foo; 1; end
763+
764+
assert_error_message(NoMethodError, <<~END) do
765+
undefined method `foo=' for #{ v.inspect }
766+
767+
(v).foo += 42
768+
^^^^^^^
769+
END
770+
771+
(v).foo += 42
772+
end
773+
end
774+
615775
def test_CONST
616776
assert_error_message(NameError, <<~END) do
617777
uninitialized constant ErrorHighlightTest::NotDefined

0 commit comments

Comments
 (0)