From 62919483df246a11810394d61979fded8c42c1d8 Mon Sep 17 00:00:00 2001 From: chico Date: Thu, 23 Jun 2016 04:07:51 +0300 Subject: [PATCH 1/3] add some typings to utils --- src/shared/utils/ReactFeatureFlags.js | 1 + src/shared/utils/ReactNodeTypes.js | 5 ++++- src/shared/utils/getIteratorFn.js | 3 ++- src/shared/utils/reactProdInvariant.js | 5 +++-- 4 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/shared/utils/ReactFeatureFlags.js b/src/shared/utils/ReactFeatureFlags.js index 30b5f4c7de2e..5b3bb1b457bd 100644 --- a/src/shared/utils/ReactFeatureFlags.js +++ b/src/shared/utils/ReactFeatureFlags.js @@ -7,6 +7,7 @@ * of patent rights can be found in the PATENTS file in the same directory. * * @providesModule ReactFeatureFlags + * @flow */ 'use strict'; diff --git a/src/shared/utils/ReactNodeTypes.js b/src/shared/utils/ReactNodeTypes.js index 7457b92661e5..2b5e40376d7a 100644 --- a/src/shared/utils/ReactNodeTypes.js +++ b/src/shared/utils/ReactNodeTypes.js @@ -7,10 +7,13 @@ * of patent rights can be found in the PATENTS file in the same directory. * * @providesModule ReactNodeTypes + * @flow */ 'use strict'; +type ReactNodeType = 0 | 1 | 2; + var ReactElement = require('ReactElement'); var invariant = require('invariant'); @@ -20,7 +23,7 @@ var ReactNodeTypes = { COMPOSITE: 1, EMPTY: 2, - getType: function(node) { + getType: function(node: ReactElement): ReactNodeType { if (node === null || node === false) { return ReactNodeTypes.EMPTY; } else if (ReactElement.isValidElement(node)) { diff --git a/src/shared/utils/getIteratorFn.js b/src/shared/utils/getIteratorFn.js index 5da5c4846a0c..4ed780aa204e 100644 --- a/src/shared/utils/getIteratorFn.js +++ b/src/shared/utils/getIteratorFn.js @@ -7,6 +7,7 @@ * of patent rights can be found in the PATENTS file in the same directory. * * @providesModule getIteratorFn + * @flow */ 'use strict'; @@ -29,7 +30,7 @@ var FAUX_ITERATOR_SYMBOL = '@@iterator'; // Before Symbol spec. * @param {?object} maybeIterable * @return {?function} */ -function getIteratorFn(maybeIterable) { +function getIteratorFn(maybeIterable: ?any): ?(p: ReactElement) => void { var iteratorFn = maybeIterable && ( (ITERATOR_SYMBOL && maybeIterable[ITERATOR_SYMBOL]) || maybeIterable[FAUX_ITERATOR_SYMBOL] diff --git a/src/shared/utils/reactProdInvariant.js b/src/shared/utils/reactProdInvariant.js index ca235bbeb9d2..ed61b59585ce 100644 --- a/src/shared/utils/reactProdInvariant.js +++ b/src/shared/utils/reactProdInvariant.js @@ -7,6 +7,7 @@ * of patent rights can be found in the PATENTS file in the same directory. * * @providesModule reactProdInvariant + * @flow */ 'use strict'; @@ -16,7 +17,7 @@ * and will _only_ be required by the corresponding babel pass. * It always throws. */ -function reactProdInvariant(code) { +function reactProdInvariant(code: string): void { var argCount = arguments.length - 1; var message = ( @@ -33,7 +34,7 @@ function reactProdInvariant(code) { ' for full errors and additional helpful warnings.' ); - var error = new Error(message); + var error: Error & { framesToPop?: number } = new Error(message); error.name = 'Invariant Violation'; error.framesToPop = 1; // we don't care about reactProdInvariant's own frame From dd20ed753ab65847d345165b62ea52db5bad6766 Mon Sep 17 00:00:00 2001 From: chico Date: Thu, 23 Jun 2016 16:25:32 +0300 Subject: [PATCH 2/3] add typing of flattenChildren --- src/shared/utils/flattenChildren.js | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/shared/utils/flattenChildren.js b/src/shared/utils/flattenChildren.js index b529f25c90ff..19a94068b248 100644 --- a/src/shared/utils/flattenChildren.js +++ b/src/shared/utils/flattenChildren.js @@ -7,6 +7,7 @@ * of patent rights can be found in the PATENTS file in the same directory. * * @providesModule flattenChildren + * @flow */ 'use strict'; @@ -22,7 +23,12 @@ var warning = require('warning'); * @param {!string} name String name of key path to child. * @param {number=} selfDebugID Optional debugID of the current internal instance. */ -function flattenSingleChildIntoContext(traverseContext, child, name, selfDebugID) { +function flattenSingleChildIntoContext( + traverseContext: any, + child: ReactElement, + name: string, + selfDebugID: number +): void { // We found a component instance. var result = traverseContext; var keyUnique = (result[name] === undefined); @@ -46,7 +52,7 @@ function flattenSingleChildIntoContext(traverseContext, child, name, selfDebugID * children will not be included in the resulting object. * @return {!object} flattened children keyed by name. */ -function flattenChildren(children, selfDebugID) { +function flattenChildren(children: ReactElement, selfDebugID: number): any { if (children == null) { return children; } From c65d02e5f17e7cb7d380b8a872755d5d28251f4c Mon Sep 17 00:00:00 2001 From: chico Date: Thu, 23 Jun 2016 18:06:36 +0300 Subject: [PATCH 3/3] more accurate typings for flattenChildren --- src/shared/utils/flattenChildren.js | 34 +++++++++++++++-------------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/src/shared/utils/flattenChildren.js b/src/shared/utils/flattenChildren.js index 19a94068b248..afb12d5a4655 100644 --- a/src/shared/utils/flattenChildren.js +++ b/src/shared/utils/flattenChildren.js @@ -24,26 +24,28 @@ var warning = require('warning'); * @param {number=} selfDebugID Optional debugID of the current internal instance. */ function flattenSingleChildIntoContext( - traverseContext: any, + traverseContext: mixed, child: ReactElement, name: string, selfDebugID: number ): void { // We found a component instance. - var result = traverseContext; - var keyUnique = (result[name] === undefined); - if (__DEV__) { - warning( - keyUnique, - 'flattenChildren(...): Encountered two children with the same key, ' + - '`%s`. Child keys must be unique; when two children share a key, only ' + - 'the first child will be used.%s', - KeyEscapeUtils.unescape(name), - ReactComponentTreeDevtool.getStackAddendumByID(selfDebugID) - ); - } - if (keyUnique && child != null) { - result[name] = child; + if (traverseContext && typeof traverseContext === 'object') { + const result = traverseContext; + const keyUnique = (result[name] === undefined); + if (__DEV__) { + warning( + keyUnique, + 'flattenChildren(...): Encountered two children with the same key, ' + + '`%s`. Child keys must be unique; when two children share a key, only ' + + 'the first child will be used.%s', + KeyEscapeUtils.unescape(name), + ReactComponentTreeDevtool.getStackAddendumByID(selfDebugID) + ); + } + if (keyUnique && child != null) { + result[name] = child; + } } } @@ -52,7 +54,7 @@ function flattenSingleChildIntoContext( * children will not be included in the resulting object. * @return {!object} flattened children keyed by name. */ -function flattenChildren(children: ReactElement, selfDebugID: number): any { +function flattenChildren(children: ReactElement, selfDebugID: number): ?{ [name: string]: ReactElement } { if (children == null) { return children; }