summaryrefslogtreecommitdiff
path: root/lib/error_highlight/core_ext.rb
diff options
context:
space:
mode:
authorYusuke Endoh <mame@ruby-lang.org>2021-06-28 13:27:35 +0900
committerYusuke Endoh <mame@ruby-lang.org>2021-06-29 23:45:49 +0900
commit9438c99590f5476a81cee8b4cf2de25084a40b42 (patch)
treec7416588e060d079a082fb62b3a96e40164e4268 /lib/error_highlight/core_ext.rb
parente94604966572bb43fc887856d54aa54b8e9f7719 (diff)
Rename error_squiggle to error_highlight
Notes
Notes: Merged: https://github.com/ruby/ruby/pull/4586
Diffstat (limited to 'lib/error_highlight/core_ext.rb')
-rw-r--r--lib/error_highlight/core_ext.rb48
1 files changed, 48 insertions, 0 deletions
diff --git a/lib/error_highlight/core_ext.rb b/lib/error_highlight/core_ext.rb
new file mode 100644
index 0000000000..ad1c15a485
--- /dev/null
+++ b/lib/error_highlight/core_ext.rb
@@ -0,0 +1,48 @@
+module ErrorHighlight
+ 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 = ErrorHighlight.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