Skip to content

fix(eslint-plugin): [switch-exhaustiveness-check] suggest with qualified name #10697

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

Merged
merged 4 commits into from
Feb 1, 2025
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