Skip to content

Commit e7a9322

Browse files
yungstersfacebook-github-bot
authored andcommitted
RN: Flowify packages/react-native-scripts (#51671)
Summary: Pull Request resolved: #51671 Adds `flow` (or `noflow`) to all files in this directory and ensures that Flow succeeds (by adding type annotations, using minor refactors, or suppressing errors due to intentionally dynamic logic). This will help improve type safety when making changes both in these files as well as files that these depend on. Changelog: [Internal] Reviewed By: SamChou19815 Differential Revision: D75581879 fbshipit-source-id: 6dcd8cc55d0021973eeae2670c1ebceb6d69fa8f
1 parent 1fd9508 commit e7a9322

32 files changed

+313
-163
lines changed

packages/react-native/scripts/bundle.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
66
*
7+
* @flow strict-local
78
* @format
89
*/
910

@@ -16,6 +17,7 @@ const {readFileSync} = require('fs');
1617
const path = require('path');
1718

1819
// Commander 12.0.0 changes from the global to named export
20+
// $FlowFixMe[signature-verification-failure]
1921
const program = commander.program ?? commander;
2022

2123
program.version(
@@ -37,7 +39,14 @@ program
3739
.allowUnknownOption()
3840
.action(async function handleAction() {
3941
let config = null;
40-
let options = program.opts();
42+
let options = program
43+
.opts /*::<{
44+
configCmd?: string,
45+
loadConfig?: string,
46+
verbose: boolean,
47+
...
48+
}>*/
49+
();
4150
if (options.loadConfig != null) {
4251
config = JSON.parse(
4352
options.loadConfig.replace(/^\W*'/, '').replace(/'\W*$/, ''),

packages/react-native/scripts/codegen/__fixtures__/fixtures.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
66
*
7+
* @flow strict-local
78
* @format
89
*/
910

@@ -120,6 +121,7 @@ const SCHEMA_TEXT = `
120121
}
121122
`;
122123

124+
// $FlowFixMe[signature-verification-failure]
123125
const SCHEMA = JSON.parse(SCHEMA_TEXT);
124126

125127
module.exports = {

packages/react-native/scripts/codegen/__tests__/generate-artifacts-executor-test.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
66
*
7+
* @flow strict-local
78
* @format
89
*/
910

@@ -243,7 +244,7 @@ describe('delete empty files and folders', () => {
243244

244245
it("when path is folder and it's empty, removes it", () => {
245246
const targetFolder = 'build';
246-
const content = [];
247+
const content = [] as Array<string>;
247248

248249
let statSyncInvocationCount = 0;
249250
let readdirInvocationCount = 0;
@@ -290,8 +291,8 @@ describe('delete empty files and folders', () => {
290291
path.normalize('build/notEmptyFile'),
291292
];
292293

293-
const emptyContent = [];
294-
let fileSizes = {};
294+
const emptyContent = [] as Array<string>;
295+
let fileSizes = {} as {[string]: number};
295296
fileSizes[path.normalize('build/emptyFile')] = 0;
296297
fileSizes[path.normalize('build/notEmptyFile')] = 32;
297298

@@ -344,7 +345,7 @@ describe('delete empty files and folders', () => {
344345
it('when path is folder and it contains only empty folders, removes everything', () => {
345346
const targetFolder = 'build';
346347
const content = ['emptyFolder1', 'emptyFolder2'];
347-
const emptyContent = [];
348+
const emptyContent = [] as Array<string>;
348349

349350
let statSyncInvocation = [];
350351
let rmSyncInvocation = [];

packages/react-native/scripts/codegen/__tests__/generate-specs-cli-executor-test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
66
*
7+
* @flow strict-local
78
* @format
89
*/
910

packages/react-native/scripts/codegen/codegen-utils.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
66
*
7+
* @flow strict-local
78
* @format
89
*/
910

@@ -19,11 +20,13 @@
1920
*
2021
* @return an object that can generate the code for the New Architecture.
2122
*/
22-
function getCodegen() {
23+
function getCodegen() /*: $FlowFixMe */ {
2324
let RNCodegen;
2425
try {
26+
// $FlowIgnore[cannot-resolve-module]
2527
RNCodegen = require('../../packages/react-native-codegen/lib/generators/RNCodegen.js');
2628
} catch (e) {
29+
// $FlowIgnore[cannot-resolve-module]
2730
RNCodegen = require('@react-native/codegen/lib/generators/RNCodegen.js');
2831
}
2932
if (!RNCodegen) {
@@ -32,11 +35,13 @@ function getCodegen() {
3235
return RNCodegen;
3336
}
3437

35-
function getCombineJSToSchema() {
38+
function getCombineJSToSchema() /*: $FlowFixMe */ {
3639
let combineJSToSchema;
3740
try {
41+
// $FlowIgnore[cannot-resolve-module]
3842
combineJSToSchema = require('../../packages/react-native-codegen/lib/cli/combine/combine-js-to-schema.js');
3943
} catch (e) {
44+
// $FlowIgnore[cannot-resolve-module]
4045
combineJSToSchema = require('@react-native/codegen/lib/cli/combine/combine-js-to-schema.js');
4146
}
4247
if (!combineJSToSchema) {
@@ -46,6 +51,6 @@ function getCombineJSToSchema() {
4651
}
4752

4853
module.exports = {
49-
getCodegen: getCodegen,
50-
getCombineJSToSchema: getCombineJSToSchema,
54+
getCodegen,
55+
getCombineJSToSchema,
5156
};

packages/react-native/scripts/codegen/generate-artifacts-executor/constants.js

Lines changed: 36 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
66
*
7+
* @flow strict-local
78
* @format
89
*/
910

@@ -21,54 +22,63 @@ const REACT_NATIVE_REPOSITORY_ROOT = path.join(
2122
'..',
2223
);
2324

24-
const REACT_NATIVE_PACKAGE_ROOT_FOLDER = path.join(__dirname, '..', '..', '..');
25+
const REACT_NATIVE_PACKAGE_ROOT_FOLDER = path.join(
26+
__dirname,
27+
'..',
28+
'..',
29+
'..',
30+
) /*:: as string */;
2531
const CODEGEN_REPO_PATH = `${REACT_NATIVE_REPOSITORY_ROOT}/packages/react-native-codegen`;
2632

2733
const RNCORE_CONFIGS = {
28-
ios: path.join(REACT_NATIVE_PACKAGE_ROOT_FOLDER, 'ReactCommon'),
34+
ios: path.join(
35+
REACT_NATIVE_PACKAGE_ROOT_FOLDER,
36+
'ReactCommon',
37+
) /*:: as string */,
2938
android: path.join(
3039
REACT_NATIVE_PACKAGE_ROOT_FOLDER,
3140
'ReactAndroid',
3241
'build',
3342
'generated',
3443
'source',
3544
'codegen',
36-
),
45+
) /*:: as string */,
3746
};
3847

3948
const CORE_LIBRARIES_WITH_OUTPUT_FOLDER = {
40-
rncore: RNCORE_CONFIGS,
41-
FBReactNativeSpec: {
42-
ios: path.join(
43-
REACT_NATIVE_PACKAGE_ROOT_FOLDER,
44-
'React',
45-
'FBReactNativeSpec',
46-
),
47-
android: path.join(
48-
REACT_NATIVE_PACKAGE_ROOT_FOLDER,
49-
'ReactAndroid',
50-
'build',
51-
'generated',
52-
'source',
53-
'codegen',
54-
),
55-
},
56-
};
49+
rncore: RNCORE_CONFIGS,
50+
FBReactNativeSpec: {
51+
ios: path.join(
52+
REACT_NATIVE_PACKAGE_ROOT_FOLDER,
53+
'React',
54+
'FBReactNativeSpec',
55+
) /*:: as string */,
56+
android: path.join(
57+
REACT_NATIVE_PACKAGE_ROOT_FOLDER,
58+
'ReactAndroid',
59+
'build',
60+
'generated',
61+
'source',
62+
'codegen',
63+
) /*:: as string */,
64+
},
65+
} /*:: as {[string]: $FlowFixMe} */;
5766

5867
const packageJsonPath = path.join(
5968
REACT_NATIVE_PACKAGE_ROOT_FOLDER,
6069
'package.json',
6170
);
6271

63-
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath));
72+
// $FlowFixMe[signature-verification-failure]
73+
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'));
6474
const REACT_NATIVE = packageJson.name;
6575

6676
const TEMPLATES_FOLDER_PATH = path.join(
67-
REACT_NATIVE_PACKAGE_ROOT_FOLDER,
68-
'scripts',
69-
'codegen',
70-
'templates',
71-
);
77+
REACT_NATIVE_PACKAGE_ROOT_FOLDER,
78+
'scripts',
79+
'codegen',
80+
'templates',
81+
) /*:: as string */;
7282

7383
module.exports = {
7484
CODEGEN_REPO_PATH,

packages/react-native/scripts/codegen/generate-artifacts-executor/generateAppDependencyProvider.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
66
*
7+
* @flow strict-local
78
* @format
89
*/
910

@@ -28,7 +29,7 @@ const APP_DEPENDENCY_PROVIDER_PODSPEC_TEMPLATE_PATH = path.join(
2829
'ReactAppDependencyProvider.podspec.template',
2930
);
3031

31-
function generateAppDependencyProvider(outputDir) {
32+
function generateAppDependencyProvider(outputDir /*: string */) {
3233
fs.mkdirSync(outputDir, {recursive: true});
3334
codegenLog('Generating RCTAppDependencyProvider');
3435

packages/react-native/scripts/codegen/generate-artifacts-executor/generateCustomURLHandlers.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
66
*
7+
* @flow strict-local
78
* @format
89
*/
910

@@ -23,13 +24,17 @@ const MODULES_PROTOCOLS_MM_TEMPLATE_PATH = path.join(
2324
'RCTModulesConformingToProtocolsProviderMM.template',
2425
);
2526

26-
function generateCustomURLHandlers(libraries, outputDir) {
27+
function generateCustomURLHandlers(
28+
libraries /*: $ReadOnlyArray<$FlowFixMe> */,
29+
outputDir /*: string */,
30+
) {
2731
const iosAnnotations = parseiOSAnnotations(libraries);
2832

29-
const imageURLLoaderModules = new Set();
30-
const imageDataDecoderModules = new Set();
31-
const urlRequestHandlersModules = new Set();
33+
const imageURLLoaderModules = new Set /*::<string>*/();
34+
const imageDataDecoderModules = new Set /*::<string>*/();
35+
const urlRequestHandlersModules = new Set /*::<string>*/();
3236

37+
// $FlowFixMe[missing-local-annot]]
3338
const wrapInArrayIfNecessary = value =>
3439
Array.isArray(value) || value == null ? value : [value];
3540
// Old API

packages/react-native/scripts/codegen/generate-artifacts-executor/generateFBReactNativeSpecIOS.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
66
*
7+
* @flow strict-local
78
* @format
89
*/
910

packages/react-native/scripts/codegen/generate-artifacts-executor/generateNativeCode.js

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
66
*
7+
* @flow strict-local
78
* @format
89
*/
910

@@ -17,17 +18,22 @@ const os = require('os');
1718
const path = require('path');
1819

1920
function generateNativeCode(
20-
outputPath,
21-
schemaInfos,
22-
includesGeneratedCode,
23-
platform,
24-
) {
21+
outputPath /*: string */,
22+
schemaInfos /*: $ReadOnlyArray<$FlowFixMe> */,
23+
includesGeneratedCode /*: boolean */,
24+
platform /*: string */,
25+
) /*: Array<void> */ {
2526
return schemaInfos.map(schemaInfo => {
2627
generateCode(outputPath, schemaInfo, includesGeneratedCode, platform);
2728
});
2829
}
2930

30-
function generateCode(outputPath, schemaInfo, includesGeneratedCode, platform) {
31+
function generateCode(
32+
outputPath /*: string */,
33+
schemaInfo /*: $FlowFixMe */,
34+
includesGeneratedCode /*: boolean */,
35+
platform /*: string */,
36+
) {
3137
if (shouldSkipGenerationForRncore(schemaInfo, platform)) {
3238
codegenLog(
3339
'[Codegen - rncore] Skipping iOS code generation for rncore as it has been generated already.',
@@ -65,11 +71,15 @@ function generateCode(outputPath, schemaInfo, includesGeneratedCode, platform) {
6571
const outputDir =
6672
reactNativeCoreLibraryOutputPath(libraryName, platform) ?? outputPath;
6773
fs.mkdirSync(outputDir, {recursive: true});
74+
// $FlowIssue[prop-missing] - `fs.cpSync` is missing in Flow libdefs.
6875
fs.cpSync(tmpOutputDir, outputDir, {recursive: true});
6976
codegenLog(`Generated artifacts: ${outputDir}`);
7077
}
7178

72-
function shouldSkipGenerationForRncore(schemaInfo, platform) {
79+
function shouldSkipGenerationForRncore(
80+
schemaInfo /*: $FlowFixMe */,
81+
platform /*: string */,
82+
) {
7383
if (platform !== 'ios' || schemaInfo.library.config.name !== 'rncore') {
7484
return false;
7585
}
@@ -82,13 +92,19 @@ function shouldSkipGenerationForRncore(schemaInfo, platform) {
8292
);
8393
}
8494

85-
function reactNativeCoreLibraryOutputPath(libraryName, platform) {
95+
function reactNativeCoreLibraryOutputPath(
96+
libraryName /*: string */,
97+
platform /*: string */,
98+
) {
8699
return CORE_LIBRARIES_WITH_OUTPUT_FOLDER[libraryName]
87100
? CORE_LIBRARIES_WITH_OUTPUT_FOLDER[libraryName][platform]
88101
: null;
89102
}
90103

91-
function shouldSkipGenerationForFBReactNativeSpec(schemaInfo, platform) {
104+
function shouldSkipGenerationForFBReactNativeSpec(
105+
schemaInfo /*: $FlowFixMe */,
106+
platform /*: string */,
107+
) {
92108
if (
93109
platform !== 'ios' ||
94110
schemaInfo.library.config.name !== 'FBReactNativeSpec'

0 commit comments

Comments
 (0)