Switches - disallow fall through
See original GitHub issueHey,
I’m not sure if this kind of things is outside of the scope of the language (is it “only” supposed to add types to JS, or is it an opportunity to improve other areas too?), but I thought I would suggest.
First, I love that you force exhaustive switches! Something that feels sorely lacking in TS.
I am wondering if you could also error if a case
doesn’t contain a break/return statement. This is a common source of errors in switch statements, and the fall-through behaviour is generally considered a bad design decision in JS. Disallowing fall through will prevent bugs, especially as it’s likely switch
will see more common use if it’s used regularly for type exhaustion.
Issue Analytics
- State:
- Created 3 years ago
- Reactions:1
- Comments:6 (3 by maintainers)
Top Results From Across the Web
no-switch-case-fall-through - Rule
Rule: no-switch-case-fall-through. Disallows falling through case statements. For example, the following is not allowed: switch(foo) { case 1: ...
Read more >Switch statement fall-through...should it be allowed? [closed]
Fall-through should be used only when it is used as a jump table into a block of code. If ...
Read more >7.5 — Switch fallthrough and scoping - Learn C++
Once the statements underneath a case or default label have started executing, they will overflow (fallthrough) into subsequent cases. Break or ...
Read more >Fallthrough in C++ - GeeksforGeeks
To avoid Fall through, the idea is to use a break statement after each and every case so that after matching it goes...
Read more >Java fall through switch statements - Tutorialspoint
When the variable being switched on is equal to a case, the statements following that case will execute until a break statement is...
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
Of course it is for your consideration. It’s just a suggestion, but one which I think corrects a bad design decision in JS and will certainly prevent bugs. I made this exact mistake today 😃
I just realised I missed an important condition:
case
s must have abreak
or areturn
Would this be considered?
It is almost always a bug to not have a
return
/break
in cases with a body. eslint has this on by default in its recommended configuration, so people are used to tools telling them this. C# has this exact rule, so there’s precedent in other languages.