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
[callback-return] only accepts callbacks that are by themselves or prefixed with a return #13064
Comments
This does indeed look like a bug. Verified with the demo link above. |
@G-Rath mind if I take this ? here is the fix which I think is better fit @kaicataldo if the callback is assigned to some variable without the call like this function fn(err, cb){
var cb2 = cb
} now, it should check and add cause, function fn(err, cb){
var cb2 = cb
if (err) {
cb2(err); // throw error here
}
cb2();
} right ? |
I think the bug here is what @G-Rath describes above. We'll need to figure out the best heuristic to catch this. I think we'll want to make this check more robust. |
I think, to fix this issue we need to check where that callExpression (getting check from Let me know if I am wrong about it !
Even I don't want to make this rule this much complicated to analyze the variable like how it's being used. (not in the scope of this rule ) !
I can submit a PR for this and we can discuss with the implementation once getting clarification from @G-Rath about this |
I don't think this will work, because the following wouldn't be caught: function fn(val, err, cb) {
let value = val;
if (err) {
value = cb(err);
}
console.log(value);
cb(val);
} This is a really contrived example, but I think it should still be caught. Feels to me like we should be using code path analysis to ensure that anytime the callback is being called that it cannot be called again. |
yes, this seems like a not complete fix. I will try to implement something else then . ! |
If I understand this correctly, it can't be called more than once for a single block right ? function(cb){
cb()
...
cb() // not allowed
} But it should be allowed for this function(cb){
if(foo) cb()
cb(foo) // no error
} right ? |
function(cb){
if(foo) cb()
cb(foo) // no error
} This should be an error. From the rule's docs:
The intention of the rule is to prevent a callback from being called multiple times, and in the example above, it would be called twice if |
Sorry for the late response:
Go for gold! |
OH , sorry I forgot to add return before the cb. I meant this function(cb){
if(foo) return cb()
return cb(foo) // no error
} |
@G-Rath I anticipated this rule a bit, I guess it would take time for me to fix it. As my solution is not covering all cases If anyone else can submit before that. Go for it! |
is it a Node.js specific rule? it might be better to be in refs: #12898 |
I dont think its node-specific, as callbacks are pretty common in the browser with a maximum browser supporting it 👍 |
This is an interesting point. I've definitely encountered this pattern is Node-land more than in the browser. |
@kaicataldo I think this rule should not throw error for those callbacks which are in the function body irrespective of how many times they are being called. the following should be lint free (they are currently error in master) function a(err) { callback (err); callback(); }
var a = (err) => { callback (err); callback(); } And the following example should be of if (x) { callback(); } Cause as per rule's docs
If callback is in the function body ( WDYT ? |
@anikethsaha I respectfully disagree. The practical purpose of the rule is to prevent callbacks from being invoked multiple times. From the docs (emphasis my own):
If the goal is prevent callbacks from being invoked multiple times, the following should be disallowed since it will always invoke the callback twice. function a(err) { callback (err); callback(); }
var a = (err) => { callback (err); callback(); } I actually would like to explore @aladdin-add's point about this being a pattern that's specifically used in Node's core modules and more often in Node-land than in the browser. @mysticatea What are your thoughts on this? Would it make sense to add this to |
I'm clearly a bit scattered, because this is one of the rules we just ported over to |
Unfortunately, it looks like there wasn't enough interest from the team Thanks for contributing to ESLint and we appreciate your understanding. |
callback-return
only accepts callbacks that are by themselves on a line, or prefixed withreturn
.Tell us about your environment
What parser (default, Babel-ESLint, etc.) are you using?
@typescript-eslint/parser
Please show your full configuration:
Configuration
What did you do? Please include the actual source code causing the issue, as well as the command that you used to run ESLint.
eslint . --ext ts
What did you expect to happen?
callback
not to be flagged bycallback-return
.What actually happened? Please include the actual, raw output from ESLint.
callback
is flagged bycallback-return
:Are you willing to submit a pull request to fix this bug?
Maybe - looking at the source code & documentation, it might be tough to implement a sound fix.
Demo reproduction.
The text was updated successfully, but these errors were encountered: