1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

[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 committed by git
parent f428ced69c
commit ca4e5b1eb3
2 changed files with 16 additions and 18 deletions

View file

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

View file

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