diff --git a/package.json b/package.json index 82371df4e2611..12b1adb0fa2cb 100644 --- a/package.json +++ b/package.json @@ -62,11 +62,13 @@ "fbjs-scripts": "1.2.0", "filesize": "^6.0.1", "flow-bin": "^0.190.0", + "flow-remove-types": "^2.196.1", "glob": "^7.1.6", "glob-stream": "^6.1.0", "google-closure-compiler": "^20200517.0.0", "gzip-size": "^5.1.1", "hermes-eslint": "^0.9.0", + "hermes-parser": "^0.9.0", "jasmine-check": "^1.0.0-rc.0", "jest": "^26.6.3", "jest-cli": "^26.6.3", diff --git a/packages/react-dom-bindings/src/shared/DOMNamespaces.js b/packages/react-dom-bindings/src/shared/DOMNamespaces.js index c939595c0463c..43fdf00865155 100644 --- a/packages/react-dom-bindings/src/shared/DOMNamespaces.js +++ b/packages/react-dom-bindings/src/shared/DOMNamespaces.js @@ -3,6 +3,8 @@ * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. + * + * @noflow */ export const HTML_NAMESPACE = 'http://www.w3.org/1999/xhtml'; diff --git a/packages/react-dom-bindings/src/shared/ReactControlledValuePropTypes.js b/packages/react-dom-bindings/src/shared/ReactControlledValuePropTypes.js index 596fe81cf1b64..ac1b16010647b 100644 --- a/packages/react-dom-bindings/src/shared/ReactControlledValuePropTypes.js +++ b/packages/react-dom-bindings/src/shared/ReactControlledValuePropTypes.js @@ -3,6 +3,8 @@ * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. + * + * @noflow */ const hasReadOnlyValue = { diff --git a/packages/react-dom-bindings/src/shared/assertValidProps.js b/packages/react-dom-bindings/src/shared/assertValidProps.js index 32e462b4e026b..9424838e0a1a8 100644 --- a/packages/react-dom-bindings/src/shared/assertValidProps.js +++ b/packages/react-dom-bindings/src/shared/assertValidProps.js @@ -3,6 +3,8 @@ * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. + * + * @noflow */ import voidElementTags from './voidElementTags'; diff --git a/packages/react-dom/src/test-utils/ReactTestUtils.js b/packages/react-dom/src/test-utils/ReactTestUtils.js index 94e15d2811275..0dc8ceabf955a 100644 --- a/packages/react-dom/src/test-utils/ReactTestUtils.js +++ b/packages/react-dom/src/test-utils/ReactTestUtils.js @@ -3,6 +3,8 @@ * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. + * + * @noflow */ import * as React from 'react'; diff --git a/packages/react-native-renderer/src/legacy-events/ResponderEventPlugin.js b/packages/react-native-renderer/src/legacy-events/ResponderEventPlugin.js index e3d1cf1bcd56e..8b92b3766f4b3 100644 --- a/packages/react-native-renderer/src/legacy-events/ResponderEventPlugin.js +++ b/packages/react-native-renderer/src/legacy-events/ResponderEventPlugin.js @@ -3,6 +3,8 @@ * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. + * + * @noflow */ import { diff --git a/packages/react-server/src/ReactFizzServer.js b/packages/react-server/src/ReactFizzServer.js index 236c68dfe8677..9922a36db8c43 100644 --- a/packages/react-server/src/ReactFizzServer.js +++ b/packages/react-server/src/ReactFizzServer.js @@ -1668,7 +1668,7 @@ function erroredTask( } } -function abortTaskSoft(task: Task): void { +function abortTaskSoft(this: Request, task: Task): void { // This aborts task without aborting the parent boundary that it blocks. // It's used for when we didn't need this task to complete the tree. // If task was needed, then it should use abortTask instead. diff --git a/packages/react/src/ReactForwardRef.js b/packages/react/src/ReactForwardRef.js index bfd8fc9afd3a2..5b2514cad04f7 100644 --- a/packages/react/src/ReactForwardRef.js +++ b/packages/react/src/ReactForwardRef.js @@ -3,6 +3,8 @@ * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. + * + * @noflow */ import {REACT_FORWARD_REF_TYPE, REACT_MEMO_TYPE} from 'shared/ReactSymbols'; diff --git a/packages/react/src/ReactMemo.js b/packages/react/src/ReactMemo.js index 3f797de451d10..5bce1d4c3d9cd 100644 --- a/packages/react/src/ReactMemo.js +++ b/packages/react/src/ReactMemo.js @@ -3,6 +3,8 @@ * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. + * + * @noflow */ import {REACT_MEMO_TYPE} from 'shared/ReactSymbols'; diff --git a/packages/shared/forks/invokeGuardedCallbackImpl.www.js b/packages/shared/forks/invokeGuardedCallbackImpl.www.js index 858f3331a35b7..0fec4be6614eb 100644 --- a/packages/shared/forks/invokeGuardedCallbackImpl.www.js +++ b/packages/shared/forks/invokeGuardedCallbackImpl.www.js @@ -3,6 +3,8 @@ * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. + * + * @noflow */ // Provided by www diff --git a/scripts/babel/getComments.js b/scripts/babel/getComments.js new file mode 100644 index 0000000000000..886e7d4b36136 --- /dev/null +++ b/scripts/babel/getComments.js @@ -0,0 +1,31 @@ +/** + * 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. + */ + +'use strict'; + +function getComments(path) { + const allComments = path.hub.file.ast.comments; + if (path.node.leadingComments) { + // Babel AST includes comments. + return path.node.leadingComments; + } + // In Hermes AST we need to find the comments by range. + const comments = []; + let line = path.node.loc.start.line; + let i = allComments.length - 1; + while (i >= 0 && allComments[i].loc.end.line >= line) { + i--; + } + while (i >= 0 && allComments[i].loc.end.line === line - 1) { + line = allComments[i].loc.start.line; + comments.unshift(allComments[i]); + i--; + } + return comments; +} + +module.exports = getComments; diff --git a/scripts/babel/transform-react-version-pragma.js b/scripts/babel/transform-react-version-pragma.js index 272d0d2fa59d3..1414852d2cf55 100644 --- a/scripts/babel/transform-react-version-pragma.js +++ b/scripts/babel/transform-react-version-pragma.js @@ -2,6 +2,8 @@ /* eslint-disable no-for-of-loops/no-for-of-loops */ +const getComments = require('./getComments'); + const GATE_VERSION_STR = '@reactVersion '; function transform(babel) { @@ -65,7 +67,7 @@ function transform(babel) { callee.name === 'it' || callee.name === 'fit' ) { - const comments = statement.leadingComments; + const comments = getComments(path); const condition = buildGateVersionCondition(comments); if (condition !== null) { callee.name = @@ -87,7 +89,7 @@ function transform(babel) { callee.property.type === 'Identifier' && callee.property.name === 'only' ) { - const comments = statement.leadingComments; + const comments = getComments(path); const condition = buildGateVersionCondition(comments); if (condition !== null) { statement.expression = t.callExpression( diff --git a/scripts/babel/transform-test-gate-pragma.js b/scripts/babel/transform-test-gate-pragma.js index 8b1f469f5905d..bdef629812ad3 100644 --- a/scripts/babel/transform-test-gate-pragma.js +++ b/scripts/babel/transform-test-gate-pragma.js @@ -2,6 +2,8 @@ /* eslint-disable no-for-of-loops/no-for-of-loops */ +const getComments = require('./getComments'); + function transform(babel) { const {types: t} = babel; @@ -278,7 +280,7 @@ function transform(babel) { callee.name === 'it' || callee.name === 'fit' ) { - const comments = statement.leadingComments; + const comments = getComments(path); if (comments !== undefined) { const condition = buildGateCondition(comments); if (condition !== null) { @@ -304,7 +306,7 @@ function transform(babel) { callee.property.type === 'Identifier' && callee.property.name === 'only' ) { - const comments = statement.leadingComments; + const comments = getComments(path); if (comments !== undefined) { const condition = buildGateCondition(comments); if (condition !== null) { diff --git a/scripts/jest/preprocessor.js b/scripts/jest/preprocessor.js index d8f04b5bcbfb6..4f6a49baa5a58 100644 --- a/scripts/jest/preprocessor.js +++ b/scripts/jest/preprocessor.js @@ -4,6 +4,7 @@ const path = require('path'); const babel = require('@babel/core'); const coffee = require('coffee-script'); +const hermesParser = require('hermes-parser'); const tsPreprocessor = require('./typescript/preprocessor'); const createCacheKeyFunction = require('fbjs-scripts/jest/createCacheKeyFunction'); @@ -93,7 +94,9 @@ module.exports = { ) { plugins.push(pathToTransformReactVersionPragma); } - return babel.transform( + let sourceAst = hermesParser.parse(src, {babel: true}); + return babel.transformFromAstSync( + sourceAst, src, Object.assign( {filename: path.relative(process.cwd(), filePath)}, diff --git a/scripts/rollup/build.js b/scripts/rollup/build.js index 9bd7072872035..c08adda6d9250 100644 --- a/scripts/rollup/build.js +++ b/scripts/rollup/build.js @@ -4,6 +4,7 @@ const rollup = require('rollup'); const babel = require('rollup-plugin-babel'); const closure = require('./plugins/closure-plugin'); const commonjs = require('rollup-plugin-commonjs'); +const flowRemoveTypes = require('flow-remove-types'); const prettier = require('rollup-plugin-prettier'); const replace = require('rollup-plugin-replace'); const stripBanner = require('rollup-plugin-strip-banner'); @@ -99,7 +100,6 @@ const syncWWWPath = argv['sync-www']; // Non-ES2015 stuff applied before closure compiler. const babelPlugins = [ // These plugins filter out non-ES2015. - '@babel/plugin-transform-flow-strip-types', ['@babel/plugin-proposal-class-properties', {loose: true}], 'syntax-trailing-function-commas', // These use loose mode which avoids embedding a runtime. @@ -325,6 +325,16 @@ function getPlugins( bundleType === RN_FB_PROFILING; const shouldStayReadable = isFBWWWBundle || isRNBundle || forcePrettyOutput; return [ + { + name: 'rollup-plugin-flow-remove-types', + transform(code) { + const transformed = flowRemoveTypes(code); + return { + code: transformed.toString(), + map: transformed.generateMap(), + }; + }, + }, // Shim any modules that need forking in this environment. useForks(forks), // Ensure we don't try to bundle any fbjs modules. diff --git a/yarn.lock b/yarn.lock index cda67855e9f0e..5e181ca3fdebc 100644 --- a/yarn.lock +++ b/yarn.lock @@ -7920,6 +7920,20 @@ flow-bin@^0.190.0: resolved "https://registry.yarnpkg.com/flow-bin/-/flow-bin-0.190.0.tgz#cfc50e1474facf8150232a6c498fe66a6bb75969" integrity sha512-Qo3bvN3cmGFXsq63ZxcHFZXQDvgx84fCuq8cXuKk5xbvuebBGwMqS+ku/rH+gEkciRrcTYrXqoSzb9b6ShcoJg== +flow-parser@^0.196.1: + version "0.196.1" + resolved "https://registry.yarnpkg.com/flow-parser/-/flow-parser-0.196.1.tgz#3c31f102454518f0c68eeb99f57501c2a0c9bff0" + integrity sha512-V3yaKHyBWhl+LF6sxgbfqxMlwoFKs8UKh2DYTrGj1AHi9ST7Zyp+9ToF4l9eoL6l/DxdFwCNF3MAJ1vCVrgJmw== + +flow-remove-types@^2.196.1: + version "2.196.1" + resolved "https://registry.yarnpkg.com/flow-remove-types/-/flow-remove-types-2.196.1.tgz#c77ab53679beb1b1ba420c16865cea714a67defc" + integrity sha512-pAEe2B/fKtV96MVGWQgmjP5Z1nLeFFe++r83ql1Zj86+p+3IujsbvwxiXCiF/SS6ObbB6TmciCxxd+FsOUyY3Q== + dependencies: + flow-parser "^0.196.1" + pirates "^3.0.2" + vlq "^0.2.1" + fluent-syntax@0.13.0: version "0.13.0" resolved "https://registry.yarnpkg.com/fluent-syntax/-/fluent-syntax-0.13.0.tgz#417144d99cba94ff474c422b3e6623d5a842855a" @@ -8749,7 +8763,7 @@ hermes-estree@0.9.0: resolved "https://registry.yarnpkg.com/hermes-estree/-/hermes-estree-0.9.0.tgz#026e0abe6db1dcf50a81a79014b779a83db3b814" integrity sha512-5DZ7Y0CbHVk8zPqgRCvqp8iw+P05svnQDI1aJFjdqCfXJ/1CZ+8aYpGlhJ29zCG5SE5duGTzSxogAYYI4QqXqw== -hermes-parser@0.9.0: +hermes-parser@0.9.0, hermes-parser@^0.9.0: version "0.9.0" resolved "https://registry.yarnpkg.com/hermes-parser/-/hermes-parser-0.9.0.tgz#ede3044d50479c61843cef5bbdcea83933d4e4ec" integrity sha512-IcvJIlAn+9tpHkP+HTsxWKrIdQPp0gvGrrQmxlL4XnNS+Oh6R/Fpxbcoflm2kY3zgQjEvxZxLiK/2+k3/5wsrw== @@ -12991,6 +13005,13 @@ pinpoint@^1.1.0: resolved "https://registry.yarnpkg.com/pinpoint/-/pinpoint-1.1.0.tgz#0cf7757a6977f1bf7f6a32207b709e377388e874" integrity sha1-DPd1eml38b9/ajIge3CeN3OI6HQ= +pirates@^3.0.2: + version "3.0.2" + resolved "https://registry.yarnpkg.com/pirates/-/pirates-3.0.2.tgz#7e6f85413fd9161ab4e12b539b06010d85954bb9" + integrity sha512-c5CgUJq6H2k6MJz72Ak1F5sN9n9wlSlJyEnwvpm9/y3WB4E3pHBDT2c6PEiS1vyJvq2bUxUAIu0EGf8Cx4Ic7Q== + dependencies: + node-modules-regexp "^1.0.0" + pirates@^4.0.0, pirates@^4.0.1: version "4.0.1" resolved "https://registry.yarnpkg.com/pirates/-/pirates-4.0.1.tgz#643a92caf894566f91b2b986d2c66950a8e2fb87"