Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
re-implement babel traverse cache pollution bug workaround using a se…
…cond copy of babel traverse Summary: ### The issue Traverse in babel 7 is currently polluting the cache with nodes that have no `hub` entry (see babel/babel#6437). Since the traverse cache is used by other babel operations that expect `hub` to be present, this breaks the code in all kind of unexpected scenarios. While this issue is fixed in babel 8 (not released at this moment), it's still an issue in the latest version of babel 7. ### The previous workaround We implemented a workaround for it in #854 (comment) however in the latest version of `babel/traverse`, that workaround cannot be used anymore (more about that below). ### The new workaround Instead, we are implementing a different workaround that installs traverse twice, including installing it's cache file twice. We use the second copy of traverse `babel/traverse--for-generate-function-map` only in `forEachMapping`, allowing the rest of the system to use the traverse caching without the pollution issue mentioned above. ### Why the previous workaround stopped working Due to the use of a `let export` in the latest version of traverse cache, and how it's used in the latest version, we can't re-write `traverse.cache.path` anymore. This cache is exported with a `let export`: https://github.com/babel/babel/blob/5ebab544af2f1c6fc6abdaae6f4e5426975c9a16/packages/babel-traverse/src/cache.ts#L6-L20 And it compiles to: ``` let pathsCache = exports.path = new WeakMap(); function clearPath() { exports.path = pathsCache = new WeakMap(); } ``` and then used like this: ``` function getCachedPaths(hub, parent) { // ... pathsCache.get(hub); // ... } ``` Which means that re-writing the export like we used to do breaks the traverse cache because `exports.path` is re-written, but not `pathsCache` while the latter is used inside the file: ``` const previousCache = traverse.cache.path; traverse.cache.clearPath(); traverse(ast, { /* settings */ }); // this line is breaking the traverse cache traverse.cache.path = previousCache; ``` Differential Revision: D61917782
- Loading branch information