replace(): replace with 2 nodes?
See original GitHub issueWould 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.
Issue Analytics
- State:
- Created 9 years ago
- Reactions:6
- Comments:10 (2 by maintainers)
Top Results From Across the Web
Replace even nodes of a doubly linked list with the elements ...
Given a doubly linked list and an array with only odd values. Both are of equal size N. The task is replace all...
Read more >Javascript - How to replace multiple HTML nodes
One way would simply be to replace the parentNode 's innerHTML s with the .components-drop-area innerHTML s: let dropAreas = document.
Read more >String.prototype.replace() - JavaScript - MDN Web Docs
The replace() method returns a new string with one, some, or all matches of a pattern replaced by a replacement .
Read more >Replace storage node in a two-node cluster
Replace storage node in a two-node cluster · Physically remove the faulty node from the rack. · Install the replacement node in the...
Read more >How to Replace Single Node with Two Nodes in Abstract ...
2. Replacing One Node by Another. Let's say we want to upgrade to PHP 8 and use new str_contains() function: -Nette ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Dirty workaround that should work with BlockStatement:
Using this instead of WelaurS’ fix, because inserting a Program node inside a BlockStatement can mess up escodegen’s formatting.
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.