Skip to content

Commit 9ddc1f3

Browse files
committed
Ensure first_line and last_line are set
Fixes #58
1 parent a221a4b commit 9ddc1f3

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

lib/error_highlight/base.rb

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,7 @@ def spot_call_for_name
344344
end
345345
elsif mid.to_s =~ /\A\W+\z/ && lines.match(/\G\s*(#{ Regexp.quote(mid) })=.*\n/, nd_recv.last_column)
346346
@snippet = $` + $&
347+
@beg_lineno = @end_lineno = lineno
347348
@beg_column = $~.begin(1)
348349
@end_column = $~.end(1)
349350
end
@@ -582,8 +583,9 @@ def spot_colon2
582583
@beg_column = nd_parent.last_column
583584
@end_column = @node.last_column
584585
else
585-
@snippet = @fetch[@node.last_lineno]
586+
fetch_line(@node.last_lineno)
586587
if @snippet[...@node.last_column].match(/#{ Regexp.quote(const) }\z/)
588+
@beg_lineno = @end_lineno = @node.last_lineno
587589
@beg_column = $~.begin(0)
588590
@end_column = $~.end(0)
589591
end
@@ -597,7 +599,7 @@ def spot_op_cdecl
597599
nd_lhs, op, _nd_rhs = @node.children
598600
*nd_parent_lhs, _const = nd_lhs.children
599601
if @name == op
600-
@snippet = @fetch[nd_lhs.last_lineno]
602+
fetch_line(nd_lhs.last_lineno)
601603
if @snippet.match(/\G\s*(#{ Regexp.quote(op) })=/, nd_lhs.last_column)
602604
@beg_column = $~.begin(1)
603605
@end_column = $~.end(1)
@@ -607,12 +609,12 @@ def spot_op_cdecl
607609
@end_column = nd_lhs.last_column
608610
if nd_parent_lhs.empty? # example: ::C += 1
609611
if nd_lhs.first_lineno == nd_lhs.last_lineno
610-
@snippet = @fetch[nd_lhs.last_lineno]
612+
fetch_line(nd_lhs.last_lineno)
611613
@beg_column = nd_lhs.first_column
612614
end
613615
else # example: Foo::Bar::C += 1
614616
if nd_parent_lhs.last.last_lineno == nd_lhs.last_lineno
615-
@snippet = @fetch[nd_lhs.last_lineno]
617+
fetch_line(nd_lhs.last_lineno)
616618
@beg_column = nd_parent_lhs.last.last_column
617619
end
618620
end

test/test_error_highlight.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,15 @@ def preprocess(msg)
4444
def assert_error_message(klass, expected_msg, &blk)
4545
omit unless klass < ErrorHighlight::CoreExt
4646
err = assert_raise(klass, &blk)
47+
spot = ErrorHighlight.spot(err)
48+
if spot
49+
assert_kind_of(Integer, spot[:first_lineno])
50+
assert_kind_of(Integer, spot[:first_column])
51+
assert_kind_of(Integer, spot[:last_lineno])
52+
assert_kind_of(Integer, spot[:last_column])
53+
assert_kind_of(String, spot[:snippet])
54+
assert_kind_of(Array, spot[:script_lines])
55+
end
4756
assert_equal(preprocess(expected_msg).chomp, err.detailed_message(highlight: false).sub(/ \((?:NoMethod|Name)Error\)/, ""))
4857
end
4958
else

0 commit comments

Comments
 (0)