]>
Commit | Line | Data |
---|---|---|
eb39fafa DC |
1 | # Enforces return statements in callbacks of array's methods (array-callback-return) |
2 | ||
3 | `Array` has several methods for filtering, mapping, and folding. | |
4 | If we forget to write `return` statement in a callback of those, it's probably a mistake. If you don't want to use a return or don't need the returned results, consider using [.forEach](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach) instead. | |
5 | ||
6 | ```js | |
7 | // example: convert ['a', 'b', 'c'] --> {a: 0, b: 1, c: 2} | |
8 | var indexMap = myArray.reduce(function(memo, item, index) { | |
9 | memo[item] = index; | |
10 | }, {}); // Error: cannot set property 'b' of undefined | |
11 | ``` | |
12 | ||
13 | ## Rule Details | |
14 | ||
15 | This rule enforces usage of `return` statement in callbacks of array's methods. | |
56c4a2cb | 16 | Additionally, it may also enforce the `forEach` array method callback to __not__ return a value by using the `checkForEach` option. |
eb39fafa DC |
17 | |
18 | This rule finds callback functions of the following methods, then checks usage of `return` statement. | |
19 | ||
20 | * [`Array.from`](https://www.ecma-international.org/ecma-262/6.0/#sec-array.from) | |
21 | * [`Array.prototype.every`](https://www.ecma-international.org/ecma-262/6.0/#sec-array.prototype.every) | |
22 | * [`Array.prototype.filter`](https://www.ecma-international.org/ecma-262/6.0/#sec-array.prototype.filter) | |
23 | * [`Array.prototype.find`](https://www.ecma-international.org/ecma-262/6.0/#sec-array.prototype.find) | |
24 | * [`Array.prototype.findIndex`](https://www.ecma-international.org/ecma-262/6.0/#sec-array.prototype.findindex) | |
25 | * [`Array.prototype.flatMap`](https://www.ecma-international.org/ecma-262/10.0/#sec-array.prototype.flatmap) | |
26 | * [`Array.prototype.forEach`](https://www.ecma-international.org/ecma-262/6.0/#sec-array.prototype.foreach) (optional, based on `checkForEach` parameter) | |
27 | * [`Array.prototype.map`](https://www.ecma-international.org/ecma-262/6.0/#sec-array.prototype.map) | |
28 | * [`Array.prototype.reduce`](https://www.ecma-international.org/ecma-262/6.0/#sec-array.prototype.reduce) | |
29 | * [`Array.prototype.reduceRight`](https://www.ecma-international.org/ecma-262/6.0/#sec-array.prototype.reduceright) | |
30 | * [`Array.prototype.some`](https://www.ecma-international.org/ecma-262/6.0/#sec-array.prototype.some) | |
31 | * [`Array.prototype.sort`](https://www.ecma-international.org/ecma-262/6.0/#sec-array.prototype.sort) | |
32 | * And above of typed arrays. | |
33 | ||
34 | Examples of **incorrect** code for this rule: | |
35 | ||
36 | ```js | |
37 | /*eslint array-callback-return: "error"*/ | |
38 | ||
39 | var indexMap = myArray.reduce(function(memo, item, index) { | |
40 | memo[item] = index; | |
41 | }, {}); | |
42 | ||
43 | var foo = Array.from(nodes, function(node) { | |
44 | if (node.tagName === "DIV") { | |
45 | return true; | |
46 | } | |
47 | }); | |
48 | ||
49 | var bar = foo.filter(function(x) { | |
50 | if (x) { | |
51 | return true; | |
52 | } else { | |
53 | return; | |
54 | } | |
55 | }); | |
56 | ``` | |
57 | ||
58 | Examples of **correct** code for this rule: | |
59 | ||
60 | ```js | |
61 | /*eslint array-callback-return: "error"*/ | |
62 | ||
63 | var indexMap = myArray.reduce(function(memo, item, index) { | |
64 | memo[item] = index; | |
65 | return memo; | |
66 | }, {}); | |
67 | ||
68 | var foo = Array.from(nodes, function(node) { | |
69 | if (node.tagName === "DIV") { | |
70 | return true; | |
71 | } | |
72 | return false; | |
73 | }); | |
74 | ||
75 | var bar = foo.map(node => node.getAttribute("id")); | |
76 | ``` | |
77 | ||
78 | ## Options | |
79 | ||
80 | This rule accepts a configuration object with two options: | |
81 | ||
82 | * `"allowImplicit": false` (default) When set to `true`, allows callbacks of methods that require a return value to implicitly return `undefined` with a `return` statement containing no expression. | |
83 | * `"checkForEach": false` (default) When set to `true`, rule will also report `forEach` callbacks that return a value. | |
84 | ||
85 | ### allowImplicit | |
86 | ||
87 | Examples of **correct** code for the `{ "allowImplicit": true }` option: | |
88 | ||
89 | ```js | |
90 | /*eslint array-callback-return: ["error", { allowImplicit: true }]*/ | |
91 | var undefAllTheThings = myArray.map(function(item) { | |
92 | return; | |
93 | }); | |
94 | ``` | |
95 | ||
96 | ### checkForEach | |
97 | ||
98 | Examples of **incorrect** code for the `{ "checkForEach": true }` option: | |
99 | ||
100 | ```js | |
101 | /*eslint array-callback-return: ["error", { checkForEach: true }]*/ | |
102 | ||
103 | myArray.forEach(function(item) { | |
104 | return handleItem(item) | |
105 | }); | |
106 | ||
107 | myArray.forEach(function(item) { | |
108 | if (item < 0) { | |
109 | return x; | |
110 | } | |
111 | handleItem(item); | |
112 | }); | |
113 | ||
114 | myArray.forEach(item => handleItem(item)); | |
115 | ||
116 | myArray.forEach(item => { | |
117 | return handleItem(item); | |
118 | }); | |
119 | ``` | |
120 | ||
121 | Examples of **correct** code for the `{ "checkForEach": true }` option: | |
122 | ||
123 | ```js | |
124 | /*eslint array-callback-return: ["error", { checkForEach: true }]*/ | |
125 | ||
126 | myArray.forEach(function(item) { | |
127 | handleItem(item) | |
128 | }); | |
129 | ||
130 | myArray.forEach(function(item) { | |
131 | if (item < 0) { | |
132 | return; | |
133 | } | |
134 | handleItem(item); | |
135 | }); | |
136 | ||
137 | myArray.forEach(function(item) { | |
138 | handleItem(item); | |
139 | return; | |
140 | }); | |
141 | ||
142 | myArray.forEach(item => { | |
143 | handleItem(item); | |
144 | }); | |
145 | ``` | |
146 | ||
eb39fafa DC |
147 | ## Known Limitations |
148 | ||
149 | This rule checks callback functions of methods with the given names, *even if* the object which has the method is *not* an array. | |
150 | ||
151 | ## When Not To Use It | |
152 | ||
153 | If you don't want to warn about usage of `return` statement in callbacks of array's methods, then it's safe to disable this rule. |