From cd0701b4d4fb2404f0761760df23d65a44554348 Mon Sep 17 00:00:00 2001 From: yeonjuan Date: Fri, 2 Feb 2024 02:53:54 +0900 Subject: [PATCH 1/4] fix(eslint-plugin): [switch-exhaustiveness-check] suggest with qualified name --- .../src/rules/switch-exhaustiveness-check.ts | 17 +++++-- .../rules/switch-exhaustiveness-check.test.ts | 46 +++++++++++++++++++ 2 files changed, 59 insertions(+), 4 deletions(-) diff --git a/packages/eslint-plugin/src/rules/switch-exhaustiveness-check.ts b/packages/eslint-plugin/src/rules/switch-exhaustiveness-check.ts index 316770b6b131..29579255b700 100644 --- a/packages/eslint-plugin/src/rules/switch-exhaustiveness-check.ts +++ b/packages/eslint-plugin/src/rules/switch-exhaustiveness-check.ts @@ -142,6 +142,16 @@ export default createRule({ 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, @@ -230,7 +240,7 @@ export default createRule({ .map(missingType => tsutils.isTypeFlagSet(missingType, ts.TypeFlags.ESSymbolLike) ? `typeof ${missingType.getSymbol()?.escapedName as string}` - : checker.typeToString(missingType), + : typeToString(missingType), ) .join(' | '), }, @@ -280,9 +290,8 @@ export default createRule({ missingBranchType, ts.TypeFlags.ESSymbolLike, ) - ? // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - missingBranchName! - : checker.typeToString(missingBranchType); + ? missingBranchName! + : typeToString(missingBranchType); if ( symbolName && diff --git a/packages/eslint-plugin/tests/rules/switch-exhaustiveness-check.test.ts b/packages/eslint-plugin/tests/rules/switch-exhaustiveness-check.test.ts index 263c9b81bb5c..82fb3427119b 100644 --- a/packages/eslint-plugin/tests/rules/switch-exhaustiveness-check.test.ts +++ b/packages/eslint-plugin/tests/rules/switch-exhaustiveness-check.test.ts @@ -2947,5 +2947,51 @@ switch (literal) { }, ], }, + { + code: ` + export namespace A { + export enum B { + C, + D, + } + } + declare const foo: A.B; + switch (foo) { + case A.B.C: { + break; + } + } + `, + errors: [ + { + messageId: 'switchIsNotExhaustive', + data: { + missingBranches: 'A.B.D', + }, + line: 9, + column: 17, + 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') } + } + `, + }, + ], + }, + ], + }, ], }); From 375d3a8b36d812257dabbb10f1012f3c208fe877 Mon Sep 17 00:00:00 2001 From: yeonjuan Date: Fri, 2 Feb 2024 18:59:55 +0900 Subject: [PATCH 2/4] add tests --- .../fixtures/switch-exhaustiveness-check.ts | 6 ++++ .../tests/fixtures/tsconfig.json | 3 +- .../rules/switch-exhaustiveness-check.test.ts | 36 +++++++++++++++++++ 3 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 packages/eslint-plugin/tests/fixtures/switch-exhaustiveness-check.ts diff --git a/packages/eslint-plugin/tests/fixtures/switch-exhaustiveness-check.ts b/packages/eslint-plugin/tests/fixtures/switch-exhaustiveness-check.ts new file mode 100644 index 000000000000..277dc2a7c28c --- /dev/null +++ b/packages/eslint-plugin/tests/fixtures/switch-exhaustiveness-check.ts @@ -0,0 +1,6 @@ +export namespace A { + export enum B { + C, + D, + } +} diff --git a/packages/eslint-plugin/tests/fixtures/tsconfig.json b/packages/eslint-plugin/tests/fixtures/tsconfig.json index d8141e1ddfa4..9fb244b4171e 100644 --- a/packages/eslint-plugin/tests/fixtures/tsconfig.json +++ b/packages/eslint-plugin/tests/fixtures/tsconfig.json @@ -15,6 +15,7 @@ "mixed-enums-decl.ts", "react.tsx", "var-declaration.ts", - "errors.ts" + "errors.ts", + "switch-exhaustiveness-check.ts" ] } diff --git a/packages/eslint-plugin/tests/rules/switch-exhaustiveness-check.test.ts b/packages/eslint-plugin/tests/rules/switch-exhaustiveness-check.test.ts index 82fb3427119b..0809294fcfaa 100644 --- a/packages/eslint-plugin/tests/rules/switch-exhaustiveness-check.test.ts +++ b/packages/eslint-plugin/tests/rules/switch-exhaustiveness-check.test.ts @@ -2993,5 +2993,41 @@ switch (literal) { }, ], }, + { + code: ` + import { A } from './switch-exhaustiveness-check'; + declare const foo: A.B; + switch (foo) { + case A.B.C: { + break; + } + } + `, + errors: [ + { + messageId: 'switchIsNotExhaustive', + data: { + missingBranches: 'A.B.D', + }, + line: 4, + column: 17, + 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') } + } + `, + }, + ], + }, + ], + }, ], }); From 66647741b4f6116cfa1b8ae834d56d5e097293fe Mon Sep 17 00:00:00 2001 From: YeonJuan Date: Wed, 22 Jan 2025 21:08:42 +0900 Subject: [PATCH 3/4] Update switch-exhaustiveness-check.ts --- .../eslint-plugin/src/rules/switch-exhaustiveness-check.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/eslint-plugin/src/rules/switch-exhaustiveness-check.ts b/packages/eslint-plugin/src/rules/switch-exhaustiveness-check.ts index 29579255b700..6849d98f62d9 100644 --- a/packages/eslint-plugin/src/rules/switch-exhaustiveness-check.ts +++ b/packages/eslint-plugin/src/rules/switch-exhaustiveness-check.ts @@ -290,7 +290,8 @@ export default createRule({ missingBranchType, ts.TypeFlags.ESSymbolLike, ) - ? missingBranchName! + ? // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + missingBranchName! : typeToString(missingBranchType); if ( From a5414c3ec5d6cdf8bc6d04a907af7acc23d1f239 Mon Sep 17 00:00:00 2001 From: YeonJuan Date: Wed, 22 Jan 2025 21:54:16 +0900 Subject: [PATCH 4/4] fix lint errors --- .../tests/rules/switch-exhaustiveness-check.test.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/eslint-plugin/tests/rules/switch-exhaustiveness-check.test.ts b/packages/eslint-plugin/tests/rules/switch-exhaustiveness-check.test.ts index 0809294fcfaa..5c8539dec279 100644 --- a/packages/eslint-plugin/tests/rules/switch-exhaustiveness-check.test.ts +++ b/packages/eslint-plugin/tests/rules/switch-exhaustiveness-check.test.ts @@ -2964,12 +2964,12 @@ switch (literal) { `, errors: [ { - messageId: 'switchIsNotExhaustive', + column: 17, data: { missingBranches: 'A.B.D', }, line: 9, - column: 17, + messageId: 'switchIsNotExhaustive', suggestions: [ { messageId: 'addMissingCases', @@ -3005,12 +3005,12 @@ switch (literal) { `, errors: [ { - messageId: 'switchIsNotExhaustive', + column: 17, data: { missingBranches: 'A.B.D', }, line: 4, - column: 17, + messageId: 'switchIsNotExhaustive', suggestions: [ { messageId: 'addMissingCases', 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