prism icon indicating copy to clipboard operation
prism copied to clipboard

Support `node_id`

Open kddnewton opened this issue 1 year ago • 1 comments

We need to support an incrementing node_id integer on every node allocation. This is used to power error_highlight, but also other tools like action view.

kddnewton avatar Feb 08 '24 20:02 kddnewton

Is there maybe another way these 2 usages could do what they do once they migrate to Prism? (they will need to change anyway since they use the experimental unstable RubyVM::AbstractSyntaxTree). I guess the fundamental need is to identify a call node from a bytecode.

One idea to avoid the need to take/waste that space for every node would be to implicitly number all nodes by their order in the tree with some defined traversal (e.g. pre-order). Then to find node id N, we'd iterate nodes, incrementing our id counter for every node visited, and assuming they are the same nodes in the same order (which is very much what these usages need to assume to work), then return that nth node. E.g. by having some method on ParseResult making this easy.

https://github.com/rails/rails/blob/1007cc8c033f7713783faa02dc13f845640b464d/actionview/lib/action_view/template.rb#L395 seems to be almost doing this already, except it doesn't have a counter for every node visited.

eregon avatar Feb 08 '24 20:02 eregon