rse / astq

Abstract Syntax Tree (AST) Query Engine
https://npmjs.com/astq
201 stars 15 forks source link

Which version of SpiderMonkey AST supports getParentNode? #19

Open jmeit-fwdsec opened 1 year ago

jmeit-fwdsec commented 1 year ago

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 1 year ago

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.

moonyoulove commented 3 months ago

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