Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

re-implement babel traverse cache pollution bug workaround using a second copy of babel traverse #1340

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
13 changes: 4 additions & 9 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 traverseSecondInstallation from '@babel/traverse--for-generate-function-map';
import {
isAssignmentExpression,
isClassBody,
Expand Down Expand Up @@ -220,21 +221,15 @@ function forEachMapping(
) {
const visitor = getFunctionMapVisitor(context, pushMapping);

// 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, {
// TODO: improve comment
traverseSecondInstallation(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
Loading