From 6d0dee4d78b53112a30af60c9060bc76362cc29c Mon Sep 17 00:00:00 2001 From: Matijs van Zuijlen Date: Thu, 28 Dec 2023 10:54:00 +0100 Subject: [PATCH] Fix source location for eval with binding and no location When using binding but no location, the original behavior is to set the source location to "(eval)". This change makes the eval replacement behave the same way. --- lib/live_ast/common.rb | 13 +++++-------- lib/live_ast/replace_eval.rb | 2 +- test/backtrace_test.rb | 5 +---- test/full/replace_eval_test.rb | 17 ++++++++++++++++- 4 files changed, 23 insertions(+), 14 deletions(-) diff --git a/lib/live_ast/common.rb b/lib/live_ast/common.rb index bf8639c..9cea0fd 100644 --- a/lib/live_ast/common.rb +++ b/lib/live_ast/common.rb @@ -43,15 +43,12 @@ def check_is_binding(obj) raise TypeError, message end - def location_for_eval(bind = nil, filename = nil, lineno = nil) - if bind - if filename - lineno ||= 1 - [filename, lineno] - else - bind.source_location - end + def location_for_eval(bind, filename = nil, lineno = nil) + if filename + lineno ||= 1 + [filename, lineno] else + bind.source_location ["(eval)", 1] end end diff --git a/lib/live_ast/replace_eval.rb b/lib/live_ast/replace_eval.rb index cd6524f..3bbab9a 100644 --- a/lib/live_ast/replace_eval.rb +++ b/lib/live_ast/replace_eval.rb @@ -57,7 +57,7 @@ def eval(string, binding = nil, filename = nil, lineno = nil) LiveAST.eval( string, binding || Binding.of_caller(1), - *LiveAST::Common.location_for_eval(binding, filename, lineno)) + filename, lineno) end end diff --git a/test/backtrace_test.rb b/test/backtrace_test.rb index a4c8f0b..31ba3f5 100644 --- a/test/backtrace_test.rb +++ b/test/backtrace_test.rb @@ -34,7 +34,7 @@ def test_raise_in_eval def test_raise_no_overrides 3.times do orig = exception_backtrace do - eval <<~RUBY, binding, __FILE__, (__LINE__ + 9) + eval <<~RUBY, binding raise @@ -52,9 +52,6 @@ def test_raise_no_overrides end assert_equal orig.first, live.first - here = Regexp.quote __FILE__ - - assert_match(/#{here}/, live.first) end end diff --git a/test/full/replace_eval_test.rb b/test/full/replace_eval_test.rb index 669c921..1127677 100644 --- a/test/full/replace_eval_test.rb +++ b/test/full/replace_eval_test.rb @@ -403,7 +403,7 @@ def test_local_var_collision assert_equal 33, Class.new.instance_eval("args") end - def test_location_without_binding + def test_eval_location_without_binding expected = ["(eval)", 2] assert_equal expected, live_ast_original_eval("\n[__FILE__, __LINE__]") @@ -418,6 +418,21 @@ def test_location_without_binding assert_equal expected, [file, line] end + def test_eval_location_with_binding + expected = ["(eval)", 2] + + assert_equal expected, live_ast_original_eval("\n[__FILE__, __LINE__]", binding) + + unfixable do + assert_equal expected, eval("\n[__FILE__, __LINE__]", binding) + end + + file, line = eval("\n[__FILE__, __LINE__]", binding) + file = LiveAST.strip_token file + + assert_equal expected, [file, line] + end + DEFINE_BO_TEST = lambda do class BasicObject Kernel.eval("1 + 1")