summaryrefslogtreecommitdiff
path: root/lib/error_highlight/base.rb
diff options
context:
space:
mode:
Diffstat (limited to 'lib/error_highlight/base.rb')
-rw-r--r--lib/error_highlight/base.rb53
1 files changed, 50 insertions, 3 deletions
diff --git a/lib/error_highlight/base.rb b/lib/error_highlight/base.rb
index dbd173a5cd..b9c68b8eb8 100644
--- a/lib/error_highlight/base.rb
+++ b/lib/error_highlight/base.rb
@@ -22,6 +22,14 @@ module ErrorHighlight
# snippet: String,
# script_lines: [String],
# } | nil
+ #
+ # Limitations:
+ #
+ # Currently, ErrorHighlight.spot only supports a single-line code fragment.
+ # Therefore, if the return value is not nil, first_lineno and last_lineno will have
+ # the same value. If the relevant code fragment spans multiple lines
+ # (e.g., Array#[] of +ary[(newline)expr(newline)]+), the method will return nil.
+ # This restriction may be removed in the future.
def self.spot(obj, **opts)
case obj
when Exception
@@ -46,13 +54,21 @@ module ErrorHighlight
return nil unless Thread::Backtrace::Location === loc
- node = RubyVM::AbstractSyntaxTree.of(loc, keep_script_lines: true)
+ node =
+ begin
+ RubyVM::AbstractSyntaxTree.of(loc, keep_script_lines: true)
+ rescue RuntimeError => error
+ # RubyVM::AbstractSyntaxTree.of raises an error with a message that
+ # includes "prism" when the ISEQ was compiled with the prism compiler.
+ # In this case, we'll set the node to `nil`. In the future, we will
+ # reparse with the prism parser and pass the parsed node to Spotter.
+ raise unless error.message.include?("prism")
+ end
Spotter.new(node, **opts).spot
when RubyVM::AbstractSyntaxTree::Node
- # Just for compatibility
- Spotter.new(node, **opts).spot
+ Spotter.new(obj, **opts).spot
else
raise TypeError, "Exception is expected"
@@ -91,9 +107,40 @@ module ErrorHighlight
end
end
+ OPT_GETCONSTANT_PATH = (RUBY_VERSION.split(".").map {|s| s.to_i } <=> [3, 2]) >= 0
+ private_constant :OPT_GETCONSTANT_PATH
+
def spot
return nil unless @node
+ if OPT_GETCONSTANT_PATH && @node.type == :COLON2
+ # In Ruby 3.2 or later, a nested constant access (like `Foo::Bar::Baz`)
+ # is compiled to one instruction (opt_getconstant_path).
+ # @node points to the node of the whole `Foo::Bar::Baz` even if `Foo`
+ # or `Foo::Bar` causes NameError.
+ # So we try to spot the sub-node that causes the NameError by using
+ # `NameError#name`.
+ subnodes = []
+ node = @node
+ while node.type == :COLON2
+ node2, const = node.children
+ subnodes << node if const == @name
+ node = node2
+ end
+ if node.type == :CONST || node.type == :COLON3
+ if node.children.first == @name
+ subnodes << node
+ end
+
+ # If we found only one sub-node whose name is equal to @name, use it
+ return nil if subnodes.size != 1
+ @node = subnodes.first
+ else
+ # Do nothing; opt_getconstant_path is used only when the const base is
+ # NODE_CONST (`Foo`) or NODE_COLON3 (`::Foo`)
+ end
+ end
+
case @node.type
when :CALL, :QCALL