Skip to content

Commit 4409642

Browse files
rubennortefacebook-github-bot
authored andcommitted
Migrate large amount of modules to flow strict and strict-local
Summary: | Group | Before | After | Change | | Untyped | 50 | 49 | -1 | | flow | 197 | 155 | -42 | | flow strict-local | 226 | 185 | -41 | | flow strict | 33 | 117 | +84 Changelog: [Changed] Improved Flow typing of multiple modules (with migrations to `flow strict` and `flow strict-local` Reviewed By: motiz88 Differential Revision: D22549140 fbshipit-source-id: ed29415332cfce15b244ee4dea9e13d035543175
1 parent 050a7dd commit 4409642

File tree

105 files changed

+461
-271
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

105 files changed

+461
-271
lines changed

Libraries/BatchedBridge/BatchedBridge.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* LICENSE file in the root directory of this source tree.
66
*
77
* @format
8-
* @flow strict-local
8+
* @flow strict
99
*/
1010

1111
'use strict';

Libraries/BatchedBridge/MessageQueue.js

+64-56
Original file line numberDiff line numberDiff line change
@@ -4,7 +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
7+
* @flow strict
88
* @format
99
*/
1010

@@ -22,7 +22,7 @@ export type SpyData = {
2222
type: number,
2323
module: ?string,
2424
method: string | number,
25-
args: any[],
25+
args: mixed[],
2626
...
2727
};
2828

@@ -40,10 +40,10 @@ const TRACE_TAG_REACT_APPS = 1 << 17;
4040
const DEBUG_INFO_LIMIT = 32;
4141

4242
class MessageQueue {
43-
_lazyCallableModules: {[key: string]: (void) => Object, ...};
44-
_queue: [number[], number[], any[], number];
45-
_successCallbacks: Map<number, ?Function>;
46-
_failureCallbacks: Map<number, ?Function>;
43+
_lazyCallableModules: {[key: string]: (void) => {...}, ...};
44+
_queue: [number[], number[], mixed[], number];
45+
_successCallbacks: Map<number, ?(...mixed[]) => void>;
46+
_failureCallbacks: Map<number, ?(...mixed[]) => void>;
4747
_callID: number;
4848
_lastFlush: number;
4949
_eventLoopStartTime: number;
@@ -71,11 +71,15 @@ class MessageQueue {
7171
this._remoteMethodTable = {};
7272
}
7373

74-
(this: any).callFunctionReturnFlushedQueue = this.callFunctionReturnFlushedQueue.bind(
74+
// $FlowFixMe[cannot-write]
75+
this.callFunctionReturnFlushedQueue = this.callFunctionReturnFlushedQueue.bind(
7576
this,
7677
);
77-
(this: any).flushedQueue = this.flushedQueue.bind(this);
78-
(this: any).invokeCallbackAndReturnFlushedQueue = this.invokeCallbackAndReturnFlushedQueue.bind(
78+
// $FlowFixMe[cannot-write]
79+
this.flushedQueue = this.flushedQueue.bind(this);
80+
81+
// $FlowFixMe[cannot-write]
82+
this.invokeCallbackAndReturnFlushedQueue = this.invokeCallbackAndReturnFlushedQueue.bind(
7983
this,
8084
);
8185
}
@@ -89,7 +93,7 @@ class MessageQueue {
8993
MessageQueue.prototype.__spy = info => {
9094
console.log(
9195
`${info.type === TO_JS ? 'N->JS' : 'JS->N'} : ` +
92-
`${info.module ? info.module + '.' : ''}${info.method}` +
96+
`${info.module != null ? info.module + '.' : ''}${info.method}` +
9397
`(${JSON.stringify(info.args)})`,
9498
);
9599
};
@@ -103,8 +107,8 @@ class MessageQueue {
103107
callFunctionReturnFlushedQueue(
104108
module: string,
105109
method: string,
106-
args: any[],
107-
): null | [Array<number>, Array<number>, Array<any>, number] {
110+
args: mixed[],
111+
): null | [Array<number>, Array<number>, Array<mixed>, number] {
108112
this.__guard(() => {
109113
this.__callFunction(module, method, args);
110114
});
@@ -114,16 +118,16 @@ class MessageQueue {
114118

115119
invokeCallbackAndReturnFlushedQueue(
116120
cbID: number,
117-
args: any[],
118-
): null | [Array<number>, Array<number>, Array<any>, number] {
121+
args: mixed[],
122+
): null | [Array<number>, Array<number>, Array<mixed>, number] {
119123
this.__guard(() => {
120124
this.__invokeCallback(cbID, args);
121125
});
122126

123127
return this.flushedQueue();
124128
}
125129

126-
flushedQueue(): null | [Array<number>, Array<number>, Array<any>, number] {
130+
flushedQueue(): null | [Array<number>, Array<number>, Array<mixed>, number] {
127131
this.__guard(() => {
128132
this.__callImmediates();
129133
});
@@ -137,13 +141,13 @@ class MessageQueue {
137141
return Date.now() - this._eventLoopStartTime;
138142
}
139143

140-
registerCallableModule(name: string, module: Object) {
144+
registerCallableModule(name: string, module: {...}) {
141145
this._lazyCallableModules[name] = () => module;
142146
}
143147

144-
registerLazyCallableModule(name: string, factory: void => Object) {
145-
let module: Object;
146-
let getValue: ?(void) => Object = factory;
148+
registerLazyCallableModule(name: string, factory: void => {...}) {
149+
let module: {...};
150+
let getValue: ?(void) => {...} = factory;
147151
this._lazyCallableModules[name] = () => {
148152
if (getValue) {
149153
module = getValue();
@@ -153,18 +157,18 @@ class MessageQueue {
153157
};
154158
}
155159

156-
getCallableModule(name: string): any | null {
160+
getCallableModule(name: string): {...} | null {
157161
const getValue = this._lazyCallableModules[name];
158162
return getValue ? getValue() : null;
159163
}
160164

161165
callNativeSyncHook(
162166
moduleID: number,
163167
methodID: number,
164-
params: any[],
165-
onFail: ?Function,
166-
onSucc: ?Function,
167-
): any {
168+
params: mixed[],
169+
onFail: ?(...mixed[]) => void,
170+
onSucc: ?(...mixed[]) => void,
171+
): mixed {
168172
if (__DEV__) {
169173
invariant(
170174
global.nativeCallSyncHook,
@@ -181,10 +185,10 @@ class MessageQueue {
181185
processCallbacks(
182186
moduleID: number,
183187
methodID: number,
184-
params: any[],
185-
onFail: ?Function,
186-
onSucc: ?Function,
187-
) {
188+
params: mixed[],
189+
onFail: ?(...mixed[]) => void,
190+
onSucc: ?(...mixed[]) => void,
191+
): void {
188192
if (onFail || onSucc) {
189193
if (__DEV__) {
190194
this._debugInfo[this._callID] = [moduleID, methodID];
@@ -232,9 +236,9 @@ class MessageQueue {
232236
enqueueNativeCall(
233237
moduleID: number,
234238
methodID: number,
235-
params: any[],
236-
onFail: ?Function,
237-
onSucc: ?Function,
239+
params: mixed[],
240+
onFail: ?(...mixed[]) => void,
241+
onSucc: ?(...mixed[]) => void,
238242
) {
239243
this.processCallbacks(moduleID, methodID, params, onFail, onSucc);
240244

@@ -247,30 +251,34 @@ class MessageQueue {
247251
// function it is permitted here, and special-cased in the
248252
// conversion.
249253
const isValidArgument = val => {
250-
const t = typeof val;
251-
if (
252-
t === 'undefined' ||
253-
t === 'null' ||
254-
t === 'boolean' ||
255-
t === 'string'
256-
) {
257-
return true;
258-
}
259-
if (t === 'number') {
260-
return isFinite(val);
261-
}
262-
if (t === 'function' || t !== 'object') {
263-
return false;
264-
}
265-
if (Array.isArray(val)) {
266-
return val.every(isValidArgument);
267-
}
268-
for (const k in val) {
269-
if (typeof val[k] !== 'function' && !isValidArgument(val[k])) {
254+
switch (typeof val) {
255+
case 'undefined':
256+
case 'boolean':
257+
case 'string':
258+
return true;
259+
case 'number':
260+
return isFinite(val);
261+
case 'object':
262+
if (val == null) {
263+
return true;
264+
}
265+
266+
if (Array.isArray(val)) {
267+
return val.every(isValidArgument);
268+
}
269+
270+
for (const k in val) {
271+
if (typeof val[k] !== 'function' && !isValidArgument(val[k])) {
272+
return false;
273+
}
274+
}
275+
276+
return true;
277+
case 'function':
278+
return false;
279+
default:
270280
return false;
271-
}
272281
}
273-
return true;
274282
};
275283

276284
// Replacement allows normally non-JSON-convertible values to be
@@ -295,7 +303,7 @@ class MessageQueue {
295303
);
296304

297305
// The params object should not be mutated after being queued
298-
deepFreezeAndThrowOnMutationInDev((params: any));
306+
deepFreezeAndThrowOnMutationInDev(params);
299307
}
300308
this._queue[PARAMS].push(params);
301309

@@ -382,7 +390,7 @@ class MessageQueue {
382390
Systrace.endEvent();
383391
}
384392

385-
__callFunction(module: string, method: string, args: any[]): void {
393+
__callFunction(module: string, method: string, args: mixed[]): void {
386394
this._lastFlush = Date.now();
387395
this._eventLoopStartTime = this._lastFlush;
388396
if (__DEV__ || this.__spy) {
@@ -410,7 +418,7 @@ class MessageQueue {
410418
Systrace.endEvent();
411419
}
412420

413-
__invokeCallback(cbID: number, args: any[]) {
421+
__invokeCallback(cbID: number, args: mixed[]) {
414422
this._lastFlush = Date.now();
415423
this._eventLoopStartTime = this._lastFlush;
416424

Libraries/BatchedBridge/NativeModules.js

+23-15
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* LICENSE file in the root directory of this source tree.
66
*
77
* @format
8-
* @flow
8+
* @flow strict
99
*/
1010

1111
'use strict';
@@ -18,7 +18,7 @@ import type {ExtendedError} from '../Core/Devtools/parseErrorStack';
1818

1919
export type ModuleConfig = [
2020
string /* name */,
21-
?Object /* constants */,
21+
?{...} /* constants */,
2222
?$ReadOnlyArray<string> /* functions */,
2323
?$ReadOnlyArray<number> /* promise method IDs */,
2424
?$ReadOnlyArray<number> /* sync method IDs */,
@@ -31,7 +31,7 @@ function genModule(
3131
moduleID: number,
3232
): ?{
3333
name: string,
34-
module?: Object,
34+
module?: {...},
3535
...
3636
} {
3737
if (!config) {
@@ -55,8 +55,9 @@ function genModule(
5555
methods &&
5656
methods.forEach((methodName, methodID) => {
5757
const isPromise =
58-
promiseMethods && arrayContains(promiseMethods, methodID);
59-
const isSync = syncMethods && arrayContains(syncMethods, methodID);
58+
(promiseMethods && arrayContains(promiseMethods, methodID)) || false;
59+
const isSync =
60+
(syncMethods && arrayContains(syncMethods, methodID)) || false;
6061
invariant(
6162
!isPromise || !isSync,
6263
'Cannot have a method that is both async and a sync hook',
@@ -85,7 +86,7 @@ function genModule(
8586
// export this method as a global so we can call it from native
8687
global.__fbGenNativeModule = genModule;
8788

88-
function loadModule(name: string, moduleID: number): ?Object {
89+
function loadModule(name: string, moduleID: number): ?{...} {
8990
invariant(
9091
global.nativeRequireModuleConfig,
9192
"Can't lazily create module without nativeRequireModuleConfig",
@@ -98,7 +99,7 @@ function loadModule(name: string, moduleID: number): ?Object {
9899
function genMethod(moduleID: number, methodID: number, type: MethodType) {
99100
let fn = null;
100101
if (type === 'promise') {
101-
fn = function promiseMethodWrapper(...args: Array<any>) {
102+
fn = function promiseMethodWrapper(...args: Array<mixed>) {
102103
// In case we reject, capture a useful stack trace here.
103104
const enqueueingFrameError: ExtendedError = new Error();
104105
return new Promise((resolve, reject) => {
@@ -108,12 +109,17 @@ function genMethod(moduleID: number, methodID: number, type: MethodType) {
108109
args,
109110
data => resolve(data),
110111
errorData =>
111-
reject(updateErrorWithErrorData(errorData, enqueueingFrameError)),
112+
reject(
113+
updateErrorWithErrorData(
114+
(errorData: $FlowFixMe),
115+
enqueueingFrameError,
116+
),
117+
),
112118
);
113119
});
114120
};
115121
} else {
116-
fn = function nonPromiseMethodWrapper(...args: Array<any>) {
122+
fn = function nonPromiseMethodWrapper(...args: Array<mixed>) {
117123
const lastArg = args.length > 0 ? args[args.length - 1] : null;
118124
const secondLastArg = args.length > 1 ? args[args.length - 2] : null;
119125
const hasSuccessCallback = typeof lastArg === 'function';
@@ -123,23 +129,25 @@ function genMethod(moduleID: number, methodID: number, type: MethodType) {
123129
hasSuccessCallback,
124130
'Cannot have a non-function arg after a function arg.',
125131
);
126-
const onSuccess = hasSuccessCallback ? lastArg : null;
127-
const onFail = hasErrorCallback ? secondLastArg : null;
132+
// $FlowFixMe[incompatible-type]
133+
const onSuccess: ?(mixed) => void = hasSuccessCallback ? lastArg : null;
134+
// $FlowFixMe[incompatible-type]
135+
const onFail: ?(mixed) => void = hasErrorCallback ? secondLastArg : null;
128136
const callbackCount = hasSuccessCallback + hasErrorCallback;
129-
args = args.slice(0, args.length - callbackCount);
137+
const newArgs = args.slice(0, args.length - callbackCount);
130138
if (type === 'sync') {
131139
return BatchedBridge.callNativeSyncHook(
132140
moduleID,
133141
methodID,
134-
args,
142+
newArgs,
135143
onFail,
136144
onSuccess,
137145
);
138146
} else {
139147
BatchedBridge.enqueueNativeCall(
140148
moduleID,
141149
methodID,
142-
args,
150+
newArgs,
143151
onFail,
144152
onSuccess,
145153
);
@@ -161,7 +169,7 @@ function updateErrorWithErrorData(
161169
return Object.assign(error, errorData || {});
162170
}
163171

164-
let NativeModules: {[moduleName: string]: Object, ...} = {};
172+
let NativeModules: {[moduleName: string]: $FlowFixMe, ...} = {};
165173
if (global.nativeModuleProxy) {
166174
NativeModules = global.nativeModuleProxy;
167175
} else if (!global.nativeExtensions) {

0 commit comments

Comments
 (0)