@typescript-eslint/eslint-plugin 6.11.1-alpha.28 → 6.11.1-alpha.36
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,23 +32,35 @@ 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
|
|
35
|
+
description: 'Require switch-case statements to be exhaustive',
|
|
36
36
|
requiresTypeChecking: true,
|
|
37
37
|
},
|
|
38
38
|
hasSuggestions: true,
|
|
39
|
-
schema: [
|
|
39
|
+
schema: [
|
|
40
|
+
{
|
|
41
|
+
type: 'object',
|
|
42
|
+
additionalProperties: false,
|
|
43
|
+
properties: {
|
|
44
|
+
requireDefaultForNonUnion: {
|
|
45
|
+
description: `If 'true', require a 'default' clause for switches on non-union types.`,
|
|
46
|
+
type: 'boolean',
|
|
47
|
+
},
|
|
48
|
+
},
|
|
49
|
+
},
|
|
50
|
+
],
|
|
40
51
|
messages: {
|
|
41
52
|
switchIsNotExhaustive: 'Switch is not exhaustive. Cases not matched: {{missingBranches}}',
|
|
42
53
|
addMissingCases: 'Add branches for missing cases.',
|
|
43
54
|
},
|
|
44
55
|
},
|
|
45
|
-
defaultOptions: [],
|
|
46
|
-
create(context) {
|
|
56
|
+
defaultOptions: [{ requireDefaultForNonUnion: false }],
|
|
57
|
+
create(context, [{ requireDefaultForNonUnion }]) {
|
|
47
58
|
const sourceCode = (0, eslint_utils_1.getSourceCode)(context);
|
|
48
59
|
const services = (0, util_1.getParserServices)(context);
|
|
49
60
|
const checker = services.program.getTypeChecker();
|
|
50
61
|
const compilerOptions = services.program.getCompilerOptions();
|
|
51
|
-
function fixSwitch(fixer, node, missingBranchTypes,
|
|
62
|
+
function fixSwitch(fixer, node, missingBranchTypes, // null means default branch
|
|
63
|
+
symbolName) {
|
|
52
64
|
const lastCase = node.cases.length > 0 ? node.cases[node.cases.length - 1] : null;
|
|
53
65
|
const caseIndent = lastCase
|
|
54
66
|
? ' '.repeat(lastCase.loc.start.column)
|
|
@@ -57,6 +69,10 @@ exports.default = (0, util_1.createRule)({
|
|
|
57
69
|
' '.repeat(node.loc.start.column);
|
|
58
70
|
const missingCases = [];
|
|
59
71
|
for (const missingBranchType of missingBranchTypes) {
|
|
72
|
+
if (missingBranchType == null) {
|
|
73
|
+
missingCases.push(`default: { throw new Error('default case') }`);
|
|
74
|
+
continue;
|
|
75
|
+
}
|
|
60
76
|
// While running this rule on checker.ts of TypeScript project
|
|
61
77
|
// the fix introduced a compiler error due to:
|
|
62
78
|
//
|
|
@@ -134,6 +150,26 @@ exports.default = (0, util_1.createRule)({
|
|
|
134
150
|
],
|
|
135
151
|
});
|
|
136
152
|
}
|
|
153
|
+
else if (requireDefaultForNonUnion) {
|
|
154
|
+
const hasDefault = node.cases.some(switchCase => switchCase.test == null);
|
|
155
|
+
if (!hasDefault) {
|
|
156
|
+
context.report({
|
|
157
|
+
node: node.discriminant,
|
|
158
|
+
messageId: 'switchIsNotExhaustive',
|
|
159
|
+
data: {
|
|
160
|
+
missingBranches: 'default',
|
|
161
|
+
},
|
|
162
|
+
suggest: [
|
|
163
|
+
{
|
|
164
|
+
messageId: 'addMissingCases',
|
|
165
|
+
fix(fixer) {
|
|
166
|
+
return fixSwitch(fixer, node, [null]);
|
|
167
|
+
},
|
|
168
|
+
},
|
|
169
|
+
],
|
|
170
|
+
});
|
|
171
|
+
}
|
|
172
|
+
}
|
|
137
173
|
}
|
|
138
174
|
return {
|
|
139
175
|
SwitchStatement: checkSwitchExhaustive,
|
|
@@ -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;
|
|
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;AAcjB,kBAAe,IAAA,iBAAU,EAAsB;IAC7C,IAAI,EAAE,6BAA6B;IACnC,IAAI,EAAE;QACJ,IAAI,EAAE,YAAY;QAClB,IAAI,EAAE;YACJ,WAAW,EAAE,iDAAiD;YAC9D,oBAAoB,EAAE,IAAI;SAC3B;QACD,cAAc,EAAE,IAAI;QACpB,MAAM,EAAE;YACN;gBACE,IAAI,EAAE,QAAQ;gBACd,oBAAoB,EAAE,KAAK;gBAC3B,UAAU,EAAE;oBACV,yBAAyB,EAAE;wBACzB,WAAW,EAAE,wEAAwE;wBACrF,IAAI,EAAE,SAAS;qBAChB;iBACF;aACF;SACF;QACD,QAAQ,EAAE;YACR,qBAAqB,EACnB,kEAAkE;YACpE,eAAe,EAAE,iCAAiC;SACnD;KACF;IACD,cAAc,EAAE,CAAC,EAAE,yBAAyB,EAAE,KAAK,EAAE,CAAC;IACtD,MAAM,CAAC,OAAO,EAAE,CAAC,EAAE,yBAAyB,EAAE,CAAC;QAC7C,MAAM,UAAU,GAAG,IAAA,4BAAa,EAAC,OAAO,CAAC,CAAC;QAC1C,MAAM,QAAQ,GAAG,IAAA,wBAAiB,EAAC,OAAO,CAAC,CAAC;QAC5C,MAAM,OAAO,GAAG,QAAQ,CAAC,OAAO,CAAC,cAAc,EAAE,CAAC;QAClD,MAAM,eAAe,GAAG,QAAQ,CAAC,OAAO,CAAC,kBAAkB,EAAE,CAAC;QAE9D,SAAS,SAAS,CAChB,KAAyB,EACzB,IAA8B,EAC9B,kBAAsC,EAAE,4BAA4B;QACpE,UAAmB;YAEnB,MAAM,QAAQ,GACZ,IAAI,CAAC,KAAK,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,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ACJ,kBAAkB,EAClB,UAAU,EAAE,QAAQ,EAAE,CACvB,CAAC;4BACJ,CAAC;yBACF;qBACF;iBACF,CAAC,CAAC;YACL,CAAC;iBAAM,IAAI,yBAAyB,EAAE,CAAC;gBACrC,MAAM,UAAU,GAAG,IAAI,CAAC,KAAK,CAAC,IAAI,CAChC,UAAU,CAAC,EAAE,CAAC,UAAU,CAAC,IAAI,IAAI,IAAI,CACtC,CAAC;gBAEF,IAAI,CAAC,UAAU,EAAE,CAAC;oBAChB,OAAO,CAAC,MAAM,CAAC;wBACb,IAAI,EAAE,IAAI,CAAC,YAAY;wBACvB,SAAS,EAAE,uBAAuB;wBAClC,IAAI,EAAE;4BACJ,eAAe,EAAE,SAAS;yBAC3B;wBACD,OAAO,EAAE;4BACP;gCACE,SAAS,EAAE,iBAAiB;gCAC5B,GAAG,CAAC,KAAK;oCACP,OAAO,SAAS,CAAC,KAAK,EAAE,IAAI,EAAE,CAAC,IAAI,CAAC,CAAC,CAAC;gCACxC,CAAC;6BACF;yBACF;qBACF,CAAC,CAAC;gBACL,CAAC;YACH,CAAC;QACH,CAAC;QAED,OAAO;YACL,eAAe,EAAE,qBAAqB;SACvC,CAAC;IACJ,CAAC;CACF,CAAC,CAAC"}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
---
|
|
2
|
-
description: 'Require switch-case statements to be exhaustive
|
|
2
|
+
description: 'Require switch-case statements to be exhaustive.'
|
|
3
3
|
---
|
|
4
4
|
|
|
5
5
|
> 🛑 This file is source code, not the primary documentation location! 🛑
|
|
@@ -11,6 +11,8 @@ However, if the union type or the enum changes, it's easy to forget to modify th
|
|
|
11
11
|
|
|
12
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
|
+
There is also an option to check the exhaustiveness of switches on non-union types by requiring a default clause.
|
|
15
|
+
|
|
14
16
|
## Examples
|
|
15
17
|
|
|
16
18
|
When the switch doesn't have exhaustive cases, either filling them all out or adding a default will correct the rule's complaint.
|
|
@@ -179,6 +181,27 @@ switch (fruit) {
|
|
|
179
181
|
|
|
180
182
|
<!--/tabs-->
|
|
181
183
|
|
|
184
|
+
## Options
|
|
185
|
+
|
|
186
|
+
### `requireDefaultForNonUnion`
|
|
187
|
+
|
|
188
|
+
Examples of additional **incorrect** code for this rule with `{ requireDefaultForNonUnion: true }`:
|
|
189
|
+
|
|
190
|
+
```ts option='{ "requireDefaultForNonUnion": true }' showPlaygroundButton
|
|
191
|
+
const value: number = Math.floor(Math.random() * 3);
|
|
192
|
+
|
|
193
|
+
switch (value) {
|
|
194
|
+
case 0:
|
|
195
|
+
return 0;
|
|
196
|
+
case 1:
|
|
197
|
+
return 1;
|
|
198
|
+
}
|
|
199
|
+
```
|
|
200
|
+
|
|
201
|
+
Since `value` is a non-union type it requires the switch case to have a default clause only with `requireDefaultForNonUnion` enabled.
|
|
202
|
+
|
|
203
|
+
<!--/tabs-->
|
|
204
|
+
|
|
182
205
|
## When Not To Use It
|
|
183
206
|
|
|
184
207
|
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.36+4a079d719",
|
|
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.36+4a079d719",
|
|
61
|
+
"@typescript-eslint/type-utils": "6.11.1-alpha.36+4a079d719",
|
|
62
|
+
"@typescript-eslint/utils": "6.11.1-alpha.36+4a079d719",
|
|
63
|
+
"@typescript-eslint/visitor-keys": "6.11.1-alpha.36+4a079d719",
|
|
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.36+4a079d719",
|
|
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": "4a079d71902cc2e14e4ec402f62a9fd0be2bf9b3"
|
|
107
107
|
}
|