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

Re-use old AST #66

Closed
OliverJAsh opened this issue Nov 3, 2015 · 2 comments
Closed

Re-use old AST #66

OliverJAsh opened this issue Nov 3, 2015 · 2 comments
Labels

Comments

@OliverJAsh
Copy link

When transforming the AST, it would be useful to re-use part of the old AST so I don't have to manually reconstruct all nodes nested in the part I'm transforming:

module.exports = function(file, api) {
  const j = api.jscodeshift;
  const {expression, statement, statements} = j.template;

  return j(file.source)
    .find(j.Identifier)
    .replaceWith(
      p => j.memberExpression(j.identifier('foo'), p)
    )
    .toSource();
};

Is this possible?

@fkling
Copy link
Contributor

fkling commented Nov 3, 2015

Of course. The builders don't care where the node comes from. But in your example, p is a NodePath. If you want to access the contained AST node, you need to use p.node or p.value.

http://astexplorer.net/#/l4WHRkBGRg

@OliverJAsh
Copy link
Author

Thanks!

euphocat pushed a commit to euphocat/jscodeshift that referenced this issue Oct 22, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants