Add option for setting "parent" property on each node


#1

I’m building a codegen library that combines acorn and magic-string.

It would be really nice if you added a parents: true option that sets the parent property of each node as code is parsed.

Another useful property is a ref string on each node that equals the parent property that references it. This can be used as a quick way to identify sibling nodes. This property could be set when the parents: true option is defined, or maybe a refs: true option for granularity’s sake.

Would you be okay with adding options that diverge from the ESTree spec?


#2

We have a few of those already, but this one doesn’t seem like it belongs in the code library. Adding extra links is easy enough to write as a post-processing step.


#3

The incentive is avoiding the need to crawl the entire AST before the transform phase.

The alternative I’m choosing is to give nodes a walk(prop, iter) method that populates the parent and ref properties before the child nodes are transformed. That should work well enough, I suppose.

The other option is to “visit” children before their parents, but manipulating children of a parent that is bound to be removed later in the transform phase seems like something to avoid, if possible.