question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

replace(): replace with 2 nodes?

See original GitHub issue

Would it be possible to replace a node with multiple statements? I understand this can’t work on all places (only in bodys), 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:open
  • Created 9 years ago
  • Reactions:6
  • Comments:10 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
ptrcnullcommented, Feb 24, 2021

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.

1reaction
trusktrcommented, Jan 22, 2017

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found