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

Incorrect reporting of `no-callback-literal` #809

Closed
tunnckoCore opened this issue Mar 3, 2017 · 8 comments

Comments

@tunnckoCore
Copy link

commented Mar 3, 2017

I have a iterator function, that is called recursively. Something like

function iterator (foo, bar, baz) {
  return function next (index) {
    // ... code
    // that `next` function is named intentionally
    // because it is called recursively when
    // some condition.
    if (foo === 123) {
      next(index + 1)
    }
  }
}

If that's not so clear enough, see what i mean here each-promise/utils.js#L44-L83

Basically this next function in parallel flow is called for each item in array, passing the index of the item next(index). And in serial flow, that next function is just called recursively.

And it reports that the literal passed as first argument to next is not valid. Easiest fix for me is to just rename that function to, say, nextStep... but yea. I believe the implementation in eslint-plugin-standard can be done better, to report only if there is argument called next, cb or callback.

It is just the more correct approach.

For me, it should report that warning only if the case is such like that

function iterator (foo, bar, baz) {
  return function someAsyncFn (index, cb) {
    // ... code
    if (foo === 123) {
      cb(index + 1)
    }
  }
}

only when there is last argument named next/cb/callback and is passed with wrong literal.

@feross

This comment has been minimized.

Copy link
Member

commented Mar 3, 2017

@xjamundx Thoughts?

Maybe we should only check cb and callback?

@tunnckoCore

This comment has been minimized.

Copy link
Author

commented Mar 3, 2017

Maybe we should only check cb and callback?

They are good, even can add done? Actually I have package called common-callback-names, but it is for different purpose - used in another lib that checks if there is last argument called like that names. Notice that some of them are with trailing _, because there is some internal node core methods that use such argument names - for example fs.readFile, fs.appendFile, fs.stat and etc.

Here we don't need this, but done is pretty used too.

edit: also is-async-function may be in help in the impl. actually it can't ;d

@juliangruber

This comment has been minimized.

Copy link

commented Mar 3, 2017

also discovered this in https://github.com/juliangruber/hyperdrive-import-files, in a setting like this:

function next (mode) {
  // 
}

next('created')

Using common callback names sounds good to me, or maybe checking if the value of the last argument is ever called as a function.

@xjamundx

This comment has been minimized.

Copy link

commented Mar 3, 2017

done is tricky, because grunt uses it with a status code, but we could try it. i'm pretty ok with just cb and callback though as that should grab the majority of use cases.

@tunnckoCore

This comment has been minimized.

Copy link
Author

commented Mar 3, 2017

yea it can stay as it is currently, but the main point is to detect and warn only if there is argument with such name and it is called incorrectlly

@dcousens

This comment has been minimized.

Copy link
Member

commented Mar 4, 2017

Shouldn't the callback detection be limited to a function argument?

@tunnckoCore

This comment has been minimized.

Copy link
Author

commented Mar 4, 2017

@dcousens absolutely.

@feross feross added this to the standard v10 milestone Mar 6, 2017

@feross feross added the bug label Mar 6, 2017

@feross

This comment has been minimized.

Copy link
Member

commented Apr 5, 2017

I don't have the bandwidth to change this rule to only detect callbacks that are passed in as function arguments before getting out the 10.0.0 release.

I'm going to remove next from the list of possible callback function names, since it's more likely to be used in other contexts like iterators, recursive functions, etc. We'll just ship with callback and cb for now and see how it goes.

@feross feross closed this Apr 5, 2017

@lock lock bot locked as resolved and limited conversation to collaborators May 10, 2018

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
5 participants
You can’t perform that action at this time.