File tree Expand file tree Collapse file tree 2 files changed +16
-18
lines changed Expand file tree Collapse file tree 2 files changed +16
-18
lines changed Original file line number Diff line number Diff line change @@ -4,10 +4,9 @@ module ErrorHighlight
4
4
# Identify the code fragment that seems associated with a given error
5
5
#
6
6
# 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
9
9
# name: The name associated with the NameError/NoMethodError
10
- # fetch: A block to fetch a specified code line (or lines)
11
10
#
12
11
# Returns:
13
12
# {
@@ -22,16 +21,18 @@ def self.spot(...)
22
21
end
23
22
24
23
class Spotter
25
- def initialize ( node , point , name : nil , & fetch )
24
+ def initialize ( node , point_type : :name , name : nil )
26
25
@node = node
27
- @point = point
26
+ @point_type = point_type
28
27
@name = name
29
28
30
29
# Not-implemented-yet options
31
30
@arg = nil # Specify the index or keyword at which argument caused the TypeError/ArgumentError
32
31
@multiline = false # Allow multiline spot
33
32
34
- @fetch = fetch
33
+ @fetch = -> ( lineno , last_lineno = lineno ) do
34
+ @node . script_lines [ lineno - 1 .. last_lineno - 1 ] . join ( "" )
35
+ end
35
36
end
36
37
37
38
def spot
@@ -40,31 +41,31 @@ def spot
40
41
case @node . type
41
42
42
43
when :CALL , :QCALL
43
- case @point
44
+ case @point_type
44
45
when :name
45
46
spot_call_for_name
46
47
when :args
47
48
spot_call_for_args
48
49
end
49
50
50
51
when :ATTRASGN
51
- case @point
52
+ case @point_type
52
53
when :name
53
54
spot_attrasgn_for_name
54
55
when :args
55
56
spot_attrasgn_for_args
56
57
end
57
58
58
59
when :OPCALL
59
- case @point
60
+ case @point_type
60
61
when :name
61
62
spot_opcall_for_name
62
63
when :args
63
64
spot_opcall_for_args
64
65
end
65
66
66
67
when :FCALL
67
- case @point
68
+ case @point_type
68
69
when :name
69
70
spot_fcall_for_name
70
71
when :args
@@ -75,15 +76,15 @@ def spot
75
76
spot_vcall
76
77
77
78
when :OP_ASGN1
78
- case @point
79
+ case @point_type
79
80
when :name
80
81
spot_op_asgn1_for_name
81
82
when :args
82
83
spot_op_asgn1_for_args
83
84
end
84
85
85
86
when :OP_ASGN2
86
- case @point
87
+ case @point_type
87
88
when :name
88
89
spot_op_asgn2_for_name
89
90
when :args
Original file line number Diff line number Diff line change @@ -21,16 +21,13 @@ def to_s
21
21
22
22
case self
23
23
when NoMethodError , NameError
24
- point = :name
24
+ opts [ :point_type ] = :name
25
25
opts [ :name ] = name
26
26
when TypeError , ArgumentError
27
- point = :args
27
+ opts [ :point_type ] = :args
28
28
end
29
29
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 )
34
31
35
32
rescue Errno ::ENOENT
36
33
end
You can’t perform that action at this time.
0 commit comments