Option to permit no-useless-return inside blocks
See original GitHub issueAs discussed in https://github.com/airbnb/javascript/issues/1210, it will be great if no-useless-return
offers an option to allow return
inside blocks, to benefit consistency between blocks when using a useless return
to quit the function execution at that point.
Here is an example:
async function f(p) {
if (p === 'a') {
// some lines of code
await fn()
return
}
if (p === 'b') {
// some lines of code
await fn()
return
}
if (p === 'c') {
// some lines of code
await fn()
return // <-- option to allow tihs one
}
}
Having them separated by blank lines make it clearer and easy to maintain instead of having a big if-else if-else
block.
Having this last useless return enforces consistency with other blocks and also, if you add a new block below, you will not have to modify the (until now) last one block to add a return
into it.
Issue Analytics
- State:
- Created 7 years ago
- Comments:8 (6 by maintainers)
Top Results From Across the Web
no-useless-return breaks consistency · Issue #1210 - GitHub
On the one hand, I think no-useless-return could perhaps benefit from an option that allows a useless return inside a conditional block.
Read more >no-useless-return - ESLint - Pluggable JavaScript Linter
A return; statement with nothing after it is redundant, and has no effect on the runtime behavior of a function. This can be...
Read more >eslint-config-openstack/.eslintrc at master - OpenDev
Re-allow return, throw, break, and continue statements inside finally blocks. # http://eslint.org/docs/rules/no-unsafe-finally. no-unsafe-finally: 0.
Read more >Disallow redundant return statements (no-useless-return)
A return; statement with nothing after it is redundant, and has no effect on the runtime behavior of a function. This can be...
Read more >Documentation: 15: DECLARE - PostgreSQL
The BINARY option specifies that the cursor should return data in binary format. ... Thus, DECLARE without WITH HOLD is useless outside a...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@felixsanz We appreciate the suggestion regardless - thanks for contributing!
I don’t really see the problem myself, but you could resolve that by adding linebreaks if necessary:
But if you forget to add an explicit
return
statement in any of the blocks, your code will silently do the wrong thing. Personally, I think having multipleif
blocks withreturn
statements in each block is much harder to read than just usingif/else
directly, because to look at that code, I have to scan through each individualif
statement to check if it usesreturn
, and then I have to wonder what happens if none of the conditions match (it seems like nothing happens in your example, but it would be unclear whether this behavior is intentional).