Skip to content

Commit

Permalink
metro-react-native-babel-preset: Remove exponentiation operator trans…
Browse files Browse the repository at this point in the history
…form

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 ([[email protected]](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
  • Loading branch information
motiz88 authored and facebook-github-bot committed Sep 21, 2022
1 parent 69607ae commit c2365bb
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 34 deletions.
1 change: 0 additions & 1 deletion packages/metro-react-native-babel-preset/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
"@babel/plugin-transform-classes": "^7.0.0",
"@babel/plugin-transform-computed-properties": "^7.0.0",
"@babel/plugin-transform-destructuring": "^7.0.0",
"@babel/plugin-transform-exponentiation-operator": "^7.0.0",
"@babel/plugin-transform-flow-strip-types": "^7.0.0",
"@babel/plugin-transform-function-name": "^7.0.0",
"@babel/plugin-transform-literals": "^7.0.0",
Expand Down
5 changes: 0 additions & 5 deletions packages/metro-react-native-babel-preset/src/configs/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,6 @@ const getPreset = (src, options) => {
]);
extraPlugins.push([require('@babel/plugin-transform-async-to-generator')]);
}
if (!isHermes && (isNull || src.indexOf('**') !== -1)) {
extraPlugins.push([
require('@babel/plugin-transform-exponentiation-operator'),
]);
}
if (
isNull ||
src.indexOf('React.createClass') !== -1 ||
Expand Down
1 change: 0 additions & 1 deletion packages/metro/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@
},
"devDependencies": {
"@babel/plugin-transform-flow-strip-types": "^7.0.0",
"acorn": "^8.7.1",
"babel-jest": "^29.0.3",
"dedent": "^0.7.0",
"jest-snapshot": "^26.5.2",
Expand Down
14 changes: 14 additions & 0 deletions packages/metro/src/integration_tests/__tests__/build-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,17 @@ it('build a simple bundle with polyfills', async () => {
});
expect(execBundle(result.code)).toBe('POLYFILL_IS_INJECTED');
});

it('builds a bundle with BigInt and exponentiation syntax', async () => {
const config = await Metro.loadConfig({
config: require.resolve('../metro.config.js'),
});

const result = await Metro.runBuild(config, {
entry: 'TestBigInt.js',
});

// $FlowIssue[cannot-resolve-name] Flow is missing BigInt support
const BI = BigInt;
expect(execBundle(result.code)).toBe(BI(8));
});
17 changes: 17 additions & 0 deletions packages/metro/src/integration_tests/basic_bundle/TestBigInt.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
* @flow
*/

'use strict';

// $FlowIssue[bigint-unsupported]
var a = 2n;
// $FlowIssue[bigint-unsupported]
var b = 3n;
module.exports = a ** b;
4 changes: 0 additions & 4 deletions packages/metro/src/integration_tests/execBundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,8 @@

'use strict';

const acorn = require('acorn');
const vm = require('vm');

module.exports = function execBundle(code: string, context: {...} = {}): mixed {
// Verify the code can run on older VMs by parsing it as ES5 (versus ES6+).
acorn.parse(code, {ecmaVersion: 5});

return vm.runInNewContext(code, context);
};
23 changes: 0 additions & 23 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -157,14 +157,6 @@
dependencies:
"@babel/types" "^7.16.7"

"@babel/helper-builder-binary-assignment-operator-visitor@^7.12.13":
version "7.12.13"
resolved "https://registry.yarnpkg.com/@babel/helper-builder-binary-assignment-operator-visitor/-/helper-builder-binary-assignment-operator-visitor-7.12.13.tgz#6bc20361c88b0a74d05137a65cac8d3cbf6f61fc"
integrity sha512-CZOv9tGphhDRlVjVkAgm8Nhklm9RzSmWpX2my+t7Ua/KT616pEzXsQCjinzvkRvHWJ9itO4f296efroX23XCMA==
dependencies:
"@babel/helper-explode-assignable-expression" "^7.12.13"
"@babel/types" "^7.12.13"

"@babel/helper-compilation-targets@^7.13.0", "@babel/helper-compilation-targets@^7.13.16", "@babel/helper-compilation-targets@^7.13.8":
version "7.13.16"
resolved "https://registry.yarnpkg.com/@babel/helper-compilation-targets/-/helper-compilation-targets-7.13.16.tgz#6e91dccf15e3f43e5556dffe32d860109887563c"
Expand Down Expand Up @@ -254,13 +246,6 @@
resolved "https://registry.yarnpkg.com/@babel/helper-environment-visitor/-/helper-environment-visitor-7.18.9.tgz#0c0cee9b35d2ca190478756865bb3528422f51be"
integrity sha512-3r/aACDJ3fhQ/EVgFy0hpj8oHyHpQc+LPtJoY9SzTThAsStm4Ptegq92vqKoE3vD706ZVFWITnMnxucw+S9Ipg==

"@babel/helper-explode-assignable-expression@^7.12.13":
version "7.13.0"
resolved "https://registry.yarnpkg.com/@babel/helper-explode-assignable-expression/-/helper-explode-assignable-expression-7.13.0.tgz#17b5c59ff473d9f956f40ef570cf3a76ca12657f"
integrity sha512-qS0peLTDP8kOisG1blKbaoBg/o9OSa1qoumMjTK5pM+KDTtpxpsiubnCGP34vK8BXGcb2M9eigwgvoJryrzwWA==
dependencies:
"@babel/types" "^7.13.0"

"@babel/helper-function-name@^7.12.13":
version "7.12.13"
resolved "https://registry.yarnpkg.com/@babel/helper-function-name/-/helper-function-name-7.12.13.tgz#93ad656db3c3c2232559fd7b2c3dbdcbe0eb377a"
Expand Down Expand Up @@ -844,14 +829,6 @@
dependencies:
"@babel/helper-plugin-utils" "^7.13.0"

"@babel/plugin-transform-exponentiation-operator@^7.0.0":
version "7.12.13"
resolved "https://registry.yarnpkg.com/@babel/plugin-transform-exponentiation-operator/-/plugin-transform-exponentiation-operator-7.12.13.tgz#4d52390b9a273e651e4aba6aee49ef40e80cd0a1"
integrity sha512-fbUelkM1apvqez/yYx1/oICVnGo2KM5s63mhGylrmXUxK/IAXSIf87QIxVfZldWf4QsOafY6vV3bX8aMHSvNrA==
dependencies:
"@babel/helper-builder-binary-assignment-operator-visitor" "^7.12.13"
"@babel/helper-plugin-utils" "^7.12.13"

"@babel/plugin-transform-flow-strip-types@^7.0.0":
version "7.13.0"
resolved "https://registry.yarnpkg.com/@babel/plugin-transform-flow-strip-types/-/plugin-transform-flow-strip-types-7.13.0.tgz#58177a48c209971e8234e99906cb6bd1122addd3"
Expand Down

0 comments on commit c2365bb

Please sign in to comment.