]>
Commit | Line | Data |
---|---|---|
8f9d1d4d DC |
1 | --- |
2 | title: array-callback-return | |
8f9d1d4d DC |
3 | rule_type: problem |
4 | --- | |
5 | ||
eb39fafa DC |
6 | |
7 | `Array` has several methods for filtering, mapping, and folding. | |
8 | 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. | |
9 | ||
10 | ```js | |
11 | // example: convert ['a', 'b', 'c'] --> {a: 0, b: 1, c: 2} | |
12 | var indexMap = myArray.reduce(function(memo, item, index) { | |
13 | memo[item] = index; | |
14 | }, {}); // Error: cannot set property 'b' of undefined | |
15 | ``` | |
16 | ||
17 | ## Rule Details | |
18 | ||
19 | This rule enforces usage of `return` statement in callbacks of array's methods. | |
8f9d1d4d | 20 | Additionally, it may also enforce the `forEach` array method callback to **not** return a value by using the `checkForEach` option. |
eb39fafa DC |
21 | |
22 | This rule finds callback functions of the following methods, then checks usage of `return` statement. | |
23 | ||
24 | * [`Array.from`](https://www.ecma-international.org/ecma-262/6.0/#sec-array.from) | |
25 | * [`Array.prototype.every`](https://www.ecma-international.org/ecma-262/6.0/#sec-array.prototype.every) | |
26 | * [`Array.prototype.filter`](https://www.ecma-international.org/ecma-262/6.0/#sec-array.prototype.filter) | |
27 | * [`Array.prototype.find`](https://www.ecma-international.org/ecma-262/6.0/#sec-array.prototype.find) | |
28 | * [`Array.prototype.findIndex`](https://www.ecma-international.org/ecma-262/6.0/#sec-array.prototype.findindex) | |
f2a92ac6 DC |
29 | * [`Array.prototype.findLast`](https://tc39.es/ecma262/#sec-array.prototype.findlast) |
30 | * [`Array.prototype.findLastIndex`](https://tc39.es/ecma262/#sec-array.prototype.findlastindex) | |
eb39fafa DC |
31 | * [`Array.prototype.flatMap`](https://www.ecma-international.org/ecma-262/10.0/#sec-array.prototype.flatmap) |
32 | * [`Array.prototype.forEach`](https://www.ecma-international.org/ecma-262/6.0/#sec-array.prototype.foreach) (optional, based on `checkForEach` parameter) | |
33 | * [`Array.prototype.map`](https://www.ecma-international.org/ecma-262/6.0/#sec-array.prototype.map) | |
34 | * [`Array.prototype.reduce`](https://www.ecma-international.org/ecma-262/6.0/#sec-array.prototype.reduce) | |
35 | * [`Array.prototype.reduceRight`](https://www.ecma-international.org/ecma-262/6.0/#sec-array.prototype.reduceright) | |
36 | * [`Array.prototype.some`](https://www.ecma-international.org/ecma-262/6.0/#sec-array.prototype.some) | |
37 | * [`Array.prototype.sort`](https://www.ecma-international.org/ecma-262/6.0/#sec-array.prototype.sort) | |
f2a92ac6 | 38 | * [`Array.prototype.toSorted`](https://tc39.es/ecma262/#sec-array.prototype.tosorted) |
eb39fafa DC |
39 | * And above of typed arrays. |
40 | ||
41 | Examples of **incorrect** code for this rule: | |
42 | ||
8f9d1d4d DC |
43 | :::incorrect |
44 | ||
eb39fafa DC |
45 | ```js |
46 | /*eslint array-callback-return: "error"*/ | |
47 | ||
48 | var indexMap = myArray.reduce(function(memo, item, index) { | |
49 | memo[item] = index; | |
50 | }, {}); | |
51 | ||
52 | var foo = Array.from(nodes, function(node) { | |
53 | if (node.tagName === "DIV") { | |
54 | return true; | |
55 | } | |
56 | }); | |
57 | ||
58 | var bar = foo.filter(function(x) { | |
59 | if (x) { | |
60 | return true; | |
61 | } else { | |
62 | return; | |
63 | } | |
64 | }); | |
65 | ``` | |
66 | ||
8f9d1d4d DC |
67 | ::: |
68 | ||
eb39fafa DC |
69 | Examples of **correct** code for this rule: |
70 | ||
8f9d1d4d DC |
71 | :::correct |
72 | ||
eb39fafa DC |
73 | ```js |
74 | /*eslint array-callback-return: "error"*/ | |
75 | ||
76 | var indexMap = myArray.reduce(function(memo, item, index) { | |
77 | memo[item] = index; | |
78 | return memo; | |
79 | }, {}); | |
80 | ||
81 | var foo = Array.from(nodes, function(node) { | |
82 | if (node.tagName === "DIV") { | |
83 | return true; | |
84 | } | |
85 | return false; | |
86 | }); | |
87 | ||
88 | var bar = foo.map(node => node.getAttribute("id")); | |
89 | ``` | |
90 | ||
8f9d1d4d DC |
91 | ::: |
92 | ||
eb39fafa DC |
93 | ## Options |
94 | ||
95 | This rule accepts a configuration object with two options: | |
96 | ||
97 | * `"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. | |
98 | * `"checkForEach": false` (default) When set to `true`, rule will also report `forEach` callbacks that return a value. | |
99 | ||
100 | ### allowImplicit | |
101 | ||
102 | Examples of **correct** code for the `{ "allowImplicit": true }` option: | |
103 | ||
8f9d1d4d DC |
104 | :::correct |
105 | ||
eb39fafa DC |
106 | ```js |
107 | /*eslint array-callback-return: ["error", { allowImplicit: true }]*/ | |
108 | var undefAllTheThings = myArray.map(function(item) { | |
109 | return; | |
110 | }); | |
111 | ``` | |
112 | ||
8f9d1d4d DC |
113 | ::: |
114 | ||
eb39fafa DC |
115 | ### checkForEach |
116 | ||
117 | Examples of **incorrect** code for the `{ "checkForEach": true }` option: | |
118 | ||
8f9d1d4d DC |
119 | :::incorrect |
120 | ||
eb39fafa DC |
121 | ```js |
122 | /*eslint array-callback-return: ["error", { checkForEach: true }]*/ | |
123 | ||
124 | myArray.forEach(function(item) { | |
125 | return handleItem(item) | |
126 | }); | |
127 | ||
128 | myArray.forEach(function(item) { | |
129 | if (item < 0) { | |
130 | return x; | |
131 | } | |
132 | handleItem(item); | |
133 | }); | |
134 | ||
135 | myArray.forEach(item => handleItem(item)); | |
136 | ||
137 | myArray.forEach(item => { | |
138 | return handleItem(item); | |
139 | }); | |
140 | ``` | |
141 | ||
8f9d1d4d DC |
142 | ::: |
143 | ||
eb39fafa DC |
144 | Examples of **correct** code for the `{ "checkForEach": true }` option: |
145 | ||
8f9d1d4d DC |
146 | :::correct |
147 | ||
eb39fafa DC |
148 | ```js |
149 | /*eslint array-callback-return: ["error", { checkForEach: true }]*/ | |
150 | ||
151 | myArray.forEach(function(item) { | |
152 | handleItem(item) | |
153 | }); | |
154 | ||
155 | myArray.forEach(function(item) { | |
156 | if (item < 0) { | |
157 | return; | |
158 | } | |
159 | handleItem(item); | |
160 | }); | |
161 | ||
162 | myArray.forEach(function(item) { | |
163 | handleItem(item); | |
164 | return; | |
165 | }); | |
166 | ||
167 | myArray.forEach(item => { | |
168 | handleItem(item); | |
169 | }); | |
170 | ``` | |
171 | ||
8f9d1d4d DC |
172 | ::: |
173 | ||
eb39fafa DC |
174 | ## Known Limitations |
175 | ||
176 | This rule checks callback functions of methods with the given names, *even if* the object which has the method is *not* an array. | |
177 | ||
178 | ## When Not To Use It | |
179 | ||
180 | 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. |