Skip to content

Commit 88058e0

Browse files
committed
Baseline isTypeParameterUnused with new tests
This commit introduces new tests against the baseline (ie, unchanged from before this PR) `isTypeParameterUnused` implementation.
1 parent 191625e commit 88058e0

12 files changed

+1032
-90
lines changed

src/compiler/checker.ts

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44390,7 +44390,6 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
4439044390
}
4439144391
}
4439244392
}
44393-
4439444393
function isTypeParameterUnused(typeParameter: TypeParameterDeclaration): boolean {
4439544394
return !(getMergedSymbol(typeParameter.symbol).isReferenced! & SymbolFlags.TypeParameter) && !isIdentifierThatStartsWithUnderscore(typeParameter.name);
4439644395
}
@@ -44411,13 +44410,6 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
4441144410
}
4441244411

4441344412
function isValidUnusedLocalDeclaration(declaration: Declaration): boolean {
44414-
if (isTypeAliasDeclaration(declaration)) {
44415-
/**
44416-
* ignore starts with underscore names _
44417-
* type _T = number;
44418-
*/
44419-
return isIdentifierThatStartsWithUnderscore(declaration.name);
44420-
}
4442144413
if (isBindingElement(declaration)) {
4442244414
if (isObjectBindingPattern(declaration.parent)) {
4442344415
/**
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
noUnusedLocals_types.ts(4,6): error TS6196: 'UnusedType1' is declared but never used.
2+
noUnusedLocals_types.ts(5,11): error TS6196: 'UnusedInterface1' is declared but never used.
3+
noUnusedLocals_types.ts(6,7): error TS6196: 'UnusedClass1' is declared but never used.
4+
noUnusedLocals_types.ts(9,6): error TS6196: '_UnusedType2' is declared but never used.
5+
noUnusedLocals_types.ts(10,11): error TS6196: '_UnusedInterface2' is declared but never used.
6+
noUnusedLocals_types.ts(11,7): error TS6196: '_UnusedClass2' is declared but never used.
7+
noUnusedLocals_types.ts(15,6): error TS6196: '_Helper' is declared but never used.
8+
9+
10+
==== noUnusedLocals_types.ts (7 errors) ====
11+
// Test specifically for type declarations with underscore prefix
12+
13+
// These should all produce errors (no underscore)
14+
type UnusedType1 = string;
15+
~~~~~~~~~~~
16+
!!! error TS6196: 'UnusedType1' is declared but never used.
17+
interface UnusedInterface1 { x: number; }
18+
~~~~~~~~~~~~~~~~
19+
!!! error TS6196: 'UnusedInterface1' is declared but never used.
20+
class UnusedClass1 { }
21+
~~~~~~~~~~~~
22+
!!! error TS6196: 'UnusedClass1' is declared but never used.
23+
24+
// These should NOT produce errors (underscore prefix)
25+
type _UnusedType2 = string;
26+
~~~~~~~~~~~~
27+
!!! error TS6196: '_UnusedType2' is declared but never used.
28+
interface _UnusedInterface2 { x: number; }
29+
~~~~~~~~~~~~~~~~~
30+
!!! error TS6196: '_UnusedInterface2' is declared but never used.
31+
class _UnusedClass2 { }
32+
~~~~~~~~~~~~~
33+
!!! error TS6196: '_UnusedClass2' is declared but never used.
34+
35+
// Mixed usage - only the one without underscore should error
36+
type UsedInOther = number;
37+
type _Helper = UsedInOther; // _Helper is not an error, but it uses UsedInOther
38+
~~~~~~~
39+
!!! error TS6196: '_Helper' is declared but never used.
40+
41+
export {};
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
//// [tests/cases/compiler/noUnusedLocals_types.ts] ////
2+
3+
//// [noUnusedLocals_types.ts]
4+
// Test specifically for type declarations with underscore prefix
5+
6+
// These should all produce errors (no underscore)
7+
type UnusedType1 = string;
8+
interface UnusedInterface1 { x: number; }
9+
class UnusedClass1 { }
10+
11+
// These should NOT produce errors (underscore prefix)
12+
type _UnusedType2 = string;
13+
interface _UnusedInterface2 { x: number; }
14+
class _UnusedClass2 { }
15+
16+
// Mixed usage - only the one without underscore should error
17+
type UsedInOther = number;
18+
type _Helper = UsedInOther; // _Helper is not an error, but it uses UsedInOther
19+
20+
export {};
21+
22+
//// [noUnusedLocals_types.js]
23+
"use strict";
24+
// Test specifically for type declarations with underscore prefix
25+
Object.defineProperty(exports, "__esModule", { value: true });
26+
var UnusedClass1 = /** @class */ (function () {
27+
function UnusedClass1() {
28+
}
29+
return UnusedClass1;
30+
}());
31+
var _UnusedClass2 = /** @class */ (function () {
32+
function _UnusedClass2() {
33+
}
34+
return _UnusedClass2;
35+
}());
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//// [tests/cases/compiler/noUnusedLocals_types.ts] ////
2+
3+
=== noUnusedLocals_types.ts ===
4+
// Test specifically for type declarations with underscore prefix
5+
6+
// These should all produce errors (no underscore)
7+
type UnusedType1 = string;
8+
>UnusedType1 : Symbol(UnusedType1, Decl(noUnusedLocals_types.ts, 0, 0))
9+
10+
interface UnusedInterface1 { x: number; }
11+
>UnusedInterface1 : Symbol(UnusedInterface1, Decl(noUnusedLocals_types.ts, 3, 26))
12+
>x : Symbol(UnusedInterface1.x, Decl(noUnusedLocals_types.ts, 4, 28))
13+
14+
class UnusedClass1 { }
15+
>UnusedClass1 : Symbol(UnusedClass1, Decl(noUnusedLocals_types.ts, 4, 41))
16+
17+
// These should NOT produce errors (underscore prefix)
18+
type _UnusedType2 = string;
19+
>_UnusedType2 : Symbol(_UnusedType2, Decl(noUnusedLocals_types.ts, 5, 22))
20+
21+
interface _UnusedInterface2 { x: number; }
22+
>_UnusedInterface2 : Symbol(_UnusedInterface2, Decl(noUnusedLocals_types.ts, 8, 27))
23+
>x : Symbol(_UnusedInterface2.x, Decl(noUnusedLocals_types.ts, 9, 29))
24+
25+
class _UnusedClass2 { }
26+
>_UnusedClass2 : Symbol(_UnusedClass2, Decl(noUnusedLocals_types.ts, 9, 42))
27+
28+
// Mixed usage - only the one without underscore should error
29+
type UsedInOther = number;
30+
>UsedInOther : Symbol(UsedInOther, Decl(noUnusedLocals_types.ts, 10, 23))
31+
32+
type _Helper = UsedInOther; // _Helper is not an error, but it uses UsedInOther
33+
>_Helper : Symbol(_Helper, Decl(noUnusedLocals_types.ts, 13, 26))
34+
>UsedInOther : Symbol(UsedInOther, Decl(noUnusedLocals_types.ts, 10, 23))
35+
36+
export {};
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
//// [tests/cases/compiler/noUnusedLocals_types.ts] ////
2+
3+
=== noUnusedLocals_types.ts ===
4+
// Test specifically for type declarations with underscore prefix
5+
6+
// These should all produce errors (no underscore)
7+
type UnusedType1 = string;
8+
>UnusedType1 : string
9+
> : ^^^^^^
10+
11+
interface UnusedInterface1 { x: number; }
12+
>x : number
13+
> : ^^^^^^
14+
15+
class UnusedClass1 { }
16+
>UnusedClass1 : UnusedClass1
17+
> : ^^^^^^^^^^^^
18+
19+
// These should NOT produce errors (underscore prefix)
20+
type _UnusedType2 = string;
21+
>_UnusedType2 : string
22+
> : ^^^^^^
23+
24+
interface _UnusedInterface2 { x: number; }
25+
>x : number
26+
> : ^^^^^^
27+
28+
class _UnusedClass2 { }
29+
>_UnusedClass2 : _UnusedClass2
30+
> : ^^^^^^^^^^^^^
31+
32+
// Mixed usage - only the one without underscore should error
33+
type UsedInOther = number;
34+
>UsedInOther : number
35+
> : ^^^^^^
36+
37+
type _Helper = UsedInOther; // _Helper is not an error, but it uses UsedInOther
38+
>_Helper : number
39+
> : ^^^^^^
40+
41+
export {};
Lines changed: 182 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,189 @@
1-
/a.ts(7,11): error TS6133: '_ns' is declared but its value is never read.
2-
/a.ts(8,9): error TS6133: '_' is declared but its value is never read.
1+
unusedLocalsStartingWithUnderscore.ts(2,7): error TS6133: 'unusedVar' is declared but its value is never read.
2+
unusedLocalsStartingWithUnderscore.ts(3,7): error TS6133: '_unusedVar' is declared but its value is never read.
3+
unusedLocalsStartingWithUnderscore.ts(5,5): error TS6133: 'unusedLet' is declared but its value is never read.
4+
unusedLocalsStartingWithUnderscore.ts(6,5): error TS6133: '_unusedLet' is declared but its value is never read.
5+
unusedLocalsStartingWithUnderscore.ts(8,5): error TS6133: 'unusedVar2' is declared but its value is never read.
6+
unusedLocalsStartingWithUnderscore.ts(9,5): error TS6133: '_unusedVar2' is declared but its value is never read.
7+
unusedLocalsStartingWithUnderscore.ts(11,7): error TS6198: All destructured elements are unused.
8+
unusedLocalsStartingWithUnderscore.ts(12,7): error TS6198: All destructured elements are unused.
9+
unusedLocalsStartingWithUnderscore.ts(15,10): error TS6133: 'unusedFunc' is declared but its value is never read.
10+
unusedLocalsStartingWithUnderscore.ts(16,10): error TS6133: '_unusedFunc' is declared but its value is never read.
11+
unusedLocalsStartingWithUnderscore.ts(18,7): error TS6133: 'unusedArrow' is declared but its value is never read.
12+
unusedLocalsStartingWithUnderscore.ts(19,7): error TS6133: '_unusedArrow' is declared but its value is never read.
13+
unusedLocalsStartingWithUnderscore.ts(22,7): error TS6196: 'UnusedClass' is declared but never used.
14+
unusedLocalsStartingWithUnderscore.ts(23,7): error TS6196: '_UnusedClass' is declared but never used.
15+
unusedLocalsStartingWithUnderscore.ts(26,11): error TS6196: 'UnusedInterface' is declared but never used.
16+
unusedLocalsStartingWithUnderscore.ts(27,11): error TS6196: '_UnusedInterface' is declared but never used.
17+
unusedLocalsStartingWithUnderscore.ts(30,6): error TS6196: 'UnusedType' is declared but never used.
18+
unusedLocalsStartingWithUnderscore.ts(31,6): error TS6196: '_UnusedType' is declared but never used.
19+
unusedLocalsStartingWithUnderscore.ts(34,6): error TS6196: 'UnusedEnum' is declared but never used.
20+
unusedLocalsStartingWithUnderscore.ts(35,6): error TS6196: '_UnusedEnum' is declared but never used.
21+
unusedLocalsStartingWithUnderscore.ts(39,12): error TS6133: 'x' is declared but its value is never read.
22+
unusedLocalsStartingWithUnderscore.ts(41,12): error TS6133: 'x' is declared but its value is never read.
23+
unusedLocalsStartingWithUnderscore.ts(44,11): error TS6133: 'UnusedNamespace' is declared but its value is never read.
24+
unusedLocalsStartingWithUnderscore.ts(47,11): error TS6133: '_UnusedNamespace' is declared but its value is never read.
25+
unusedLocalsStartingWithUnderscore.ts(52,7): error TS6133: 'unusedA' is declared but its value is never read.
26+
unusedLocalsStartingWithUnderscore.ts(54,8): error TS6133: 'unusedC' is declared but its value is never read.
27+
unusedLocalsStartingWithUnderscore.ts(61,7): error TS6196: 'TestClass' is declared but never used.
28+
unusedLocalsStartingWithUnderscore.ts(62,13): error TS6133: 'unusedMember' is declared but its value is never read.
29+
unusedLocalsStartingWithUnderscore.ts(63,13): error TS6133: '_unusedMember' is declared but its value is never read.
30+
unusedLocalsStartingWithUnderscore.ts(65,13): error TS6133: 'unusedMethod' is declared but its value is never read.
31+
unusedLocalsStartingWithUnderscore.ts(66,13): error TS6133: '_unusedMethod' is declared but its value is never read.
32+
unusedLocalsStartingWithUnderscore.ts(69,6): error TS6196: 'TestEnum' is declared but never used.
33+
unusedLocalsStartingWithUnderscore.ts(74,11): error TS6196: 'TestInterface' is declared but never used.
34+
unusedLocalsStartingWithUnderscore.ts(79,7): error TS6133: 'obj' is declared but its value is never read.
335

436

5-
==== /a.ts (2 errors) ====
6-
import * as _ from "./a";
37+
==== unusedLocalsStartingWithUnderscore.ts (34 errors) ====
38+
// Variables
39+
const unusedVar = 1; // error
40+
~~~~~~~~~
41+
!!! error TS6133: 'unusedVar' is declared but its value is never read.
42+
const _unusedVar = 2; // ok
43+
~~~~~~~~~~
44+
!!! error TS6133: '_unusedVar' is declared but its value is never read.
745

8-
for (const _ of []) { }
46+
let unusedLet = 3; // error
47+
~~~~~~~~~
48+
!!! error TS6133: 'unusedLet' is declared but its value is never read.
49+
let _unusedLet = 4; // ok
50+
~~~~~~~~~~
51+
!!! error TS6133: '_unusedLet' is declared but its value is never read.
952

10-
for (const _ in []) { }
53+
var unusedVar2 = 5; // error
54+
~~~~~~~~~~
55+
!!! error TS6133: 'unusedVar2' is declared but its value is never read.
56+
var _unusedVar2 = 6; // ok
57+
~~~~~~~~~~~
58+
!!! error TS6133: '_unusedVar2' is declared but its value is never read.
1159

12-
namespace _ns {
13-
~~~
14-
!!! error TS6133: '_ns' is declared but its value is never read.
15-
let _;
16-
~
17-
!!! error TS6133: '_' is declared but its value is never read.
18-
for (const _ of []) { }
60+
const { a1, _b1 } = { a1: 1, _b1: 2 }; // error on a1
61+
~~~~~~~~~~~
62+
!!! error TS6198: All destructured elements are unused.
63+
const { _a2, _b2 } = { _a2: 1, _b2: 2 }; // ok
64+
~~~~~~~~~~~~
65+
!!! error TS6198: All destructured elements are unused.
1966

20-
for (const _ in []) { }
67+
// Functions
68+
function unusedFunc() { } // error
69+
~~~~~~~~~~
70+
!!! error TS6133: 'unusedFunc' is declared but its value is never read.
71+
function _unusedFunc() { } // ok
72+
~~~~~~~~~~~
73+
!!! error TS6133: '_unusedFunc' is declared but its value is never read.
74+
75+
const unusedArrow = () => { }; // error
76+
~~~~~~~~~~~
77+
!!! error TS6133: 'unusedArrow' is declared but its value is never read.
78+
const _unusedArrow = () => { }; // ok
79+
~~~~~~~~~~~~
80+
!!! error TS6133: '_unusedArrow' is declared but its value is never read.
81+
82+
// Classes
83+
class UnusedClass { } // error
84+
~~~~~~~~~~~
85+
!!! error TS6196: 'UnusedClass' is declared but never used.
86+
class _UnusedClass { } // ok
87+
~~~~~~~~~~~~
88+
!!! error TS6196: '_UnusedClass' is declared but never used.
89+
90+
// Interfaces
91+
interface UnusedInterface { } // error
92+
~~~~~~~~~~~~~~~
93+
!!! error TS6196: 'UnusedInterface' is declared but never used.
94+
interface _UnusedInterface { } // ok
95+
~~~~~~~~~~~~~~~~
96+
!!! error TS6196: '_UnusedInterface' is declared but never used.
97+
98+
// Type aliases
99+
type UnusedType = string; // error
100+
~~~~~~~~~~
101+
!!! error TS6196: 'UnusedType' is declared but never used.
102+
type _UnusedType = string; // ok
103+
~~~~~~~~~~~
104+
!!! error TS6196: '_UnusedType' is declared but never used.
105+
106+
// Enums
107+
enum UnusedEnum { A } // error
108+
~~~~~~~~~~
109+
!!! error TS6196: 'UnusedEnum' is declared but never used.
110+
enum _UnusedEnum { A } // ok
111+
~~~~~~~~~~~
112+
!!! error TS6196: '_UnusedEnum' is declared but never used.
113+
114+
// Declarations in for loops
115+
for (const _x of []) { } // ok
116+
for (const x of []) { } // error
117+
~
118+
!!! error TS6133: 'x' is declared but its value is never read.
119+
for (const _x in []) { } // ok
120+
for (const x in []) { } // error
121+
~
122+
!!! error TS6133: 'x' is declared but its value is never read.
123+
124+
// Namespaces
125+
namespace UnusedNamespace { // error
126+
~~~~~~~~~~~~~~~
127+
!!! error TS6133: 'UnusedNamespace' is declared but its value is never read.
128+
export const x = 1;
129+
}
130+
namespace _UnusedNamespace { // ok
131+
~~~~~~~~~~~~~~~~
132+
!!! error TS6133: '_UnusedNamespace' is declared but its value is never read.
133+
export const x = 1;
21134
}
22-
135+
136+
// Destructuring
137+
const { a: unusedA } = { a: 1 }; // error
138+
~~~~~~~~~~~~~~
139+
!!! error TS6133: 'unusedA' is declared but its value is never read.
140+
const { b: _unusedB } = { b: 2 }; // ok
141+
const [unusedC] = [3]; // error
142+
~~~~~~~
143+
!!! error TS6133: 'unusedC' is declared but its value is never read.
144+
const [_unusedD] = [4]; // ok
145+
146+
//
147+
// The following declarations may _not_ use an underscore to bypass @noUnusedLocals
148+
//
149+
150+
class TestClass {
151+
~~~~~~~~~
152+
!!! error TS6196: 'TestClass' is declared but never used.
153+
private unusedMember = 1; // error
154+
~~~~~~~~~~~~
155+
!!! error TS6133: 'unusedMember' is declared but its value is never read.
156+
private _unusedMember = 2; // still error
157+
~~~~~~~~~~~~~
158+
!!! error TS6133: '_unusedMember' is declared but its value is never read.
159+
160+
private unusedMethod() { } // error
161+
~~~~~~~~~~~~
162+
!!! error TS6133: 'unusedMethod' is declared but its value is never read.
163+
private _unusedMethod() { } // still error
164+
~~~~~~~~~~~~~
165+
!!! error TS6133: '_unusedMethod' is declared but its value is never read.
166+
}
167+
168+
enum TestEnum {
169+
~~~~~~~~
170+
!!! error TS6196: 'TestEnum' is declared but never used.
171+
UnusedMember = 1, // error
172+
_UnusedMember = 2, // still error
173+
}
174+
175+
interface TestInterface {
176+
~~~~~~~~~~~~~
177+
!!! error TS6196: 'TestInterface' is declared but never used.
178+
unusedProp: number; // error
179+
_unusedProp: number; // still error
180+
}
181+
182+
const obj = {
183+
~~~
184+
!!! error TS6133: 'obj' is declared but its value is never read.
185+
unusedProp: 1, // error
186+
_unusedProp: 2, // still error
187+
};
188+
189+
export { };

0 commit comments

Comments
 (0)