From b549e2666567d39b0944173d7754850f7474f510 Mon Sep 17 00:00:00 2001 From: Magnus Kulke Date: Wed, 20 Sep 2017 01:57:26 +0200 Subject: [PATCH] Consider underscore for type parameters in unused-local checks (#18539) * Consider underscore for type parameters in unused-local errors. * Addressed review comments. --- src/compiler/checker.ts | 5 ++-- ...sedTypeParametersWithUnderscore.errors.txt | 23 +++++++++++++++++++ .../unusedTypeParametersWithUnderscore.js | 19 +++++++++++++++ .../unusedTypeParametersWithUnderscore.ts | 9 ++++++++ 4 files changed, 54 insertions(+), 2 deletions(-) create mode 100644 tests/baselines/reference/unusedTypeParametersWithUnderscore.errors.txt create mode 100644 tests/baselines/reference/unusedTypeParametersWithUnderscore.js create mode 100644 tests/cases/compiler/unusedTypeParametersWithUnderscore.ts diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 98b7a9d044f..e00465caa14 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -19973,7 +19973,8 @@ namespace ts { const node = getNameOfDeclaration(declaration) || declaration; if (isIdentifierThatStartsWithUnderScore(node)) { const declaration = getRootDeclaration(node.parent); - if (declaration.kind === SyntaxKind.VariableDeclaration && isForInOrOfStatement(declaration.parent.parent)) { + if ((declaration.kind === SyntaxKind.VariableDeclaration && isForInOrOfStatement(declaration.parent.parent)) || + declaration.kind === SyntaxKind.TypeParameter) { return; } } @@ -20023,7 +20024,7 @@ namespace ts { return; } for (const typeParameter of node.typeParameters) { - if (!getMergedSymbol(typeParameter.symbol).isReferenced) { + if (!getMergedSymbol(typeParameter.symbol).isReferenced && !isIdentifierThatStartsWithUnderScore(typeParameter.name)) { error(typeParameter.name, Diagnostics._0_is_declared_but_its_value_is_never_read, unescapeLeadingUnderscores(typeParameter.symbol.escapedName)); } } diff --git a/tests/baselines/reference/unusedTypeParametersWithUnderscore.errors.txt b/tests/baselines/reference/unusedTypeParametersWithUnderscore.errors.txt new file mode 100644 index 00000000000..cd44a68b78c --- /dev/null +++ b/tests/baselines/reference/unusedTypeParametersWithUnderscore.errors.txt @@ -0,0 +1,23 @@ +tests/cases/compiler/unusedTypeParametersWithUnderscore.ts(1,16): error TS6133: 'U' is declared but its value is never read. +tests/cases/compiler/unusedTypeParametersWithUnderscore.ts(3,12): error TS6133: 'U' is declared but its value is never read. +tests/cases/compiler/unusedTypeParametersWithUnderscore.ts(5,17): error TS6133: 'U' is declared but its value is never read. +tests/cases/compiler/unusedTypeParametersWithUnderscore.ts(7,13): error TS6133: 'U' is declared but its value is never read. + + +==== tests/cases/compiler/unusedTypeParametersWithUnderscore.ts (4 errors) ==== + function f<_T, U>() { } + ~ +!!! error TS6133: 'U' is declared but its value is never read. + + type T<_T, U> = { }; + ~ +!!! error TS6133: 'U' is declared but its value is never read. + + interface I<_T, U> { }; + ~ +!!! error TS6133: 'U' is declared but its value is never read. + + class C<_T, U> { }; + ~ +!!! error TS6133: 'U' is declared but its value is never read. + \ No newline at end of file diff --git a/tests/baselines/reference/unusedTypeParametersWithUnderscore.js b/tests/baselines/reference/unusedTypeParametersWithUnderscore.js new file mode 100644 index 00000000000..095cf2da85f --- /dev/null +++ b/tests/baselines/reference/unusedTypeParametersWithUnderscore.js @@ -0,0 +1,19 @@ +//// [unusedTypeParametersWithUnderscore.ts] +function f<_T, U>() { } + +type T<_T, U> = { }; + +interface I<_T, U> { }; + +class C<_T, U> { }; + + +//// [unusedTypeParametersWithUnderscore.js] +function f() { } +; +var C = /** @class */ (function () { + function C() { + } + return C; +}()); +; diff --git a/tests/cases/compiler/unusedTypeParametersWithUnderscore.ts b/tests/cases/compiler/unusedTypeParametersWithUnderscore.ts new file mode 100644 index 00000000000..dc66534118f --- /dev/null +++ b/tests/cases/compiler/unusedTypeParametersWithUnderscore.ts @@ -0,0 +1,9 @@ +//@noUnusedLocals:true + +function f<_T, U>() { } + +type T<_T, U> = { }; + +interface I<_T, U> { }; + +class C<_T, U> { };