array-callback-return
强制数组方法的回调函数中使用 return 语句
💡 hasSuggestions
此规则报告的一些问题可以通过编辑器建议手动修复
Array 有几个用于过滤、映射和折叠的方法。如果我们忘记在这些方法的回调函数中编写 return 语句,那很可能是一个错误。如果您不想使用 return 或不需要返回的结果,请考虑使用 .forEach 代替。
// example: convert ['a', 'b', 'c'] --> {a: 0, b: 1, c: 2}
const indexMap = myArray.reduce(function(memo, item, index) {
memo[item] = index;
}, {}); // Error: cannot set property 'b' of undefined
规则详情
此规则强制在数组方法的回调函数中使用 return 语句。此外,它还可以强制 forEach 数组方法的回调函数不返回值,通过使用 checkForEach 选项。
此规则查找以下方法的回调函数,然后检查 return 语句的使用情况。
Array.fromArray.prototype.everyArray.prototype.filterArray.prototype.findArray.prototype.findIndexArray.prototype.findLastArray.prototype.findLastIndexArray.prototype.flatMapArray.prototype.forEach(可选,基于checkForEach参数)Array.prototype.mapArray.prototype.reduceArray.prototype.reduceRightArray.prototype.someArray.prototype.sortArray.prototype.toSorted- 以及上述类型的数组。
此规则的 错误 代码示例
在 Playground 中打开
/*eslint array-callback-return: "error"*/
const indexMap = myArray.reduce((memo, item, index) {
memo[item] = index;
}, {});
const foo = Array.from(nodes, (node) {
if (node.tagName === "DIV") {
return true;
}
});
const bar = foo.filter(function(x) {
if (x) {
return true;
} else {
}
});
此规则的 正确 代码示例
在 Playground 中打开
/*eslint array-callback-return: "error"*/
const indexMap = myArray.reduce(function(memo, item, index) {
memo[item] = index;
return memo;
}, {});
const foo = Array.from(nodes, function(node) {
if (node.tagName === "DIV") {
return true;
}
return false;
});
const bar = foo.map(node => node.getAttribute("id"));
选项
此规则接受带有三个选项的配置对象
"allowImplicit": false(默认)当设置为true时,允许需要返回值的回调函数隐式返回undefined,使用不包含表达式的return语句。"checkForEach": false(默认)当设置为true时,规则还将报告返回值的forEach回调函数。"allowVoid": false(默认)当设置为true时,允许在forEach回调函数中使用void,因此规则不会报告带有void运算符的返回值。
注意: { "allowVoid": true } 仅在 checkForEach 选项设置为 true 时有效。
allowImplicit
{ "allowImplicit": true } 选项的 正确 代码示例
在 Playground 中打开
/*eslint array-callback-return: ["error", { allowImplicit: true }]*/
const undefAllTheThings = myArray.map(function(item) {
return;
});
checkForEach
{ "checkForEach": true } 选项的 错误 代码示例
在 Playground 中打开
/*eslint array-callback-return: ["error", { checkForEach: true }]*/
myArray.forEach(function(item) {
});
myArray.forEach(function(item) {
if (item < 0) {
}
handleItem(item);
});
myArray.forEach(function(item) {
if (item < 0) {
}
handleItem(item);
});
myArray.forEach(item handleItem(item));
myArray.forEach(item void handleItem(item));
myArray.forEach(item => {
});
myArray.forEach(item => {
});
{ "checkForEach": true } 选项的 正确 代码示例
在 Playground 中打开
/*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);
});
allowVoid
{ "allowVoid": true } 选项的 正确 代码示例
在 Playground 中打开
/*eslint array-callback-return: ["error", { checkForEach: true, allowVoid: true }]*/
myArray.forEach(item => void handleItem(item));
myArray.forEach(item => {
return void handleItem(item);
});
myArray.forEach(item => {
if (item < 0) {
return void x;
}
handleItem(item);
});
已知限制
此规则检查具有给定名称的方法的回调函数,即使 具有该方法的对象不是数组。
何时不使用
如果您不想警告在数组方法的回调函数中使用 return 语句,那么禁用此规则是安全的。
版本
此规则在 ESLint v2.0.0-alpha-1 中引入。