Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
improve traverse cache pollution bug workaround testing (#1330)
Summary: Pull Request resolved: #1330 **Problem:** `traverse(ast` was polluting `traverse.cache.path` with values missing the `hub` property needed by Babel transformation. **Former solution:** the solution implemented formally, was saving `traverse.cache.path`, clearing the cache, and then re-assigning `traverse.cache.path` to be the one before the traversal. Unfortunately, with the latest version of `babel/traverse` the assignment `traverse.cache.path = previousCache` no longer works. This is because `path` inside the esmodule `traverse.cache` is a `let export` ([see source code](https://github.com/babel/babel/blob/main/packages/babel-traverse/src/cache.ts#L6C1-L21C1)) which is resoved to: ``` let pathsCache = exports.path = new WeakMap(); function clearPath() { exports.path = pathsCache = new WeakMap(); } function getCachedPaths(hub, parent) { // ... pathsCache.get( //... // ... } ``` this means that re-writing `exports.path` breaks the module since `exports.path` won't be identical to `pathsCache` anymore and because it is used inside the module, the module breaks. **Proposed solution:** A one time shallow copy ("`shallowCopiedAST`") of the `ast` for the traverse ensures that the original `ast` is not polluted. The copy is then deleted when `forEachMapping` which calls `traverse` is ends and `shallowCopiedAST` goes out of scope, referenced only by `traverse.cache.path` which is a `WeakMap`. Reviewed By: robhogan Differential Revision: D61336390 fbshipit-source-id: 600f4032a10fa289a323beceedd182417914261b
- Loading branch information