A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://eslint.org/docs/rules/array-callback-return below:

array-callback-return - ESLint - Pluggable JavaScript Linter

array-callback-return

Enforce return statements in callbacks of array methods

💡 hasSuggestions

Some problems reported by this rule are manually fixable by editor suggestions

Table of Contents

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.


const indexMap = myArray.reduce(function(memo, item, index) {
  memo[item] = index;
}, {}); 

1
2
3
4

Rule Details

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.

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

Examples of incorrect code for this rule:

Open in Playground


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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

Examples of correct code for this rule:

Open in Playground


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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

Options

This rule accepts a configuration object with three options:

Note: { "allowVoid": true } works only if checkForEach option is set to true.

allowImplicit

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

Open in Playground

const undefAllTheThings = myArray.map(function(item) {
    return;
});

1
2
3
4

checkForEach

Examples of incorrect code for the { "checkForEach": true } option:

Open in Playground


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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31

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

Open in Playground


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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

allowVoid

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

Open in Playground


myArray.forEach(item => void handleItem(item));

myArray.forEach(item => {
    return void handleItem(item);
});

myArray.forEach(item => {
    if (item < 0) {
        return void x;
    }
    handleItem(item);
});

1
2
3
4
5
6
7
8
9
10
11
12
13
14

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.

Version

This rule was introduced in ESLint v2.0.0-alpha-1.

Resources

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4