Skip to content

Commit acb2046

Browse files
committed
Reconsider the API of ErrorHighlight.spot
1 parent f40a1de commit acb2046

File tree

2 files changed

+16
-18
lines changed

2 files changed

+16
-18
lines changed

lib/error_highlight/base.rb

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,9 @@ module ErrorHighlight
44
# Identify the code fragment that seems associated with a given error
55
#
66
# Arguments:
7-
# node: RubyVM::AbstractSyntaxTree::Node
8-
# point: :name | :args
7+
# node: RubyVM::AbstractSyntaxTree::Node (script_lines should be enabled)
8+
# point_type: :name | :args
99
# name: The name associated with the NameError/NoMethodError
10-
# fetch: A block to fetch a specified code line (or lines)
1110
#
1211
# Returns:
1312
# {
@@ -22,16 +21,18 @@ def self.spot(...)
2221
end
2322

2423
class Spotter
25-
def initialize(node, point, name: nil, &fetch)
24+
def initialize(node, point_type: :name, name: nil)
2625
@node = node
27-
@point = point
26+
@point_type = point_type
2827
@name = name
2928

3029
# Not-implemented-yet options
3130
@arg = nil # Specify the index or keyword at which argument caused the TypeError/ArgumentError
3231
@multiline = false # Allow multiline spot
3332

34-
@fetch = fetch
33+
@fetch = -> (lineno, last_lineno = lineno) do
34+
@node.script_lines[lineno - 1 .. last_lineno - 1].join("")
35+
end
3536
end
3637

3738
def spot
@@ -40,31 +41,31 @@ def spot
4041
case @node.type
4142

4243
when :CALL, :QCALL
43-
case @point
44+
case @point_type
4445
when :name
4546
spot_call_for_name
4647
when :args
4748
spot_call_for_args
4849
end
4950

5051
when :ATTRASGN
51-
case @point
52+
case @point_type
5253
when :name
5354
spot_attrasgn_for_name
5455
when :args
5556
spot_attrasgn_for_args
5657
end
5758

5859
when :OPCALL
59-
case @point
60+
case @point_type
6061
when :name
6162
spot_opcall_for_name
6263
when :args
6364
spot_opcall_for_args
6465
end
6566

6667
when :FCALL
67-
case @point
68+
case @point_type
6869
when :name
6970
spot_fcall_for_name
7071
when :args
@@ -75,15 +76,15 @@ def spot
7576
spot_vcall
7677

7778
when :OP_ASGN1
78-
case @point
79+
case @point_type
7980
when :name
8081
spot_op_asgn1_for_name
8182
when :args
8283
spot_op_asgn1_for_args
8384
end
8485

8586
when :OP_ASGN2
86-
case @point
87+
case @point_type
8788
when :name
8889
spot_op_asgn2_for_name
8990
when :args

lib/error_highlight/core_ext.rb

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,13 @@ def to_s
2121

2222
case self
2323
when NoMethodError, NameError
24-
point = :name
24+
opts[:point_type] = :name
2525
opts[:name] = name
2626
when TypeError, ArgumentError
27-
point = :args
27+
opts[:point_type] = :args
2828
end
2929

30-
spot = ErrorHighlight.spot(node, point, **opts) do |lineno, last_lineno|
31-
last_lineno ||= lineno
32-
node.script_lines[lineno - 1 .. last_lineno - 1].join("")
33-
end
30+
spot = ErrorHighlight.spot(node, **opts)
3431

3532
rescue Errno::ENOENT
3633
end

0 commit comments

Comments
 (0)