array-callback-return
在数组方法的回调中强制执行 return 语句
此规则报告的一些问题可通过编辑器 建议 手动修复
Array 有几种过滤、映射和折叠的方法。如果我们忘记在这些回调中写 return 语句,那可能是一个错误。如果你不想使用 return 或不需要返回的结果,请考虑使用 .forEach 代替。
¥Array has several methods for filtering, mapping, and folding.
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 instead.
// 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.prototype.forEach(可选,基于 参数)¥ Array.prototype.forEach(optional, based oncheckForEachparameter)
- 
及以上类型的数组。 ¥And above of typed arrays. 
此规则的错误代码示例:
¥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时,允许需要返回值的方法的回调隐式返回undefined,其中return语句不包含表达式。¥ "allowImplicit": false(default) When set totrue, allows callbacks of methods that require a return value to implicitly returnundefinedwith areturnstatement containing no expression.
- 
"checkForEach": false(默认)当设置为true时,规则还将报告返回值的forEach回调。¥ "checkForEach": false(default) When set totrue, rule will also reportforEachcallbacks that return a value.
- 
"allowVoid": false(默认)当设置为true时,允许forEach回调中使用void,因此规则不会使用void运算符报告返回值。¥ "allowVoid": false(default) When set totrue, allowsvoidinforEachcallbacks, so rule will not report the return value with avoidoperator.
注意:仅当 checkForEach 选项设置为 true 时,{ "allowVoid": true } 才起作用。
¥Note: { "allowVoid": true } works only if checkForEach option is set to 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 中引入。