Index

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

规则详情

🌐 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.

此规则的错误代码示例:

🌐 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 中引入。

资源