aboutsummaryrefslogtreecommitdiffstats
path: root/ast.rb
diff options
context:
space:
mode:
authoreileencodes <eileencodes@gmail.com>2022-10-19 13:23:53 -0400
committerYusuke Endoh <mame@ruby-lang.org>2022-10-31 13:39:56 +0900
commit3391c51effcd61f9a718adf59740661d99f57b5b (patch)
treec45ce41d5b960638724db4376c0a5a87dab8c0df /ast.rb
parent7ed10abdd97a324583c9b9794088a41a550abcd0 (diff)
downloadruby-3391c51effcd61f9a718adf59740661d99f57b5b.tar.gz
Add `node_id_for_backtrace_location` function
We want to use error highlight with eval'd code, specifically ERB templates. We're able to recover the generated code for eval'd templates and can get a parse tree for the ERB generated code, but we don't have a way to get the node id from the backtrace location. So we can't pass the right node into error highlight. This patch gives us an API to get the node id from the backtrace location so we can find the node in the AST. Error Highlight PR: https://github.com/ruby/error_highlight/pull/26 Co-authored-by: Aaron Patterson <tenderlove@ruby-lang.org>
Diffstat (limited to 'ast.rb')
-rw-r--r--ast.rb15
1 files changed, 15 insertions, 0 deletions
diff --git a/ast.rb b/ast.rb
index 24fd8e5526..1bc1168b80 100644
--- a/ast.rb
+++ b/ast.rb
@@ -67,6 +67,21 @@ module RubyVM::AbstractSyntaxTree
Primitive.ast_s_of body, keep_script_lines, error_tolerant
end
+ # call-seq:
+ # RubyVM::AbstractSyntaxTree.node_id_for_backtrace_location(backtrace_location) -> integer
+ #
+ # Returns the node id for the given backtrace location.
+ #
+ # begin
+ # raise
+ # rescue => e
+ # loc = e.backtrace_locations.first
+ # RubyVM::AbstractSyntaxTree.node_id_for_backtrace_location(loc)
+ # end # => 0
+ def self.node_id_for_backtrace_location backtrace_location
+ Primitive.node_id_for_backtrace_location backtrace_location
+ end
+
# RubyVM::AbstractSyntaxTree::Node instances are created by parse methods in
# RubyVM::AbstractSyntaxTree.
#