2021-06-28 07:27:35 +03:00
|
|
|
module ErrorHighlight
|
2021-06-18 11:11:39 +03:00
|
|
|
module CoreExt
|
2021-06-30 05:44:48 +03: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 11:11:39 +03: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
|
|
|
|
node = RubyVM::AbstractSyntaxTree.of(loc, save_script_lines: true)
|
|
|
|
opts = {}
|
|
|
|
|
|
|
|
case self
|
|
|
|
when NoMethodError, NameError
|
|
|
|
point = :name
|
|
|
|
opts[:name] = name
|
|
|
|
when TypeError, ArgumentError
|
|
|
|
point = :args
|
|
|
|
end
|
|
|
|
|
2021-06-28 07:27:35 +03:00
|
|
|
spot = ErrorHighlight.spot(node, point, **opts) do |lineno, last_lineno|
|
2021-06-18 11:11:39 +03:00
|
|
|
last_lineno ||= lineno
|
|
|
|
node.script_lines[lineno - 1 .. last_lineno - 1].join("")
|
|
|
|
end
|
|
|
|
|
|
|
|
rescue Errno::ENOENT
|
|
|
|
end
|
|
|
|
|
|
|
|
if spot
|
|
|
|
marker = " " * spot[:first_column] + "^" * (spot[:last_column] - spot[:first_column])
|
|
|
|
points = "\n\n#{ spot[:line] }#{ marker }"
|
|
|
|
msg << points if !msg.include?(points)
|
|
|
|
end
|
|
|
|
|
|
|
|
msg
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
NameError.prepend(CoreExt)
|
|
|
|
|
2021-06-30 05:44:48 +03:00
|
|
|
# The extension for TypeError/ArgumentError is temporarily disabled due to many test failures
|
|
|
|
|
2021-06-18 11:11:39 +03:00
|
|
|
#TypeError.prepend(CoreExt)
|
|
|
|
#ArgumentError.prepend(CoreExt)
|
|
|
|
end
|