diff options
Diffstat (limited to 'lib/error_highlight')
-rw-r--r-- | lib/error_highlight/base.rb | 53 | ||||
-rw-r--r-- | lib/error_highlight/core_ext.rb | 10 | ||||
-rw-r--r-- | lib/error_highlight/version.rb | 2 |
3 files changed, 57 insertions, 8 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 diff --git a/lib/error_highlight/core_ext.rb b/lib/error_highlight/core_ext.rb index 130f9ef832..b69093f74e 100644 --- a/lib/error_highlight/core_ext.rb +++ b/lib/error_highlight/core_ext.rb @@ -38,8 +38,10 @@ module ErrorHighlight 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/version.rb b/lib/error_highlight/version.rb index 4279b6d05f..506d37fbc1 100644 --- a/lib/error_highlight/version.rb +++ b/lib/error_highlight/version.rb @@ -1,3 +1,3 @@ module ErrorHighlight - VERSION = "0.4.0" + VERSION = "0.6.0" end |