Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enforces return statements in callbacks of array's methods (array-callback-return) #859

Closed
feross opened this issue Apr 13, 2017 · 5 comments

Comments

@feross
Copy link
Member

feross commented Apr 13, 2017

ESLint just promoted this rule to their 'eslint:recommended' rule set, so it's probably pretty reliable. I want to check how much of the ecosystem this effects and consider enabling it.

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.

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

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

Array.from
Array.prototype.every
Array.prototype.filter
Array.prototype.find
Array.prototype.findIndex
Array.prototype.map
Array.prototype.reduce
Array.prototype.reduceRight
Array.prototype.some
Array.prototype.sort

https://github.com/eslint/eslint/blob/master/docs/rules/array-callback-return.md

@dcousens
Copy link
Member

Yes.

@dcousens
Copy link
Member

dcousens commented Apr 13, 2017

Though, I have used .some in the past to short-circuit a forEach loop

@feross
Copy link
Member Author

feross commented Apr 13, 2017

@dcousens It's fine to use .some in that way. You're still using return in the function callback right?

@dcousens
Copy link
Member

dcousens commented Apr 14, 2017

If we forget to write return statement in a callback of those, it's probably a mistake.

I misread it as if we forget to write return statement for those, indeed, you are right 👍

@feross
Copy link
Member Author

feross commented Oct 28, 2020

About 7% ecosystem effect:

1..207
# tests 207
# pass  193
# fail  14

Examples of failures that I think are good errors to report:

    const tuples = this.stringTuples().filter((tuple) => {
      if (tuple[0] === protocols.names.ipfs.code) {
        return true
      }
    })
  tuples.map(tup => {
    const proto = protoFromTuple(tup)
    parts.push(proto.name)
    if (tup.length > 1) {
      parts.push(tup[1])
    }
  })

More marginal cases where more explicitness doesn't hurt:

    keys.some(function (key, i) {
      tasks[key](function (err, result) { each(key, err, result) })
      if (i === limit - 1) return true // early return
    })
    input.some(item => {
      if (typeof item === 'string') {
        opts.name = corePath.basename(item)
        return true
      } else if (!item.unknownName) {
        opts.name = item.path[item.path.length - 1]
        return true
      }
    })

@feross feross added this to the standard 16 milestone Oct 28, 2020
feross added a commit that referenced this issue Oct 29, 2020
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 30, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
Archived in project
Development

No branches or pull requests

2 participants