array-callback-return
在数组方法的回调中强制执行 return
语句
Array
有几种过滤、映射和折叠的方法。如果我们忘记在这些回调中写 return
语句,那可能是一个错误。如果您不想使用 return 或不需要返回的结果,请考虑使用 .forEach 代替。
// example: convert ['a', 'b', 'c'] --> {a: 0, b: 1, c: 2}
var indexMap = myArray.reduce(function(memo, item, index) {
memo[item] = index;
}, {}); // Error: cannot set property 'b' of undefined
规则详情
此规则强制在数组方法的回调中使用 return
语句。此外,它还可以通过使用 checkForEach
选项强制 forEach
数组方法回调不返回值。
此规则查找以下方法的回调函数,然后检查 return
语句的使用情况。
Array.from
Array.prototype.every
Array.prototype.filter
Array.prototype.find
Array.prototype.findIndex
Array.prototype.findLast
Array.prototype.findLastIndex
Array.prototype.flatMap
Array.prototype.forEach
(可选,基于checkForEach
参数)Array.prototype.map
Array.prototype.reduce
Array.prototype.reduceRight
Array.prototype.some
Array.prototype.sort
Array.prototype.toSorted
- 及以上类型的数组。
zblpFAyoEiDF1GS7EEgT+Fw+zDBsVLJNOcYSNQAXW6dSdz0JClE4rOOJ51S8csCM
/*eslint array-callback-return: "error"*/
var indexMap = myArray.reduce(function(memo, item, index) {
memo[item] = index;
}, {});
var foo = Array.from(nodes, function(node) {
if (node.tagName === "DIV") {
return true;
}
});
var bar = foo.filter(function(x) {
if (x) {
return true;
} else {
return;
}
});
h+1Jf/rEEE6yYiKhWnK/aHl9/UYIfowckm44qhmqvwwaWj0QX+JXnab7YJgxSmDw
/*eslint array-callback-return: "error"*/
var indexMap = myArray.reduce(function(memo, item, index) {
memo[item] = index;
return memo;
}, {});
var foo = Array.from(nodes, function(node) {
if (node.tagName === "DIV") {
return true;
}
return false;
});
var bar = foo.map(node => node.getAttribute("id"));
选项
wy2GpXl8v4rmUqhQEZyMWK15PDTccEJG3CKtPLEeoUzdBWs7cFwj6UCNOlQs62y6GY0m4j6Sa3eiymLbU46N6w==
- ldCrFEnrz3iPLZFQOlK0jNn/DkhErArjPErMpRNmRPFhs7zY0+99t85cqtm4LiXRHCtqKfRlkd+/CrouYmafWh/bRFf6brM2iKlkRbcHKKmR7vG8xwCHaekcTGtQiRbe2y4le3VM15kRpOgcreG1PqePsGg6KTUQbsWOoWTZPgnRp+ynFFDMF23XfewvSaP6EdcG1llYPUyZmewMx8tljjKoBX0RUV8OOerTVNc5+ajKmC95oiSk7FMJdNqbyABwTK/w0+DeL3z1D6V3E83FZBQIDRAZkYLGw5pdygvXZ9iojBp5pZuctxWY8psJI97tRbIQKcjrhQfmEvPknB1aVLq/V7OXHl14LbM9WgtswXO0C6i+ZCjk5qChB+wSJFeEbIg+tlYU8HHFh7nT32CAUmeLk+AGxQTMkbBGsJzOC3zHafYDhPHsm/d46meHg33McpRoIMAQvrhizbnw2We9EScEq5mWD2Td06sCiHa781HvtHFBYNPgDzuKbeLh5b7z
allowImplicit
FlmPJcARAwelvqQij1k7AUENJQfbSXvVug+UEW04NS+nEpoxUCoz9AsX2Ga/QKyHfflXdUv5DNHhCyhKU92QKE4lnsPYvK4jTlroLPUKlPo=
/*eslint array-callback-return: ["error", { allowImplicit: true }]*/
var undefAllTheThings = myArray.map(function(item) {
return;
});
checkForEach
RmtMO8NtEaH10JdrZ/S2tEZqsduYK0B+i6PvNyuQKUo8PjWnYfDAGEyLdR+6kkNbNDz0BSh6vWdAmQzsHL2k9rttHnXt4yge6FpjIX4FzNQ=
/*eslint array-callback-return: ["error", { checkForEach: true }]*/
myArray.forEach(function(item) {
return handleItem(item)
});
myArray.forEach(function(item) {
if (item < 0) {
return x;
}
handleItem(item);
});
myArray.forEach(item => handleItem(item));
myArray.forEach(item => {
return handleItem(item);
});
RmtMO8NtEaH10JdrZ/S2tEZqsduYK0B+i6PvNyuQKUqMCBBNDzrYMSxE/v1kZQTO9BIugGyFKKGcOBXUf2L2ft8Xb59H7gbjFLtWQS5l3V8=
/*eslint array-callback-return: ["error", { checkForEach: true }]*/
myArray.forEach(function(item) {
handleItem(item)
});
myArray.forEach(function(item) {
if (item < 0) {
return;
}
handleItem(item);
});
myArray.forEach(function(item) {
handleItem(item);
return;
});
myArray.forEach(item => {
handleItem(item);
});
已知限制
g7lugKRdgt37Ay8Jb+ljW7l98zxEJL0DGhz7KzJEh5SaU9XZx+PccxAo29JFVTfYTyRWEZnfaVKRFyolDHJjB425AoJpGbVeCUdwbuvLbfofrC/TZL3/D79BaHzhcq/Jb330ecZ1lWpfjYXb+ZYOxQ==
何时不使用
LlvfTlGNiqWwT39ynjGQIWKmZvnRUL/kipMa7b6IICzTrUBtfz8SseDUU816OxiLEUhJOSyWkOGodC9qTaJwU5y0kRUNg1N5x7+tnK23u/bV9sWpliCHRHL4zhQ37bYKGwyhYYMFVr3DiLnYlPa8hnhS+BvRwTRGDx11zojqlvs=