37

I get an ESLint warning:

Expected to return a value art the end of arrow function ( consistent-return)

errors.details.forEach((error) => {
  const errorExists = find(errObj, (item) => {  // <== ESLint warning
    if (item && item.field === error.path && item.location === location) {
      item.messages.push(error.message);
      item.types.push(error.type);
      return item;
    }
  });
  if (!errorExists) {
    errObj.push({
      field: error.path,
      location: error.location,
      messages: [error.message],
      types: [error.type]
    });
  }
});

However if I insert a return

  const errorExists = find(errObj, (item) => {    // <== ESLint warning
    if (item && item.field === error.path && item.location === location) {
      item.messages.push(error.message);
      item.types.push(error.type);
      return item;
    }
    return;   // <== inserted return
  });

Then no more warning on this line , but then I get 2 warnings on the inserted return ...

Arrow function expected a return value (consistently-return) Unnecessary return statement (no-useless-return) I don't see how to solve correctly this issue .. any feedback welcome

1
  • The find callback should always return a boolean value. Neither item nor undefined.
    – Bergi
    Commented May 14, 2017 at 11:08

2 Answers 2

30

http://eslint.org/docs/rules/consistent-return says:

This rule requires return statements to either always or never specify values.

When your if-condition is not met, the arrow function will terminate without encountering a return statement, which violates this rule. Your second version violates this rule because your second return does not specify a value, contrary to the first return. The second warning tells you that your additional return statement is redundant.

To make the linter happy, you probably should think about what to properly return from the arrow-function if the condition is not met. I do not know what your find function does exactly, but if it behaves similar to Array.prototype.find you might want to return false at the end of the arrow function. If you need to return undefined in that case, this paragraph from the same page applies:

When Not To Use It

If you want to allow functions to have different return behavior depending on code branching, then it is safe to disable this rule.

EDIT: I previously wrote to have a look at the option treatUndefinedAsUnspecified, but looks like either setting will not help if you need to return undefined in just one of the branches.

3
  • how do you disable the rule?
    – zero_cool
    Commented Jan 14, 2019 at 22:14
  • 2
    One way is on your .eslintrc.json (or eslint config file), add on its "rules" main property: ..."rules": { "consistent-return": "off""}... // You can set that as "off", "warn" or "error" Commented Feb 25, 2020 at 23:24
  • 4
    The above will disable the rule for everything. To disable for a specific file, at the top of the file put as a comment /* eslint-disable consistent-return */. Disable a line with // eslint-disable-line consistent-return. Disable the next line with // eslint-disable-next-line consistent-return. eslint.org/docs/user-guide/… Commented Nov 12, 2020 at 0:31
24

the return is correctly inserted, however its value should be given ...

return false;

is the correct value