@samitouri / QOS-React / commits / 1c71970431

[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

Christopher Chedeau committed Jun 16, 2016 at 17:18 UTC 1c71970431410d2ea39f049341b0b5d4be529e86
4 files changed +33 -30
package.json
+2 -1
@@ -78,7 +78,8 @@
78 "linc": "git diff --name-only --diff-filter=ACMRTUB `git merge-base HEAD master` | grep '\\.js$' | xargs eslint --",
79 "lint": "grunt lint",
80 "postinstall": "node node_modules/fbjs-scripts/node/check-dev-engines.js package.json",
81 - "test": "jest"
81 + "test": "jest",
82 + "flow": "flow"
83 },
84 "jest": {
85 "modulePathIgnorePatterns": [
src/shared/utils/accumulate.js
+15 -15
@@ -7,6 +7,7 @@
7 * of patent rights can be found in the PATENTS file in the same directory.
8 *
9 * @providesModule accumulate
10 + * @flow
11 */
12
13 'use strict';
@@ -20,28 +21,27 @@ var invariant = require('invariant');
21 *
22 * @return {*|array<*>} An accumulation of items.
23 */
23 -function accumulate(current, next) {
24 +function accumulate<T>(current: ?(T | Array<T>), next: T | Array<T>): T | Array<T> {
25 invariant(
26 next != null,
27 'accumulate(...): Accumulated items must be not be null or undefined.'
28 );
29 +
30 if (current == null) {
31 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 - }
32 }
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];
45 }
46
47 module.exports = accumulate;
src/shared/utils/accumulateInto.js
+9 -12
@@ -7,6 +7,7 @@
7 * of patent rights can be found in the PATENTS file in the same directory.
8 *
9 * @providesModule accumulateInto
10 + * @flow
11 */
12
13 'use strict';
@@ -14,7 +15,6 @@
15 var invariant = require('invariant');
16
17 /**
17 - *
18 * Accumulates items that must not be null or undefined into the first one. This
19 * is used to conserve memory by avoiding array allocations, and thus sacrifices
20 * API cleanness. Since `current` can be null before being passed in and not
@@ -27,31 +27,28 @@ var invariant = require('invariant');
27 * @return {*|array<*>} An accumulation of items.
28 */
29
30 -function accumulateInto(current, next) {
30 +function accumulateInto<T>(current: ?(T | Array<T>), next: T | Array<T>): T | Array<T> {
31 invariant(
32 next != null,
33 'accumulateInto(...): Accumulated items must not be null or undefined.'
34 );
35 +
36 if (current == null) {
37 return next;
38 }
39
40 // Both are not empty. Warning: Never call x.concat(y) when you are not
41 // 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 + }
47 current.push(next);
48 return current;
49 }
50
54 - if (nextIsArray) {
51 + if (Array.isArray(next)) {
52 // A bit too dangerous to mutate `next`.
53 return [current].concat(next);
54 }
src/shared/utils/forEachAccumulated.js
+7 -2
@@ -7,6 +7,7 @@
7 * of patent rights can be found in the PATENTS file in the same directory.
8 *
9 * @providesModule forEachAccumulated
10 + * @flow
11 */
12
13 'use strict';
@@ -18,12 +19,16 @@
19 * handling the case when there is exactly one item (and we do not need to
20 * allocate an array).
21 */
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 +) {
27 if (Array.isArray(arr)) {
28 arr.forEach(cb, scope);
29 } else if (arr) {
30 cb.call(scope, arr);
31 }
27 -};
32 +}
33
34 module.exports = forEachAccumulated;