Rule proposal: `prefer-else-if`
See original GitHub issueFail
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:
- Created 2 years ago
- Comments:5 (3 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I changed my mind about this, I think this should be a separate rule, maybe called
prefer-else-if
,prefer-switch
has a limitation onminimumCases
,There are only two cases, we should not ask user to use
switch
, butshould still be preferred, because the old one always compares
foo
twice, event value offoo
is1
.After we fix it to
else-if
, if there are many cases,prefer-switch
will report.Sounds good.