@typescript-eslint/eslint-plugin 6.11.1-alpha.19 → 6.11.1-alpha.20
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -32,7 +32,7 @@ exports.default = (0, util_1.createRule)({
|
|
|
32
32
|
meta: {
|
|
33
33
|
type: 'suggestion',
|
|
34
34
|
docs: {
|
|
35
|
-
description: 'Require switch-case statements to be exhaustive with union
|
|
35
|
+
description: 'Require switch-case statements to be exhaustive with union types and enums',
|
|
36
36
|
requiresTypeChecking: true,
|
|
37
37
|
},
|
|
38
38
|
hasSuggestions: true,
|
|
@@ -75,10 +75,15 @@ exports.default = (0, util_1.createRule)({
|
|
|
75
75
|
if (symbolName &&
|
|
76
76
|
(missingBranchName || missingBranchName === '') &&
|
|
77
77
|
(0, util_1.requiresQuoting)(missingBranchName.toString(), compilerOptions.target)) {
|
|
78
|
-
|
|
78
|
+
const escapedBranchName = missingBranchName
|
|
79
|
+
.replace(/'/g, "\\'")
|
|
80
|
+
.replace(/\n/g, '\\n')
|
|
81
|
+
.replace(/\r/g, '\\r');
|
|
82
|
+
caseTest = `${symbolName}['${escapedBranchName}']`;
|
|
79
83
|
}
|
|
80
84
|
const errorMessage = `Not implemented yet: ${caseTest} case`;
|
|
81
|
-
|
|
85
|
+
const escapedErrorMessage = errorMessage.replace(/'/g, "\\'");
|
|
86
|
+
missingCases.push(`case ${caseTest}: { throw new Error('${escapedErrorMessage}') }`);
|
|
82
87
|
}
|
|
83
88
|
const fixString = missingCases
|
|
84
89
|
.map(code => `${caseIndent}${code}`)
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"switch-exhaustiveness-check.js","sourceRoot":"","sources":["../../src/rules/switch-exhaustiveness-check.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;AACA,wEAAsE;AACtE,sDAAwC;AACxC,+CAAiC;AAEjC,kCAOiB;AAEjB,kBAAe,IAAA,iBAAU,EAAC;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ements to be exhaustive with union
|
|
2
|
+
description: 'Require switch-case statements to be exhaustive with union types and enums.'
|
|
3
3
|
---
|
|
4
4
|
|
|
5
5
|
> 🛑 This file is source code, not the primary documentation location! 🛑
|
|
6
6
|
>
|
|
7
7
|
> See **https://typescript-eslint.io/rules/switch-exhaustiveness-check** for documentation.
|
|
8
8
|
|
|
9
|
-
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).
|
|
10
|
-
However, if the union type changes, it's easy to forget to modify the cases to account for any new types.
|
|
9
|
+
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).
|
|
10
|
+
However, if the union type or the enum changes, it's easy to forget to modify the cases to account for any new types.
|
|
11
11
|
|
|
12
|
-
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.
|
|
12
|
+
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.
|
|
13
13
|
|
|
14
14
|
## Examples
|
|
15
15
|
|
|
16
|
+
When the switch doesn't have exhaustive cases, either filling them all out or adding a default will correct the rule's complaint.
|
|
17
|
+
|
|
18
|
+
Here are some examples of code working with a union of literals:
|
|
19
|
+
|
|
16
20
|
<!--tabs-->
|
|
17
21
|
|
|
18
22
|
### ❌ Incorrect
|
|
@@ -27,7 +31,7 @@ type Day =
|
|
|
27
31
|
| 'Saturday'
|
|
28
32
|
| 'Sunday';
|
|
29
33
|
|
|
30
|
-
const day
|
|
34
|
+
declare const day: Day;
|
|
31
35
|
let result = 0;
|
|
32
36
|
|
|
33
37
|
switch (day) {
|
|
@@ -49,7 +53,7 @@ type Day =
|
|
|
49
53
|
| 'Saturday'
|
|
50
54
|
| 'Sunday';
|
|
51
55
|
|
|
52
|
-
const day
|
|
56
|
+
declare const day: Day;
|
|
53
57
|
let result = 0;
|
|
54
58
|
|
|
55
59
|
switch (day) {
|
|
@@ -89,7 +93,7 @@ type Day =
|
|
|
89
93
|
| 'Saturday'
|
|
90
94
|
| 'Sunday';
|
|
91
95
|
|
|
92
|
-
const day
|
|
96
|
+
declare const day: Day;
|
|
93
97
|
let result = 0;
|
|
94
98
|
|
|
95
99
|
switch (day) {
|
|
@@ -101,6 +105,80 @@ switch (day) {
|
|
|
101
105
|
}
|
|
102
106
|
```
|
|
103
107
|
|
|
108
|
+
<!--/tabs-->
|
|
109
|
+
|
|
110
|
+
Likewise, here are some examples of code working with an enum:
|
|
111
|
+
|
|
112
|
+
<!--tabs-->
|
|
113
|
+
|
|
114
|
+
### ❌ Incorrect
|
|
115
|
+
|
|
116
|
+
```ts
|
|
117
|
+
enum Fruit {
|
|
118
|
+
Apple,
|
|
119
|
+
Banana,
|
|
120
|
+
Cherry,
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
declare const fruit: Fruit;
|
|
124
|
+
|
|
125
|
+
switch (fruit) {
|
|
126
|
+
case Fruit.Apple:
|
|
127
|
+
console.log('an apple');
|
|
128
|
+
break;
|
|
129
|
+
}
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
### ✅ Correct
|
|
133
|
+
|
|
134
|
+
```ts
|
|
135
|
+
enum Fruit {
|
|
136
|
+
Apple,
|
|
137
|
+
Banana,
|
|
138
|
+
Cherry,
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
declare const fruit: Fruit;
|
|
142
|
+
|
|
143
|
+
switch (fruit) {
|
|
144
|
+
case Fruit.Apple:
|
|
145
|
+
console.log('an apple');
|
|
146
|
+
break;
|
|
147
|
+
|
|
148
|
+
case Fruit.Banana:
|
|
149
|
+
console.log('a banana');
|
|
150
|
+
break;
|
|
151
|
+
|
|
152
|
+
case Fruit.Cherry:
|
|
153
|
+
console.log('a cherry');
|
|
154
|
+
break;
|
|
155
|
+
}
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
### ✅ Correct
|
|
159
|
+
|
|
160
|
+
```ts
|
|
161
|
+
enum Fruit {
|
|
162
|
+
Apple,
|
|
163
|
+
Banana,
|
|
164
|
+
Cherry,
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
declare const fruit: Fruit;
|
|
168
|
+
|
|
169
|
+
switch (fruit) {
|
|
170
|
+
case Fruit.Apple:
|
|
171
|
+
console.log('an apple');
|
|
172
|
+
break;
|
|
173
|
+
|
|
174
|
+
default:
|
|
175
|
+
console.log('a fruit');
|
|
176
|
+
break;
|
|
177
|
+
}
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
<!--/tabs-->
|
|
181
|
+
|
|
104
182
|
## When Not To Use It
|
|
105
183
|
|
|
106
|
-
If you don't frequently `switch` over union types with many parts, or intentionally wish to leave out some parts.
|
|
184
|
+
If you don't frequently `switch` over union types or enums with many parts, or intentionally wish to leave out some parts.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@typescript-eslint/eslint-plugin",
|
|
3
|
-
"version": "6.11.1-alpha.
|
|
3
|
+
"version": "6.11.1-alpha.20+a034d0a38",
|
|
4
4
|
"description": "TypeScript plugin for ESLint",
|
|
5
5
|
"files": [
|
|
6
6
|
"dist",
|
|
@@ -57,10 +57,10 @@
|
|
|
57
57
|
},
|
|
58
58
|
"dependencies": {
|
|
59
59
|
"@eslint-community/regexpp": "^4.5.1",
|
|
60
|
-
"@typescript-eslint/scope-manager": "6.11.1-alpha.
|
|
61
|
-
"@typescript-eslint/type-utils": "6.11.1-alpha.
|
|
62
|
-
"@typescript-eslint/utils": "6.11.1-alpha.
|
|
63
|
-
"@typescript-eslint/visitor-keys": "6.11.1-alpha.
|
|
60
|
+
"@typescript-eslint/scope-manager": "6.11.1-alpha.20+a034d0a38",
|
|
61
|
+
"@typescript-eslint/type-utils": "6.11.1-alpha.20+a034d0a38",
|
|
62
|
+
"@typescript-eslint/utils": "6.11.1-alpha.20+a034d0a38",
|
|
63
|
+
"@typescript-eslint/visitor-keys": "6.11.1-alpha.20+a034d0a38",
|
|
64
64
|
"debug": "^4.3.4",
|
|
65
65
|
"graphemer": "^1.4.0",
|
|
66
66
|
"ignore": "^5.2.4",
|
|
@@ -74,7 +74,7 @@
|
|
|
74
74
|
"@types/marked": "*",
|
|
75
75
|
"@types/natural-compare": "*",
|
|
76
76
|
"@typescript-eslint/rule-schema-to-typescript-types": "6.11.0",
|
|
77
|
-
"@typescript-eslint/rule-tester": "6.11.1-alpha.
|
|
77
|
+
"@typescript-eslint/rule-tester": "6.11.1-alpha.20+a034d0a38",
|
|
78
78
|
"ajv": "^6.12.6",
|
|
79
79
|
"chalk": "^5.3.0",
|
|
80
80
|
"cross-fetch": "*",
|
|
@@ -103,5 +103,5 @@
|
|
|
103
103
|
"type": "opencollective",
|
|
104
104
|
"url": "https://opencollective.com/typescript-eslint"
|
|
105
105
|
},
|
|
106
|
-
"gitHead": "
|
|
106
|
+
"gitHead": "a034d0a3856aa07bd2d52b557fa33c7a88e9e511"
|
|
107
107
|
}
|