[flow] add some typings to utils (#7104)
* add some typings to utils * add typing of flattenChildren * more accurate typings for flattenChildren
Sergey Rubanov committed
Jun 23, 2016 at 19:16 UTC
3b5c756c7ab711a938b9c10115698ae43eceac86
5 files changed
+34
-20
src/shared/utils/ReactFeatureFlags.js
+1
@@ -7,6 +7,7 @@
7
* of patent rights can be found in the PATENTS file in the same directory.
8
*
9
* @providesModule ReactFeatureFlags
10
+ * @flow
11
*/
12
13
'use strict';
src/shared/utils/ReactNodeTypes.js
+4
-1
@@ -7,10 +7,13 @@
7
* of patent rights can be found in the PATENTS file in the same directory.
8
*
9
* @providesModule ReactNodeTypes
10
+ * @flow
11
*/
12
13
'use strict';
14
15
+type ReactNodeType = 0 | 1 | 2;
16
+
17
var ReactElement = require('ReactElement');
18
19
var invariant = require('invariant');
@@ -20,7 +23,7 @@ var ReactNodeTypes = {
23
COMPOSITE: 1,
24
EMPTY: 2,
25
23
- getType: function(node) {
26
+ getType: function(node: ReactElement<any>): ReactNodeType {
27
if (node === null || node === false) {
28
return ReactNodeTypes.EMPTY;
29
} else if (ReactElement.isValidElement(node)) {
src/shared/utils/flattenChildren.js
+24
-16
@@ -7,6 +7,7 @@
7
* of patent rights can be found in the PATENTS file in the same directory.
8
*
9
* @providesModule flattenChildren
10
+ * @flow
11
*/
12
13
'use strict';
@@ -22,22 +23,29 @@ var warning = require('warning');
23
* @param {!string} name String name of key path to child.
24
* @param {number=} selfDebugID Optional debugID of the current internal instance.
25
*/
25
-function flattenSingleChildIntoContext(traverseContext, child, name, selfDebugID) {
26
+function flattenSingleChildIntoContext(
27
+ traverseContext: mixed,
28
+ child: ReactElement<any>,
29
+ name: string,
30
+ selfDebugID: number
31
+): void {
32
// We found a component instance.
27
- var result = traverseContext;
28
- var keyUnique = (result[name] === undefined);
29
- if (__DEV__) {
30
- warning(
31
- keyUnique,
32
- 'flattenChildren(...): Encountered two children with the same key, ' +
33
- '`%s`. Child keys must be unique; when two children share a key, only ' +
34
- 'the first child will be used.%s',
35
- KeyEscapeUtils.unescape(name),
36
- ReactComponentTreeDevtool.getStackAddendumByID(selfDebugID)
37
- );
38
- }
39
- if (keyUnique && child != null) {
40
- result[name] = child;
33
+ if (traverseContext && typeof traverseContext === 'object') {
34
+ const result = traverseContext;
35
+ const keyUnique = (result[name] === undefined);
36
+ if (__DEV__) {
37
+ warning(
38
+ keyUnique,
39
+ 'flattenChildren(...): Encountered two children with the same key, ' +
40
+ '`%s`. Child keys must be unique; when two children share a key, only ' +
41
+ 'the first child will be used.%s',
42
+ KeyEscapeUtils.unescape(name),
43
+ ReactComponentTreeDevtool.getStackAddendumByID(selfDebugID)
44
+ );
45
+ }
46
+ if (keyUnique && child != null) {
47
+ result[name] = child;
48
+ }
49
}
50
}
51
@@ -46,7 +54,7 @@ function flattenSingleChildIntoContext(traverseContext, child, name, selfDebugID
54
* children will not be included in the resulting object.
55
* @return {!object} flattened children keyed by name.
56
*/
49
-function flattenChildren(children, selfDebugID) {
57
+function flattenChildren(children: ReactElement<any>, selfDebugID: number): ?{ [name: string]: ReactElement<any> } {
58
if (children == null) {
59
return children;
60
}
src/shared/utils/getIteratorFn.js
+2
-1
@@ -7,6 +7,7 @@
7
* of patent rights can be found in the PATENTS file in the same directory.
8
*
9
* @providesModule getIteratorFn
10
+ * @flow
11
*/
12
13
'use strict';
@@ -29,7 +30,7 @@ var FAUX_ITERATOR_SYMBOL = '@@iterator'; // Before Symbol spec.
30
* @param {?object} maybeIterable
31
* @return {?function}
32
*/
32
-function getIteratorFn(maybeIterable) {
33
+function getIteratorFn(maybeIterable: ?any): ?(p: ReactElement<any>) => void {
34
var iteratorFn = maybeIterable && (
35
(ITERATOR_SYMBOL && maybeIterable[ITERATOR_SYMBOL]) ||
36
maybeIterable[FAUX_ITERATOR_SYMBOL]
src/shared/utils/reactProdInvariant.js
+3
-2
@@ -7,6 +7,7 @@
7
* of patent rights can be found in the PATENTS file in the same directory.
8
*
9
* @providesModule reactProdInvariant
10
+ * @flow
11
*/
12
'use strict';
13
@@ -16,7 +17,7 @@
17
* and will _only_ be required by the corresponding babel pass.
18
* It always throws.
19
*/
19
-function reactProdInvariant(code) {
20
+function reactProdInvariant(code: string): void {
21
var argCount = arguments.length - 1;
22
23
var message = (
@@ -33,7 +34,7 @@ function reactProdInvariant(code) {
34
' for full errors and additional helpful warnings.'
35
);
36
36
- var error = new Error(message);
37
+ var error: Error & { framesToPop?: number } = new Error(message);
38
error.name = 'Invariant Violation';
39
error.framesToPop = 1; // we don't care about reactProdInvariant's own frame
40