diff --git a/Libraries/Utilities/Dimensions.js b/Libraries/Utilities/Dimensions.js index 3bfaf9c52..80d564a2b 100644 --- a/Libraries/Utilities/Dimensions.js +++ b/Libraries/Utilities/Dimensions.js @@ -7,6 +7,7 @@ * of patent rights can be found in the PATENTS file in the same directory. * * @providesModule Dimensions + * @flow */ 'use strict'; @@ -22,7 +23,7 @@ class Dimensions { * * @param {object} dims Simple string-keyed object of dimensions to set */ - static set(dims) { + static set(dims: {[key:string]: any}): bool { Object.assign(dimensions, dims); return true; } @@ -40,7 +41,7 @@ class Dimensions { * @param {string} dim Name of dimension as defined when calling `set`. * @returns {Object?} Value for the dimension. */ - static get(dim) { + static get(dim: string): Object { invariant(dimensions[dim], 'No dimension set for key ' + dim); return dimensions[dim]; } diff --git a/Libraries/Utilities/MessageQueue.js b/Libraries/Utilities/MessageQueue.js index 81091a54f..9d6eb867e 100644 --- a/Libraries/Utilities/MessageQueue.js +++ b/Libraries/Utilities/MessageQueue.js @@ -7,6 +7,7 @@ * of patent rights can be found in the PATENTS file in the same directory. * * @providesModule MessageQueue + * @flow */ 'use strict'; var ErrorUtils = require('ErrorUtils'); @@ -18,6 +19,18 @@ var JSTimersExecution = require('JSTimersExecution'); var INTERNAL_ERROR = 'Error in MessageQueue implementation'; +type ModulesConfig = { + [key:string]: { + moduleID: number; + methods: {[key:string]: { + methodID: number; + }}; + } +} + +type NameToID = {[key:string]: number} +type IDToName = {[key:number]: string} + /** * So as not to confuse static build system. */ @@ -45,7 +58,11 @@ var jsCall = function(module, methodName, params) { * efficient numeric IDs. * @class MessageQueue */ -var MessageQueue = function(remoteModulesConfig, localModulesConfig, customRequire) { +var MessageQueue = function( + remoteModulesConfig: ModulesConfig, + localModulesConfig: ModulesConfig, + customRequire: (id: string) => any +) { this._requireFunc = customRequire || requireFunc; this._initBookeeping(); this._initNamingMap(remoteModulesConfig, localModulesConfig); @@ -128,7 +145,10 @@ var MessageQueueMixin = { * @param {object} remoteModulesConfig Configuration of modules and their * methods. */ - _initNamingMap: function(remoteModulesConfig, localModulesConfig) { + _initNamingMap: function( + remoteModulesConfig: ModulesConfig, + localModulesConfig: ModulesConfig + ) { this._remoteModuleNameToModuleID = {}; this._remoteModuleIDToModuleName = {}; // Reverse @@ -142,11 +162,11 @@ var MessageQueueMixin = { this._localModuleNameToMethodIDToName = {}; // Reverse function fillMappings( - modulesConfig, - moduleNameToModuleID, - moduleIDToModuleName, - moduleNameToMethodNameToID, - moduleNameToMethodIDToName + modulesConfig: ModulesConfig, + moduleNameToModuleID: NameToID, + moduleIDToModuleName: IDToName, + moduleNameToMethodNameToID: {[key:string]: NameToID}, + moduleNameToMethodIDToName: {[key:string]: IDToName} ) { for (var moduleName in modulesConfig) { var moduleConfig = modulesConfig[moduleName]; diff --git a/Libraries/Utilities/PixelRatio.js b/Libraries/Utilities/PixelRatio.js index 4770937b1..f8e23398a 100644 --- a/Libraries/Utilities/PixelRatio.js +++ b/Libraries/Utilities/PixelRatio.js @@ -7,6 +7,7 @@ * of patent rights can be found in the PATENTS file in the same directory. * * @providesModule PixelRatio + * @flow */ 'use strict'; @@ -52,7 +53,7 @@ class PixelRatio { * - PixelRatio.get() === 3 * - iPhone 6 plus */ - static get() { + static get(): number { return Dimensions.get('window').scale; } } diff --git a/Libraries/Utilities/Platform.ios.js b/Libraries/Utilities/Platform.ios.js index 515e7d288..d02e6fde2 100644 --- a/Libraries/Utilities/Platform.ios.js +++ b/Libraries/Utilities/Platform.ios.js @@ -7,6 +7,7 @@ * of patent rights can be found in the PATENTS file in the same directory. * * @providesModule Platform + * @flow */ 'use strict'; diff --git a/Libraries/Utilities/PushNotificationIOS.js b/Libraries/Utilities/PushNotificationIOS.js index fe77c69b3..ed581ba20 100644 --- a/Libraries/Utilities/PushNotificationIOS.js +++ b/Libraries/Utilities/PushNotificationIOS.js @@ -7,6 +7,7 @@ * of patent rights can be found in the PATENTS file in the same directory. * * @providesModule PushNotificationIOS + * @flow */ 'use strict'; @@ -23,6 +24,10 @@ var _notifHandlers = {}; var DEVICE_NOTIF_EVENT = 'remoteNotificationReceived'; class PushNotificationIOS { + _data: Object; + _alert: string | Object; + _sound: string; + _badgeCount: number; static addEventListener(type, handler) { _notifHandlers[handler] = RCTDeviceEventEmitter.addListener( @@ -68,24 +73,24 @@ class PushNotificationIOS { }); } - getMessage() { + getMessage(): ?string | ?Object { // alias because "alert" is an ambiguous name return this._alert; } - getSound() { + getSound(): ?string { return this._sound; } - getAlert() { + getAlert(): ?string | ?Object { return this._alert; } - getBadgeCount() { + getBadgeCount(): ?number { return this._badgeCount; } - getData() { + getData(): ?Object { return this._data; } } diff --git a/Libraries/Utilities/RCTLog.js b/Libraries/Utilities/RCTLog.js index 8955382e0..e5a8db49c 100644 --- a/Libraries/Utilities/RCTLog.js +++ b/Libraries/Utilities/RCTLog.js @@ -7,6 +7,7 @@ * of patent rights can be found in the PATENTS file in the same directory. * * @providesModule RCTLog + * @flow */ /* globals nativeLoggingHook */ 'use strict'; @@ -31,7 +32,7 @@ class RCTLog { logFn, 'Level "' + level + '" not one of ' + Object.keys(levelsMap) ); - if (typeof nativeLoggingHook === 'undefined') { + if (typeof global.nativeLoggingHook === 'undefined') { // We already printed in xcode, so only log here if using a js debugger console[logFn].apply(console, args); } diff --git a/Libraries/Utilities/RCTRenderingPerf.js b/Libraries/Utilities/RCTRenderingPerf.js index 5662902f0..c6466aa64 100644 --- a/Libraries/Utilities/RCTRenderingPerf.js +++ b/Libraries/Utilities/RCTRenderingPerf.js @@ -7,6 +7,7 @@ * of patent rights can be found in the PATENTS file in the same directory. * * @providesModule RCTRenderingPerf + * @flow */ 'use strict'; @@ -15,6 +16,11 @@ var ReactPerf = require('ReactPerf'); var invariant = require('invariant'); +type perfModule = { + start: () => void; + stop: () => void; +} + var perfModules = []; var enabled = false; @@ -58,7 +64,7 @@ var RCTRenderingPerf = { perfModules.forEach((module) => module.stop()); }, - register: function(module) { + register: function(module: perfModule) { invariant( typeof module.start === 'function', 'Perf module should have start() function' diff --git a/Libraries/Utilities/TimerMixin.js b/Libraries/Utilities/TimerMixin.js index 2c570660a..0fc68457d 100644 --- a/Libraries/Utilities/TimerMixin.js +++ b/Libraries/Utilities/TimerMixin.js @@ -7,9 +7,13 @@ * of patent rights can be found in the PATENTS file in the same directory. * * @providesModule TimerMixin + * @flow */ 'use strict'; +var setImmediate = require('setImmediate'); +var clearImmediate = require('clearImmediate'); + /** * Using bare setTimeout, setInterval, setImmediate and * requestAnimationFrame calls is very dangerous because if you forget to cancel @@ -35,7 +39,10 @@ */ var setter = function(setter, clearer, array) { - return function(callback, delta) { + return function( + callback: () => void, + delta: number + ): number { var id = setter(() => { clearer.call(this, id); callback.apply(this, arguments); @@ -51,7 +58,7 @@ }; var clearer = function(clearer, array) { - return function(id) { + return function(id: number) { if (this[array]) { var index = this[array].indexOf(id); if (index !== -1) { @@ -75,8 +82,8 @@ var _setImmediate = setter(setImmediate, _clearImmediate, _immediates); var _rafs = 'TimerMixin_rafs'; - var _cancelAnimationFrame = clearer(cancelAnimationFrame, _rafs); - var _requestAnimationFrame = setter(requestAnimationFrame, _cancelAnimationFrame, _rafs); + var _cancelAnimationFrame = clearer(window.cancelAnimationFrame, _rafs); + var _requestAnimationFrame = setter(window.requestAnimationFrame, _cancelAnimationFrame, _rafs); var TimerMixin = { componentWillUnmount: function() { diff --git a/Libraries/Utilities/createStrictShapeTypeChecker.js b/Libraries/Utilities/createStrictShapeTypeChecker.js index 0812aa6f4..cd0d5757a 100644 --- a/Libraries/Utilities/createStrictShapeTypeChecker.js +++ b/Libraries/Utilities/createStrictShapeTypeChecker.js @@ -7,6 +7,7 @@ * of patent rights can be found in the PATENTS file in the same directory. * * @providesModule createStrictShapeTypeChecker + * @flow */ 'use strict'; @@ -15,8 +16,10 @@ var ReactPropTypeLocationNames = require('ReactPropTypeLocationNames'); var invariant = require('invariant'); var merge = require('merge'); -function createStrictShapeTypeChecker(shapeTypes) { - function checkType(isRequired, props, propName, componentName, location) { +function createStrictShapeTypeChecker( + shapeTypes: {[key: string]: ReactPropsCheckType} +): ReactPropsChainableTypeChecker { + function checkType(isRequired, props, propName, componentName, location?) { if (!props[propName]) { if (isRequired) { invariant( @@ -29,7 +32,8 @@ function createStrictShapeTypeChecker(shapeTypes) { } var propValue = props[propName]; var propType = typeof propValue; - var locationName = ReactPropTypeLocationNames[location]; + var locationName = + location && ReactPropTypeLocationNames[location] || '(unknown)'; if (propType !== 'object') { invariant( false, @@ -57,11 +61,17 @@ function createStrictShapeTypeChecker(shapeTypes) { error.message + `\nBad object: ` + JSON.stringify(props[propName], null, ' ') ); - return error; } } } - var chainedCheckType = checkType.bind(null, false); + function chainedCheckType( + props: {[key: string]: any}, + propName: string, + componentName: string, + location?: string + ): ?Error { + return checkType(false, props, propName, componentName, location); + } chainedCheckType.isRequired = checkType.bind(null, true); return chainedCheckType; } diff --git a/Libraries/Utilities/deepFreezeAndThrowOnMutationInDev.js b/Libraries/Utilities/deepFreezeAndThrowOnMutationInDev.js index e5530204e..9263116ed 100644 --- a/Libraries/Utilities/deepFreezeAndThrowOnMutationInDev.js +++ b/Libraries/Utilities/deepFreezeAndThrowOnMutationInDev.js @@ -7,8 +7,11 @@ * of patent rights can be found in the PATENTS file in the same directory. * * @providesModule deepFreezeAndThrowOnMutationInDev + * @flow */ +'use strict'; + /** * If your application is accepting different values for the same field over * time and is doing a diff on them, you can either (1) create a copy or @@ -26,7 +29,7 @@ * Freezing the object and adding the throw mechanism is expensive and will * only be used in DEV. */ -function deepFreezeAndThrowOnMutationInDev(object) { +function deepFreezeAndThrowOnMutationInDev(object: Object) { if (__DEV__) { if (typeof object !== 'object' || object === null || diff --git a/Libraries/Utilities/differ/insetsDiffer.js b/Libraries/Utilities/differ/insetsDiffer.js index 887a482a1..b0a409470 100644 --- a/Libraries/Utilities/differ/insetsDiffer.js +++ b/Libraries/Utilities/differ/insetsDiffer.js @@ -7,9 +7,17 @@ * of patent rights can be found in the PATENTS file in the same directory. * * @providesModule insetsDiffer + * @flow */ 'use strict'; +type Inset = { + top: ?number; + left: ?number; + right: ?number; + bottom: ?number; +} + var dummyInsets = { top: undefined, left: undefined, @@ -17,7 +25,10 @@ var dummyInsets = { bottom: undefined, }; -var insetsDiffer = function(one, two) { +var insetsDiffer = function( + one: ?Inset, + two: ?Inset +): bool { one = one || dummyInsets; two = two || dummyInsets; return one !== two && ( diff --git a/Libraries/Utilities/differ/pointsDiffer.js b/Libraries/Utilities/differ/pointsDiffer.js index cd2c558d2..c4939199f 100644 --- a/Libraries/Utilities/differ/pointsDiffer.js +++ b/Libraries/Utilities/differ/pointsDiffer.js @@ -7,12 +7,18 @@ * of patent rights can be found in the PATENTS file in the same directory. * * @providesModule pointsDiffer + * @flow */ 'use strict'; +type Point = { + x: ?number; + y: ?number; +} + var dummyPoint = {x: undefined, y: undefined}; -var pointsDiffer = function(one, two) { +var pointsDiffer = function(one: ?Point, two: ?Point): bool { one = one || dummyPoint; two = two || dummyPoint; return one !== two && ( diff --git a/Libraries/Utilities/groupByEveryN.js b/Libraries/Utilities/groupByEveryN.js index d1127c1d1..a8b353ad6 100644 --- a/Libraries/Utilities/groupByEveryN.js +++ b/Libraries/Utilities/groupByEveryN.js @@ -7,6 +7,7 @@ * of patent rights can be found in the PATENTS file in the same directory. * * @providesModule groupByEveryN + * @flow */ /** @@ -26,7 +27,7 @@ */ 'use strict'; -function groupByEveryN(array, n) { +function groupByEveryN(array: Array, n: number): Array> { var result = []; var temp = []; diff --git a/Libraries/Utilities/logError.js b/Libraries/Utilities/logError.js index 1a0be1a13..aac41be0b 100644 --- a/Libraries/Utilities/logError.js +++ b/Libraries/Utilities/logError.js @@ -7,6 +7,7 @@ * of patent rights can be found in the PATENTS file in the same directory. * * @providesModule logError + * @flow */ 'use strict'; diff --git a/Libraries/Utilities/mergeFast.js b/Libraries/Utilities/mergeFast.js index 8923167e7..e55ac76ed 100644 --- a/Libraries/Utilities/mergeFast.js +++ b/Libraries/Utilities/mergeFast.js @@ -7,6 +7,7 @@ * of patent rights can be found in the PATENTS file in the same directory. * * @providesModule mergeFast + * @flow */ 'use strict'; @@ -19,7 +20,7 @@ * @return {object} Merging of two objects, including prototype * inherited properties. */ -var mergeFast = function(one, two) { +var mergeFast = function(one: Object, two: Object): Object { var ret = {}; for (var keyOne in one) { ret[keyOne] = one[keyOne]; diff --git a/Libraries/Utilities/mergeIntoFast.js b/Libraries/Utilities/mergeIntoFast.js index 666e7384e..1a97d07b7 100644 --- a/Libraries/Utilities/mergeIntoFast.js +++ b/Libraries/Utilities/mergeIntoFast.js @@ -7,6 +7,7 @@ * of patent rights can be found in the PATENTS file in the same directory. * * @providesModule mergeIntoFast + * @flow */ 'use strict'; @@ -17,7 +18,7 @@ * @param {object} one Object to assign to. * @param {object} two Object to assign from. */ -var mergeIntoFast = function(one, two) { +var mergeIntoFast = function(one: Object, two: Object): void { for (var keyTwo in two) { one[keyTwo] = two[keyTwo]; } diff --git a/Libraries/Utilities/nativeModulePrefixNormalizer.js b/Libraries/Utilities/nativeModulePrefixNormalizer.js index 3c33fa503..151417bc5 100644 --- a/Libraries/Utilities/nativeModulePrefixNormalizer.js +++ b/Libraries/Utilities/nativeModulePrefixNormalizer.js @@ -7,11 +7,14 @@ * of patent rights can be found in the PATENTS file in the same directory. * * @providesModule nativeModulePrefixNormalizer + * @flow */ 'use strict'; // Dirty hack to support old (RK) and new (RCT) native module name conventions -function nativeModulePrefixNormalizer(modules) { +function nativeModulePrefixNormalizer( + modules: {[key: string]: any} +): void { Object.keys(modules).forEach((moduleName) => { var strippedName = moduleName.replace(/^(RCT|RK)/, ''); if (modules['RCT' + strippedName] && modules['RK' + strippedName]) { diff --git a/Libraries/Utilities/truncate.js b/Libraries/Utilities/truncate.js index b35c06588..1d318e835 100644 --- a/Libraries/Utilities/truncate.js +++ b/Libraries/Utilities/truncate.js @@ -7,11 +7,18 @@ * of patent rights can be found in the PATENTS file in the same directory. * * @providesModule truncate + * @flow */ 'use strict'; var merge = require('merge'); +type truncateOptions = { + breakOnWords: boolean; + minDelta: number; + elipsis: string; +} + var defaultOptions = { breakOnWords: true, minDelta: 10, // Prevents truncating a tiny bit off the end @@ -19,7 +26,11 @@ var defaultOptions = { }; // maxChars (including elipsis) -var truncate = function(str, maxChars, options) { +var truncate = function( + str: ?string, + maxChars: number, + options: truncateOptions +): ?string { options = merge(defaultOptions, options); if (str && str.length && str.length - options.minDelta + options.elipsis.length >= maxChars) { diff --git a/Libraries/Vibration/VibrationIOS.ios.js b/Libraries/Vibration/VibrationIOS.ios.js index c24a50606..2a1dc701c 100644 --- a/Libraries/Vibration/VibrationIOS.ios.js +++ b/Libraries/Vibration/VibrationIOS.ios.js @@ -7,6 +7,7 @@ * of patent rights can be found in the PATENTS file in the same directory. * * @providesModule VibrationIOS + * @flow */ 'use strict';