Skip to content

Commit

Permalink
Add LTI annotations to xplat/js
Browse files Browse the repository at this point in the history
Summary:
This diff runs the codemod to add type annotations to function parameters in preparation for Flow's local type inference (LTI) project. I ran the codemod over xplat/js and reverted any files that had flow errors in them. See the list of commands run to see the regeneration of various files.

Changelog:
[Internal][Changed] - Added type annotations

Reviewed By: yungsters

Differential Revision: D32075270

fbshipit-source-id: 6a9cd85aab120b4d9e690bac142a415525dbf298
  • Loading branch information
evanyeung authored and facebook-github-bot committed Nov 10, 2021
1 parent 83a1791 commit 037e346
Show file tree
Hide file tree
Showing 53 changed files with 320 additions and 103 deletions.
20 changes: 10 additions & 10 deletions Libraries/Animated/SpringConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ type SpringConfigType = {
...
};

function stiffnessFromOrigamiValue(oValue) {
function stiffnessFromOrigamiValue(oValue: number) {
return (oValue - 30) * 3.62 + 194;
}

function dampingFromOrigamiValue(oValue) {
function dampingFromOrigamiValue(oValue: number) {
return (oValue - 8) * 3 + 25;
}

Expand All @@ -38,31 +38,31 @@ function fromBouncinessAndSpeed(
bounciness: number,
speed: number,
): SpringConfigType {
function normalize(value, startValue, endValue) {
function normalize(value: number, startValue: number, endValue: number) {
return (value - startValue) / (endValue - startValue);
}

function projectNormal(n, start, end) {
function projectNormal(n: number, start: number, end: number) {
return start + n * (end - start);
}

function linearInterpolation(t, start, end) {
function linearInterpolation(t: number, start: number, end: number) {
return t * end + (1 - t) * start;
}

function quadraticOutInterpolation(t, start, end) {
function quadraticOutInterpolation(t: number, start: number, end: number) {
return linearInterpolation(2 * t - t * t, start, end);
}

function b3Friction1(x) {
function b3Friction1(x: number) {
return 0.0007 * Math.pow(x, 3) - 0.031 * Math.pow(x, 2) + 0.64 * x + 1.28;
}

function b3Friction2(x) {
function b3Friction2(x: number) {
return 0.000044 * Math.pow(x, 3) - 0.006 * Math.pow(x, 2) + 0.36 * x + 2;
}

function b3Friction3(x) {
function b3Friction3(x: number) {
return (
0.00000045 * Math.pow(x, 3) -
0.000332 * Math.pow(x, 2) +
Expand All @@ -71,7 +71,7 @@ function fromBouncinessAndSpeed(
);
}

function b3Nobounce(tension) {
function b3Nobounce(tension: number) {
if (tension <= 18) {
return b3Friction1(tension);
} else if (tension > 18 && tension <= 44) {
Expand Down
27 changes: 19 additions & 8 deletions Libraries/Animated/bezier.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,27 +27,33 @@ const kSampleStepSize = 1.0 / (kSplineTableSize - 1.0);

const float32ArraySupported = typeof Float32Array === 'function';

function A(aA1, aA2) {
function A(aA1: number, aA2: number) {
return 1.0 - 3.0 * aA2 + 3.0 * aA1;
}
function B(aA1, aA2) {
function B(aA1: number, aA2: number) {
return 3.0 * aA2 - 6.0 * aA1;
}
function C(aA1) {
function C(aA1: number) {
return 3.0 * aA1;
}

// Returns x(t) given t, x1, and x2, or y(t) given t, y1, and y2.
function calcBezier(aT, aA1, aA2) {
function calcBezier(aT: number, aA1: number, aA2: number) {
return ((A(aA1, aA2) * aT + B(aA1, aA2)) * aT + C(aA1)) * aT;
}

// Returns dx/dt given t, x1, and x2, or dy/dt given t, y1, and y2.
function getSlope(aT, aA1, aA2) {
function getSlope(aT: number, aA1: number, aA2: number) {
return 3.0 * A(aA1, aA2) * aT * aT + 2.0 * B(aA1, aA2) * aT + C(aA1);
}

function binarySubdivide(aX, _aA, _aB, mX1, mX2) {
function binarySubdivide(
aX: number,
_aA: number,
_aB: number,
mX1: number,
mX2: number,
) {
let currentX,
currentT,
i = 0,
Expand All @@ -68,7 +74,12 @@ function binarySubdivide(aX, _aA, _aB, mX1, mX2) {
return currentT;
}

function newtonRaphsonIterate(aX, _aGuessT, mX1, mX2) {
function newtonRaphsonIterate(
aX: number,
_aGuessT: number,
mX1: number,
mX2: number,
) {
let aGuessT = _aGuessT;
for (let i = 0; i < NEWTON_ITERATIONS; ++i) {
const currentSlope = getSlope(aGuessT, mX1, mX2);
Expand Down Expand Up @@ -101,7 +112,7 @@ module.exports = function bezier(
}
}

function getTForX(aX) {
function getTForX(aX: number) {
let intervalStart = 0.0;
let currentSample = 1;
const lastSample = kSplineTableSize - 1;
Expand Down
6 changes: 3 additions & 3 deletions Libraries/Animated/createAnimatedComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ function createAnimatedComponent<Props: {+[string]: mixed, ...}, Instance>(
}
};

_attachProps(nextProps) {
_attachProps(nextProps: any) {
const oldPropsAnimated = this._propsAnimated;

this._propsAnimated = new AnimatedProps(
Expand Down Expand Up @@ -234,12 +234,12 @@ function createAnimatedComponent<Props: {+[string]: mixed, ...}, Instance>(
this._markUpdateComplete();
}

UNSAFE_componentWillReceiveProps(newProps) {
UNSAFE_componentWillReceiveProps(newProps: any) {
this._waitForUpdate();
this._attachProps(newProps);
}

componentDidUpdate(prevProps) {
componentDidUpdate(prevProps: any) {
if (this._component !== this._prevComponent) {
this._propsAnimated.setNativeView(this._component);
}
Expand Down
4 changes: 2 additions & 2 deletions Libraries/Animated/nodes/AnimatedInterpolation.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export type InterpolationConfigType = {
extrapolateRight?: ExtrapolateType,
};

const linear = t => t;
const linear = (t: number) => t;

/**
* Very handy helper to map input ranges to output ranges with an easing
Expand Down Expand Up @@ -248,7 +248,7 @@ function createInterpolationFromStringOutputRange(
};
}

function isRgbOrRgba(range) {
function isRgbOrRgba(range: string) {
return typeof range === 'string' && range.startsWith('rgb');
}

Expand Down
4 changes: 2 additions & 2 deletions Libraries/Animated/nodes/AnimatedStyle.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class AnimatedStyle extends AnimatedWithChildren {
}

// Recursively get values for nested styles (like iOS's shadowOffset)
_walkStyleAndGetValues(style) {
_walkStyleAndGetValues(style: any) {
const updatedStyle = {};
for (const key in style) {
const value = style[key];
Expand All @@ -58,7 +58,7 @@ class AnimatedStyle extends AnimatedWithChildren {
}

// Recursively get animated values for nested styles (like iOS's shadowOffset)
_walkStyleAndGetAnimatedValues(style) {
_walkStyleAndGetAnimatedValues(style: any) {
const updatedStyle = {};
for (const key in style) {
const value = style[key];
Expand Down
19 changes: 14 additions & 5 deletions Libraries/BugReporting/getReactData.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,11 @@ function getData(element: Object): Object {
};
}

function setInProps(internalInst, path: Array<string | number>, value: any) {
function setInProps(
internalInst: any,
path: Array<string | number>,
value: any,
) {
const element = internalInst._currentElement;
internalInst._currentElement = {
...element,
Expand All @@ -130,12 +134,12 @@ function setInProps(internalInst, path: Array<string | number>, value: any) {
internalInst._instance.forceUpdate();
}

function setInState(inst, path: Array<string | number>, value: any) {
function setInState(inst: any, path: Array<string | number>, value: any) {
setIn(inst.state, path, value);
inst.forceUpdate();
}

function setInContext(inst, path: Array<string | number>, value: any) {
function setInContext(inst: any, path: Array<string | number>, value: any) {
setIn(inst.context, path, value);
inst.forceUpdate();
}
Expand All @@ -148,15 +152,20 @@ function setIn(obj: Object, path: Array<string | number>, value: any) {
}
}

function childrenList(children) {
function childrenList(children: any) {
const res = [];
for (const name in children) {
res.push(children[name]);
}
return res;
}

function copyWithSetImpl(obj, path, idx, value) {
function copyWithSetImpl(
obj: any | Array<any>,
path: Array<string | number>,
idx: number,
value: any,
) {
if (idx >= path.length) {
return value;
}
Expand Down
18 changes: 17 additions & 1 deletion Libraries/Components/DatePicker/DatePickerIOS.ios.js
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,23 @@ const styles = StyleSheet.create({
},
});

function getHeight(pickerStyle, mode) {
function getHeight(
pickerStyle: ?(
| 'compact'
| 'inline'
| 'spinner'
| $TEMPORARY$string<'compact'>
| $TEMPORARY$string<'inline'>
| $TEMPORARY$string<'spinner'>
),
mode:
| 'date'
| 'datetime'
| 'time'
| $TEMPORARY$string<'date'>
| $TEMPORARY$string<'datetime'>
| $TEMPORARY$string<'time'>,
) {
if (pickerStyle === 'compact') {
return styles.datePickerIOSCompact;
}
Expand Down
4 changes: 2 additions & 2 deletions Libraries/Components/Keyboard/KeyboardAvoidingView.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import type {
ViewLayout,
ViewLayoutEvent,
} from '../View/ViewPropTypes';
import type {KeyboardEvent} from './Keyboard';
import type {KeyboardEvent, KeyboardEventCoordinates} from './Keyboard';

type Props = $ReadOnly<{|
...ViewProps,
Expand Down Expand Up @@ -71,7 +71,7 @@ class KeyboardAvoidingView extends React.Component<Props, State> {
this.viewRef = React.createRef();
}

_relativeKeyboardHeight(keyboardFrame): number {
_relativeKeyboardHeight(keyboardFrame: KeyboardEventCoordinates): number {
const frame = this._frame;
if (!frame || !keyboardFrame) {
return 0;
Expand Down
13 changes: 11 additions & 2 deletions Libraries/Components/Keyboard/__tests__/Keyboard-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,14 @@ describe('Keyboard', () => {
});

describe('scheduling layout animation', () => {
const scheduleLayoutAnimation = (duration, easing): void =>
const scheduleLayoutAnimation = (
duration: null | number,
easing:
| null
| $TEMPORARY$string<'linear'>
| $TEMPORARY$string<'some-unknown-animation-type'>
| $TEMPORARY$string<'spring'>,
): void =>
// $FlowFixMe[incompatible-call]
Keyboard.scheduleLayoutAnimation({duration, easing});

Expand All @@ -53,7 +60,9 @@ describe('Keyboard', () => {
});

describe('animation update type', () => {
const assertAnimationUpdateType = type =>
const assertAnimationUpdateType = (
type: $TEMPORARY$string<'keyboard'> | $TEMPORARY$string<'linear'>,
) =>
expect(LayoutAnimation.configureNext).toHaveBeenCalledWith(
expect.objectContaining({
duration: expect.anything(),
Expand Down
2 changes: 1 addition & 1 deletion Libraries/Components/ScrollView/ScrollViewStickyHeader.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ class ScrollViewStickyHeader extends React.Component<Props, State> {
);
}

_onLayout = event => {
_onLayout = (event: any) => {
const layoutY = event.nativeEvent.layout.y;
const layoutHeight = event.nativeEvent.layout.height;
const measured = true;
Expand Down
11 changes: 10 additions & 1 deletion Libraries/Core/ReactNativeVersionCheck.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,16 @@ exports.checkVersions = function checkVersions(): void {
}
};

function _formatVersion(version): string {
function _formatVersion(
version:
| {major: number, minor: number, patch: number, prerelease: ?number}
| $TEMPORARY$object<{
major: number,
minor: number,
patch: number,
prerelease: null,
}>,
): string {
return (
`${version.major}.${version.minor}.${version.patch}` +
// eslint-disable-next-line eqeqeq
Expand Down
16 changes: 14 additions & 2 deletions Libraries/Core/setUpBatchedBridge.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,20 @@ if (global.RN$Bridgeless === true && global.RN$registerCallableModule) {
registerModule = global.RN$registerCallableModule;
} else {
const BatchedBridge = require('../BatchedBridge/BatchedBridge');
registerModule = (moduleName, factory) =>
BatchedBridge.registerLazyCallableModule(moduleName, factory);
registerModule = (
moduleName:
| $TEMPORARY$string<'GlobalPerformanceLogger'>
| $TEMPORARY$string<'HMRClient'>
| $TEMPORARY$string<'HeapCapture'>
| $TEMPORARY$string<'JSDevSupportModule'>
| $TEMPORARY$string<'JSTimers'>
| $TEMPORARY$string<'RCTDeviceEventEmitter'>
| $TEMPORARY$string<'RCTLog'>
| $TEMPORARY$string<'RCTNativeAppEventEmitter'>
| $TEMPORARY$string<'SamplingProfiler'>
| $TEMPORARY$string<'Systrace'>,
factory,
) => BatchedBridge.registerLazyCallableModule(moduleName, factory);
}

registerModule('Systrace', () => require('../Performance/Systrace'));
Expand Down
2 changes: 1 addition & 1 deletion Libraries/Core/setUpErrorHandling.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ ExceptionsManager.installConsoleErrorReporter();

// Set up error handler
if (!global.__fbDisableExceptionsManager) {
const handleError = (e, isFatal) => {
const handleError = (e: mixed, isFatal: boolean) => {
try {
ExceptionsManager.handleException(e, isFatal);
} catch (ee) {
Expand Down
12 changes: 11 additions & 1 deletion Libraries/Core/setUpTimers.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,17 @@ if (global.RN$Bridgeless !== true) {
* Set up timers.
* You can use this module directly, or just require InitializeCore.
*/
const defineLazyTimer = name => {
const defineLazyTimer = (
name:
| $TEMPORARY$string<'cancelAnimationFrame'>
| $TEMPORARY$string<'cancelIdleCallback'>
| $TEMPORARY$string<'clearInterval'>
| $TEMPORARY$string<'clearTimeout'>
| $TEMPORARY$string<'requestAnimationFrame'>
| $TEMPORARY$string<'requestIdleCallback'>
| $TEMPORARY$string<'setInterval'>
| $TEMPORARY$string<'setTimeout'>,
) => {
polyfillGlobal(name, () => require('./Timers/JSTimers')[name]);
};
defineLazyTimer('setTimeout');
Expand Down
2 changes: 1 addition & 1 deletion Libraries/Inspector/Inspector.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ class Inspector extends React.Component<
}, 100);
};

_onAgentShowNativeHighlight = node => {
_onAgentShowNativeHighlight = (node: any) => {
clearTimeout(this._hideTimeoutID);

// Shape of `node` is different in Fabric.
Expand Down
Loading

0 comments on commit 037e346

Please sign in to comment.