ruby-changes:74299
From: eileencodes <ko1@a...>
Date: Mon, 31 Oct 2022 13:58:53 +0900 (JST)
Subject: [ruby-changes:74299] 350d0aa023 (master): [ruby/error_highlight] Support nodes in `spot`
https://git.ruby-lang.org/ruby.git/commit/?id=350d0aa023 From 350d0aa023223f560d812ef61396a2c5359f09a6 Mon Sep 17 00:00:00 2001 From: eileencodes <eileencodes@g...> Date: Wed, 19 Oct 2022 13:50:30 -0400 Subject: [ruby/error_highlight] Support nodes in `spot` Fixes a bug where `spot` was using the wrong local variable. We want to use error highlight with code that has been eval'd, specifically ERB templates. We can recover the compiled source code of the ERB template but we need an API to pass the node into error highlight's `spot`. Required Ruby PR: https://github.com/ruby/ruby/pull/6593 https://github.com/ruby/error_highlight/commit/0b1b650a59 Co-authored-by: Aaron Patterson <tenderlove@r...> --- lib/error_highlight/base.rb | 3 +-- test/error_highlight/test_error_highlight.rb | 32 ++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/lib/error_highlight/base.rb b/lib/error_highlight/base.rb index 4c115cc828..062871ee16 100644 --- a/lib/error_highlight/base.rb +++ b/lib/error_highlight/base.rb @@ -59,8 +59,7 @@ module ErrorHighlight https://github.com/ruby/ruby/blob/trunk/lib/error_highlight/base.rb#L59 Spotter.new(node, **opts).spot when RubyVM::AbstractSyntaxTree::Node - # Just for compatibility - Spotter.new(node, **opts).spot + Spotter.new(obj, **opts).spot else raise TypeError, "Exception is expected" diff --git a/test/error_highlight/test_error_highlight.rb b/test/error_highlight/test_error_highlight.rb index c4a998092b..d5f57a88bc 100644 --- a/test/error_highlight/test_error_highlight.rb +++ b/test/error_highlight/test_error_highlight.rb @@ -1257,4 +1257,36 @@ undefined method `foo' for nil:NilClass https://github.com/ruby/ruby/blob/trunk/test/error_highlight/test_error_highlight.rb#L1257 assert_equal(22, spot[:last_column]) assert_equal(" raise_name_error\n", spot[:snippet]) end + + def test_spot_with_node + omit unless RubyVM::AbstractSyntaxTree.respond_to?(:node_id_for_backtrace_location) + + begin + raise_name_error + rescue NameError => exc + end + + bl = exc.backtrace_locations.first + expected_spot = ErrorHighlight.spot(exc, backtrace_location: bl) + ast = RubyVM::AbstractSyntaxTree.parse_file(__FILE__, keep_script_lines: true) + node_id = RubyVM::AbstractSyntaxTree.node_id_for_backtrace_location(bl) + node = find_node_by_id(ast, node_id) + actual_spot = ErrorHighlight.spot(node) + + assert_equal expected_spot, actual_spot + end + + private + + def find_node_by_id(node, node_id) + return node if node.node_id == node_id + + node.children.each do |child| + next unless child.is_a?(RubyVM::AbstractSyntaxTree::Node) + found = find_node_by_id(child, node_id) + return found if found + end + + return false + end end -- cgit v1.2.3 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/