Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[flow] add some typings to utils #7104

Merged
merged 3 commits into from Jun 23, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions src/shared/utils/ReactFeatureFlags.js
Expand Up @@ -7,6 +7,7 @@
* of patent rights can be found in the PATENTS file in the same directory.
*
* @providesModule ReactFeatureFlags
* @flow
*/

'use strict';
Expand Down
5 changes: 4 additions & 1 deletion src/shared/utils/ReactNodeTypes.js
Expand Up @@ -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');
Expand All @@ -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)) {
Expand Down
40 changes: 24 additions & 16 deletions src/shared/utils/flattenChildren.js
Expand Up @@ -7,6 +7,7 @@
* of patent rights can be found in the PATENTS file in the same directory.
*
* @providesModule flattenChildren
* @flow
*/

'use strict';
Expand All @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, just saw this. selfDebugID is actually an optional parameter that will only be available in dev mode. You can also see from the number= JSDoc annotation. cc @vjeux

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! #7110

): 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;
}
}
}

Expand All @@ -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> } {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@keyanzhang you are rigth. I'll file PR

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@chicoxyzzy Thanks, there's already #7110 :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yep sorry didn't noticed that

if (children == null) {
return children;
}
Expand Down
3 changes: 2 additions & 1 deletion src/shared/utils/getIteratorFn.js
Expand Up @@ -7,6 +7,7 @@
* of patent rights can be found in the PATENTS file in the same directory.
*
* @providesModule getIteratorFn
* @flow
*/

'use strict';
Expand All @@ -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]
Expand Down
5 changes: 3 additions & 2 deletions src/shared/utils/reactProdInvariant.js
Expand Up @@ -7,6 +7,7 @@
* of patent rights can be found in the PATENTS file in the same directory.
*
* @providesModule reactProdInvariant
* @flow
*/
'use strict';

Expand All @@ -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 = (
Expand All @@ -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

Expand Down