Skip to content

Commit

Permalink
Ensure first_line and last_line are set
Browse files Browse the repository at this point in the history
Fixes #58
  • Loading branch information
mame committed Jan 29, 2025
1 parent a221a4b commit 9ddc1f3
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
10 changes: 6 additions & 4 deletions lib/error_highlight/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,7 @@ def spot_call_for_name
end
elsif mid.to_s =~ /\A\W+\z/ && lines.match(/\G\s*(#{ Regexp.quote(mid) })=.*\n/, nd_recv.last_column)
@snippet = $` + $&
@beg_lineno = @end_lineno = lineno
@beg_column = $~.begin(1)
@end_column = $~.end(1)
end
Expand Down Expand Up @@ -582,8 +583,9 @@ def spot_colon2
@beg_column = nd_parent.last_column
@end_column = @node.last_column
else
@snippet = @fetch[@node.last_lineno]
fetch_line(@node.last_lineno)
if @snippet[...@node.last_column].match(/#{ Regexp.quote(const) }\z/)
@beg_lineno = @end_lineno = @node.last_lineno
@beg_column = $~.begin(0)
@end_column = $~.end(0)
end
Expand All @@ -597,7 +599,7 @@ def spot_op_cdecl
nd_lhs, op, _nd_rhs = @node.children
*nd_parent_lhs, _const = nd_lhs.children
if @name == op
@snippet = @fetch[nd_lhs.last_lineno]
fetch_line(nd_lhs.last_lineno)
if @snippet.match(/\G\s*(#{ Regexp.quote(op) })=/, nd_lhs.last_column)
@beg_column = $~.begin(1)
@end_column = $~.end(1)
Expand All @@ -607,12 +609,12 @@ def spot_op_cdecl
@end_column = nd_lhs.last_column
if nd_parent_lhs.empty? # example: ::C += 1
if nd_lhs.first_lineno == nd_lhs.last_lineno
@snippet = @fetch[nd_lhs.last_lineno]
fetch_line(nd_lhs.last_lineno)
@beg_column = nd_lhs.first_column
end
else # example: Foo::Bar::C += 1
if nd_parent_lhs.last.last_lineno == nd_lhs.last_lineno
@snippet = @fetch[nd_lhs.last_lineno]
fetch_line(nd_lhs.last_lineno)
@beg_column = nd_parent_lhs.last.last_column
end
end
Expand Down
9 changes: 9 additions & 0 deletions test/test_error_highlight.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,15 @@ def preprocess(msg)
def assert_error_message(klass, expected_msg, &blk)
omit unless klass < ErrorHighlight::CoreExt
err = assert_raise(klass, &blk)
spot = ErrorHighlight.spot(err)
if spot
assert_kind_of(Integer, spot[:first_lineno])
assert_kind_of(Integer, spot[:first_column])
assert_kind_of(Integer, spot[:last_lineno])
assert_kind_of(Integer, spot[:last_column])
assert_kind_of(String, spot[:snippet])
assert_kind_of(Array, spot[:script_lines])
end
assert_equal(preprocess(expected_msg).chomp, err.detailed_message(highlight: false).sub(/ \((?:NoMethod|Name)Error\)/, ""))
end
else
Expand Down

0 comments on commit 9ddc1f3

Please sign in to comment.