array-callback-return
Enforce return statements in callbacks of array methods
💡 hasSuggestions
Some problems reported by this rule are manually fixable by editor suggestions
Array 有几种过滤、映射和折叠的方法。
如果我们忘记在这些回调中写上 return 语句,那可能是个错误。如果你不想使用返回,或者不需要返回的结果,可以考虑使用 .forEach 代替。
// 示例: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.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- 以及以上类型数组。
此规则的错误示例:
Open in Playground
/*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;
}
});
此规则的正确示例:
Open in Playground
/*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"));
选项
该规则的配置对象有三个选项:
- 当将
"allowImplicit": false(默认值)设置为true时, 允许需要返回值的方法的回调隐含地返回undefined,其return语句不包含表达式。 - 当将
"checkForEach": false(默认值)设置为true时, 规则也将报告返回一个值的forEach回调。 "allowVoid": false(默认值)设置为true时,允许forEach回调中的void,所以规则不会报告返回值带有void操作符。
注意:{ "allowVoid": true } 仅在 checkForEach 选项设置为 true 时可用。
allowImplicit
使用 { "allowImplicit": true } 选项的正确示例:
Open in Playground
/*eslint array-callback-return: ["error", { allowImplicit: true }]*/
var undefAllTheThings = myArray.map(function(item) {
return;
});
checkForEach
使用 { "checkForEach": true } 选项的错误示例:
Open in Playground
/*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 } 选项的正确示例:
Open in 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 } 选项的正确示例:
Open in 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 语句。,禁用此规则毫无风险。
Version
This rule was introduced in ESLint v2.0.0-alpha-1.