[ruby/error_highlight] Reconsider the API of ErrorHighlight.spot

https://github.com/ruby/error_highlight/commit/acb2046a82
This commit is contained in:
Yusuke Endoh 2021-06-30 12:31:55 +09:00 коммит произвёл git
Родитель f428ced69c
Коммит ca4e5b1eb3
2 изменённых файлов: 16 добавлений и 18 удалений

Просмотреть файл

@ -4,10 +4,9 @@ module ErrorHighlight
# Identify the code fragment that seems associated with a given error
#
# Arguments:
# node: RubyVM::AbstractSyntaxTree::Node
# point: :name | :args
# node: RubyVM::AbstractSyntaxTree::Node (script_lines should be enabled)
# point_type: :name | :args
# name: The name associated with the NameError/NoMethodError
# fetch: A block to fetch a specified code line (or lines)
#
# Returns:
# {
@ -22,16 +21,18 @@ module ErrorHighlight
end
class Spotter
def initialize(node, point, name: nil, &fetch)
def initialize(node, point_type: :name, name: nil)
@node = node
@point = point
@point_type = point_type
@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
@fetch = fetch
@fetch = -> (lineno, last_lineno = lineno) do
@node.script_lines[lineno - 1 .. last_lineno - 1].join("")
end
end
def spot
@ -40,7 +41,7 @@ module ErrorHighlight
case @node.type
when :CALL, :QCALL
case @point
case @point_type
when :name
spot_call_for_name
when :args
@ -48,7 +49,7 @@ module ErrorHighlight
end
when :ATTRASGN
case @point
case @point_type
when :name
spot_attrasgn_for_name
when :args
@ -56,7 +57,7 @@ module ErrorHighlight
end
when :OPCALL
case @point
case @point_type
when :name
spot_opcall_for_name
when :args
@ -64,7 +65,7 @@ module ErrorHighlight
end
when :FCALL
case @point
case @point_type
when :name
spot_fcall_for_name
when :args
@ -75,7 +76,7 @@ module ErrorHighlight
spot_vcall
when :OP_ASGN1
case @point
case @point_type
when :name
spot_op_asgn1_for_name
when :args
@ -83,7 +84,7 @@ module ErrorHighlight
end
when :OP_ASGN2
case @point
case @point_type
when :name
spot_op_asgn2_for_name
when :args

Просмотреть файл

@ -21,16 +21,13 @@ module ErrorHighlight
case self
when NoMethodError, NameError
point = :name
opts[:point_type] = :name
opts[:name] = name
when TypeError, ArgumentError
point = :args
opts[:point_type] = :args
end
spot = ErrorHighlight.spot(node, point, **opts) do |lineno, last_lineno|
last_lineno ||= lineno
node.script_lines[lineno - 1 .. last_lineno - 1].join("")
end
spot = ErrorHighlight.spot(node, **opts)
rescue Errno::ENOENT
end