summaryrefslogtreecommitdiff
path: root/lib/error_highlight
diff options
context:
space:
mode:
Diffstat (limited to 'lib/error_highlight')
-rw-r--r--lib/error_highlight/base.rb107
-rw-r--r--lib/error_highlight/core_ext.rb69
-rw-r--r--lib/error_highlight/formatter.rb8
-rw-r--r--lib/error_highlight/version.rb2
4 files changed, 137 insertions, 49 deletions
diff --git a/lib/error_highlight/base.rb b/lib/error_highlight/base.rb
index 8392979e24..b9c68b8eb8 100644
--- a/lib/error_highlight/base.rb
+++ b/lib/error_highlight/base.rb
@@ -1,12 +1,17 @@
require_relative "version"
module ErrorHighlight
- # Identify the code fragment that seems associated with a given error
+ # Identify the code fragment at that a given exception occurred.
#
- # Arguments:
- # node: RubyVM::AbstractSyntaxTree::Node (script_lines should be enabled)
- # point_type: :name | :args
- # name: The name associated with the NameError/NoMethodError
+ # Options:
+ #
+ # point_type: :name | :args
+ # :name (default) points the method/variable name that the exception occurred.
+ # :args points the arguments of the method call that the exception occurred.
+ #
+ # backtrace_location: Thread::Backtrace::Location
+ # It locates the code fragment of the given backtrace_location.
+ # By default, it uses the first frame of backtrace_locations of the given exception.
#
# Returns:
# {
@@ -15,9 +20,65 @@ module ErrorHighlight
# last_lineno: Integer,
# last_column: Integer,
# snippet: String,
+ # script_lines: [String],
# } | nil
- def self.spot(...)
- Spotter.new(...).spot
+ #
+ # 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
+ exc = obj
+ loc = opts[:backtrace_location]
+ opts = { point_type: opts.fetch(:point_type, :name) }
+
+ unless loc
+ case exc
+ when TypeError, ArgumentError
+ opts[:point_type] = :args
+ end
+
+ locs = exc.backtrace_locations
+ return nil unless locs
+
+ loc = locs.first
+ return nil unless loc
+
+ opts[:name] = exc.name if NameError === obj
+ end
+
+ return nil unless Thread::Backtrace::Location === loc
+
+ 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
+ Spotter.new(obj, **opts).spot
+
+ else
+ raise TypeError, "Exception is expected"
+ end
+
+ rescue SyntaxError,
+ SystemCallError, # file not found or something
+ ArgumentError # eval'ed code
+
+ return nil
end
class Spotter
@@ -46,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
@@ -122,6 +214,7 @@ module ErrorHighlight
last_lineno: @end_lineno,
last_column: @end_column,
snippet: @snippet,
+ script_lines: @node.script_lines,
}
else
return nil
diff --git a/lib/error_highlight/core_ext.rb b/lib/error_highlight/core_ext.rb
index 1ae180aeac..b69093f74e 100644
--- a/lib/error_highlight/core_ext.rb
+++ b/lib/error_highlight/core_ext.rb
@@ -2,49 +2,46 @@ require_relative "formatter"
module ErrorHighlight
module CoreExt
- # This is a marker to let `DidYouMean::Correctable#original_message` skip
- # the following method definition of `to_s`.
- # See https://github.com/ruby/did_you_mean/pull/152
- SKIP_TO_S_FOR_SUPER_LOOKUP = true
- private_constant :SKIP_TO_S_FOR_SUPER_LOOKUP
-
- def to_s
- msg = super.dup
-
- locs = backtrace_locations
- return msg unless locs
-
- loc = locs.first
- begin
- node = RubyVM::AbstractSyntaxTree.of(loc, keep_script_lines: true)
- opts = {}
+ private def generate_snippet
+ spot = ErrorHighlight.spot(self)
+ return "" unless spot
+ return ErrorHighlight.formatter.message_for(spot)
+ end
- case self
- when NoMethodError, NameError
- opts[:point_type] = :name
- opts[:name] = name
- when TypeError, ArgumentError
- opts[:point_type] = :args
+ if Exception.method_defined?(:detailed_message)
+ def detailed_message(highlight: false, error_highlight: true, **)
+ return super unless error_highlight
+ snippet = generate_snippet
+ if highlight
+ snippet = snippet.gsub(/.+/) { "\e[1m" + $& + "\e[m" }
end
-
- spot = ErrorHighlight.spot(node, **opts)
-
- rescue Errno::ENOENT, SyntaxError
+ super + snippet
end
-
- if spot
- points = ErrorHighlight.formatter.message_for(spot)
- msg << points if !msg.include?(points)
+ else
+ # This is a marker to let `DidYouMean::Correctable#original_message` skip
+ # the following method definition of `to_s`.
+ # See https://github.com/ruby/did_you_mean/pull/152
+ SKIP_TO_S_FOR_SUPER_LOOKUP = true
+ private_constant :SKIP_TO_S_FOR_SUPER_LOOKUP
+
+ def to_s
+ msg = super
+ snippet = generate_snippet
+ if snippet != "" && !msg.include?(snippet)
+ msg + snippet
+ else
+ msg
+ end
end
-
- msg
end
end
NameError.prepend(CoreExt)
- # The extension for TypeError/ArgumentError is temporarily disabled due to many test failures
-
- #TypeError.prepend(CoreExt)
- #ArgumentError.prepend(CoreExt)
+ if Exception.method_defined?(:detailed_message)
+ # ErrorHighlight is enabled for TypeError and ArgumentError only when Exception#detailed_message is available.
+ # This is because changing ArgumentError#message is highly incompatible.
+ TypeError.prepend(CoreExt)
+ ArgumentError.prepend(CoreExt)
+ end
end
diff --git a/lib/error_highlight/formatter.rb b/lib/error_highlight/formatter.rb
index ce687fb2a2..20ca78d468 100644
--- a/lib/error_highlight/formatter.rb
+++ b/lib/error_highlight/formatter.rb
@@ -1,6 +1,6 @@
module ErrorHighlight
class DefaultFormatter
- def message_for(spot)
+ def self.message_for(spot)
# currently only a one-line code snippet is supported
if spot[:first_lineno] == spot[:last_lineno]
indent = spot[:snippet][0...spot[:first_column]].gsub(/[^\t]/, " ")
@@ -14,12 +14,10 @@ module ErrorHighlight
end
def self.formatter
- @@formatter
+ Ractor.current[:__error_highlight_formatter__] || DefaultFormatter
end
def self.formatter=(formatter)
- @@formatter = formatter
+ Ractor.current[:__error_highlight_formatter__] = formatter
end
-
- self.formatter = DefaultFormatter.new
end
diff --git a/lib/error_highlight/version.rb b/lib/error_highlight/version.rb
index 0dda1ba375..506d37fbc1 100644
--- a/lib/error_highlight/version.rb
+++ b/lib/error_highlight/version.rb
@@ -1,3 +1,3 @@
module ErrorHighlight
- VERSION = "0.2.0"
+ VERSION = "0.6.0"
end