2021-06-18 04:11:39 -04:00
|
|
|
require_relative "version"
|
|
|
|
|
2021-06-28 00:27:35 -04:00
|
|
|
module ErrorHighlight
|
2021-06-18 04:11:39 -04:00
|
|
|
# Identify the code fragment that seems associated with a given error
|
|
|
|
#
|
|
|
|
# Arguments:
|
2021-06-29 23:31:55 -04:00
|
|
|
# node: RubyVM::AbstractSyntaxTree::Node (script_lines should be enabled)
|
|
|
|
# point_type: :name | :args
|
2021-06-18 04:11:39 -04:00
|
|
|
# name: The name associated with the NameError/NoMethodError
|
|
|
|
#
|
|
|
|
# Returns:
|
|
|
|
# {
|
|
|
|
# first_lineno: Integer,
|
|
|
|
# first_column: Integer,
|
|
|
|
# last_lineno: Integer,
|
|
|
|
# last_column: Integer,
|
2021-06-29 23:28:22 -04:00
|
|
|
# snippet: String,
|
2021-06-18 04:11:39 -04:00
|
|
|
# } | nil
|
|
|
|
def self.spot(...)
|
|
|
|
Spotter.new(...).spot
|
|
|
|
end
|
|
|
|
|
|
|
|
class Spotter
|
2021-07-12 03:47:24 -04:00
|
|
|
class NonAscii < Exception; end
|
|
|
|
private_constant :NonAscii
|
|
|
|
|
2021-06-29 23:31:55 -04:00
|
|
|
def initialize(node, point_type: :name, name: nil)
|
2021-06-18 04:11:39 -04:00
|
|
|
@node = node
|
2021-06-29 23:31:55 -04:00
|
|
|
@point_type = point_type
|
2021-06-18 04:11:39 -04:00
|
|
|
@name = name
|
|
|
|
|
|
|
|
# Not-implemented-yet options
|
|
|
|
@arg = nil # Specify the index or keyword at which argument caused the TypeError/ArgumentError
|
|
|
|
@multiline = false # Allow multiline spot
|
|
|
|
|
2021-06-29 23:31:55 -04:00
|
|
|
@fetch = -> (lineno, last_lineno = lineno) do
|
2021-07-12 03:47:24 -04:00
|
|
|
snippet = @node.script_lines[lineno - 1 .. last_lineno - 1].join("")
|
2021-07-13 03:53:52 -04:00
|
|
|
snippet += "\n" unless snippet.end_with?("\n")
|
2021-07-12 03:47:24 -04:00
|
|
|
|
|
|
|
# It require some work to support Unicode (or multibyte) characters.
|
|
|
|
# Tentatively, we stop highlighting if the code snippet has non-ascii characters.
|
|
|
|
# See https://github.com/ruby/error_highlight/issues/4
|
|
|
|
raise NonAscii unless snippet.ascii_only?
|
|
|
|
|
|
|
|
snippet
|
2021-06-29 23:31:55 -04:00
|
|
|
end
|
2021-06-18 04:11:39 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def spot
|
|
|
|
return nil unless @node
|
|
|
|
|
|
|
|
case @node.type
|
|
|
|
|
|
|
|
when :CALL, :QCALL
|
2021-06-29 23:31:55 -04:00
|
|
|
case @point_type
|
2021-06-18 04:11:39 -04:00
|
|
|
when :name
|
|
|
|
spot_call_for_name
|
|
|
|
when :args
|
|
|
|
spot_call_for_args
|
|
|
|
end
|
|
|
|
|
|
|
|
when :ATTRASGN
|
2021-06-29 23:31:55 -04:00
|
|
|
case @point_type
|
2021-06-18 04:11:39 -04:00
|
|
|
when :name
|
|
|
|
spot_attrasgn_for_name
|
|
|
|
when :args
|
|
|
|
spot_attrasgn_for_args
|
|
|
|
end
|
|
|
|
|
|
|
|
when :OPCALL
|
2021-06-29 23:31:55 -04:00
|
|
|
case @point_type
|
2021-06-18 04:11:39 -04:00
|
|
|
when :name
|
|
|
|
spot_opcall_for_name
|
|
|
|
when :args
|
|
|
|
spot_opcall_for_args
|
|
|
|
end
|
|
|
|
|
|
|
|
when :FCALL
|
2021-06-29 23:31:55 -04:00
|
|
|
case @point_type
|
2021-06-18 04:11:39 -04:00
|
|
|
when :name
|
|
|
|
spot_fcall_for_name
|
|
|
|
when :args
|
|
|
|
spot_fcall_for_args
|
|
|
|
end
|
|
|
|
|
|
|
|
when :VCALL
|
|
|
|
spot_vcall
|
|
|
|
|
|
|
|
when :OP_ASGN1
|
2021-06-29 23:31:55 -04:00
|
|
|
case @point_type
|
2021-06-18 04:11:39 -04:00
|
|
|
when :name
|
|
|
|
spot_op_asgn1_for_name
|
|
|
|
when :args
|
|
|
|
spot_op_asgn1_for_args
|
|
|
|
end
|
|
|
|
|
|
|
|
when :OP_ASGN2
|
2021-06-29 23:31:55 -04:00
|
|
|
case @point_type
|
2021-06-18 04:11:39 -04:00
|
|
|
when :name
|
|
|
|
spot_op_asgn2_for_name
|
|
|
|
when :args
|
|
|
|
spot_op_asgn2_for_args
|
|
|
|
end
|
|
|
|
|
|
|
|
when :CONST
|
|
|
|
spot_vcall
|
|
|
|
|
|
|
|
when :COLON2
|
|
|
|
spot_colon2
|
|
|
|
|
|
|
|
when :COLON3
|
|
|
|
spot_vcall
|
|
|
|
|
|
|
|
when :OP_CDECL
|
|
|
|
spot_op_cdecl
|
|
|
|
end
|
|
|
|
|
2021-06-29 23:28:22 -04:00
|
|
|
if @snippet && @beg_column && @end_column && @beg_column < @end_column
|
2021-06-18 04:11:39 -04:00
|
|
|
return {
|
|
|
|
first_lineno: @beg_lineno,
|
|
|
|
first_column: @beg_column,
|
|
|
|
last_lineno: @end_lineno,
|
|
|
|
last_column: @end_column,
|
2021-06-29 23:28:22 -04:00
|
|
|
snippet: @snippet,
|
2021-06-18 04:11:39 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
return nil
|
|
|
|
end
|
2021-07-12 03:47:24 -04:00
|
|
|
|
|
|
|
rescue NonAscii
|
|
|
|
nil
|
2021-06-18 04:11:39 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
# Example:
|
|
|
|
# x.foo
|
|
|
|
# ^^^^
|
|
|
|
# x.foo(42)
|
|
|
|
# ^^^^
|
|
|
|
# x&.foo
|
|
|
|
# ^^^^^
|
|
|
|
# x[42]
|
|
|
|
# ^^^^
|
|
|
|
# x += 1
|
|
|
|
# ^
|
|
|
|
def spot_call_for_name
|
|
|
|
nd_recv, mid, nd_args = @node.children
|
|
|
|
lineno = nd_recv.last_lineno
|
|
|
|
lines = @fetch[lineno, @node.last_lineno]
|
|
|
|
if mid == :[] && lines.match(/\G\s*(\[(?:\s*\])?)/, nd_recv.last_column)
|
|
|
|
@beg_column = $~.begin(1)
|
2021-06-29 23:28:22 -04:00
|
|
|
@snippet = lines[/.*\n/]
|
2021-06-18 04:11:39 -04:00
|
|
|
@beg_lineno = @end_lineno = lineno
|
|
|
|
if nd_args
|
2021-06-29 23:28:22 -04:00
|
|
|
if nd_recv.last_lineno == nd_args.last_lineno && @snippet.match(/\s*\]/, nd_args.last_column)
|
2021-06-18 04:11:39 -04:00
|
|
|
@end_column = $~.end(0)
|
|
|
|
end
|
|
|
|
else
|
|
|
|
if lines.match(/\G\s*?\[\s*\]/, nd_recv.last_column)
|
|
|
|
@end_column = $~.end(0)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
elsif lines.match(/\G\s*?(\&?\.)(\s*?)(#{ Regexp.quote(mid) }).*\n/, nd_recv.last_column)
|
|
|
|
lines = $` + $&
|
|
|
|
@beg_column = $~.begin($2.include?("\n") ? 3 : 1)
|
|
|
|
@end_column = $~.end(3)
|
|
|
|
if i = lines[..@beg_column].rindex("\n")
|
|
|
|
@beg_lineno = @end_lineno = lineno + lines[..@beg_column].count("\n")
|
2021-06-29 23:28:22 -04:00
|
|
|
@snippet = lines[i + 1..]
|
2021-06-18 04:11:39 -04:00
|
|
|
@beg_column -= i + 1
|
|
|
|
@end_column -= i + 1
|
|
|
|
else
|
2021-06-29 23:28:22 -04:00
|
|
|
@snippet = lines
|
2021-06-18 04:11:39 -04:00
|
|
|
@beg_lineno = @end_lineno = lineno
|
|
|
|
end
|
|
|
|
elsif mid.to_s =~ /\A\W+\z/ && lines.match(/\G\s*(#{ Regexp.quote(mid) })=.*\n/, nd_recv.last_column)
|
2021-06-29 23:28:22 -04:00
|
|
|
@snippet = $` + $&
|
2021-06-18 04:11:39 -04:00
|
|
|
@beg_column = $~.begin(1)
|
|
|
|
@end_column = $~.end(1)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Example:
|
|
|
|
# x.foo(42)
|
|
|
|
# ^^
|
|
|
|
# x[42]
|
|
|
|
# ^^
|
|
|
|
# x += 1
|
|
|
|
# ^
|
|
|
|
def spot_call_for_args
|
|
|
|
_nd_recv, _mid, nd_args = @node.children
|
|
|
|
if nd_args && nd_args.first_lineno == nd_args.last_lineno
|
|
|
|
fetch_line(nd_args.first_lineno)
|
|
|
|
@beg_column = nd_args.first_column
|
|
|
|
@end_column = nd_args.last_column
|
|
|
|
end
|
|
|
|
# TODO: support @arg
|
|
|
|
end
|
|
|
|
|
|
|
|
# Example:
|
|
|
|
# x.foo = 1
|
|
|
|
# ^^^^^^
|
|
|
|
# x[42] = 1
|
|
|
|
# ^^^^^^
|
|
|
|
def spot_attrasgn_for_name
|
|
|
|
nd_recv, mid, nd_args = @node.children
|
|
|
|
*nd_args, _nd_last_arg, _nil = nd_args.children
|
|
|
|
fetch_line(nd_recv.last_lineno)
|
2021-06-29 23:28:22 -04:00
|
|
|
if mid == :[]= && @snippet.match(/\G\s*(\[)/, nd_recv.last_column)
|
2021-06-18 04:11:39 -04:00
|
|
|
@beg_column = $~.begin(1)
|
|
|
|
args_last_column = $~.end(0)
|
|
|
|
if nd_args.last && nd_recv.last_lineno == nd_args.last.last_lineno
|
|
|
|
args_last_column = nd_args.last.last_column
|
|
|
|
end
|
2021-06-29 23:28:22 -04:00
|
|
|
if @snippet.match(/\s*\]\s*=/, args_last_column)
|
2021-06-18 04:11:39 -04:00
|
|
|
@end_column = $~.end(0)
|
|
|
|
end
|
2021-06-29 23:28:22 -04:00
|
|
|
elsif @snippet.match(/\G\s*(\.\s*#{ Regexp.quote(mid.to_s.sub(/=\z/, "")) }\s*=)/, nd_recv.last_column)
|
2021-06-18 04:11:39 -04:00
|
|
|
@beg_column = $~.begin(1)
|
|
|
|
@end_column = $~.end(1)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Example:
|
|
|
|
# x.foo = 1
|
|
|
|
# ^
|
|
|
|
# x[42] = 1
|
|
|
|
# ^^^^^^^
|
|
|
|
# x[] = 1
|
|
|
|
# ^^^^^
|
|
|
|
def spot_attrasgn_for_args
|
|
|
|
nd_recv, mid, nd_args = @node.children
|
|
|
|
fetch_line(nd_recv.last_lineno)
|
2021-06-29 23:28:22 -04:00
|
|
|
if mid == :[]= && @snippet.match(/\G\s*\[/, nd_recv.last_column)
|
2021-06-18 04:11:39 -04:00
|
|
|
@beg_column = $~.end(0)
|
|
|
|
if nd_recv.last_lineno == nd_args.last_lineno
|
|
|
|
@end_column = nd_args.last_column
|
|
|
|
end
|
|
|
|
elsif nd_args && nd_args.first_lineno == nd_args.last_lineno
|
|
|
|
@beg_column = nd_args.first_column
|
|
|
|
@end_column = nd_args.last_column
|
|
|
|
end
|
|
|
|
# TODO: support @arg
|
|
|
|
end
|
|
|
|
|
|
|
|
# Example:
|
|
|
|
# x + 1
|
|
|
|
# ^
|
|
|
|
# +x
|
|
|
|
# ^
|
|
|
|
def spot_opcall_for_name
|
|
|
|
nd_recv, op, nd_arg = @node.children
|
|
|
|
fetch_line(nd_recv.last_lineno)
|
|
|
|
if nd_arg
|
|
|
|
# binary operator
|
2021-06-29 23:28:22 -04:00
|
|
|
if @snippet.match(/\G\s*(#{ Regexp.quote(op) })/, nd_recv.last_column)
|
2021-06-18 04:11:39 -04:00
|
|
|
@beg_column = $~.begin(1)
|
|
|
|
@end_column = $~.end(1)
|
|
|
|
end
|
|
|
|
else
|
|
|
|
# unary operator
|
2021-06-29 23:28:22 -04:00
|
|
|
if @snippet[...nd_recv.first_column].match(/(#{ Regexp.quote(op.to_s.sub(/@\z/, "")) })\s*\(?\s*\z/)
|
2021-06-18 04:11:39 -04:00
|
|
|
@beg_column = $~.begin(1)
|
|
|
|
@end_column = $~.end(1)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Example:
|
|
|
|
# x + 1
|
|
|
|
# ^
|
|
|
|
def spot_opcall_for_args
|
|
|
|
_nd_recv, _op, nd_arg = @node.children
|
|
|
|
if nd_arg && nd_arg.first_lineno == nd_arg.last_lineno
|
|
|
|
# binary operator
|
|
|
|
fetch_line(nd_arg.first_lineno)
|
|
|
|
@beg_column = nd_arg.first_column
|
|
|
|
@end_column = nd_arg.last_column
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Example:
|
|
|
|
# foo(42)
|
|
|
|
# ^^^
|
|
|
|
# foo 42
|
|
|
|
# ^^^
|
|
|
|
def spot_fcall_for_name
|
|
|
|
mid, _nd_args = @node.children
|
|
|
|
fetch_line(@node.first_lineno)
|
2021-06-29 23:28:22 -04:00
|
|
|
if @snippet.match(/(#{ Regexp.quote(mid) })/, @node.first_column)
|
2021-06-18 04:11:39 -04:00
|
|
|
@beg_column = $~.begin(1)
|
|
|
|
@end_column = $~.end(1)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Example:
|
|
|
|
# foo(42)
|
|
|
|
# ^^
|
|
|
|
# foo 42
|
|
|
|
# ^^
|
|
|
|
def spot_fcall_for_args
|
|
|
|
_mid, nd_args = @node.children
|
|
|
|
if nd_args && nd_args.first_lineno == nd_args.last_lineno
|
|
|
|
# binary operator
|
|
|
|
fetch_line(nd_args.first_lineno)
|
|
|
|
@beg_column = nd_args.first_column
|
|
|
|
@end_column = nd_args.last_column
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Example:
|
|
|
|
# foo
|
|
|
|
# ^^^
|
|
|
|
def spot_vcall
|
|
|
|
if @node.first_lineno == @node.last_lineno
|
|
|
|
fetch_line(@node.last_lineno)
|
|
|
|
@beg_column = @node.first_column
|
|
|
|
@end_column = @node.last_column
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Example:
|
|
|
|
# x[1] += 42
|
|
|
|
# ^^^ (for [])
|
|
|
|
# x[1] += 42
|
|
|
|
# ^ (for +)
|
|
|
|
# x[1] += 42
|
|
|
|
# ^^^^^^ (for []=)
|
|
|
|
def spot_op_asgn1_for_name
|
|
|
|
nd_recv, op, nd_args, _nd_rhs = @node.children
|
|
|
|
fetch_line(nd_recv.last_lineno)
|
2021-06-29 23:28:22 -04:00
|
|
|
if @snippet.match(/\G\s*(\[)/, nd_recv.last_column)
|
2021-06-18 04:11:39 -04:00
|
|
|
bracket_beg_column = $~.begin(1)
|
|
|
|
args_last_column = $~.end(0)
|
|
|
|
if nd_args && nd_recv.last_lineno == nd_args.last_lineno
|
|
|
|
args_last_column = nd_args.last_column
|
|
|
|
end
|
2021-06-29 23:28:22 -04:00
|
|
|
if @snippet.match(/\s*\](\s*)(#{ Regexp.quote(op) })=()/, args_last_column)
|
2021-06-18 04:11:39 -04:00
|
|
|
case @name
|
|
|
|
when :[], :[]=
|
|
|
|
@beg_column = bracket_beg_column
|
|
|
|
@end_column = $~.begin(@name == :[] ? 1 : 3)
|
|
|
|
when op
|
|
|
|
@beg_column = $~.begin(2)
|
|
|
|
@end_column = $~.end(2)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Example:
|
|
|
|
# x[1] += 42
|
|
|
|
# ^^^^^^^^
|
|
|
|
def spot_op_asgn1_for_args
|
|
|
|
nd_recv, mid, nd_args, nd_rhs = @node.children
|
|
|
|
fetch_line(nd_recv.last_lineno)
|
2021-06-29 23:28:22 -04:00
|
|
|
if mid == :[]= && @snippet.match(/\G\s*\[/, nd_recv.last_column)
|
2021-06-18 04:11:39 -04:00
|
|
|
@beg_column = $~.end(0)
|
|
|
|
if nd_recv.last_lineno == nd_rhs.last_lineno
|
|
|
|
@end_column = nd_rhs.last_column
|
|
|
|
end
|
|
|
|
elsif nd_args && nd_args.first_lineno == nd_rhs.last_lineno
|
|
|
|
@beg_column = nd_args.first_column
|
|
|
|
@end_column = nd_rhs.last_column
|
|
|
|
end
|
|
|
|
# TODO: support @arg
|
|
|
|
end
|
|
|
|
|
|
|
|
# Example:
|
|
|
|
# x.foo += 42
|
|
|
|
# ^^^ (for foo)
|
|
|
|
# x.foo += 42
|
|
|
|
# ^ (for +)
|
|
|
|
# x.foo += 42
|
|
|
|
# ^^^^^^^ (for foo=)
|
|
|
|
def spot_op_asgn2_for_name
|
|
|
|
nd_recv, _qcall, attr, op, _nd_rhs = @node.children
|
|
|
|
fetch_line(nd_recv.last_lineno)
|
2021-06-29 23:28:22 -04:00
|
|
|
if @snippet.match(/\G\s*(\.)\s*#{ Regexp.quote(attr) }()\s*(#{ Regexp.quote(op) })(=)/, nd_recv.last_column)
|
2021-06-18 04:11:39 -04:00
|
|
|
case @name
|
|
|
|
when attr
|
|
|
|
@beg_column = $~.begin(1)
|
|
|
|
@end_column = $~.begin(2)
|
|
|
|
when op
|
|
|
|
@beg_column = $~.begin(3)
|
|
|
|
@end_column = $~.end(3)
|
|
|
|
when :"#{ attr }="
|
|
|
|
@beg_column = $~.begin(1)
|
|
|
|
@end_column = $~.end(4)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Example:
|
|
|
|
# x.foo += 42
|
|
|
|
# ^^
|
|
|
|
def spot_op_asgn2_for_args
|
|
|
|
_nd_recv, _qcall, _attr, _op, nd_rhs = @node.children
|
|
|
|
if nd_rhs.first_lineno == nd_rhs.last_lineno
|
|
|
|
fetch_line(nd_rhs.first_lineno)
|
|
|
|
@beg_column = nd_rhs.first_column
|
|
|
|
@end_column = nd_rhs.last_column
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Example:
|
|
|
|
# Foo::Bar
|
|
|
|
# ^^^^^
|
|
|
|
def spot_colon2
|
|
|
|
nd_parent, const = @node.children
|
|
|
|
if nd_parent.last_lineno == @node.last_lineno
|
|
|
|
fetch_line(nd_parent.last_lineno)
|
|
|
|
@beg_column = nd_parent.last_column
|
|
|
|
@end_column = @node.last_column
|
|
|
|
else
|
2021-06-29 23:28:22 -04:00
|
|
|
@snippet = @fetch[@node.last_lineno]
|
|
|
|
if @snippet[...@node.last_column].match(/#{ Regexp.quote(const) }\z/)
|
2021-06-18 04:11:39 -04:00
|
|
|
@beg_column = $~.begin(0)
|
|
|
|
@end_column = $~.end(0)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Example:
|
|
|
|
# Foo::Bar += 1
|
|
|
|
# ^^^^^^^^
|
|
|
|
def spot_op_cdecl
|
|
|
|
nd_lhs, op, _nd_rhs = @node.children
|
|
|
|
*nd_parent_lhs, _const = nd_lhs.children
|
|
|
|
if @name == op
|
2021-06-29 23:28:22 -04:00
|
|
|
@snippet = @fetch[nd_lhs.last_lineno]
|
|
|
|
if @snippet.match(/\G\s*(#{ Regexp.quote(op) })=/, nd_lhs.last_column)
|
2021-06-18 04:11:39 -04:00
|
|
|
@beg_column = $~.begin(1)
|
|
|
|
@end_column = $~.end(1)
|
|
|
|
end
|
|
|
|
else
|
|
|
|
# constant access error
|
|
|
|
@end_column = nd_lhs.last_column
|
|
|
|
if nd_parent_lhs.empty? # example: ::C += 1
|
|
|
|
if nd_lhs.first_lineno == nd_lhs.last_lineno
|
2021-06-29 23:28:22 -04:00
|
|
|
@snippet = @fetch[nd_lhs.last_lineno]
|
2021-06-18 04:11:39 -04:00
|
|
|
@beg_column = nd_lhs.first_column
|
|
|
|
end
|
|
|
|
else # example: Foo::Bar::C += 1
|
|
|
|
if nd_parent_lhs.last.last_lineno == nd_lhs.last_lineno
|
2021-06-29 23:28:22 -04:00
|
|
|
@snippet = @fetch[nd_lhs.last_lineno]
|
2021-06-18 04:11:39 -04:00
|
|
|
@beg_column = nd_parent_lhs.last.last_column
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def fetch_line(lineno)
|
|
|
|
@beg_lineno = @end_lineno = lineno
|
2021-06-29 23:28:22 -04:00
|
|
|
@snippet = @fetch[lineno]
|
2021-06-18 04:11:39 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private_constant :Spotter
|
|
|
|
end
|