mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
[ruby/error_highlight] Use Exception#detailed_message instead of overriding #message (https://github.com/ruby/error_highlight/pull/24)
See https://bugs.ruby-lang.org/issues/18564. Ref: https://github.com/ruby/did_you_mean/pull/177 https://github.com/ruby/error_highlight/commit/671b7c61b2
This commit is contained in:
parent
11b9dd8ccb
commit
f075be3dcb
2 changed files with 43 additions and 17 deletions
|
@ -2,20 +2,13 @@ require_relative "formatter"
|
||||||
|
|
||||||
module ErrorHighlight
|
module ErrorHighlight
|
||||||
module CoreExt
|
module CoreExt
|
||||||
# This is a marker to let `DidYouMean::Correctable#original_message` skip
|
private def generate_snippet
|
||||||
# the following method definition of `to_s`.
|
|
||||||
# See https://github.com/ruby/did_you_mean/pull/152
|
|
||||||
SKIP_TO_S_FOR_SUPER_LOOKUP = true
|
|
||||||
private_constant :SKIP_TO_S_FOR_SUPER_LOOKUP
|
|
||||||
|
|
||||||
def to_s
|
|
||||||
msg = super.dup
|
|
||||||
|
|
||||||
locs = backtrace_locations
|
locs = backtrace_locations
|
||||||
return msg unless locs
|
return "" unless locs
|
||||||
|
|
||||||
loc = locs.first
|
loc = locs.first
|
||||||
return msg unless loc
|
return "" unless loc
|
||||||
|
|
||||||
begin
|
begin
|
||||||
node = RubyVM::AbstractSyntaxTree.of(loc, keep_script_lines: true)
|
node = RubyVM::AbstractSyntaxTree.of(loc, keep_script_lines: true)
|
||||||
opts = {}
|
opts = {}
|
||||||
|
@ -36,11 +29,37 @@ module ErrorHighlight
|
||||||
end
|
end
|
||||||
|
|
||||||
if spot
|
if spot
|
||||||
points = ErrorHighlight.formatter.message_for(spot)
|
return ErrorHighlight.formatter.message_for(spot)
|
||||||
msg << points if !msg.include?(points)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
msg
|
""
|
||||||
|
end
|
||||||
|
|
||||||
|
if Exception.method_defined?(:detailed_message)
|
||||||
|
def detailed_message(highlight: false, error_highlight: true, **)
|
||||||
|
return super unless error_highlight
|
||||||
|
snippet = generate_snippet
|
||||||
|
if highlight
|
||||||
|
snippet = snippet.gsub(/.+/) { "\e[1m" + $& + "\e[m" }
|
||||||
|
end
|
||||||
|
super + snippet
|
||||||
|
end
|
||||||
|
else
|
||||||
|
# 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
|
||||||
|
SKIP_TO_S_FOR_SUPER_LOOKUP = true
|
||||||
|
private_constant :SKIP_TO_S_FOR_SUPER_LOOKUP
|
||||||
|
|
||||||
|
def to_s
|
||||||
|
msg = super
|
||||||
|
snippet = generate_snippet
|
||||||
|
if snippet != "" && !msg.include?(snippet)
|
||||||
|
msg + snippet
|
||||||
|
else
|
||||||
|
msg
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -23,9 +23,16 @@ class ErrorHighlightTest < Test::Unit::TestCase
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def assert_error_message(klass, expected_msg, &blk)
|
if Exception.method_defined?(:detailed_message)
|
||||||
err = assert_raise(klass, &blk)
|
def assert_error_message(klass, expected_msg, &blk)
|
||||||
assert_equal(expected_msg.chomp, err.message)
|
err = assert_raise(klass, &blk)
|
||||||
|
assert_equal(expected_msg.chomp, err.detailed_message(highlight: false).sub(/ \((?:NoMethod|Name)Error\)/, ""))
|
||||||
|
end
|
||||||
|
else
|
||||||
|
def assert_error_message(klass, expected_msg, &blk)
|
||||||
|
err = assert_raise(klass, &blk)
|
||||||
|
assert_equal(expected_msg.chomp, err.message)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_CALL_noarg_1
|
def test_CALL_noarg_1
|
||||||
|
|
Loading…
Reference in a new issue