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
|
|
|
|
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-07-02 03:45:48 -04:00
|
|
|
rescue Errno::ENOENT, SyntaxError
|
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
|