Consider underscore for type parameters in unused-local checks (#18539)

* Consider underscore for type parameters in unused-local errors.

* Addressed review comments.
This commit is contained in:
Magnus Kulke 2017-09-20 01:57:26 +02:00 committed by Mohamed Hegazy
parent ab6bb1618f
commit b549e26665
4 changed files with 54 additions and 2 deletions

View file

@ -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));
}
}

View file

@ -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.

View file

@ -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;
}());
;

View file

@ -0,0 +1,9 @@
//@noUnusedLocals:true
function f<_T, U>() { }
type T<_T, U> = { };
interface I<_T, U> { };
class C<_T, U> { };