Repository navigation
Don't insert the same node into the AST multiple times (fixes babel/babili#556) - #6054
Conversation
|
@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. |
|
Thanks for the investigation/PR @not-an-aardvark! Just had a similar issue with not cloning nodes in #6046 via #6038 |
|
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 |
|
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! |
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. |
|
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 |
|
@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? |
|
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 |
This updates
babel-plugin-transform-es2015-modules-commonjsto avoid inserting the same object for multiple AST nodes.The issue in babel/minify#556 is that when
babiliis used in combination withbabel-plugin-transform-es2015-modules-commonjs, thepath.scopeproperty here is sometimes incorrect. (From the code sample in babel/minify#556 (comment), the issue, thescopewhen traversingextends _baz2.defaultwas actually the scope from the following function expression.) This causes an error becausebabel-plugin-transform-es2015-modules-commonjsencounters a scope it hasn't seen before.I think the issue is that
babel-plugin-transform-es2015-modules-commonjsis 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 updatesbabel-plugin-transform-es2015-modules-commonjsto avoid using the same object for multiple different node locations, and it seems to fix the problem inbabili. 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.