Repository navigation
Avoid reuse of AST nodes by making defensive clones - #347
Conversation
| // that have been marked as branch/jump targets. | ||
| this.marked = [true]; | ||
|
|
||
| this.insertedLocs = new Set(); |
There was a problem hiding this comment.
not sure what version of node do u want to support here, should I just leave it or import a ponyfill?
There was a problem hiding this comment.
I'm comfortable with only supporting Node 4+.
|
|
||
| const insertedLocs = emitter.getInsertedLocs(); | ||
|
|
||
| path.traverse({ |
There was a problem hiding this comment.
this is a hack-ish solution for "intentional" duplicates, cant rly clone them when inserting to AST because they need to be the same object for mark to take effect on all inserted instances
This whole thing with updating those nodes after they get inserted simplifies the transform. Rewriting it to avoid duplicates has too big cost and would be really hard.
I've considered tracking all insertions but it would complicate things quite a bit because there is no single place for AST insertions (kinda general rule for any babel transform 😉 )
So I've decided to just "fix" the transformed subtree after whole transform takes place.
|
Your choice of wording is interesting: the problem with these nodes is that they're not duplicated, but rather reused in multiple places in the AST. I'm not opposed to your solution, but calling |
I support this 100%. |
|
Your wording is indeed more accurate 😉 And you are ofc right that probably from time to time those clones are defensive, although I've inserted them only at places that were causing nodes reuse.
This might be somewhat less memory efficient but ultimately aligns the codebase to what babel's monorepo is doing at the moment. Im not aware of any issue fixed by those changes here, but we can imagine other transforms relaying on nodes being unique and i.e. doing some caching or storing some metadata - those cases were breaking from time to time for babel users and also for babel's codebase because of nodes reuse. |
|
@Andarist Do you think this should be a minor semantic version bump, or just a patch? |
|
Just a patch - its purely internal change, no API changes, existing tests pass. I doubt there is anyone relaying on the fact that those nodes were previously reused |
Motivation
Duplicated AST nodes were causing some obscure, hard to find bugs and they got forbidden in babel's output.
At the moment this is unfortunately only checked in core tests. Because regenerator is used by babel as core transform and because it produces numerous duplicated nodes (only 1 "kind" of them intentionally) some nasty lines got introduced to babel's check functions to allow regenerator-created nodes.
I'd like to get rid of those nasty lines with this PR 😉