array-callback-return

在数组方法的回调中强制执行 return 语句

💡 hasSuggestions

此规则报告的一些问题可通过编辑器建议手动修复

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}
var 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"*/

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;
    }
});

此规则的正确代码示例:

¥Examples of correct code for this rule:

在线运行
/*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"));

选项

¥Options

该规则接受具有三个选项的配置对象:

¥This rule accepts a configuration object with three options:

  • "allowImplicit": false(默认)当设置为 true 时,允许需要返回值的方法的回调隐式返回 undefined,其中 return 语句不包含表达式。

    ¥"allowImplicit": false (default) When set to true, allows callbacks of methods that require a return value to implicitly return undefined with a return statement containing no expression.

  • "checkForEach": false(默认)当设置为 true 时,规则还将报告返回值的 forEach 回调。

    ¥"checkForEach": false (default) When set to true, rule will also report forEach callbacks that return a value.

  • "allowVoid": false(默认)当设置为 true 时,允许 forEach 回调中使用 void,因此规则不会使用 void 运算符报告返回值。

    ¥"allowVoid": false (default) When set to true, allows void in forEach callbacks, so rule will not report the return value with a void operator.

注意:仅当 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 }]*/
var 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 中引入。

资源

ESLint 中文网
粤ICP备13048890号