astq icon indicating copy to clipboard operation
astq copied to clipboard

Which version of SpiderMonkey AST supports getParentNode?

Open jmeit-fwdsec opened this issue 1 year ago • 2 comments

I'm receiving the error message stating that my version of SpiderMonkey AST doesn't support parent node traversal from /lib/astq.node.js:362. The query I'm using is in fact trying to traverse parent nodes of an AST built with Acorn. The query is //Identifier[@name=='startTimer'] ..// *

What version of SpiderMonkey do I need to install to get this feature? Or is there a different adapter I should try and use?

jmeit-fwdsec avatar Mar 05 '23 00:03 jmeit-fwdsec

The adapter is hard-coded to assume that no parent link exists in the SpiderMonkey AST. I don't know if any SpiderMonkey AST variant exists which nowaways has a parent link from childs (I do not expect this), but if this is the case you would have to use a different adapter.

rse avatar Mar 05 '23 09:03 rse

Through AST explorer, I found rocambole can add parent property to Node. And I also wrote a code adding parent propery to acorn by acorn-walk.

acorn.walk.fullAncestor(ast, (node, state, ancestors, type) => {
  Object.defineProperty(node, "parent", {
    value: ancestors.at(-2) ?? null,
    enumerable: false
  });
});

moonyoulove avatar Jun 06 '24 14:06 moonyoulove