[前][次][番号順一覧][スレッド一覧]

ruby-changes:72889

From: Yusuke <ko1@a...>
Date: Wed, 10 Aug 2022 21:19:25 +0900 (JST)
Subject: [ruby-changes:72889] 3a58009066 (master): [ruby/error_highlight] Make backtrace_location keyword work

https://git.ruby-lang.org/ruby.git/commit/?id=3a58009066

From 3a58009066049f9af09ea3171cecab88ba0d9f1c Mon Sep 17 00:00:00 2001
From: Yusuke Endoh <mame@r...>
Date: Wed, 10 Aug 2022 21:17:04 +0900
Subject: [ruby/error_highlight] Make backtrace_location keyword work

We had to keep backtrace_location before opts is overwritten.

https://github.com/ruby/error_highlight/commit/2735e4681a
---
 lib/error_highlight/base.rb                  |  4 +++-
 test/error_highlight/test_error_highlight.rb | 26 ++++++++++++++++++++++++++
 2 files changed, 29 insertions(+), 1 deletion(-)

diff --git a/lib/error_highlight/base.rb b/lib/error_highlight/base.rb
index 51f1ce369d..dbd173a5cd 100644
--- a/lib/error_highlight/base.rb
+++ b/lib/error_highlight/base.rb
@@ -26,9 +26,9 @@ module ErrorHighlight https://github.com/ruby/ruby/blob/trunk/lib/error_highlight/base.rb#L26
     case obj
     when Exception
       exc = obj
+      loc = opts[:backtrace_location]
       opts = { point_type: opts.fetch(:point_type, :name) }
 
-      loc = opts[:backtrace_location]
       unless loc
         case exc
         when TypeError, ArgumentError
@@ -44,6 +44,8 @@ module ErrorHighlight https://github.com/ruby/ruby/blob/trunk/lib/error_highlight/base.rb#L44
         opts[:name] = exc.name if NameError === obj
       end
 
+      return nil unless Thread::Backtrace::Location === loc
+
       node = RubyVM::AbstractSyntaxTree.of(loc, keep_script_lines: true)
 
       Spotter.new(node, **opts).spot
diff --git a/test/error_highlight/test_error_highlight.rb b/test/error_highlight/test_error_highlight.rb
index 89c930f5e6..c4a998092b 100644
--- a/test/error_highlight/test_error_highlight.rb
+++ b/test/error_highlight/test_error_highlight.rb
@@ -1231,4 +1231,30 @@ undefined method `foo' for nil:NilClass https://github.com/ruby/ruby/blob/trunk/test/error_highlight/test_error_highlight.rb#L1231
       end
     end
   end
+
+  def raise_name_error
+    1.time
+  end
+
+  def test_spot_with_backtrace_location
+    lineno = __LINE__
+    begin
+      raise_name_error
+    rescue NameError => exc
+    end
+
+    spot = ErrorHighlight.spot(exc).except(:script_lines)
+    assert_equal(lineno - 4, spot[:first_lineno])
+    assert_equal(lineno - 4, spot[:last_lineno])
+    assert_equal(5, spot[:first_column])
+    assert_equal(10, spot[:last_column])
+    assert_equal("    1.time\n", spot[:snippet])
+
+    spot = ErrorHighlight.spot(exc, backtrace_location: exc.backtrace_locations[1]).except(:script_lines)
+    assert_equal(lineno + 2, spot[:first_lineno])
+    assert_equal(lineno + 2, spot[:last_lineno])
+    assert_equal(6, spot[:first_column])
+    assert_equal(22, spot[:last_column])
+    assert_equal("      raise_name_error\n", spot[:snippet])
+  end
 end
-- 
cgit v1.2.1


--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]