[flow] add some typings to utils (#7104)

* add some typings to utils

* add typing of flattenChildren

* more accurate typings for flattenChildren

(cherry picked from commit 3b5c756c7a)
This commit is contained in:
Sergey Rubanov
2016-07-08 10:48:56 -07:00
committed by Paul O’Shannessy
parent 24cf5c7a5e
commit a3c0d68af9
5 changed files with 34 additions and 20 deletions
+1
View File
@@ -7,6 +7,7 @@
* of patent rights can be found in the PATENTS file in the same directory.
*
* @providesModule ReactFeatureFlags
* @flow
*/
'use strict';
+4 -1
View File
@@ -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<any>): ReactNodeType {
if (node === null || node === false) {
return ReactNodeTypes.EMPTY;
} else if (ReactElement.isValidElement(node)) {
+24 -16
View File
@@ -7,6 +7,7 @@
* of patent rights can be found in the PATENTS file in the same directory.
*
* @providesModule flattenChildren
* @flow
*/
'use strict';
@@ -22,22 +23,29 @@ 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: mixed,
child: ReactElement<any>,
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;
}
}
}
@@ -46,7 +54,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<any>, selfDebugID: number): ?{ [name: string]: ReactElement<any> } {
if (children == null) {
return children;
}
+2 -1
View File
@@ -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<any>) => void {
var iteratorFn = maybeIterable && (
(ITERATOR_SYMBOL && maybeIterable[ITERATOR_SYMBOL]) ||
maybeIterable[FAUX_ITERATOR_SYMBOL]
+3 -2
View File
@@ -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