Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3952cb7

Browse files
vzaidmanmeta-codesync[bot]
authored andcommitted
fix @babel/traverse path cache workaround not needed in latest babel (#1650)
Summary: Pull Request resolved: #1650 [#17708](babel/babel#17708) fixes the longstanding `babel/traverse` pollution issue in [v7.29.0](https://github.com/babel/babel/releases/tag/v7.29.0). This diff opts-out of the workaround if the version of traverse is above the version where it was fixed. Changelog: Internal Reviewed By: robhogan Differential Revision: D92710292 fbshipit-source-id: 6b202c001cdf5d2ec02c81c3e9f280752d83c998
1 parent 10354e6 commit 3952cb7

4 files changed

Lines changed: 58 additions & 11 deletions

File tree

‎packages/metro-source-map/package.json‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@
3232
"devDependencies": {
3333
"@babel/core": "^7.25.2",
3434
"@babel/parser": "^7.25.3",
35-
"terser": "^5.15.0"
35+
"terser": "^5.15.0",
36+
"semver": "^7.1.3"
3637
},
3738
"engines": {
3839
"node": ">=20.19.4"

‎packages/metro-source-map/src/__tests__/generateFunctionMap-test.js‎

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,15 @@ const {
3333
SourceMetadataMapConsumer,
3434
} = require('metro-symbolicate/private/Symbolication');
3535

36+
// $FlowFixMe[cannot-resolve-module] - reading version from package.json
37+
// $FlowFixMe[untyped-import]
38+
const babelTraverseVersion = require('@babel/traverse/package.json').version;
39+
// $FlowFixMe[untyped-import]
40+
const babelTraverseCacheBugFixed = require('semver').gte(
41+
babelTraverseVersion,
42+
'7.29.0',
43+
);
44+
3645
function getAst(source: string) {
3746
return parse(source, {
3847
plugins: ['classProperties', 'dynamicImport', 'jsx', 'flow'],
@@ -1902,8 +1911,19 @@ window.foo();
19021911
// Perform a trivial traversal.
19031912
traverse(ast, {});
19041913

1905-
// Expect that the path cache is polluted with entries lacking `hub`.
1906-
expect(() => expectTransformPathesToHaveHub(ast)).toThrow();
1914+
try {
1915+
if (babelTraverseCacheBugFixed) {
1916+
expect(() => expectTransformPathesToHaveHub(ast)).not.toThrow();
1917+
} else {
1918+
// Expect that the path cache is polluted with entries lacking `hub`.
1919+
expect(() => expectTransformPathesToHaveHub(ast)).toThrow();
1920+
}
1921+
} catch (e) {
1922+
console.error(
1923+
'Test failed with @babel/traverse version: ' + babelTraverseVersion,
1924+
);
1925+
throw e;
1926+
}
19071927
});
19081928

19091929
test('successfully works around traverse cache pollution', () => {
@@ -1914,6 +1934,10 @@ window.foo();
19141934
});
19151935

19161936
test('does not reset the path cache', () => {
1937+
if (babelTraverseCacheBugFixed) {
1938+
return;
1939+
}
1940+
19171941
const dummyCache: Map<unknown, unknown> = new Map();
19181942
// $FlowFixMe[prop-missing] - Writing to readonly map for test purposes.
19191943
traverse.cache.path.set(ast, dummyCache);

‎packages/metro-source-map/src/generateFunctionMap.js‎

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,11 @@
1111

1212
import type {FBSourceFunctionMap} from './source-map';
1313
import type {PluginObj} from '@babel/core';
14-
import type {NodePath} from '@babel/traverse';
14+
import type {NodePath, Traverse} from '@babel/traverse';
1515
import type {Node as BabelNode} from '@babel/types';
1616
import type {MetroBabelFileMetadata} from 'metro-babel-transformer';
1717

1818
import B64Builder from './B64Builder';
19-
// $FlowFixMe[cannot-resolve-module] - resolves to @babel/traverse
20-
import traverseForGenerateFunctionMap from '@babel/traverse--for-generate-function-map';
2119
import * as t from '@babel/types';
2220
import {
2321
isAssignmentExpression,
@@ -46,6 +44,29 @@ import invariant from 'invariant';
4644
import nullthrows from 'nullthrows';
4745
import fsPath from 'path';
4846

47+
// $FlowFixMe[untyped-import]
48+
import semver from 'semver';
49+
50+
const isBabelTraverseCacheBugFixed = semver.gte(
51+
// $FlowFixMe[cannot-resolve-module] - reading version from package.json
52+
// $FlowFixMe[untyped-import]
53+
require('@babel/traverse/package.json').version, // eslint-disable-line import/no-commonjs
54+
'7.29.0',
55+
);
56+
57+
// TODO: when babel is bumpedabove 7.29.0, remove:
58+
// - this workaround
59+
// - the '@babel/traverse--for-generate-function-map' dependency from "package.json"
60+
// - the test from this workaround from "generateFunctionMap-test.js"
61+
// Before Babel 7.29.0, "traverse" populates/pollutes the path cache (`traverse.cache.path`)
62+
// with values missing the `hub` property needed by Babel transformation, so we
63+
// use a separate copy of traverse to populate a separate cache to not pollute
64+
// the main @babel/traverse cache. See: https://github.com/facebook/metro/pull/1340
65+
const traverseFixed: Traverse = isBabelTraverseCacheBugFixed
66+
? require('@babel/traverse').default
67+
: // $FlowFixMe[cannot-resolve-module] - resolves to @babel/traverse
68+
require('@babel/traverse--for-generate-function-map').default;
69+
4970
type Position = {
5071
line: number,
5172
column: number,
@@ -218,11 +239,7 @@ function forEachMapping(
218239
) {
219240
const visitor = getFunctionMapVisitor(context, pushMapping);
220241

221-
// Traversing populates/pollutes the path cache (`traverse.cache.path`) with
222-
// values missing the `hub` property needed by Babel transformation, so we
223-
// use a separate copy of traverse to populate a separate cache to not pollute
224-
// the main @babel/traverse cache. See: https://github.com/facebook/metro/pull/1340
225-
traverseForGenerateFunctionMap(ast, {
242+
traverseFixed(ast, {
226243
// Our visitor doesn't care about scope
227244
noScope: true,
228245

‎yarn.lock‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4952,6 +4952,11 @@ semver@^6.0.0, semver@^6.3.0, semver@^6.3.1:
49524952
resolved "https://registry.yarnpkg.com/semver/-/semver-6.3.1.tgz#556d2ef8689146e46dcea4bfdd095f3434dffcb4"
49534953
integrity sha512-BR7VvDCVHO+q2xBEWskxS6DJE1qRnb7DxzUrogb71CWoSficBxYsiAGd+Kl0mmq/MprG9yArRkyrQxTO6XjMzA==
49544954

4955+
semver@^7.1.3:
4956+
version "7.7.4"
4957+
resolved "https://registry.yarnpkg.com/semver/-/semver-7.7.4.tgz#28464e36060e991fa7a11d0279d2d3f3b57a7e8a"
4958+
integrity sha512-vFKC2IEtQnVhpT78h1Yp8wzwrf8CM+MzKMHGJZfBtzhZNycRFnXsHk6E5TxIkkMsgNS7mdX3AGB7x2QM2di4lA==
4959+
49554960
semver@^7.3.5, semver@^7.5.3, semver@^7.5.4, semver@^7.6.0:
49564961
version "7.6.3"
49574962
resolved "https://registry.yarnpkg.com/semver/-/semver-7.6.3.tgz#980f7b5550bc175fb4dc09403085627f9eb33143"

0 commit comments

Comments
 (0)