-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
[no-fallthrough] Lint rule needs type information to avoid false positives e.g. nested switch #3455
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Another example of code with false positive switch(x) {
case 0:
process.exit(0);
// putting `break` here silents no-fallthrough, but causes unreachable code warning from ts
default:
// ...
} |
Wanted to make new rule proposal but then found this. So core Such code with core `no-fallthrough enabled requires unneeded break: const fn = (x: "A" | "B", n: "Err" | "Ok"): number => {
switch (x) {
case "A":
switch (n) {
case "Ok":
return 1
case "Err":
return 0
}
break; // this break actually not needed with typescript
default:
return -1
}
} TS config actually has the option |
Had this problem today with a nested switch. The formatter removes the unnecessary |
The difficulty with building this out will be the interaction between both types and control-flow analysis. TS does have |
I don't think fixing this by changing typescript-eslint to allow the missing break statements is a good idea. This just introduces a footgun for code without 100% accurate typing, incorrect/missing data validation etc because we have no way of knowing for sure if the switch case statements will actually be exhaustive at runtime. The safer and imo better fix here would be for Typescripts compile time error to allow for break statements in supposedly unreachable code somehow. |
Honestly not a crazy use of a TS error suppression switch (foo) {
case bar:
neverReturningFunction()
// @ts-expect-error -- ensure this branch is syntactically guaranteed to terminate
break;
} |
Repro
Expected Result
No lint error
Actual Result
Lint error (see comment)
Additional Info
I think the TS ESLint plugin needs to provide its own version of
no-fallthrough
in order to take advantage of type information so we can avoid false positives such as this.Versions
@typescript-eslint/eslint-plugin
@typescript-eslint/parser
TypeScript
ESLint
node
The text was updated successfully, but these errors were encountered: