question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Enhancement of 'no-else-return' rule

See original GitHub issue

What rule do you want to change? no-else-return

Does this change cause the rule to produce more or fewer warnings? more

How will the change be implemented? (New option, new default behavior, etc.)? adding some options / extension to this rule.

Please provide some example code that this change will affect:

if(something) {
   throw new Error();
} else {
  return true;
}
---
if(something) {
   throw new Error();
}
return true;
if(something) {
  return 1;
} else if(sthMore) {
  return 2;
}
---
if(something) {
  return 1;
}
if(sthMore) {
  return 2;
}
if(something) {
  return 1;
} else {
  throw new Error();
}
---
if(something) {
  return 1;
}
throw new Error();

What does the rule currently do for this code? it does sometimes work for the last case. it doesn’t change if else statements.

What will the rule do after it’s changed? adds support for Errors - they ‘work quite similar to returns’ adds support for if else

Are you willing to submit a pull request to implement this change? unfortunatelly don’t have time. Could help with some information

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
anikethsahacommented, Sep 15, 2020

hmmm it seems it doesn’t work in some cases, or with Typescript itself

We dont support typescript in core rules. Please use typescript-eslint for config and plugins

function h(){
if(something) {
  throw new Error();
} else if(sthMore) {
  return 2;
}
}

it seems to not be working

because this rule is not about throw statements. as per the docs

If an if block contains a return statement, the else block becomes unnecessary. Its contents can be placed outside of the block.

So even if there is an interest to support throw, IMO it has to be in a separate new rule.

0reactions
eslint-deprecated[bot]commented, Oct 31, 2020

Unfortunately, it looks like there wasn’t enough interest from the team or community to implement this change. While we wish we’d be able to accommodate everyone’s requests, we do need to prioritize. We’ve found that issues failing to reach accepted status after 21 days tend to never be accepted, and as such, we close those issues. This doesn’t mean the idea isn’t interesting or useful, just that it’s not something the team can commit to.

Thanks for contributing to ESLint and we appreciate your understanding.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Rule Change: no-else-return should catch break / continue ...
What will the rule do after it's changed? should propose to remove unnecessary else because: break statement terminates the current loop ...
Read more >
javascript - Unnecessary 'else' after 'return'. (No-else-return)
What that is basically saying is that the else part of the if statement is unnecessary if there is a return in the...
Read more >
no-else-return - Pluggable JavaScript Linter
This rule is aimed at highlighting an unnecessary block of code following an if containing a return statement. As such, it will warn...
Read more >
No Else Return | SamanthaMing.com
I'm going to throw a curve ball and introduce the guard pattern. The guard clause is a block of code that is at...
Read more >
tslint-eslint-rules
You want to code in TypeScript but miss all the rules available in ESLint? Now you can combine both worlds by using this...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found