ruby/lib/error_squiggle/core_ext.rb
Yusuke Endoh e946049665 [WIP] add error_squiggle gem
```
$ ./local/bin/ruby -e '1.time {}'
-e:1:in `<main>': undefined method `time' for 1:Integer (NoMethodError)

1.time {}
 ^^^^^
Did you mean?  times
```

https://bugs.ruby-lang.org/issues/17930
2021-06-29 23:45:49 +09:00

48 lines
1.1 KiB
Ruby

module ErrorSquiggle
module CoreExt
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
spot = ErrorSquiggle.spot(node, point, **opts) do |lineno, last_lineno|
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)
# temporarily disabled
#TypeError.prepend(CoreExt)
#ArgumentError.prepend(CoreExt)
end