Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Traverse down to new nodes in .replace() #29

Open
rstacruz opened this issue Sep 2, 2014 · 2 comments
Open

Traverse down to new nodes in .replace() #29

rstacruz opened this issue Sep 2, 2014 · 2 comments

Comments

@rstacruz
Copy link

rstacruz commented Sep 2, 2014

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.

@RReverser
Copy link
Member

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.

@exander77
Copy link

exander77 commented May 20, 2016

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants