diff options
Diffstat (limited to 'lib/error_highlight/base.rb')
-rw-r--r-- | lib/error_highlight/base.rb | 127 |
1 files changed, 110 insertions, 17 deletions
diff --git a/lib/error_highlight/base.rb b/lib/error_highlight/base.rb index 696df76ff9..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 @@ -148,7 +241,7 @@ module ErrorHighlight nd_recv, mid, nd_args = @node.children lineno = nd_recv.last_lineno lines = @fetch[lineno, @node.last_lineno] - if mid == :[] && lines.match(/\G\s*(\[(?:\s*\])?)/, nd_recv.last_column) + if mid == :[] && lines.match(/\G[\s)]*(\[(?:\s*\])?)/, nd_recv.last_column) @beg_column = $~.begin(1) @snippet = lines[/.*\n/] @beg_lineno = @end_lineno = lineno @@ -157,11 +250,11 @@ module ErrorHighlight @end_column = $~.end(0) end else - if lines.match(/\G\s*?\[\s*\]/, nd_recv.last_column) + if lines.match(/\G[\s)]*?\[\s*\]/, nd_recv.last_column) @end_column = $~.end(0) end end - elsif lines.match(/\G\s*?(\&?\.)(\s*?)(#{ Regexp.quote(mid) }).*\n/, nd_recv.last_column) + elsif lines.match(/\G[\s)]*?(\&?\.)(\s*?)(#{ Regexp.quote(mid) }).*\n/, nd_recv.last_column) lines = $` + $& @beg_column = $~.begin($2.include?("\n") ? 3 : 1) @end_column = $~.end(3) @@ -207,16 +300,16 @@ module ErrorHighlight nd_recv, mid, nd_args = @node.children *nd_args, _nd_last_arg, _nil = nd_args.children fetch_line(nd_recv.last_lineno) - if mid == :[]= && @snippet.match(/\G\s*(\[)/, nd_recv.last_column) + if mid == :[]= && @snippet.match(/\G[\s)]*(\[)/, nd_recv.last_column) @beg_column = $~.begin(1) args_last_column = $~.end(0) if nd_args.last && nd_recv.last_lineno == nd_args.last.last_lineno args_last_column = nd_args.last.last_column end - if @snippet.match(/\s*\]\s*=/, args_last_column) + if @snippet.match(/[\s)]*\]\s*=/, args_last_column) @end_column = $~.end(0) end - elsif @snippet.match(/\G\s*(\.\s*#{ Regexp.quote(mid.to_s.sub(/=\z/, "")) }\s*=)/, nd_recv.last_column) + elsif @snippet.match(/\G[\s)]*(\.\s*#{ Regexp.quote(mid.to_s.sub(/=\z/, "")) }\s*=)/, nd_recv.last_column) @beg_column = $~.begin(1) @end_column = $~.end(1) end @@ -232,7 +325,7 @@ module ErrorHighlight def spot_attrasgn_for_args nd_recv, mid, nd_args = @node.children fetch_line(nd_recv.last_lineno) - if mid == :[]= && @snippet.match(/\G\s*\[/, nd_recv.last_column) + if mid == :[]= && @snippet.match(/\G[\s)]*\[/, nd_recv.last_column) @beg_column = $~.end(0) if nd_recv.last_lineno == nd_args.last_lineno @end_column = nd_args.last_column @@ -254,7 +347,7 @@ module ErrorHighlight fetch_line(nd_recv.last_lineno) if nd_arg # binary operator - if @snippet.match(/\G\s*(#{ Regexp.quote(op) })/, nd_recv.last_column) + if @snippet.match(/\G[\s)]*(#{ Regexp.quote(op) })/, nd_recv.last_column) @beg_column = $~.begin(1) @end_column = $~.end(1) end @@ -330,7 +423,7 @@ module ErrorHighlight def spot_op_asgn1_for_name nd_recv, op, nd_args, _nd_rhs = @node.children fetch_line(nd_recv.last_lineno) - if @snippet.match(/\G\s*(\[)/, nd_recv.last_column) + if @snippet.match(/\G[\s)]*(\[)/, nd_recv.last_column) bracket_beg_column = $~.begin(1) args_last_column = $~.end(0) if nd_args && nd_recv.last_lineno == nd_args.last_lineno @@ -377,7 +470,7 @@ module ErrorHighlight def spot_op_asgn2_for_name nd_recv, _qcall, attr, op, _nd_rhs = @node.children fetch_line(nd_recv.last_lineno) - if @snippet.match(/\G\s*(\.)\s*#{ Regexp.quote(attr) }()\s*(#{ Regexp.quote(op) })(=)/, nd_recv.last_column) + if @snippet.match(/\G[\s)]*(\.)\s*#{ Regexp.quote(attr) }()\s*(#{ Regexp.quote(op) })(=)/, nd_recv.last_column) case @name when attr @beg_column = $~.begin(1) |