Skip to content

Don't insert the same node into the AST multiple times (fixes babel/babili#556) - #6054

Merged
hzoo merged 1 commit into
babel:7.0from
not-an-aardvark:no-identical-nodes-in-ast
Aug 4, 2017
Merged

hzoo merged 1 commit into
babel:7.0from
not-an-aardvark:no-identical-nodes-in-ast

Conversation

@not-an-aardvark

Copy link
Copy Markdown
Contributor
Q A
Patch: Bug Fix? yes
Major: Breaking Change? no
Minor: New Feature? no
Deprecations? no
Spec Compliancy? no
Tests Added/Pass? yes
Fixed Tickets Fixes babel/minify#556
License MIT
Doc PR no
Dependency Changes no

This updates babel-plugin-transform-es2015-modules-commonjs to avoid inserting the same object for multiple AST nodes.

The issue in babel/minify#556 is that when babili is used in combination with babel-plugin-transform-es2015-modules-commonjs, the path.scope property here is sometimes incorrect. (From the code sample in babel/minify#556 (comment), the issue, the scope when traversing extends _baz2.default was actually the scope from the following function expression.) This causes an error because babel-plugin-transform-es2015-modules-commonjs encounters a scope it hasn't seen before.

I think the issue is that babel-plugin-transform-es2015-modules-commonjs is reusing the same JavaScript object to insert a node into multiple places into an AST, and this causes problems with the scope cache here, which is a WeakMap whose keys are AST node objects. This PR updates babel-plugin-transform-es2015-modules-commonjs to avoid using the same object for multiple different node locations, and it seems to fix the problem in babili. However, if inserting the same object at multiple node locations is supposed to be supported, then it's possible that this is fixing a side-effect and not the root cause. I'm unfamiliar with a lot of babel's internals, so I'm unsure whether that's the case.

@mention-bot

Copy link
Copy Markdown

@not-an-aardvark, thanks for your PR! By analyzing the history of the files in this pull request, we identified @hzoo, @existentialism and @loganfsmyth to be potential reviewers.

@hzoo

hzoo commented Aug 4, 2017

Copy link
Copy Markdown
Member

Thanks for the investigation/PR @not-an-aardvark! Just had a similar issue with not cloning nodes in #6046 via #6038

@existentialism existentialism added the PR: Bug Fix 🐛 A type of pull request used for our changelog categories label Aug 4, 2017
@hzoo
hzoo merged commit 13d931c into babel:7.0 Aug 4, 2017
@hzoo

hzoo commented Aug 4, 2017

Copy link
Copy Markdown
Member

We need to look into how we can prevent this kind of issue in general (verify nodes), or the higher level ideas of immutable ASTs as in https://github.com/babel/notes/blob/master/2016-08/august-01.md#potential-api-changes-for-traversal

@hzoo

hzoo commented Aug 4, 2017

Copy link
Copy Markdown
Member

Thanks again for this, I think you already uncovered a lot about the internals 😄 , https://github.com/thejameskyle/babel-handbook/blob/master/translations/en/plugin-handbook.md has some more info but what you already know about ASTs from ESLint mostly applies already!

@not-an-aardvark
not-an-aardvark deleted the no-identical-nodes-in-ast branch August 4, 2017 23:28
@not-an-aardvark

Copy link
Copy Markdown
Contributor Author

We need to look into how we can prevent this kind of issue in general (verify nodes)

One short-term solution for the cloning problem could be to verify the AST as part of a plugin's unit tests, by doing another traversal afterwards and making sure all the nodes in the AST are unique.

@not-an-aardvark not-an-aardvark changed the title Don't insert the same node into the AST multiple times (fixes babel/babili#56) Don't insert the same node into the AST multiple times (fixes babel/babili#556) Aug 4, 2017
@hzoo

hzoo commented Aug 5, 2017

Copy link
Copy Markdown
Member

Yeah that sounds like a good idea, @jridgewell mentioned we could add another check if nodes are reused after we land #5971 which does a deep AST check

@ndugger

ndugger commented Oct 5, 2017

Copy link
Copy Markdown

@hzoo There are still some people experiencing the error found in babel/minify#556 -- can you provide clarification on how we might adjust our configuration to help mitigate this?

@hzoo

hzoo commented Oct 5, 2017

Copy link
Copy Markdown
Member

Hey @ndugger, the fix was done in 7.0/master so you'd need to update to Babel 7, although I don't think minify is on Babel 7 either.

Don't really want to continue doing backport's for everything though. You could try just changing babel-plugin-transform-es2015-modules-commonjs specifically to a 7.0.0-beta.2 but that may not work

@lock lock Bot added the outdated A closed issue/PR that is archived due to age. Recommended to make a new issue label Oct 5, 2019
@lock lock Bot locked as resolved and limited conversation to collaborators Oct 5, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

outdated A closed issue/PR that is archived due to age. Recommended to make a new issue PR: Bug Fix 🐛 A type of pull request used for our changelog categories

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants