Skip to content

Commit

Permalink
Reconsider the API of ErrorHighlight.spot
Browse files Browse the repository at this point in the history
  • Loading branch information
mame committed Jun 30, 2021
1 parent f40a1de commit acb2046
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 18 deletions.
25 changes: 13 additions & 12 deletions lib/error_highlight/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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:
# {
Expand All @@ -22,16 +21,18 @@ def self.spot(...)
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
Expand All @@ -40,31 +41,31 @@ def spot
case @node.type

when :CALL, :QCALL
case @point
case @point_type
when :name
spot_call_for_name
when :args
spot_call_for_args
end

when :ATTRASGN
case @point
case @point_type
when :name
spot_attrasgn_for_name
when :args
spot_attrasgn_for_args
end

when :OPCALL
case @point
case @point_type
when :name
spot_opcall_for_name
when :args
spot_opcall_for_args
end

when :FCALL
case @point
case @point_type
when :name
spot_fcall_for_name
when :args
Expand All @@ -75,15 +76,15 @@ def spot
spot_vcall

when :OP_ASGN1
case @point
case @point_type
when :name
spot_op_asgn1_for_name
when :args
spot_op_asgn1_for_args
end

when :OP_ASGN2
case @point
case @point_type
when :name
spot_op_asgn2_for_name
when :args
Expand Down
9 changes: 3 additions & 6 deletions lib/error_highlight/core_ext.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,13 @@ def to_s

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
Expand Down

0 comments on commit acb2046

Please sign in to comment.