summaryrefslogtreecommitdiff
path: root/lib/error_highlight/base.rb
diff options
context:
space:
mode:
authorYusuke Endoh <[email protected]>2021-06-30 12:31:55 +0900
committergit <[email protected]>2021-06-30 12:49:27 +0900
commitca4e5b1eb33f3bae9ced2e7643ae7db3e11fa65d (patch)
tree84d6f1063974ba212da96066ae1eeb68f293b0cf /lib/error_highlight/base.rb
parentf428ced69c70473b8405aae9c98828aa6f69b254 (diff)
[ruby/error_highlight] Reconsider the API of ErrorHighlight.spot
https://github.com/ruby/error_highlight/commit/acb2046a82
Diffstat (limited to 'lib/error_highlight/base.rb')
-rw-r--r--lib/error_highlight/base.rb25
1 files changed, 13 insertions, 12 deletions
diff --git a/lib/error_highlight/base.rb b/lib/error_highlight/base.rb
index 5f3a86bbe2..ee6545b372 100644
--- a/lib/error_highlight/base.rb
+++ b/lib/error_highlight/base.rb
@@ -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