-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
fix(eslint-plugin): [switch-exhaustiveness-check] enum members with new line or single quotes are not being fixed correctly #7806
Changes from 1 commit
e2b3789
dd729d1
fd21aa4
1acfe46
17c9ef7
c63f866
82d5b37
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -1,15 +1,15 @@ | ||||||
--- | ||||||
description: 'Require switch-case statements to be exhaustive with union type.' | ||||||
description: 'Require switch-case statements to be exhaustive with union type or enum.' | ||||||
--- | ||||||
|
||||||
> 🛑 This file is source code, not the primary documentation location! 🛑 | ||||||
> | ||||||
> See **https://typescript-eslint.io/rules/switch-exhaustiveness-check** for documentation. | ||||||
|
||||||
When working with union types in TypeScript, it's common to want to write a `switch` statement intended to contain a `case` for each constituent (possible type in the union). | ||||||
However, if the union type changes, it's easy to forget to modify the cases to account for any new types. | ||||||
When working with union types or enums in TypeScript, it's common to want to write a `switch` statement intended to contain a `case` for each constituent (possible type in the union or the enum). | ||||||
However, if the union type or the enum changes, it's easy to forget to modify the cases to account for any new types. | ||||||
|
||||||
This rule reports when a `switch` statement over a value typed as a union of literals is missing a case for any of those literal types and does not have a `default` clause. | ||||||
This rule reports when a `switch` statement over a value typed as a union of literals or as an enum is missing a case for any of those literal types and does not have a `default` clause. | ||||||
|
||||||
## Examples | ||||||
|
||||||
|
@@ -101,6 +101,74 @@ switch (day) { | |||||
} | ||||||
``` | ||||||
|
||||||
<!--tabs--> | ||||||
|
||||||
### ❌ Incorrect | ||||||
|
||||||
Comment on lines
+112
to
+115
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it's probably not displayed correctly, would be happy to get some help here regarding how we should display this (maybe add a subtitle?): There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I'd also request a brief sentence before each of the examples explaining what they're for. It's kind of hard to piece together just from the code. Vague starting proposal (just off the top of my head, not attached to this):
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yup |
||||||
```ts | ||||||
enum Fruit { | ||||||
Apple, | ||||||
Banana, | ||||||
Cherry, | ||||||
} | ||||||
|
||||||
const fruit = Fruit.Cherry as Fruit; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [Style] This is a little closer to the intent:
Suggested change
(this and elsewhere) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yup, I actually wanted to do that, but followed the existing code |
||||||
|
||||||
switch (fruit) { | ||||||
case Fruit.Apple: | ||||||
console.log('an apple'); | ||||||
break; | ||||||
} | ||||||
``` | ||||||
|
||||||
### ✅ Correct | ||||||
|
||||||
```ts | ||||||
enum Fruit { | ||||||
Apple, | ||||||
Banana, | ||||||
Cherry, | ||||||
} | ||||||
|
||||||
const fruit = Fruit.Cherry as Fruit; | ||||||
|
||||||
switch (fruit) { | ||||||
case Fruit.Apple: | ||||||
console.log('an apple'); | ||||||
break; | ||||||
|
||||||
case Fruit.Banana: | ||||||
console.log('a banana'); | ||||||
break; | ||||||
|
||||||
case Fruit.Cherry: | ||||||
console.log('a cherry'); | ||||||
break; | ||||||
} | ||||||
``` | ||||||
|
||||||
### ✅ Correct | ||||||
|
||||||
```ts | ||||||
enum Fruit { | ||||||
Apple, | ||||||
Banana, | ||||||
Cherry, | ||||||
} | ||||||
|
||||||
const fruit = Fruit.Cherry as Fruit; | ||||||
|
||||||
switch (fruit) { | ||||||
case Fruit.Apple: | ||||||
console.log('an apple'); | ||||||
break; | ||||||
|
||||||
default: | ||||||
console.log('a fruit'); | ||||||
break; | ||||||
} | ||||||
``` | ||||||
|
||||||
## When Not To Use It | ||||||
|
||||||
If you don't frequently `switch` over union types with many parts, or intentionally wish to leave out some parts. | ||||||
If you don't frequently `switch` over union types or enums with many parts, or intentionally wish to leave out some parts. |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -17,7 +17,7 @@ export default createRule({ | |||||
type: 'suggestion', | ||||||
docs: { | ||||||
description: | ||||||
'Require switch-case statements to be exhaustive with union type', | ||||||
'Require switch-case statements to be exhaustive with union type or enum', | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (same existing typo-ish)
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||||||
requiresTypeChecking: true, | ||||||
}, | ||||||
hasSuggestions: true, | ||||||
|
@@ -66,17 +66,26 @@ export default createRule({ | |||||
} | ||||||
|
||||||
const missingBranchName = missingBranchType.getSymbol()?.escapedName; | ||||||
const requiresBackticks = missingBranchName?.match(/[\r\n]/g); | ||||||
let caseTest = checker.typeToString(missingBranchType); | ||||||
|
||||||
if ( | ||||||
symbolName && | ||||||
(missingBranchName || missingBranchName === '') && | ||||||
requiresQuoting(missingBranchName.toString(), compilerOptions.target) | ||||||
) { | ||||||
caseTest = `${symbolName}['${missingBranchName}']`; | ||||||
caseTest = requiresBackticks | ||||||
? `${symbolName}[\`${missingBranchName}\`]` | ||||||
: `${symbolName}['${missingBranchName}']`; | ||||||
} | ||||||
|
||||||
const errorMessage = `Not implemented yet: ${caseTest} case`; | ||||||
// escape single quotes and newlines, so that the error message is a readable and valid code. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm gonna learn English here more than ASTs |
||||||
const escapedCaseTest = caseTest | ||||||
.replace(/'/g, "\\'") | ||||||
.replace(/\n/g, '\\n') | ||||||
.replace(/\r/g, '\\r'); | ||||||
|
||||||
const errorMessage = `Not implemented yet: ${escapedCaseTest} case`; | ||||||
|
||||||
missingCases.push( | ||||||
`case ${caseTest}: { throw new Error('${errorMessage}') }`, | ||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -518,7 +518,7 @@ export enum Enum { | |
|
||
function test(arg: Enum): string { | ||
switch (arg) { | ||
case Enum['test-test']: { throw new Error('Not implemented yet: Enum['test-test'] case') } | ||
case Enum['test-test']: { throw new Error('Not implemented yet: Enum[\\'test-test\\'] case') } | ||
StyleShit marked this conversation as resolved.
Show resolved
Hide resolved
|
||
case Enum.test: { throw new Error('Not implemented yet: Enum.test case') } | ||
} | ||
} | ||
|
@@ -555,7 +555,7 @@ export enum Enum { | |
|
||
function test(arg: Enum): string { | ||
switch (arg) { | ||
case Enum['']: { throw new Error('Not implemented yet: Enum[''] case') } | ||
case Enum['']: { throw new Error('Not implemented yet: Enum[\\'\\'] case') } | ||
case Enum.test: { throw new Error('Not implemented yet: Enum.test case') } | ||
} | ||
} | ||
|
@@ -592,7 +592,7 @@ export enum Enum { | |
|
||
function test(arg: Enum): string { | ||
switch (arg) { | ||
case Enum['9test']: { throw new Error('Not implemented yet: Enum['9test'] case') } | ||
case Enum['9test']: { throw new Error('Not implemented yet: Enum[\\'9test\\'] case') } | ||
case Enum.test: { throw new Error('Not implemented yet: Enum.test case') } | ||
} | ||
} | ||
|
@@ -602,5 +602,47 @@ function test(arg: Enum): string { | |
}, | ||
], | ||
}, | ||
{ | ||
code: ` | ||
enum Enum { | ||
'a' = 1, | ||
[\`key-with | ||
|
||
new-line\`] = 2, | ||
} | ||
|
||
declare const a: Enum; | ||
|
||
switch (a) { | ||
} | ||
`, | ||
errors: [ | ||
{ | ||
messageId: 'switchIsNotExhaustive', | ||
suggestions: [ | ||
{ | ||
messageId: 'addMissingCases', | ||
output: ` | ||
enum Enum { | ||
'a' = 1, | ||
[\`key-with | ||
|
||
new-line\`] = 2, | ||
} | ||
|
||
declare const a: Enum; | ||
|
||
switch (a) { | ||
case Enum.a: { throw new Error('Not implemented yet: Enum.a case') } | ||
case Enum[\`key-with | ||
|
||
new-line\`]: { throw new Error('Not implemented yet: Enum[\`key-with\\n\\n new-line\`] case') } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. wasn't sure about this. should we throw an error with actual new lines or There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah good question. It's such a rare case, I don't think it matters too much. But in general having error messages be on one line is easier to read. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should we just strip out any spaces, tabs, and new lines? OK... it might look weird in some cases... IDK key = `key-with
new-line`
error = "not implemented: 'key-withnew-line'"; anyway, as you said, it's a rare case that probably isn't worth the effort |
||
} | ||
`, | ||
}, | ||
], | ||
}, | ||
], | ||
}, | ||
], | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[Proofreading] Existing typo with the singular/plural, heh:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done