Skip to content
This repository was archived by the owner on Feb 1, 2025. It is now read-only.

Avoid reuse of AST nodes by making defensive clones - #347

Merged
benjamn merged 1 commit into
meta:masterfrom
Andarist:no-duplicated-nodes
Jun 23, 2018
Merged

benjamn merged 1 commit into
meta:masterfrom
Andarist:no-duplicated-nodes

Conversation

@Andarist

Copy link
Copy Markdown
Contributor

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 😉

// that have been marked as branch/jump targets.
this.marked = [true];

this.insertedLocs = new Set();

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure what version of node do u want to support here, should I just leave it or import a ponyfill?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm comfortable with only supporting Node 4+.


const insertedLocs = emitter.getInsertedLocs();

path.traverse({

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@benjamn benjamn changed the title Prevent duplicated AST nodes in the output Avoid reuse of AST nodes by making defensive clones Jun 18, 2018
@benjamn

benjamn commented Jun 18, 2018

Copy link
Copy Markdown
Contributor

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 t.cloneDeep is introducing new duplication where previously there was none. 🤔

@benjamn

benjamn commented Jun 18, 2018

Copy link
Copy Markdown
Contributor

So I've decided to just "fix" the transformed subtree after whole transform takes place.

I support this 100%.

@Andarist

Copy link
Copy Markdown
Contributor Author

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.

I'm not opposed to your solution, but calling t.cloneDeep is introducing new duplication where previously there was none.

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.

@benjamn

benjamn commented Jun 19, 2018

Copy link
Copy Markdown
Contributor

@Andarist Do you think this should be a minor semantic version bump, or just a patch?

@Andarist

Copy link
Copy Markdown
Contributor Author

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

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants