Join GitHub today
GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together.
Sign upNew rule: callback-return #298
Comments
This comment has been minimized.
This comment has been minimized.
mcollina
commented
Oct 20, 2015
|
I think this is a good rule 95% of the time, so |
This comment has been minimized.
This comment has been minimized.
|
+1 here, seems like it catches errors before they get a chance to bite you. |
This comment has been minimized.
This comment has been minimized.
|
This has caught me out so many times, I'd love it to be caught in standard. |
dcousens
added
the
feature request
label
Oct 20, 2015
This comment has been minimized.
This comment has been minimized.
|
Cool I should mention that this is the setting that I tried: |
This comment has been minimized.
This comment has been minimized.
LGTM. |
This comment has been minimized.
This comment has been minimized.
|
Promises is another very common use case for this thought, which is why I suggested adding e.g. function readFile (path) {
return new Promise(function (resolve, reject) {
fs.readFile(path, function (err, result) {
if (err) reject(err) // <--- Notice missing return
resolve(result)
})
})
} |
This comment has been minimized.
This comment has been minimized.
sindresorhus
commented
Oct 20, 2015
|
Be aware that this rule is pretty fragile atm. I had to disable it in XO because of too many false positives. |
This comment has been minimized.
This comment has been minimized.
|
This could probably be revisited now that eslint/eslint#3559 is merged :) |
This comment has been minimized.
This comment has been minimized.
anyone else want to voice in on what other names can be used? I personally use |
This comment has been minimized.
This comment has been minimized.
|
Just ran the tests with eslint 1.10.3 and this rule:
All tests pass. This rule finally looks mature enough to use. Out of an abundance of caution, let's add it in v6. |
feross
added
the
v6
label
Dec 25, 2015
This comment has been minimized.
This comment has been minimized.
mcollina
commented
Dec 25, 2015
|
I agree.
|
This comment has been minimized.
This comment has been minimized.
|
I like all the names discussed here. |
This comment has been minimized.
This comment has been minimized.
|
rad! v6 sounds good (': |
This comment has been minimized.
This comment has been minimized.
|
Could we add |
This comment has been minimized.
This comment has been minimized.
|
@feross Do you want a pull request for this? I would very much add |
feross
removed
the
v6
label
Feb 4, 2016
This comment has been minimized.
This comment has been minimized.
|
Actually, I take back my earlier comment about the tests passing. I must have ran the tests without the rule actually enabled. Here's the latest results using the new thorough test suite for standard v6 that tests the 400 most popular packages on npm that use standard:
Full output: https://gist.github.com/feross/94b846d2150875bc7ecf Unfortunately, I don't think this rule is reliable enough to enable by default for everyone. I like what it's trying to do, but we can't burden every standard user with understanding this rule's quirks. When it works better, I'm looking forward to enabling it. |
LinusU commentedOct 20, 2015
Suggesting that we include the rule callback-return
Currently all but one of our repos passes this test.
I have looked at the code and it's an easy fix, I could submit a pull request over there.
Enforce Return After Callback (callback-return)
The callback pattern is at the heart of most I/O and event-driven programming
in JavaScript.
To prevent calling the callback multiple times it is important to
returnanytime the callback is triggered outsideof the main function body. Neglecting this technique often leads to issues where you do something more than once.
For example, in the case of an HTTP request, you may try to send HTTP headers more than once leading node.js to
throwa
Can't render headers after they are sent to the client.error.Rule Details
This rule is aimed at ensuring that callbacks used outside of the main function block are always part-of or immediately
preceding a
returnstatement. This rules decides what is a callback based on the name of the function being called.By default the rule treats
cb,callback, andnextas callbacks.The following patterns are considered problems:
The following patterns are not considered problems:
Options
The rule takes a single option, which is an array of possible callback names.
Gotchas
There are several cases of bad behavior that this rule will not catch and even a few cases where
the rule will warn even though you are handling your callbacks correctly. Most of these issues arise
in areas where it is difficult to understand the meaning of the code through static analysis.
Passing the Callback by Reference
Here is a case where we pass the callback to the
setTimeoutfunction. Our rule does not detect this pattern, butit is likely a mistake.
Triggering the Callback within a Nested Function
If you are calling the callback from within a nested function or an immediately invoked
function expression, we won't be able to detect that you're calling the callback and so
we won't warn.
If/Else Statements
Here is a case where you're doing the right thing in making sure to only
callback()once, but because of thedifficulty in determining what you're doing, this rule does not allow for this pattern.
When Not To Use It
There are some cases where you might want to call a callback function more than once. In those cases this rule
may lead to incorrect behavior. In those cases you may want to reserve a special name for those callbacks and
not include that in the list of callbacks that trigger warnings.
Further Reading
Related Rules
I found the rule while comparing which rules
xohas versusstandardhere: #216 (comment)