Skip to content

Commit 1c71970

Browse files
committed
[flow] annotate accumulate and accumulateInto
Summary: Trying to start adding flow types to files in React. I needed to add a script to package.json in order to run flow, flow-bin is already a dependency. I had to rewrite the implementation a bit. Flow doesn't recognize ``` var currentIsArray = Array.isArray(current); if (currentIsArray) { // not refined } ``` but the following does work ``` if (Array.isArray(current)) { // refined } ``` Test Plan: npm run-script flow npm run-script test accumulate Reviewers: @zpao @spicyj
1 parent 49238b9 commit 1c71970

File tree

4 files changed

+33
-30
lines changed

4 files changed

+33
-30
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,8 @@
7878
"linc": "git diff --name-only --diff-filter=ACMRTUB `git merge-base HEAD master` | grep '\\.js$' | xargs eslint --",
7979
"lint": "grunt lint",
8080
"postinstall": "node node_modules/fbjs-scripts/node/check-dev-engines.js package.json",
81-
"test": "jest"
81+
"test": "jest",
82+
"flow": "flow"
8283
},
8384
"jest": {
8485
"modulePathIgnorePatterns": [

src/shared/utils/accumulate.js

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
* of patent rights can be found in the PATENTS file in the same directory.
88
*
99
* @providesModule accumulate
10+
* @flow
1011
*/
1112

1213
'use strict';
@@ -20,28 +21,27 @@ var invariant = require('invariant');
2021
*
2122
* @return {*|array<*>} An accumulation of items.
2223
*/
23-
function accumulate(current, next) {
24+
function accumulate<T>(current: ?(T | Array<T>), next: T | Array<T>): T | Array<T> {
2425
invariant(
2526
next != null,
2627
'accumulate(...): Accumulated items must be not be null or undefined.'
2728
);
29+
2830
if (current == null) {
2931
return next;
30-
} else {
31-
// Both are not empty. Warning: Never call x.concat(y) when you are not
32-
// certain that x is an Array (x could be a string with concat method).
33-
var currentIsArray = Array.isArray(current);
34-
var nextIsArray = Array.isArray(next);
35-
if (currentIsArray) {
36-
return current.concat(next);
37-
} else {
38-
if (nextIsArray) {
39-
return [current].concat(next);
40-
} else {
41-
return [current, next];
42-
}
43-
}
4432
}
33+
34+
// Both are not empty. Warning: Never call x.concat(y) when you are not
35+
// certain that x is an Array (x could be a string with concat method).
36+
if (Array.isArray(current)) {
37+
return current.concat(next);
38+
}
39+
40+
if (Array.isArray(next)) {
41+
return [current].concat(next);
42+
}
43+
44+
return [current, next];
4545
}
4646

4747
module.exports = accumulate;

src/shared/utils/accumulateInto.js

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@
77
* of patent rights can be found in the PATENTS file in the same directory.
88
*
99
* @providesModule accumulateInto
10+
* @flow
1011
*/
1112

1213
'use strict';
1314

1415
var invariant = require('invariant');
1516

1617
/**
17-
*
1818
* Accumulates items that must not be null or undefined into the first one. This
1919
* is used to conserve memory by avoiding array allocations, and thus sacrifices
2020
* API cleanness. Since `current` can be null before being passed in and not
@@ -27,31 +27,28 @@ var invariant = require('invariant');
2727
* @return {*|array<*>} An accumulation of items.
2828
*/
2929

30-
function accumulateInto(current, next) {
30+
function accumulateInto<T>(current: ?(T | Array<T>), next: T | Array<T>): T | Array<T> {
3131
invariant(
3232
next != null,
3333
'accumulateInto(...): Accumulated items must not be null or undefined.'
3434
);
35+
3536
if (current == null) {
3637
return next;
3738
}
3839

3940
// Both are not empty. Warning: Never call x.concat(y) when you are not
4041
// certain that x is an Array (x could be a string with concat method).
41-
var currentIsArray = Array.isArray(current);
42-
var nextIsArray = Array.isArray(next);
43-
44-
if (currentIsArray && nextIsArray) {
45-
current.push.apply(current, next);
46-
return current;
47-
}
48-
49-
if (currentIsArray) {
42+
if (Array.isArray(current)) {
43+
if (Array.isArray(next)) {
44+
current.push.apply(current, next);
45+
return current;
46+
}
5047
current.push(next);
5148
return current;
5249
}
5350

54-
if (nextIsArray) {
51+
if (Array.isArray(next)) {
5552
// A bit too dangerous to mutate `next`.
5653
return [current].concat(next);
5754
}

src/shared/utils/forEachAccumulated.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
* of patent rights can be found in the PATENTS file in the same directory.
88
*
99
* @providesModule forEachAccumulated
10+
* @flow
1011
*/
1112

1213
'use strict';
@@ -18,12 +19,16 @@
1819
* handling the case when there is exactly one item (and we do not need to
1920
* allocate an array).
2021
*/
21-
var forEachAccumulated = function(arr, cb, scope) {
22+
function forEachAccumulated<T>(
23+
arr: ?(T | Array<T>),
24+
cb: ((elem: T) => void),
25+
scope: ?any,
26+
) {
2227
if (Array.isArray(arr)) {
2328
arr.forEach(cb, scope);
2429
} else if (arr) {
2530
cb.call(scope, arr);
2631
}
27-
};
32+
}
2833

2934
module.exports = forEachAccumulated;

0 commit comments

Comments
 (0)