Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions packages/eslint-plugin/src/rules/switch-exhaustiveness-check.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,16 @@ export default createRule<Options, MessageIds>({
return;
}

function typeToString(type: ts.Type): string {
return checker.typeToString(
type,
undefined,
ts.TypeFormatFlags.AllowUniqueESSymbolType |
ts.TypeFormatFlags.UseAliasDefinedOutsideCurrentScope |
ts.TypeFormatFlags.UseFullyQualifiedType,
);
}

function getSwitchMetadata(node: TSESTree.SwitchStatement): SwitchMetadata {
const defaultCase = node.cases.find(
switchCase => switchCase.test == null,
Expand Down Expand Up @@ -230,7 +240,7 @@ export default createRule<Options, MessageIds>({
.map(missingType =>
tsutils.isTypeFlagSet(missingType, ts.TypeFlags.ESSymbolLike)
? `typeof ${missingType.getSymbol()?.escapedName as string}`
: checker.typeToString(missingType),
: typeToString(missingType),
)
.join(' | '),
},
Expand Down Expand Up @@ -282,7 +292,7 @@ export default createRule<Options, MessageIds>({
)
? // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
missingBranchName!
: checker.typeToString(missingBranchType);
: typeToString(missingBranchType);

if (
symbolName &&
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export namespace A {
export enum B {
C,
D,
}
}
3 changes: 2 additions & 1 deletion packages/eslint-plugin/tests/fixtures/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"mixed-enums-decl.ts",
"react.tsx",
"var-declaration.ts",
"errors.ts"
"errors.ts",
"switch-exhaustiveness-check.ts"
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -2947,5 +2947,87 @@ switch (literal) {
},
],
},
{
code: `
export namespace A {
export enum B {
C,
D,
}
}
declare const foo: A.B;
switch (foo) {
case A.B.C: {
break;
}
}
`,
errors: [
{
column: 17,
data: {
missingBranches: 'A.B.D',
},
line: 9,
messageId: 'switchIsNotExhaustive',
suggestions: [
{
messageId: 'addMissingCases',
output: `
export namespace A {
export enum B {
C,
D,
}
}
declare const foo: A.B;
switch (foo) {
case A.B.C: {
break;
}
case A.B.D: { throw new Error('Not implemented yet: A.B.D case') }
}
`,
},
],
},
],
},
{
code: `
import { A } from './switch-exhaustiveness-check';
declare const foo: A.B;
switch (foo) {
case A.B.C: {
break;
}
}
`,
errors: [
{
column: 17,
data: {
missingBranches: 'A.B.D',
},
line: 4,
messageId: 'switchIsNotExhaustive',
suggestions: [
{
messageId: 'addMissingCases',
output: `
import { A } from './switch-exhaustiveness-check';
declare const foo: A.B;
switch (foo) {
case A.B.C: {
break;
}
case A.B.D: { throw new Error('Not implemented yet: A.B.D case') }
}
`,
},
],
},
],
},
],
});
Loading