From: "Eregon (Benoit Daloze) via ruby-core" Date: 2026-08-07T13:28:31+00:00 Subject: [ruby-core:126321] [Ruby Feature#21795] Methods for retrieving ASTs Issue #21795 has been updated by Eregon (Benoit Daloze). Thank you for the implementation work on this. I disagreed with parts of the approach, as is clear from the thread, and I am not going to rehash that now it is merged. Two small things and then I will stop. First, how about adding a check so we do not return a node we can detect is the wrong one? Comparing the found node's location with `#source_range` would do it ��� not `source_range` as an identifier, just validating the node that `node_id` already found. The method already returns `nil` when the `node_id` is not found at all, so this would be the same kind of failure. It would also let the warning fire only when the node really does not match, instead of whenever a non-default prism gem is loaded. Second, `Proc#source_range` describes the block while `Proc#syntax_tree` returns the enclosing call, so the two methods disagree about which region of source a proc corresponds to. #21998 discussed `CallNode` vs `BlockNode` at length and settled on the block, with matz agreeing there. I realise Prism nodes cannot reach their parent currently, which is a real constraint ��� but the inconsistency seems worth resolving at some point. Happy to send a PR for the check if you are open to it; if not, that is fine and I will leave it here. ---------------------------------------- Feature #21795: Methods for retrieving ASTs https://bugs.ruby-lang.org/issues/21795#change-118435 * Author: kddnewton (Kevin Newton) * Status: Closed ---------------------------------------- I would like to propose a handful of methods for retrieving ASTs from various objects that correspond to locations in code. This includes: * Proc#ast * Method#ast * UnboundMethod#ast * Thread::Backtrace::Location#ast * TracePoint#ast (on call/return events) The purpose of this is to make tooling easier to write and maintain. Specifically, this would be able to be used in irb, power_assert, error_highlight, and various other tools both in core and not that make use of source code. There have been many previous discussions of retrieving node_id, source_location, source, etc. All of these use cases are covered by returning the AST for some entity. In this case node_id becomes an implementation detail, invisible to the user. Source location can be derived from the information on the AST itself. Similarly, source can be derived from the AST. Internally, I do not think we have to store any more information than we already do (since we have node_id for the first four of these, it becomes rather trivial). For TracePoint we can have a larger discussion about it, but I think it should not be too much work. In terms of implementation, the only caveat I would put is that if the ISEQ were compiled through the old parser/compiler, this should return `nil`, as the node ids do not match up and we do not want to further propagate the RubyVM::AST API. The reason I am opening up this ticket with 5 different methods requested in it is to get approval first for the direction, then I can open individual tickets or just PRs for each method. I believe this feature would ease the maintenance burden of many core libraries, and unify otherwise disparate efforts to achieve the same thing. -- https://bugs.ruby-lang.org/ ______________________________________________ ruby-core mailing list -- ruby-core@ml.ruby-lang.org To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org ruby-core info -- https://ml.ruby-lang.org/mailman3/lists/ruby-core.ml.ruby-lang.org/