Skip to content

Commit f83a591

Browse files
ronamiauvred
andauthored
fix(eslint-plugin): [switch-exhaustiveness-check] add support for covering a missing property with undefined (typescript-eslint#10232)
* don't report on the 'missing' undefined type if it's covered with an undefined type * projectService: false * projectService: false on valid test too * Update packages/eslint-plugin/src/rules/switch-exhaustiveness-check.ts Co-authored-by: auvred <61150013+auvred@users.noreply.github.com> * add relevant test --------- Co-authored-by: auvred <61150013+auvred@users.noreply.github.com>
1 parent 3b97b55 commit f83a591

File tree

2 files changed

+89
-0
lines changed

2 files changed

+89
-0
lines changed

packages/eslint-plugin/src/rules/switch-exhaustiveness-check.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,15 @@ export default createRule<Options, MessageIds>({
155155
continue;
156156
}
157157

158+
// "missing", "optional" and "undefined" types are different runtime objects,
159+
// but all of them have TypeFlags.Undefined type flag
160+
if (
161+
[...caseTypes].some(tsutils.isIntrinsicUndefinedType) &&
162+
tsutils.isIntrinsicUndefinedType(intersectionPart)
163+
) {
164+
continue;
165+
}
166+
158167
missingLiteralBranchTypes.push(intersectionPart);
159168
}
160169
}

packages/eslint-plugin/tests/rules/switch-exhaustiveness-check.test.ts

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -912,6 +912,48 @@ switch (value) {
912912
},
913913
],
914914
},
915+
{
916+
code: `
917+
function foo(x: string[]) {
918+
switch (x[0]) {
919+
case 'hi':
920+
break;
921+
case undefined:
922+
break;
923+
}
924+
}
925+
`,
926+
languageOptions: {
927+
parserOptions: {
928+
project: './tsconfig.noUncheckedIndexedAccess.json',
929+
projectService: false,
930+
tsconfigRootDir: rootPath,
931+
},
932+
},
933+
},
934+
{
935+
code: `
936+
function foo(x: string[], y: string | undefined) {
937+
const a = x[0];
938+
if (typeof a === 'string') {
939+
return;
940+
}
941+
switch (y) {
942+
case 'hi':
943+
break;
944+
case a:
945+
break;
946+
}
947+
}
948+
`,
949+
languageOptions: {
950+
parserOptions: {
951+
project: './tsconfig.noUncheckedIndexedAccess.json',
952+
projectService: false,
953+
tsconfigRootDir: rootPath,
954+
},
955+
},
956+
},
915957
],
916958
invalid: [
917959
{
@@ -2717,5 +2759,43 @@ switch (value) {
27172759
},
27182760
],
27192761
},
2762+
{
2763+
code: `
2764+
function foo(x: string[]) {
2765+
switch (x[0]) {
2766+
case 'hi':
2767+
break;
2768+
}
2769+
}
2770+
`,
2771+
errors: [
2772+
{
2773+
column: 11,
2774+
line: 3,
2775+
messageId: 'switchIsNotExhaustive',
2776+
suggestions: [
2777+
{
2778+
messageId: 'addMissingCases',
2779+
output: `
2780+
function foo(x: string[]) {
2781+
switch (x[0]) {
2782+
case 'hi':
2783+
break;
2784+
case undefined: { throw new Error('Not implemented yet: undefined case') }
2785+
}
2786+
}
2787+
`,
2788+
},
2789+
],
2790+
},
2791+
],
2792+
languageOptions: {
2793+
parserOptions: {
2794+
project: './tsconfig.noUncheckedIndexedAccess.json',
2795+
projectService: false,
2796+
tsconfigRootDir: rootPath,
2797+
},
2798+
},
2799+
},
27202800
],
27212801
});

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy