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.

Bug: `no-else-after-return` does not allow `else if`'s by default like ESLint's `no-else-return` does

See original GitHub issue

Issue type

Bug

Version

v1.10.0

Overview

The no-else-after-return rule seems to, by default, be issuing warnings when else if’s are used after return statements.

The documentation for no-else-after-return states that it “Works like no-else-return from eslint”, and (at least for the current version of ESLint) the default behaviour of no-else-return allows return statements after else if’s.

Current behaviour

When no-else-after-return: true is specified, the following code causes an unnecessary else after return warning to appear for me:

const val = Math.random();

if (val > 0.5) {
  return 1;
} else if (val < 0.5) {
  return -1;
}

return 0;

Expected behaviour

When no-else-after-return: true is specified, the above code should not issue a warning.

If no-else-after-return: [true, { "allowElseIf": false }] was specified, then the above code should issue a warning.

Additional context

I use this rule as part of tslint-config-airbnb, which added no-else-after-return to their ruleset in v5.4.1.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:5
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

4reactions
ajafffcommented, Mar 27, 2018

I don’t understand why you don’t want to use no-unnecessary-else, but that’s none of my business.

I actually wanted to deprecate and eventually remove no-else-after-return because it contained a lot of duplicated and slightly modified code to handle only return statements as control flow end. The rule also had quite a few bugs that never got fixed. Fortunately I have refactored my control flow analysis and can reuse it for this rule. There’s no more duplicate code and correct control flow analysis.

TL:DR

I’m going to update the rule and also add the requested option.

But there’s one thing I don’t understand: ESLint’s docs state that the following code is invalid even if allowElseIf is enabled. Why?

function foo() {
    if (x) {
        return y;
    } else if (z) {
        return w;
    } else {
        return t;
    }
}
0reactions
ajafffcommented, Mar 28, 2018

The new option is published in v1.13.0

Read more comments on GitHub >

github_iconTop Results From Across the Web

bash - Is it a sane approach to "back up" the $IFS variable?
In my experience, it's best to set IFS manually to the default for your shell interpreter, namely $' \t\n' if you're using bash....
Read more >
When setting IFS to split on newlines, why is it necessary to ...
I'm on a Linux system that is saving files with Unix line endings. I've converted my file with newlines to hex and it...
Read more >
IFS - Greg's Wiki
The IFS variable is used in shells (Bourne, POSIX, ksh, bash) as the input field separator (or internal field separator). · The default...
Read more >
Bash IFS – What is the Internal Field Separator? | Delightly Linux
This is an environment variable provided by bash to delimit words, and by default, it is set to whitespace. Space; Tab; Carriage return/newline....
Read more >
The Meaning of IFS in Bash Scripting | Baeldung on Linux
However, this behavior is not observed when custom values are set in the IFS variable. Let's verify the default IFS behavior with an...
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