ruby-changes:72084
From: Yusuke <ko1@a...>
Date: Tue, 7 Jun 2022 17:40:32 +0900 (JST)
Subject: [ruby-changes:72084] f075be3dcb (master): [ruby/error_highlight] Use Exception#detailed_message instead of overriding #message (https://github.com/ruby/error_highlight/pull/24)
https://git.ruby-lang.org/ruby.git/commit/?id=f075be3dcb From f075be3dcb4b82b89496d1820002bf3d80f653ef Mon Sep 17 00:00:00 2001 From: Yusuke Endoh <mame@r...> Date: Tue, 7 Jun 2022 17:40:03 +0900 Subject: [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 --- lib/error_highlight/core_ext.rb | 47 +++++++++++++++++++--------- test/error_highlight/test_error_highlight.rb | 13 ++++++-- 2 files changed, 43 insertions(+), 17 deletions(-) diff --git a/lib/error_highlight/core_ext.rb b/lib/error_highlight/core_ext.rb index 78cda8ace2..53e409dd8f 100644 --- a/lib/error_highlight/core_ext.rb +++ b/lib/error_highlight/core_ext.rb @@ -2,20 +2,13 @@ require_relative "formatter" https://github.com/ruby/ruby/blob/trunk/lib/error_highlight/core_ext.rb#L2 module ErrorHighlight module CoreExt - # 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.dup - + private def generate_snippet locs = backtrace_locations - return msg unless locs + return "" unless locs loc = locs.first - return msg unless loc + return "" unless loc + begin node = RubyVM::AbstractSyntaxTree.of(loc, keep_script_lines: true) opts = {} @@ -36,11 +29,37 @@ module ErrorHighlight https://github.com/ruby/ruby/blob/trunk/lib/error_highlight/core_ext.rb#L29 end if spot - points = ErrorHighlight.formatter.message_for(spot) - msg << points if !msg.include?(points) + return ErrorHighlight.formatter.message_for(spot) + end + + "" + 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 - msg + def to_s + msg = super + snippet = generate_snippet + if snippet != "" && !msg.include?(snippet) + msg + snippet + else + msg + end + end end end diff --git a/test/error_highlight/test_error_highlight.rb b/test/error_highlight/test_error_highlight.rb index a3cc7aa149..5b7c05e5f4 100644 --- a/test/error_highlight/test_error_highlight.rb +++ b/test/error_highlight/test_error_highlight.rb @@ -23,9 +23,16 @@ class ErrorHighlightTest < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/error_highlight/test_error_highlight.rb#L23 end end - def assert_error_message(klass, expected_msg, &blk) - err = assert_raise(klass, &blk) - assert_equal(expected_msg.chomp, err.message) + if Exception.method_defined?(:detailed_message) + def assert_error_message(klass, expected_msg, &blk) + 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 def test_CALL_noarg_1 -- cgit v1.2.1 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/