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
[no-promise-executor-return]: false positive on arrow function #13668
Comments
👋 hi @ghaiklor-wix! In the original discussion around adding this rule, we explicitly decided not to make an exception for arrow functions, and you can read more about the rationale there. An arrow function is also shown as an example of incorrect code in the rule docs. For those reasons, I'm 👎 to an option that would allow any expression-bodied arrow functions. In most cases, adding curly braces makes the intent more clear, e.g. However, in the original issue, we also contemplated a future enhancement to allow returning function calls as a shortcut. That option would allow your |
Yeah, it looks like that's the case. I'll try to generalize the case... When you have an arrow function without curly braces const someOneLiner = new Promise(resolve => doSomethingElseNoMatterWhatAndNoMatterTheResult(resolve)); So, if you:
then more likely you have a correctly written piece of code without errors |
This brings in two new eslint errors, both of which I decided to disable instead of fixing the source code: * no-promise-executor-return complains about Promise code like this: new Promise( resolve => setTimeout( resolve, 1 ) ) This has been reported upstream as eslint/eslint#13668, but without a solution that I find satisfying; in particular, the suggested fix new Promise( resolve => { setTimeout( resolve, 1 ); } ) runs afoul of max-statements-per-line in our config. Maybe this rule can be reenabled later (I’ve subscribed to the issue). * no-shadow complains about test code like this: browser.executeAsync( async ( varA, varB, done ) => { // ... }, varA, varB ); But here, the inner varA doesn’t really shadow the outer one; rather, it’s the only way for the function to access varA, since it actually runs in a different execution context (in the browser) where the outer variables are not available and have to be passed in explicitly.
I personally prefer just |
After some thinking about it, I'm not in favor of adding options to this rule and other rules that disallow returning values from specific functions. I think we should keep these rules simple and support only "common" explicit code with curly braces and no Allowing any function calls could introduce many false negatives, while the additional check for the usage of I'd maybe only support an option for |
You're right - such an option would significantly complicate the rule for only marginal stylistic benefit. I was neutral before, but now I'm negatively inclined like you. I'll leave the issue open for now to give anyone else on the team time to register a different opinion. |
I was thinking after a while and experimenting with different styles and I agree that P.S. Although, it conflicts with another rule that checks for "only one statement per line". |
Unfortunately, it looks like there wasn't enough interest from the team Thanks for contributing to ESLint and we appreciate your understanding. |
@btmills I'd also like to allow expression arrow functions. In projects that use prettier, I've tended to see this rule just get turned off, because it expands new Promise(resolve => { setTimeout(resolve, 1000) }) into new Promise(resolve => {
setTimeout(resolve, 1000)
}) And taking up three lines for such a simple thing just feels wrong. It's not the hugest deal, but it makes codebases more cluttered, so it gets turned off. I'd like an option. But failing that, what if it's allowed when the return value is explicitly voided? new Promise(resolve => void setTimeout(resolve, 1000)) Would the team be open to a PR doing one of these things? |
What rule do you want to change?
no-promise-executor-return
Does this change cause the rule to produce more or fewer warnings?
Fewer warnings
How will the change be implemented? (New option, new default behavior, etc.)?
New default behaviour that could be changed via an option
Please provide some example code that this change will affect:
It is a common practice to have arrow functions as executors in Promise. Those cases are written that way because they can be one-liners which is to improve the readability.
E.g. writing a delay function as this:
What does the rule currently do for this code?
It reports
Return values from promise executor functions cannot be read
What will the rule do after it's changed?
If it is an arrow function, I believe it makes sense to suppress the warning. Although, there is a possibility to have a code such as:
Are you willing to submit a pull request to implement this change?
Not sure how to do this. It is definitely a false positive, because my intention was not consuming the value at all, but to have a one-liner.
The text was updated successfully, but these errors were encountered: