Skip to content

Commit

Permalink
re-implement babel traverse cache pollution bug workaround
Browse files Browse the repository at this point in the history
Differential Revision: D61917782
  • Loading branch information
vzaidman authored and facebook-github-bot committed Sep 2, 2024
1 parent cef7a3c commit e9b7d66
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 8 deletions.
1 change: 1 addition & 0 deletions packages/metro-source-map/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
},
"dependencies": {
"@babel/traverse": "^7.20.0",
"@babel/traverse--for-generate-function-map": "npm:@babel/traverse@^7.20.0",
"@babel/types": "^7.20.0",
"flow-enums-runtime": "^0.0.6",
"invariant": "^2.2.4",
Expand Down
12 changes: 5 additions & 7 deletions packages/metro-source-map/src/generateFunctionMap.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ import type {NodePath} from '@babel/traverse';
import type {Node} from '@babel/types';
import type {MetroBabelFileMetadata} from 'metro-babel-transformer';

import traverse from '@babel/traverse';
// $FlowFixMe[cannot-resolve-module] - resolves to @babel/traverse
import traverseForGenerateFunctionMap from '@babel/traverse--for-generate-function-map';
import {
isAssignmentExpression,
isClassBody,
Expand Down Expand Up @@ -222,19 +223,16 @@ function forEachMapping(

// Traversing populates/pollutes the path cache (`traverse.cache.path`) with
// values missing the `hub` property needed by Babel transformation, so we
// save, clear, and restore the cache around our traversal.
// See: https://github.com/facebook/metro/pull/854#issuecomment-1336499395
const previousCache = traverse.cache.path;
traverse.cache.clearPath();
traverse(ast, {
// use a separate copy of traverse to populate a separate cache to not pollute
// the main @babel/traverse cache. See: https://github.com/facebook/metro/pull/1340
traverseForGenerateFunctionMap(ast, {
// Our visitor doesn't care about scope
noScope: true,

Function: visitor,
Program: visitor,
Class: visitor,
});
traverse.cache.path = previousCache;
}

const ANONYMOUS_NAME = '<anonymous>';
Expand Down
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1089,7 +1089,7 @@
"@babel/parser" "^7.22.5"
"@babel/types" "^7.22.5"

"@babel/traverse@^7.19.1", "@babel/traverse@^7.20.0", "@babel/traverse@^7.20.1", "@babel/traverse@^7.20.5", "@babel/traverse@^7.4.3":
"@babel/traverse--for-generate-function-map@npm:@babel/traverse@^7.20.0", "@babel/traverse@^7.19.1", "@babel/traverse@^7.20.0", "@babel/traverse@^7.20.1", "@babel/traverse@^7.20.5", "@babel/traverse@^7.4.3":
version "7.20.5"
resolved "https://registry.yarnpkg.com/@babel/traverse/-/traverse-7.20.5.tgz#78eb244bea8270fdda1ef9af22a5d5e5b7e57133"
integrity sha512-WM5ZNN3JITQIq9tFZaw1ojLU3WgWdtkxnhM1AegMS+PvHjkM5IXjmYEGY7yukz5XS4sJyEf2VzWjI8uAavhxBQ==
Expand Down

0 comments on commit e9b7d66

Please sign in to comment.