Skip to content

Commit

Permalink
Add LTI annotations to function params in xplat/js [1/2]
Browse files Browse the repository at this point in the history
Summary: Add annotations to function parameters required for Flow's Local Type Inference project. This codemod prepares the codebase to match Flow's new typechecking algorithm. The new algorithm will make Flow more reliable and predicatable.

Reviewed By: evanyeung

Differential Revision: D37353648

fbshipit-source-id: e5a0c685ced85a8ff353d578b373f836b376bb28
  • Loading branch information
pieterv authored and facebook-github-bot committed Jun 23, 2022
1 parent a7db8df commit e7a4dbc
Show file tree
Hide file tree
Showing 74 changed files with 430 additions and 182 deletions.
22 changes: 19 additions & 3 deletions IntegrationTests/AsyncStorageTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const VAL_MERGE_EXPECT = {foo: 1, bar: {hoo: 2, boo: 1}, baz: 2, moo: {a: 3}};
let done = (result: ?boolean) => {};
let updateMessage = (message: string) => {};

function runTestCase(description: string, fn) {
function runTestCase(description: string, fn: () => void) {
updateMessage(description);
fn();
}
Expand Down Expand Up @@ -61,7 +61,20 @@ function stringify(
return JSON.stringify(value);
}

function expectEqual(lhs, rhs, testname: string) {
function expectEqual(
lhs: ?(any | string | Array<Array<string>>),
rhs:
| null
| string
| {
bar: {boo: number, hoo: number},
baz: number,
foo: number,
moo: {a: number},
}
| Array<Array<string>>,
testname: string,
) {
expectTrue(
!deepDiffer(lhs, rhs),
'Error in test ' +
Expand All @@ -73,7 +86,10 @@ function expectEqual(lhs, rhs, testname: string) {
);
}

function expectAsyncNoError(place, err) {
function expectAsyncNoError(
place: string,
err: ?(Error | string | Array<Error>),
) {
if (err instanceof Error) {
err = err.message;
}
Expand Down
2 changes: 1 addition & 1 deletion Libraries/Alert/Alert.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ class Alert {
options && options.onDismiss && options.onDismiss();
}
};
const onError = errorMessage => console.warn(errorMessage);
const onError = (errorMessage: string) => console.warn(errorMessage);
NativeDialogManagerAndroid.showAlert(config, onError, onAction);
}
}
Expand Down
11 changes: 7 additions & 4 deletions Libraries/Animated/AnimatedEvent.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ function attachNativeEvent(
// key path inside the `nativeEvent` object. Ex.: ['contentOffset', 'x'].
const eventMappings = [];

const traverse = (value, path) => {
const traverse = (value: mixed, path: Array<string>) => {
if (value instanceof AnimatedValue) {
value.__makeNative(platformConfig);

Expand Down Expand Up @@ -94,8 +94,8 @@ function attachNativeEvent(
};
}

function validateMapping(argMapping, args) {
const validate = (recMapping, recEvt, key) => {
function validateMapping(argMapping: $ReadOnlyArray<?Mapping>, args: any) {
const validate = (recMapping: ?Mapping, recEvt: any, key: string) => {
if (recMapping instanceof AnimatedValue) {
invariant(
typeof recEvt === 'number',
Expand Down Expand Up @@ -223,7 +223,10 @@ class AnimatedEvent {
validatedMapping = true;
}

const traverse = (recMapping, recEvt) => {
const traverse = (
recMapping: ?(Mapping | AnimatedValue),
recEvt: any,
) => {
if (recMapping instanceof AnimatedValue) {
if (typeof recEvt === 'number') {
recMapping.setValue(recEvt);
Expand Down
6 changes: 3 additions & 3 deletions Libraries/Animated/AnimatedImplementation.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ const _combineCallbacks = function (
config: $ReadOnly<{...AnimationConfig, ...}>,
) {
if (callback && config.onComplete) {
return (...args) => {
return (...args: Array<EndResult>) => {
config.onComplete && config.onComplete(...args);
callback && callback(...args);
};
Expand Down Expand Up @@ -308,7 +308,7 @@ const sequence = function (
let current = 0;
return {
start: function (callback?: ?EndCallback) {
const onComplete = function (result) {
const onComplete = function (result: EndResult) {
if (!result.finished) {
callback && callback(result);
return;
Expand Down Expand Up @@ -380,7 +380,7 @@ const parallel = function (
}

animations.forEach((animation, idx) => {
const cb = function (endResult) {
const cb = function (endResult: EndResult | {finished: boolean}) {
hasEnded[idx] = true;
doneCount++;
if (doneCount === animations.length) {
Expand Down
4 changes: 3 additions & 1 deletion Libraries/Animated/AnimatedMock.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

'use strict';

import type {EndResult} from './animations/Animation';

const {AnimatedEvent, attachNativeEvent} = require('./AnimatedEvent');
const AnimatedImplementation = require('./AnimatedImplementation');
const AnimatedInterpolation = require('./nodes/AnimatedInterpolation');
Expand Down Expand Up @@ -43,7 +45,7 @@ function mockAnimationStart(
const guardedCallback =
callback == null
? callback
: (...args) => {
: (...args: Array<EndResult>) => {
if (inAnimationCallback) {
console.warn(
'Ignoring recursive animation callback when running mock animations',
Expand Down
2 changes: 1 addition & 1 deletion Libraries/Animated/nodes/AnimatedColor.js
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ export default class AnimatedColor extends AnimatedWithChildren {
*/
addListener(callback: ColorListenerCallback): string {
const id = String(_uniqueId++);
const jointCallback = ({value: number}) => {
const jointCallback = ({value: number}: any) => {
callback(this.__getValue());
};
this._listeners[id] = {
Expand Down
2 changes: 1 addition & 1 deletion Libraries/Animated/nodes/AnimatedValueXY.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ class AnimatedValueXY extends AnimatedWithChildren {
*/
addListener(callback: ValueXYListenerCallback): string {
const id = String(_uniqueId++);
const jointCallback = ({value: number}) => {
const jointCallback = ({value: number}: any) => {
callback(this.__getValue());
};
this._listeners[id] = {
Expand Down
4 changes: 2 additions & 2 deletions Libraries/BatchedBridge/MessageQueue.js
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ class MessageQueue {
// folly-convertible. As a special case, if a prop value is a
// function it is permitted here, and special-cased in the
// conversion.
const isValidArgument = val => {
const isValidArgument = (val: mixed) => {
switch (typeof val) {
case 'undefined':
case 'boolean':
Expand Down Expand Up @@ -286,7 +286,7 @@ class MessageQueue {
// Replacement allows normally non-JSON-convertible values to be
// seen. There is ambiguity with string values, but in context,
// it should at least be a strong hint.
const replacer = (key, val) => {
const replacer = (key: string, val: $FlowFixMe) => {
const t = typeof val;
if (t === 'function') {
return '<<Function ' + val.name + '>>';
Expand Down
6 changes: 3 additions & 3 deletions Libraries/Components/ScrollView/ScrollView.js
Original file line number Diff line number Diff line change
Expand Up @@ -1107,7 +1107,7 @@ class ScrollView extends React.Component<Props, State> {
}
};

_getKeyForIndex(index, childArray) {
_getKeyForIndex(index: $FlowFixMe, childArray: $FlowFixMe) {
const child = childArray[index];
return child && child.key;
}
Expand Down Expand Up @@ -1140,7 +1140,7 @@ class ScrollView extends React.Component<Props, State> {
}
}

_onStickyHeaderLayout(index, event, key) {
_onStickyHeaderLayout(index: $FlowFixMe, event: $FlowFixMe, key: $FlowFixMe) {
const {stickyHeaderIndices} = this.props;
if (!stickyHeaderIndices) {
return;
Expand Down Expand Up @@ -1822,7 +1822,7 @@ const styles = StyleSheet.create({
},
});

function Wrapper(props, ref) {
function Wrapper(props, ref: (mixed => mixed) | {current: mixed, ...}) {
return <ScrollView {...props} scrollViewRef={ref} />;
}
Wrapper.displayName = 'ScrollView';
Expand Down
8 changes: 4 additions & 4 deletions Libraries/Components/Touchable/PooledClass.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import invariant from 'invariant';
* the Class itself, not an instance. If any others are needed, simply add them
* here, or in their own files.
*/
const oneArgumentPooler = function (copyFieldsFrom) {
const oneArgumentPooler = function (copyFieldsFrom: any) {
const Klass = this; // eslint-disable-line consistent-this
if (Klass.instancePool.length) {
const instance = Klass.instancePool.pop();
Expand All @@ -29,7 +29,7 @@ const oneArgumentPooler = function (copyFieldsFrom) {
}
};

const twoArgumentPooler = function (a1, a2) {
const twoArgumentPooler = function (a1: any, a2: any) {
const Klass = this; // eslint-disable-line consistent-this
if (Klass.instancePool.length) {
const instance = Klass.instancePool.pop();
Expand All @@ -40,7 +40,7 @@ const twoArgumentPooler = function (a1, a2) {
}
};

const threeArgumentPooler = function (a1, a2, a3) {
const threeArgumentPooler = function (a1: any, a2: any, a3: any) {
const Klass = this; // eslint-disable-line consistent-this
if (Klass.instancePool.length) {
const instance = Klass.instancePool.pop();
Expand All @@ -51,7 +51,7 @@ const threeArgumentPooler = function (a1, a2, a3) {
}
};

const fourArgumentPooler = function (a1, a2, a3, a4) {
const fourArgumentPooler = function (a1: any, a2: any, a3: any, a4: any) {
const Klass = this; // eslint-disable-line consistent-this
if (Klass.instancePool.length) {
const instance = Klass.instancePool.pop();
Expand Down
13 changes: 12 additions & 1 deletion Libraries/Components/Touchable/Touchable.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,18 @@ import type {ColorValue} from '../../StyleSheet/StyleSheet';
import type {EdgeInsetsProp} from '../../StyleSheet/EdgeInsetsPropType';
import type {PressEvent} from '../../Types/CoreEventTypes';

const extractSingleTouch = nativeEvent => {
const extractSingleTouch = (nativeEvent: {
+changedTouches: $ReadOnlyArray<PressEvent['nativeEvent']>,
+force?: number,
+identifier: number,
+locationX: number,
+locationY: number,
+pageX: number,
+pageY: number,
+target: ?number,
+timestamp: number,
+touches: $ReadOnlyArray<PressEvent['nativeEvent']>,
}) => {
const touches = nativeEvent.touches;
const changedTouches = nativeEvent.changedTouches;
const hasTouches = touches && touches.length > 0;
Expand Down
4 changes: 2 additions & 2 deletions Libraries/Components/Touchable/TouchableNativeFeedback.js
Original file line number Diff line number Diff line change
Expand Up @@ -312,11 +312,11 @@ class TouchableNativeFeedback extends React.Component<Props, State> {

const getBackgroundProp =
Platform.OS === 'android'
? (background, useForeground) =>
? (background, useForeground: boolean) =>
useForeground && TouchableNativeFeedback.canUseNativeForeground()
? {nativeForegroundAndroid: background}
: {nativeBackgroundAndroid: background}
: (background, useForeground) => null;
: (background, useForeground: boolean) => null;

TouchableNativeFeedback.displayName = 'TouchableNativeFeedback';

Expand Down
4 changes: 2 additions & 2 deletions Libraries/Core/Timers/JSTimers.js
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ const JSTimers = {
const timeout = options && options.timeout;
const id = _allocateCallback(
timeout != null
? deadline => {
? (deadline: any) => {
const timeoutId = requestIdleCallbackTimeouts[id];
if (timeoutId) {
JSTimers.clearTimeout(timeoutId);
Expand Down Expand Up @@ -364,7 +364,7 @@ const JSTimers = {
// error one at a time
for (let ii = 1; ii < errorCount; ii++) {
JSTimers.setTimeout(
(error => {
((error: Error) => {
throw error;
}).bind(null, errors[ii]),
0,
Expand Down
9 changes: 7 additions & 2 deletions Libraries/Inspector/NetworkOverlay.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

'use strict';

import type {RenderItemProps} from '../Lists/VirtualizedList';

const ScrollView = require('../Components/ScrollView/ScrollView');
const TouchableHighlight = require('../Components/Touchable/TouchableHighlight');
const View = require('../Components/View/View');
Expand Down Expand Up @@ -326,7 +328,10 @@ class NetworkOverlay extends React.Component<Props, State> {
WebSocketInterceptor.disableInterception();
}

_renderItem = ({item, index}): React.Element<any> => {
_renderItem = ({
item,
index,
}: RenderItemProps<NetworkRequestInfo>): React.Element<any> => {
const tableRowViewStyle = [
styles.tableRow,
index % 2 === 1 ? styles.tableRowOdd : styles.tableRowEven,
Expand Down Expand Up @@ -358,7 +363,7 @@ class NetworkOverlay extends React.Component<Props, State> {
);
};

_renderItemDetail(id) {
_renderItemDetail(id: number) {
const requestItem = this.state.requests[id];
const details = Object.keys(requestItem).map(key => {
if (key === 'id') {
Expand Down
10 changes: 7 additions & 3 deletions Libraries/Lists/ViewabilityHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -259,9 +259,13 @@ class ViewabilityHelper {
}

_onUpdateSync(
viewableIndicesToCheck,
onViewableItemsChanged,
createViewToken,
viewableIndicesToCheck: Array<number>,
onViewableItemsChanged: ({
changed: Array<ViewToken>,
viewableItems: Array<ViewToken>,
...
}) => void,
createViewToken: (index: number, isViewable: boolean) => ViewToken,
) {
// Filter out indices that have gone out of view since this call was scheduled.
viewableIndicesToCheck = viewableIndicesToCheck.filter(ii =>
Expand Down
17 changes: 11 additions & 6 deletions Libraries/Lists/VirtualizedList.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

import type {ScrollResponderType} from '../Components/ScrollView/ScrollView';
import type {ViewStyleProp} from '../StyleSheet/StyleSheet';
import type {LayoutEvent} from '../Types/CoreEventTypes';
import type {LayoutEvent, ScrollEvent} from '../Types/CoreEventTypes';
import type {
ViewabilityConfig,
ViewabilityConfigCallbackPair,
Expand Down Expand Up @@ -1713,7 +1713,7 @@ class VirtualizedList extends React.PureComponent<Props, State> {
}
}

_onScrollBeginDrag = (e): void => {
_onScrollBeginDrag = (e: ScrollEvent): void => {
this._nestedChildLists.forEach(childList => {
childList.ref && childList.ref._onScrollBeginDrag(e);
});
Expand All @@ -1724,7 +1724,7 @@ class VirtualizedList extends React.PureComponent<Props, State> {
this.props.onScrollBeginDrag && this.props.onScrollBeginDrag(e);
};

_onScrollEndDrag = (e): void => {
_onScrollEndDrag = (e: ScrollEvent): void => {
this._nestedChildLists.forEach(childList => {
childList.ref && childList.ref._onScrollEndDrag(e);
});
Expand All @@ -1736,14 +1736,14 @@ class VirtualizedList extends React.PureComponent<Props, State> {
this.props.onScrollEndDrag && this.props.onScrollEndDrag(e);
};

_onMomentumScrollBegin = (e): void => {
_onMomentumScrollBegin = (e: ScrollEvent): void => {
this._nestedChildLists.forEach(childList => {
childList.ref && childList.ref._onMomentumScrollBegin(e);
});
this.props.onMomentumScrollBegin && this.props.onMomentumScrollBegin(e);
};

_onMomentumScrollEnd = (e): void => {
_onMomentumScrollEnd = (e: ScrollEvent): void => {
this._nestedChildLists.forEach(childList => {
childList.ref && childList.ref._onMomentumScrollEnd(e);
});
Expand Down Expand Up @@ -2033,7 +2033,12 @@ class CellRenderer extends React.Component<
);
};

_renderElement(renderItem, ListItemComponent, item, index) {
_renderElement(
renderItem: any,
ListItemComponent: any,
item: any,
index: any,
) {
if (renderItem && ListItemComponent) {
console.warn(
'VirtualizedList: Both ListItemComponent and renderItem props are present. ListItemComponent will take' +
Expand Down
Loading

0 comments on commit e7a4dbc

Please sign in to comment.