Traverse down to new nodes in .replace()
When using estraverse.replace(), and I want to make new nodes, what's the best way to traverse down those new nodes?
For instance:
estraverse.replace(tree, {
enter: function (node, parent) {
if (/* something */) {
var newStatement = { type: 'FunctionDeclaration', body: /* ... */ };
parent.body.push(newStatement);
// ...recurse into newStatement
}
}
});
...currently, I'm just invoking estraverse.replace(newroot, { ... }) again with the same enter/exits, but I was wondering if there would be strange side effects to this of if there would be better ways.
I don't think you should modify parent from child's traversal. Better to do if (...) from your parent BlockStatement, in that case you will definitely have no side effects + will get traversal over new nodes for free.
I have a similar problem, I do replace on a node (in leave), but want to do a traversal on the replacement node too.
I tried pushing back to worklist and leavelist, but I am getting problems with escope then.