replace(): replace with 2 nodes?
rstacruz opened this issue · 10 comments
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.
estraverse.replace(tree, {
enter: function (node, parent) {
if (/* something */) {
return [
{ type: 'FunctionDeclaration', body: /* ... */ },
{ type: 'FunctionDeclaration', body: /* ... */ } ];
}
}
});
An example use case would be to to change a ForStatement
(for (x;y;z){a}
) into a WhileStatement
(x; while (y) { z; a; }
), which will be useful for js2coffee.
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
+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 ?