rse/astq

Which version of SpiderMonkey AST supports getParentNode?

Opened this issue · 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?

rse commented

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.

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
  });
});