array-callback-return
在数组方法的回调中强制执行 return 语句
此规则报告的一些问题可通过编辑器 建议 手动修复
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
规则详情
🌐 Rule Details
此规则强制在数组方法的回调中使用 return 语句。此外,它还可以通过使用 checkForEach 选项来强制 forEach 数组方法回调不返回值。
🌐 This rule enforces usage of return statement in callbacks of array’s methods.
Additionally, it may also enforce the forEach array method callback to not return a value by using the checkForEach option.
此规则查找以下方法的回调函数,然后检查 return 语句的使用情况。
🌐 This rule finds callback functions of the following methods, then checks usage of return statement.
Array.fromArray.fromAsyncArray.prototype.everyArray.prototype.filterArray.prototype.findArray.prototype.findIndexArray.prototype.findLastArray.prototype.findLastIndexArray.prototype.flatMap[Array.prototype.forEach](https://www.ecma-international.org/ecma-262/6.0/#sec-array.prototype.foreach)(可选,根据checkForEach参数)Array.prototype.mapArray.prototype.reduceArray.prototype.reduceRightArray.prototype.someArray.prototype.sortArray.prototype.toSorted- 如果适用,还包括类型化数组之上。
此规则的错误代码示例:
🌐 Examples of incorrect code for this rule:
/*eslint array-callback-return: "error"*/
const indexMap = myArray.reduce(function(memo, item, index) {
memo[item] = index;
}, {});
const foo = Array.from(nodes, function(node) {
if (node.tagName === "DIV") {
return true;
}
});
const bar = foo.filter(function(x) {
if (x) {
return true;
} else {
return;
}
});
符合此规则的正确代码示例:
🌐 Examples of correct code for this rule:
/*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"));
选项
🌐 Options
该规则接受具有三个选项的配置对象:
🌐 This rule accepts a configuration object with three options:
"allowImplicit": false(默认) 当设置为true时,允许需要返回值的方法的回调通过return语句(不包含表达式)隐式返回undefined。"checkForEach": false(默认) 当设置为true时,规则也会报告返回值的forEach回调。"allowVoid": false(默认) 当设置为true时,允许在forEach回调中使用void,因此规则不会使用void运算符报告返回值。
注意: { "allowVoid": true } 仅在 checkForEach 选项设置为 true 时才有效。
allowImplicit
适用于 { "allowImplicit": true } 选项的正确代码示例:
🌐 Examples of correct code for the { "allowImplicit": true } option:
/*eslint array-callback-return: ["error", { allowImplicit: true }]*/
const undefAllTheThings = myArray.map(function(item) {
return;
});
checkForEach
针对 { "checkForEach": true } 选项的错误代码示例:
🌐 Examples of incorrect code for the { "checkForEach": true } option:
/*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(function(item) {
if (item < 0) {
return void x;
}
handleItem(item);
});
myArray.forEach(item => handleItem(item));
myArray.forEach(item => void handleItem(item));
myArray.forEach(item => {
return handleItem(item);
});
myArray.forEach(item => {
return void handleItem(item);
});
适用于 { "checkForEach": true } 选项的正确代码示例:
🌐 Examples of correct code for the { "checkForEach": true } option:
/*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 } 选项的正确代码示例:
🌐 Examples of correct code for the { "allowVoid": true } option:
/*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);
});
已知限制
🌐 Known Limitations
此规则检查具有给定名称的方法的回调函数,即使拥有该方法的对象不是数组。
🌐 This rule checks callback functions of methods with the given names, even if the object which has the method is not an array.
何时不使用
🌐 When Not To Use It
如果你不想警告数组方法回调中使用 return 语句,那么禁用此规则是安全的。
🌐 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.
版本
此规则是在 ESLint v2.0.0-alpha-1 中引入。