2021-06-29 23:28:22 -04:00
|
|
|
require_relative "formatter"
|
|
|
|
|
2021-06-28 00:27:35 -04:00
|
|
|
module ErrorHighlight
|
2021-06-18 04:11:39 -04:00
|
|
|
module CoreExt
|
2021-06-29 22:44:48 -04:00
|
|
|
# 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
|
2021-06-18 04:11:39 -04:00
|
|
|
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
|
2021-12-29 17:10:21 -05:00
|
|
|
return msg unless loc
|
2021-06-18 04:11:39 -04:00
|
|
|
begin
|
2021-08-20 03:18:36 -04:00
|
|
|
node = RubyVM::AbstractSyntaxTree.of(loc, keep_script_lines: true)
|
2021-06-18 04:11:39 -04:00
|
|
|
opts = {}
|
|
|
|
|
|
|
|
case self
|
|
|
|
when NoMethodError, NameError
|
2021-06-29 23:31:55 -04:00
|
|
|
opts[:point_type] = :name
|
2021-06-18 04:11:39 -04:00
|
|
|
opts[:name] = name
|
|
|
|
when TypeError, ArgumentError
|
2021-06-29 23:31:55 -04:00
|
|
|
opts[:point_type] = :args
|
2021-06-18 04:11:39 -04:00
|
|
|
end
|
|
|
|
|
2021-06-29 23:31:55 -04:00
|
|
|
spot = ErrorHighlight.spot(node, **opts)
|
2021-06-18 04:11:39 -04:00
|
|
|
|
2021-12-18 14:13:14 -05:00
|
|
|
rescue SyntaxError
|
|
|
|
rescue SystemCallError # file not found or something
|
|
|
|
rescue ArgumentError # eval'ed code
|
2021-06-18 04:11:39 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
if spot
|
2021-06-29 23:28:22 -04:00
|
|
|
points = ErrorHighlight.formatter.message_for(spot)
|
2021-06-18 04:11:39 -04:00
|
|
|
msg << points if !msg.include?(points)
|
|
|
|
end
|
|
|
|
|
|
|
|
msg
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
NameError.prepend(CoreExt)
|
|
|
|
|
2021-06-29 22:44:48 -04:00
|
|
|
# The extension for TypeError/ArgumentError is temporarily disabled due to many test failures
|
|
|
|
|
2021-06-18 04:11:39 -04:00
|
|
|
#TypeError.prepend(CoreExt)
|
|
|
|
#ArgumentError.prepend(CoreExt)
|
|
|
|
end
|