Skip to content
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-else-return : else if #3015

Closed
bathos opened this issue Jul 15, 2015 · 3 comments
Closed

no-else-return : else if #3015

bathos opened this issue Jul 15, 2015 · 3 comments

Comments

@bathos
Copy link

@bathos bathos commented Jul 15, 2015

With no-else-return activated:

if (x) {
  doOneThing();
} else if (y) {
  return true;
} else {
  doAnotherThing();
}

The final else is considered a violation -- which makes technical sense given the implementation, but seems contrary to the intent of the rule (to eliminate superfluous else statements). The final else block is meaningful here despite the preceding "returning" if because it's also still an alternative of the first case, which did not return. Handling that distinction might be more trouble than it's worth, since grammatically, "else if" is not a concept, it's just "else statement" where the statement happens to be an "if", but I figured I'd ask just in case it's a viable refinement after all.


For anyone else running into this who'd like to keep the rule on -- this is how the no-else-return rule would prefer to see that logic expressed:

if (x) {
  doOneThing();
} else {
  if (y) return true;
  doAnotherThing();
}

It's not that bad, just a bit unnatural when compared to the standard else-if pattern.

@nzakas
Copy link
Member

@nzakas nzakas commented Jul 16, 2015

Yeah, looks like a bug.

@nzakas nzakas closed this in b9b592e Jul 18, 2015
nzakas added a commit that referenced this issue Jul 18, 2015
Fix: no-else-return handles multiple else-if blocks (fixes #3015)
@wingedearth
Copy link

@wingedearth wingedearth commented Aug 3, 2016

What about when you have just an if and else? Like:
if (x) { return x; } else { throw new Error('no x'); }

I can't reverse the statement with !x because the linter reports no-negated-condition violation.

@platinumazure
Copy link
Member

@platinumazure platinumazure commented Aug 3, 2016

Simply write it like this:

if (x) {
    return;
}

throw new Error();

If you have any other questions, please stop by our chat.

@eslint-deprecated eslint-deprecated bot locked and limited conversation to collaborators Feb 7, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
5 participants