Skip to content

Commit c2365bb

Browse files
motiz88facebook-github-bot
authored andcommittedSep 21, 2022
metro-react-native-babel-preset: Remove exponentiation operator transform
Summary: Context: `Math.pow(a, b)` throws when `a` or `b` is a BigInt, while `a ** b` evaluates to a BigInt in that case. This makes [`babel/plugin-transform-exponentiation-operator`](https://babeljs.io/docs/en/babel-plugin-transform-exponentiation-operator) unsafe in code that uses BigInts. See also facebook/react-native#34656, facebook/react-native#34603. Here we remove the plugin from the React Native Babel preset. This is safe to do because: 1. Hermes supports the `**` operator [natively](facebook/hermes@ae0bc93). 2. JSC on iOS has supported the `**` operator natively since version 10.3 (source: [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Exponentiation#browser_compatibility)), and React Native targets iOS 12.4+. 3. The version of JSC React Native uses on Android ([jsc-android@v250230.2.1](https://github.com/react-native-community/jsc-android-buildscripts/releases/tag/v250230.2.1)) is based on [WebKit commit 250230](https://commits.webkit.org/250230@main) which [supports](https://github.com/WebKit/WebKit/blob/1d27863e32855c7fe51d5de1aae0589f11a428be/Source/JavaScriptCore/features.json#L170-L185) the `**` operator. 4. Metro now uses Terser as the default minifier (#871) which includes support for modern language features such as the `**` operator. Changelog: * **[Fix]** Remove exponentiation operator transform from `metro-react-native-babel-preset`. NOTE: This is a fix and not a breaking change because `metro-react-native-babel-preset` is *already* meant for use only with current React Native versions, where the `**` operator is supported natively (as shown above). Reviewed By: huntie Differential Revision: D39683437 fbshipit-source-id: 67d84e7a9eecf27e40b7d7b9d4b3666a4201fec1
1 parent 69607ae commit c2365bb

File tree

7 files changed

+31
-34
lines changed

7 files changed

+31
-34
lines changed
 

‎packages/metro-react-native-babel-preset/package.json

-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
"@babel/plugin-transform-classes": "^7.0.0",
3737
"@babel/plugin-transform-computed-properties": "^7.0.0",
3838
"@babel/plugin-transform-destructuring": "^7.0.0",
39-
"@babel/plugin-transform-exponentiation-operator": "^7.0.0",
4039
"@babel/plugin-transform-flow-strip-types": "^7.0.0",
4140
"@babel/plugin-transform-function-name": "^7.0.0",
4241
"@babel/plugin-transform-literals": "^7.0.0",

‎packages/metro-react-native-babel-preset/src/configs/main.js

-5
Original file line numberDiff line numberDiff line change
@@ -123,11 +123,6 @@ const getPreset = (src, options) => {
123123
]);
124124
extraPlugins.push([require('@babel/plugin-transform-async-to-generator')]);
125125
}
126-
if (!isHermes && (isNull || src.indexOf('**') !== -1)) {
127-
extraPlugins.push([
128-
require('@babel/plugin-transform-exponentiation-operator'),
129-
]);
130-
}
131126
if (
132127
isNull ||
133128
src.indexOf('React.createClass') !== -1 ||

‎packages/metro/package.json

-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@
6767
},
6868
"devDependencies": {
6969
"@babel/plugin-transform-flow-strip-types": "^7.0.0",
70-
"acorn": "^8.7.1",
7170
"babel-jest": "^29.0.3",
7271
"dedent": "^0.7.0",
7372
"jest-snapshot": "^26.5.2",

‎packages/metro/src/integration_tests/__tests__/build-test.js

+14
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,17 @@ it('build a simple bundle with polyfills', async () => {
4949
});
5050
expect(execBundle(result.code)).toBe('POLYFILL_IS_INJECTED');
5151
});
52+
53+
it('builds a bundle with BigInt and exponentiation syntax', async () => {
54+
const config = await Metro.loadConfig({
55+
config: require.resolve('../metro.config.js'),
56+
});
57+
58+
const result = await Metro.runBuild(config, {
59+
entry: 'TestBigInt.js',
60+
});
61+
62+
// $FlowIssue[cannot-resolve-name] Flow is missing BigInt support
63+
const BI = BigInt;
64+
expect(execBundle(result.code)).toBe(BI(8));
65+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @format
8+
* @flow
9+
*/
10+
11+
'use strict';
12+
13+
// $FlowIssue[bigint-unsupported]
14+
var a = 2n;
15+
// $FlowIssue[bigint-unsupported]
16+
var b = 3n;
17+
module.exports = a ** b;

‎packages/metro/src/integration_tests/execBundle.js

-4
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,8 @@
1111

1212
'use strict';
1313

14-
const acorn = require('acorn');
1514
const vm = require('vm');
1615

1716
module.exports = function execBundle(code: string, context: {...} = {}): mixed {
18-
// Verify the code can run on older VMs by parsing it as ES5 (versus ES6+).
19-
acorn.parse(code, {ecmaVersion: 5});
20-
2117
return vm.runInNewContext(code, context);
2218
};

‎yarn.lock

-23
Original file line numberDiff line numberDiff line change
@@ -157,14 +157,6 @@
157157
dependencies:
158158
"@babel/types" "^7.16.7"
159159

160-
"@babel/helper-builder-binary-assignment-operator-visitor@^7.12.13":
161-
version "7.12.13"
162-
resolved "https://registry.yarnpkg.com/@babel/helper-builder-binary-assignment-operator-visitor/-/helper-builder-binary-assignment-operator-visitor-7.12.13.tgz#6bc20361c88b0a74d05137a65cac8d3cbf6f61fc"
163-
integrity sha512-CZOv9tGphhDRlVjVkAgm8Nhklm9RzSmWpX2my+t7Ua/KT616pEzXsQCjinzvkRvHWJ9itO4f296efroX23XCMA==
164-
dependencies:
165-
"@babel/helper-explode-assignable-expression" "^7.12.13"
166-
"@babel/types" "^7.12.13"
167-
168160
"@babel/helper-compilation-targets@^7.13.0", "@babel/helper-compilation-targets@^7.13.16", "@babel/helper-compilation-targets@^7.13.8":
169161
version "7.13.16"
170162
resolved "https://registry.yarnpkg.com/@babel/helper-compilation-targets/-/helper-compilation-targets-7.13.16.tgz#6e91dccf15e3f43e5556dffe32d860109887563c"
@@ -254,13 +246,6 @@
254246
resolved "https://registry.yarnpkg.com/@babel/helper-environment-visitor/-/helper-environment-visitor-7.18.9.tgz#0c0cee9b35d2ca190478756865bb3528422f51be"
255247
integrity sha512-3r/aACDJ3fhQ/EVgFy0hpj8oHyHpQc+LPtJoY9SzTThAsStm4Ptegq92vqKoE3vD706ZVFWITnMnxucw+S9Ipg==
256248

257-
"@babel/helper-explode-assignable-expression@^7.12.13":
258-
version "7.13.0"
259-
resolved "https://registry.yarnpkg.com/@babel/helper-explode-assignable-expression/-/helper-explode-assignable-expression-7.13.0.tgz#17b5c59ff473d9f956f40ef570cf3a76ca12657f"
260-
integrity sha512-qS0peLTDP8kOisG1blKbaoBg/o9OSa1qoumMjTK5pM+KDTtpxpsiubnCGP34vK8BXGcb2M9eigwgvoJryrzwWA==
261-
dependencies:
262-
"@babel/types" "^7.13.0"
263-
264249
"@babel/helper-function-name@^7.12.13":
265250
version "7.12.13"
266251
resolved "https://registry.yarnpkg.com/@babel/helper-function-name/-/helper-function-name-7.12.13.tgz#93ad656db3c3c2232559fd7b2c3dbdcbe0eb377a"
@@ -844,14 +829,6 @@
844829
dependencies:
845830
"@babel/helper-plugin-utils" "^7.13.0"
846831

847-
"@babel/plugin-transform-exponentiation-operator@^7.0.0":
848-
version "7.12.13"
849-
resolved "https://registry.yarnpkg.com/@babel/plugin-transform-exponentiation-operator/-/plugin-transform-exponentiation-operator-7.12.13.tgz#4d52390b9a273e651e4aba6aee49ef40e80cd0a1"
850-
integrity sha512-fbUelkM1apvqez/yYx1/oICVnGo2KM5s63mhGylrmXUxK/IAXSIf87QIxVfZldWf4QsOafY6vV3bX8aMHSvNrA==
851-
dependencies:
852-
"@babel/helper-builder-binary-assignment-operator-visitor" "^7.12.13"
853-
"@babel/helper-plugin-utils" "^7.12.13"
854-
855832
"@babel/plugin-transform-flow-strip-types@^7.0.0":
856833
version "7.13.0"
857834
resolved "https://registry.yarnpkg.com/@babel/plugin-transform-flow-strip-types/-/plugin-transform-flow-strip-types-7.13.0.tgz#58177a48c209971e8234e99906cb6bd1122addd3"

0 commit comments

Comments
 (0)
Please sign in to comment.