From d73125bf070f08aa291c16f31e6ba562e4e39711 Mon Sep 17 00:00:00 2001 From: Christian Boos Date: Wed, 29 Dec 2021 22:03:41 +0000 Subject: [PATCH 1/2] Reproduce the error seen when calling .to_s in embedded Ruby The test fails with the following error: Error: test_simulate_funcallv_from_embedded_ruby(ErrorHighlightTest): TypeError: wrong argument type nil (expected method) --- test/test_error_highlight.rb | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/test/test_error_highlight.rb b/test/test_error_highlight.rb index 08036bc..222da0f 100644 --- a/test/test_error_highlight.rb +++ b/test/test_error_highlight.rb @@ -1194,4 +1194,20 @@ def test_no_final_newline end end end + + def test_simulate_funcallv_from_embedded_ruby + assert_error_message(NoMethodError, <<~END) do +undefined method `foo' for nil:NilClass + + nil.foo + 1 + ^^^^ + END + + nil.foo + 1 + rescue NoMethodError => exc + def exc.backtrace_locations = [] + raise + end + end + end From 62eaa742ae30a9aea92295b3bc197adac6814cc9 Mon Sep 17 00:00:00 2001 From: Christian Boos Date: Wed, 29 Dec 2021 22:10:21 +0000 Subject: [PATCH 2/2] Fix the spurious TypeError. When we have no backtrace locations, we can't have the highlight, so just return the message. --- lib/error_highlight/core_ext.rb | 1 + test/test_error_highlight.rb | 3 --- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/lib/error_highlight/core_ext.rb b/lib/error_highlight/core_ext.rb index d3a4dba..78cda8a 100644 --- a/lib/error_highlight/core_ext.rb +++ b/lib/error_highlight/core_ext.rb @@ -15,6 +15,7 @@ def to_s return msg unless locs loc = locs.first + return msg unless loc begin node = RubyVM::AbstractSyntaxTree.of(loc, keep_script_lines: true) opts = {} diff --git a/test/test_error_highlight.rb b/test/test_error_highlight.rb index 222da0f..d31e394 100644 --- a/test/test_error_highlight.rb +++ b/test/test_error_highlight.rb @@ -1198,9 +1198,6 @@ def test_no_final_newline def test_simulate_funcallv_from_embedded_ruby assert_error_message(NoMethodError, <<~END) do undefined method `foo' for nil:NilClass - - nil.foo + 1 - ^^^^ END nil.foo + 1