Skip to content

Commit b549e26

Browse files
mkulkemhegazy
authored andcommitted
Consider underscore for type parameters in unused-local checks (#18539)
* Consider underscore for type parameters in unused-local errors. * Addressed review comments.
1 parent ab6bb16 commit b549e26

File tree

4 files changed

+54
-2
lines changed

4 files changed

+54
-2
lines changed

src/compiler/checker.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19973,7 +19973,8 @@ namespace ts {
1997319973
const node = getNameOfDeclaration(declaration) || declaration;
1997419974
if (isIdentifierThatStartsWithUnderScore(node)) {
1997519975
const declaration = getRootDeclaration(node.parent);
19976-
if (declaration.kind === SyntaxKind.VariableDeclaration && isForInOrOfStatement(declaration.parent.parent)) {
19976+
if ((declaration.kind === SyntaxKind.VariableDeclaration && isForInOrOfStatement(declaration.parent.parent)) ||
19977+
declaration.kind === SyntaxKind.TypeParameter) {
1997719978
return;
1997819979
}
1997919980
}
@@ -20023,7 +20024,7 @@ namespace ts {
2002320024
return;
2002420025
}
2002520026
for (const typeParameter of node.typeParameters) {
20026-
if (!getMergedSymbol(typeParameter.symbol).isReferenced) {
20027+
if (!getMergedSymbol(typeParameter.symbol).isReferenced && !isIdentifierThatStartsWithUnderScore(typeParameter.name)) {
2002720028
error(typeParameter.name, Diagnostics._0_is_declared_but_its_value_is_never_read, unescapeLeadingUnderscores(typeParameter.symbol.escapedName));
2002820029
}
2002920030
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
tests/cases/compiler/unusedTypeParametersWithUnderscore.ts(1,16): error TS6133: 'U' is declared but its value is never read.
2+
tests/cases/compiler/unusedTypeParametersWithUnderscore.ts(3,12): error TS6133: 'U' is declared but its value is never read.
3+
tests/cases/compiler/unusedTypeParametersWithUnderscore.ts(5,17): error TS6133: 'U' is declared but its value is never read.
4+
tests/cases/compiler/unusedTypeParametersWithUnderscore.ts(7,13): error TS6133: 'U' is declared but its value is never read.
5+
6+
7+
==== tests/cases/compiler/unusedTypeParametersWithUnderscore.ts (4 errors) ====
8+
function f<_T, U>() { }
9+
~
10+
!!! error TS6133: 'U' is declared but its value is never read.
11+
12+
type T<_T, U> = { };
13+
~
14+
!!! error TS6133: 'U' is declared but its value is never read.
15+
16+
interface I<_T, U> { };
17+
~
18+
!!! error TS6133: 'U' is declared but its value is never read.
19+
20+
class C<_T, U> { };
21+
~
22+
!!! error TS6133: 'U' is declared but its value is never read.
23+
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//// [unusedTypeParametersWithUnderscore.ts]
2+
function f<_T, U>() { }
3+
4+
type T<_T, U> = { };
5+
6+
interface I<_T, U> { };
7+
8+
class C<_T, U> { };
9+
10+
11+
//// [unusedTypeParametersWithUnderscore.js]
12+
function f() { }
13+
;
14+
var C = /** @class */ (function () {
15+
function C() {
16+
}
17+
return C;
18+
}());
19+
;
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
//@noUnusedLocals:true
2+
3+
function f<_T, U>() { }
4+
5+
type T<_T, U> = { };
6+
7+
interface I<_T, U> { };
8+
9+
class C<_T, U> { };

0 commit comments

Comments
 (0)