Enforces return statements in callbacks of array’s methods (array-callback-return)

强制数组方法的回调函数中有 return 语句 (array-callback-return)

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.

Array有一些方法用来过滤、映射和折叠。如果你忘记了在它们的回调函数中写return语句,这种情况可能是个错误。如果不想使用返回或者不需要返回的结果,可以考虑使用 .forEach

// 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

This rule enforces usage of return statement in callbacks of array’s methods.

该规则强制数组方法的回调函数中的return语句的使用。

Rule Details

This rule finds callback functions of the following methods, then checks usage of return statement.

该规则发现以下方法的回调函数,然后检查return语句的使用。

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 has an object option:

该规则有一个对象选项:

Examples of correct code for the { "allowImplicit": true } option:

选项 { "allowImplicit": true }正确 代码示例:

/*eslint array-callback-return: ["error", { allowImplicit: true }]*/
var undefAllTheThings = myArray.map(function(item) {
    return;
});

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

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.

如果你不想收到关于强制数组方法的回调函数中return语句的使用情况的警告,你可以禁用此规则。

Version

This rule was introduced in ESLint 2.0.0-alpha-1.

该规则在 ESLint 2.0.0-alpha-1 中被引入。

Resources