1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00
ruby--ruby/lib/error_highlight/core_ext.rb
Christian Boos 11b1ebe606 [ruby/error_highlight] Fix the spurious TypeError.
When we have no backtrace locations, we can't have the highlight,
so just return the message.

https://github.com/ruby/error_highlight/commit/9f5c639494
2022-01-04 23:09:01 +09:00

53 lines
1.3 KiB
Ruby

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
return msg unless loc
begin
node = RubyVM::AbstractSyntaxTree.of(loc, keep_script_lines: true)
opts = {}
case self
when NoMethodError, NameError
opts[:point_type] = :name
opts[:name] = name
when TypeError, ArgumentError
opts[:point_type] = :args
end
spot = ErrorHighlight.spot(node, **opts)
rescue SyntaxError
rescue SystemCallError # file not found or something
rescue ArgumentError # eval'ed code
end
if spot
points = ErrorHighlight.formatter.message_for(spot)
msg << points if !msg.include?(points)
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)
end