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

Comments

Projects
None yet
5 participants
@bathos
Copy link

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.

@ilyavolodin ilyavolodin added the triage label Jul 15, 2015

@nzakas

This comment has been minimized.

Copy link
Member

nzakas commented Jul 16, 2015

Yeah, looks like a bug.

@nzakas nzakas added bug rule accepted and removed triage labels Jul 16, 2015

@nzakas nzakas closed this in b9b592e Jul 18, 2015

nzakas added a commit that referenced this issue Jul 18, 2015

Merge pull request #3047 from jrvidal/no-else-return-3015
Fix: no-else-return handles multiple else-if blocks (fixes #3015)
@wingedearth

This comment has been minimized.

Copy link

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

This comment has been minimized.

Copy link
Member

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 eslint bot locked and limited conversation to collaborators Feb 7, 2018

@eslint eslint bot added the archived due to age label Feb 7, 2018

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.