From bed38e959222e185f42530845aaae7a6312a0789 Mon Sep 17 00:00:00 2001 From: Oleksandr T Date: Mon, 17 Jun 2024 02:01:20 +0300 Subject: [PATCH] feat(58561): allow leading underscore for types to bypass noUnusedLocals warning --- src/compiler/checker.ts | 8 ++++++++ .../reference/unusedTypeDeclarations.errors.txt | 11 +++++++++++ tests/baselines/reference/unusedTypeDeclarations.js | 12 ++++++++++++ .../reference/unusedTypeDeclarations.symbols | 11 +++++++++++ .../reference/unusedTypeDeclarations.types | 13 +++++++++++++ tests/cases/compiler/unusedTypeDeclarations.ts | 6 ++++++ 6 files changed, 61 insertions(+) create mode 100644 tests/baselines/reference/unusedTypeDeclarations.errors.txt create mode 100644 tests/baselines/reference/unusedTypeDeclarations.js create mode 100644 tests/baselines/reference/unusedTypeDeclarations.symbols create mode 100644 tests/baselines/reference/unusedTypeDeclarations.types create mode 100644 tests/cases/compiler/unusedTypeDeclarations.ts diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 3e64493503c42..7e3c22b41f0e3 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -43174,6 +43174,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { } } } + function isTypeParameterUnused(typeParameter: TypeParameterDeclaration): boolean { return !(getMergedSymbol(typeParameter.symbol).isReferenced! & SymbolFlags.TypeParameter) && !isIdentifierThatStartsWithUnderscore(typeParameter.name); } @@ -43194,6 +43195,13 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { } function isValidUnusedLocalDeclaration(declaration: Declaration): boolean { + if (isTypeAliasDeclaration(declaration)) { + /** + * ignore starts with underscore names _ + * type _T = number; + */ + return isIdentifierThatStartsWithUnderscore(declaration.name); + } if (isBindingElement(declaration)) { if (isObjectBindingPattern(declaration.parent)) { /** diff --git a/tests/baselines/reference/unusedTypeDeclarations.errors.txt b/tests/baselines/reference/unusedTypeDeclarations.errors.txt new file mode 100644 index 0000000000000..fd2768f62407b --- /dev/null +++ b/tests/baselines/reference/unusedTypeDeclarations.errors.txt @@ -0,0 +1,11 @@ +unusedTypeDeclarations.ts(1,6): error TS6196: 'T1' is declared but never used. + + +==== unusedTypeDeclarations.ts (1 errors) ==== + type T1 = number; // error + ~~ +!!! error TS6196: 'T1' is declared but never used. + type _T2 = number; // ok + + export {}; + \ No newline at end of file diff --git a/tests/baselines/reference/unusedTypeDeclarations.js b/tests/baselines/reference/unusedTypeDeclarations.js new file mode 100644 index 0000000000000..18f35ad63fad8 --- /dev/null +++ b/tests/baselines/reference/unusedTypeDeclarations.js @@ -0,0 +1,12 @@ +//// [tests/cases/compiler/unusedTypeDeclarations.ts] //// + +//// [unusedTypeDeclarations.ts] +type T1 = number; // error +type _T2 = number; // ok + +export {}; + + +//// [unusedTypeDeclarations.js] +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); diff --git a/tests/baselines/reference/unusedTypeDeclarations.symbols b/tests/baselines/reference/unusedTypeDeclarations.symbols new file mode 100644 index 0000000000000..39a216df7a34a --- /dev/null +++ b/tests/baselines/reference/unusedTypeDeclarations.symbols @@ -0,0 +1,11 @@ +//// [tests/cases/compiler/unusedTypeDeclarations.ts] //// + +=== unusedTypeDeclarations.ts === +type T1 = number; // error +>T1 : Symbol(T1, Decl(unusedTypeDeclarations.ts, 0, 0)) + +type _T2 = number; // ok +>_T2 : Symbol(_T2, Decl(unusedTypeDeclarations.ts, 0, 17)) + +export {}; + diff --git a/tests/baselines/reference/unusedTypeDeclarations.types b/tests/baselines/reference/unusedTypeDeclarations.types new file mode 100644 index 0000000000000..57a57847a46fd --- /dev/null +++ b/tests/baselines/reference/unusedTypeDeclarations.types @@ -0,0 +1,13 @@ +//// [tests/cases/compiler/unusedTypeDeclarations.ts] //// + +=== unusedTypeDeclarations.ts === +type T1 = number; // error +>T1 : number +> : ^^^^^^ + +type _T2 = number; // ok +>_T2 : number +> : ^^^^^^ + +export {}; + diff --git a/tests/cases/compiler/unusedTypeDeclarations.ts b/tests/cases/compiler/unusedTypeDeclarations.ts new file mode 100644 index 0000000000000..364b458323a25 --- /dev/null +++ b/tests/cases/compiler/unusedTypeDeclarations.ts @@ -0,0 +1,6 @@ +// @noUnusedLocals: true + +type T1 = number; // error +type _T2 = number; // ok + +export {};