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.

Switches - disallow fall through

See original GitHub issue

Hey,

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:open
  • Created 3 years ago
  • Reactions:1
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
WickyNilliamscommented, May 20, 2020

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 😃

1reaction
WickyNilliamscommented, May 20, 2020

I just realised I missed an important condition:

  1. all non-empty cases must have a break or a return

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.

Read more comments on GitHub >

github_iconTop 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 >

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