Skip to content

Commit b83baf6

Browse files
authored
Transform updates to support Flow this annotation syntax (#25918)
Flow introduced a new syntax to annotated the context type of a function, this tries to update the rest and add 1 example usage. - 2b1fb91 already added the changes required for eslint. - Jest transform is updated to use the recommended `hermes-parser` which can parse current and Flow syntax and will be updated in the future. - Rollup uses a new plugin to strip the flow types. This isn't ideal as the npm module is deprecated in favor of using `hermes-parser`, but I couldn't figure out how to integrate that with Rollup.
1 parent 2619886 commit b83baf6

File tree

16 files changed

+95
-8
lines changed

16 files changed

+95
-8
lines changed

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,13 @@
6262
"fbjs-scripts": "1.2.0",
6363
"filesize": "^6.0.1",
6464
"flow-bin": "^0.190.0",
65+
"flow-remove-types": "^2.196.1",
6566
"glob": "^7.1.6",
6667
"glob-stream": "^6.1.0",
6768
"google-closure-compiler": "^20200517.0.0",
6869
"gzip-size": "^5.1.1",
6970
"hermes-eslint": "^0.9.0",
71+
"hermes-parser": "^0.9.0",
7072
"jasmine-check": "^1.0.0-rc.0",
7173
"jest": "^26.6.3",
7274
"jest-cli": "^26.6.3",

packages/react-dom-bindings/src/shared/DOMNamespaces.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
*
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @noflow
68
*/
79

810
export const HTML_NAMESPACE = 'http://www.w3.org/1999/xhtml';

packages/react-dom-bindings/src/shared/ReactControlledValuePropTypes.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
*
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @noflow
68
*/
79

810
const hasReadOnlyValue = {

packages/react-dom-bindings/src/shared/assertValidProps.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
*
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @noflow
68
*/
79

810
import voidElementTags from './voidElementTags';

packages/react-dom/src/test-utils/ReactTestUtils.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
*
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @noflow
68
*/
79

810
import * as React from 'react';

packages/react-native-renderer/src/legacy-events/ResponderEventPlugin.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
*
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @noflow
68
*/
79

810
import {

packages/react-server/src/ReactFizzServer.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1668,7 +1668,7 @@ function erroredTask(
16681668
}
16691669
}
16701670

1671-
function abortTaskSoft(task: Task): void {
1671+
function abortTaskSoft(this: Request, task: Task): void {
16721672
// This aborts task without aborting the parent boundary that it blocks.
16731673
// It's used for when we didn't need this task to complete the tree.
16741674
// If task was needed, then it should use abortTask instead.

packages/react/src/ReactForwardRef.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
*
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @noflow
68
*/
79

810
import {REACT_FORWARD_REF_TYPE, REACT_MEMO_TYPE} from 'shared/ReactSymbols';

packages/react/src/ReactMemo.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
*
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @noflow
68
*/
79

810
import {REACT_MEMO_TYPE} from 'shared/ReactSymbols';

packages/shared/forks/invokeGuardedCallbackImpl.www.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
*
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @noflow
68
*/
79

810
// Provided by www

scripts/babel/getComments.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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+
8+
'use strict';
9+
10+
function getComments(path) {
11+
const allComments = path.hub.file.ast.comments;
12+
if (path.node.leadingComments) {
13+
// Babel AST includes comments.
14+
return path.node.leadingComments;
15+
}
16+
// In Hermes AST we need to find the comments by range.
17+
const comments = [];
18+
let line = path.node.loc.start.line;
19+
let i = allComments.length - 1;
20+
while (i >= 0 && allComments[i].loc.end.line >= line) {
21+
i--;
22+
}
23+
while (i >= 0 && allComments[i].loc.end.line === line - 1) {
24+
line = allComments[i].loc.start.line;
25+
comments.unshift(allComments[i]);
26+
i--;
27+
}
28+
return comments;
29+
}
30+
31+
module.exports = getComments;

scripts/babel/transform-react-version-pragma.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
/* eslint-disable no-for-of-loops/no-for-of-loops */
44

5+
const getComments = require('./getComments');
6+
57
const GATE_VERSION_STR = '@reactVersion ';
68

79
function transform(babel) {
@@ -65,7 +67,7 @@ function transform(babel) {
6567
callee.name === 'it' ||
6668
callee.name === 'fit'
6769
) {
68-
const comments = statement.leadingComments;
70+
const comments = getComments(path);
6971
const condition = buildGateVersionCondition(comments);
7072
if (condition !== null) {
7173
callee.name =
@@ -87,7 +89,7 @@ function transform(babel) {
8789
callee.property.type === 'Identifier' &&
8890
callee.property.name === 'only'
8991
) {
90-
const comments = statement.leadingComments;
92+
const comments = getComments(path);
9193
const condition = buildGateVersionCondition(comments);
9294
if (condition !== null) {
9395
statement.expression = t.callExpression(

scripts/babel/transform-test-gate-pragma.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
/* eslint-disable no-for-of-loops/no-for-of-loops */
44

5+
const getComments = require('./getComments');
6+
57
function transform(babel) {
68
const {types: t} = babel;
79

@@ -278,7 +280,7 @@ function transform(babel) {
278280
callee.name === 'it' ||
279281
callee.name === 'fit'
280282
) {
281-
const comments = statement.leadingComments;
283+
const comments = getComments(path);
282284
if (comments !== undefined) {
283285
const condition = buildGateCondition(comments);
284286
if (condition !== null) {
@@ -304,7 +306,7 @@ function transform(babel) {
304306
callee.property.type === 'Identifier' &&
305307
callee.property.name === 'only'
306308
) {
307-
const comments = statement.leadingComments;
309+
const comments = getComments(path);
308310
if (comments !== undefined) {
309311
const condition = buildGateCondition(comments);
310312
if (condition !== null) {

scripts/jest/preprocessor.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const path = require('path');
44

55
const babel = require('@babel/core');
66
const coffee = require('coffee-script');
7+
const hermesParser = require('hermes-parser');
78

89
const tsPreprocessor = require('./typescript/preprocessor');
910
const createCacheKeyFunction = require('fbjs-scripts/jest/createCacheKeyFunction');
@@ -93,7 +94,9 @@ module.exports = {
9394
) {
9495
plugins.push(pathToTransformReactVersionPragma);
9596
}
96-
return babel.transform(
97+
let sourceAst = hermesParser.parse(src, {babel: true});
98+
return babel.transformFromAstSync(
99+
sourceAst,
97100
src,
98101
Object.assign(
99102
{filename: path.relative(process.cwd(), filePath)},

scripts/rollup/build.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const rollup = require('rollup');
44
const babel = require('rollup-plugin-babel');
55
const closure = require('./plugins/closure-plugin');
66
const commonjs = require('rollup-plugin-commonjs');
7+
const flowRemoveTypes = require('flow-remove-types');
78
const prettier = require('rollup-plugin-prettier');
89
const replace = require('rollup-plugin-replace');
910
const stripBanner = require('rollup-plugin-strip-banner');
@@ -99,7 +100,6 @@ const syncWWWPath = argv['sync-www'];
99100
// Non-ES2015 stuff applied before closure compiler.
100101
const babelPlugins = [
101102
// These plugins filter out non-ES2015.
102-
'@babel/plugin-transform-flow-strip-types',
103103
['@babel/plugin-proposal-class-properties', {loose: true}],
104104
'syntax-trailing-function-commas',
105105
// These use loose mode which avoids embedding a runtime.
@@ -325,6 +325,16 @@ function getPlugins(
325325
bundleType === RN_FB_PROFILING;
326326
const shouldStayReadable = isFBWWWBundle || isRNBundle || forcePrettyOutput;
327327
return [
328+
{
329+
name: 'rollup-plugin-flow-remove-types',
330+
transform(code) {
331+
const transformed = flowRemoveTypes(code);
332+
return {
333+
code: transformed.toString(),
334+
map: transformed.generateMap(),
335+
};
336+
},
337+
},
328338
// Shim any modules that need forking in this environment.
329339
useForks(forks),
330340
// Ensure we don't try to bundle any fbjs modules.

yarn.lock

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7920,6 +7920,20 @@ flow-bin@^0.190.0:
79207920
resolved "https://registry.yarnpkg.com/flow-bin/-/flow-bin-0.190.0.tgz#cfc50e1474facf8150232a6c498fe66a6bb75969"
79217921
integrity sha512-Qo3bvN3cmGFXsq63ZxcHFZXQDvgx84fCuq8cXuKk5xbvuebBGwMqS+ku/rH+gEkciRrcTYrXqoSzb9b6ShcoJg==
79227922

7923+
flow-parser@^0.196.1:
7924+
version "0.196.1"
7925+
resolved "https://registry.yarnpkg.com/flow-parser/-/flow-parser-0.196.1.tgz#3c31f102454518f0c68eeb99f57501c2a0c9bff0"
7926+
integrity sha512-V3yaKHyBWhl+LF6sxgbfqxMlwoFKs8UKh2DYTrGj1AHi9ST7Zyp+9ToF4l9eoL6l/DxdFwCNF3MAJ1vCVrgJmw==
7927+
7928+
flow-remove-types@^2.196.1:
7929+
version "2.196.1"
7930+
resolved "https://registry.yarnpkg.com/flow-remove-types/-/flow-remove-types-2.196.1.tgz#c77ab53679beb1b1ba420c16865cea714a67defc"
7931+
integrity sha512-pAEe2B/fKtV96MVGWQgmjP5Z1nLeFFe++r83ql1Zj86+p+3IujsbvwxiXCiF/SS6ObbB6TmciCxxd+FsOUyY3Q==
7932+
dependencies:
7933+
flow-parser "^0.196.1"
7934+
pirates "^3.0.2"
7935+
vlq "^0.2.1"
7936+
79237937
79247938
version "0.13.0"
79257939
resolved "https://registry.yarnpkg.com/fluent-syntax/-/fluent-syntax-0.13.0.tgz#417144d99cba94ff474c422b3e6623d5a842855a"
@@ -8749,7 +8763,7 @@ [email protected]:
87498763
resolved "https://registry.yarnpkg.com/hermes-estree/-/hermes-estree-0.9.0.tgz#026e0abe6db1dcf50a81a79014b779a83db3b814"
87508764
integrity sha512-5DZ7Y0CbHVk8zPqgRCvqp8iw+P05svnQDI1aJFjdqCfXJ/1CZ+8aYpGlhJ29zCG5SE5duGTzSxogAYYI4QqXqw==
87518765

8752-
8766+
[email protected], hermes-parser@^0.9.0:
87538767
version "0.9.0"
87548768
resolved "https://registry.yarnpkg.com/hermes-parser/-/hermes-parser-0.9.0.tgz#ede3044d50479c61843cef5bbdcea83933d4e4ec"
87558769
integrity sha512-IcvJIlAn+9tpHkP+HTsxWKrIdQPp0gvGrrQmxlL4XnNS+Oh6R/Fpxbcoflm2kY3zgQjEvxZxLiK/2+k3/5wsrw==
@@ -12991,6 +13005,13 @@ pinpoint@^1.1.0:
1299113005
resolved "https://registry.yarnpkg.com/pinpoint/-/pinpoint-1.1.0.tgz#0cf7757a6977f1bf7f6a32207b709e377388e874"
1299213006
integrity sha1-DPd1eml38b9/ajIge3CeN3OI6HQ=
1299313007

13008+
pirates@^3.0.2:
13009+
version "3.0.2"
13010+
resolved "https://registry.yarnpkg.com/pirates/-/pirates-3.0.2.tgz#7e6f85413fd9161ab4e12b539b06010d85954bb9"
13011+
integrity sha512-c5CgUJq6H2k6MJz72Ak1F5sN9n9wlSlJyEnwvpm9/y3WB4E3pHBDT2c6PEiS1vyJvq2bUxUAIu0EGf8Cx4Ic7Q==
13012+
dependencies:
13013+
node-modules-regexp "^1.0.0"
13014+
1299413015
pirates@^4.0.0, pirates@^4.0.1:
1299513016
version "4.0.1"
1299613017
resolved "https://registry.yarnpkg.com/pirates/-/pirates-4.0.1.tgz#643a92caf894566f91b2b986d2c66950a8e2fb87"

0 commit comments

Comments
 (0)