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.

Rule proposal: `prefer-else-if`

See original GitHub issue

Fail

if (foo === '1') {
  // ...
}
if (foo === '2') {
  // ...
}

Pass

function a() {
  if (foo === '1') {
    // ...
    return;
  }

  if (foo === '2') {
    // ...
    return;
  }
}
if (foo === '1') {
  // ...
} else if (foo === '2') {
  // ...
}
Original proposal for `prefer-switch`
if (foo === '1') {
  ...
}
if (foo === '2') {
  ...
}
if (foo === '3') {
  ...
}
if (foo === '4') {
  ...
}

To fix this case, we can simply only check if foo is declared as const.

This come from my work, but real case is more complicated, it’s like

if (foo.bar === '1') {
  ...
}
if (foo.bar === '2') {
  ...
}
if (foo.bar === '3') {
  ...
}
if (foo.bar === '4') {
  ...
}

To fix this case, we need to check foo.bar and foo didn’t get changed in each if block, I feel this is hard (even impossible) to do.

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
fiskercommented, Aug 8, 2021

I changed my mind about this, I think this should be a separate rule, maybe called prefer-else-if, prefer-switch has a limitation on minimumCases,

if (foo === '1') {
  ...
}
if (foo === '2') {
  ...
}

There are only two cases, we should not ask user to use switch, but

if (foo === '1') {
  ...
} else if (foo === '2') {
  ...
}

should still be preferred, because the old one always compares foo twice, event value of foo is 1.

After we fix it to else-if, if there are many cases, prefer-switch will report.

0reactions
sindresorhuscommented, Aug 8, 2021

Sounds good.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Proposed rule: Order Competition Rule - SEC.gov
proposed rule would prohibit a restricted competition trading center from internally executing certain orders of individual investors at a ...
Read more >
The SEC's Proposed “Preferential Treatment Rule”
On February 9, 2022, the SEC proposed a litany of new rules relating to private fund advisers (the “Private Fund Adviser Proposal”) that...
Read more >
no-else-after-return - ajafff/tslint-consistent-codestyle · GitHub
Works like no-else-return from eslint. This rule can automatically fix errors. If an if block contains a return statement, the else block becomes...
Read more >
SEC's Proposed Preferential Treatment Rule: Does the Side ...
By Jennifer Kalmanides. On February 9, 2022, the Securities and Exchange Commission (the “SEC”) unveiled a slate of newly proposed rules, ...
Read more >
I added a little to the file naming sect… – Make WordPress Core
... no rule which to prefer. elseif can save you some hassles sometimes.” ... In it, I've proposed s/else if/elseif/ and I plan...
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