-
Notifications
You must be signed in to change notification settings - Fork 131
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
replace(): replace with 2 nodes? #38
Comments
Came here to ask the same question. If it is possible it's undocumented. |
Ah, currently, estraverse doesn't support it. |
I second this feature. |
Just came looking for this too. A workaround is to run logic on the parent, replace the target child with the two children on the parent, then return the parent when you've traversed to that parent. |
Temporary fix: return {
type: esprima.Syntax.Program,
body: bodyStatementsArray,
} Will do pull request later |
+1 |
2 similar comments
+1 |
+1 |
Dirty workaround that should work with BlockStatement: estraverse.replace(tree, {
enter: (node, parent) => {
const nodes = // ...
const index = parent.body.findIndex(n => n === node)
parent.body.splice(index, 1, ...nodes)
}
}) Using this instead of WelaurS' fix, because inserting a Program node inside a BlockStatement can mess up escodegen's formatting. |
Can we make ptrcnull's implementation into codebase ? |
Would it be possible to replace a node with multiple statements? I understand this can't work on all places (only in
body
s), but it'd be useful for some cases.An example use case would be to to change a
ForStatement
(for (x;y;z){a}
) into aWhileStatement
(x; while (y) { z; a; }
), which will be useful for js2coffee.The text was updated successfully, but these errors were encountered: