Skip to content

Commit 359646b

Browse files
authored
Fixed a regression with reporting unused parameters in potential predicates (microsoft#58514)
1 parent 346df34 commit 359646b

5 files changed

+60
-2
lines changed

src/compiler/utilities.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -11461,7 +11461,7 @@ export function createNameResolver({
1146111461
}
1146211462
break;
1146311463
}
11464-
if (isSelfReferenceLocation(location)) {
11464+
if (isSelfReferenceLocation(location, lastLocation)) {
1146511465
lastSelfReferenceLocation = location;
1146611466
}
1146711467
lastLocation = location;
@@ -11595,15 +11595,18 @@ export function createNameResolver({
1159511595
}
1159611596

1159711597
type SelfReferenceLocation =
11598+
| ParameterDeclaration
1159811599
| FunctionDeclaration
1159911600
| ClassDeclaration
1160011601
| InterfaceDeclaration
1160111602
| EnumDeclaration
1160211603
| TypeAliasDeclaration
1160311604
| ModuleDeclaration;
1160411605

11605-
function isSelfReferenceLocation(node: Node): node is SelfReferenceLocation {
11606+
function isSelfReferenceLocation(node: Node, lastLocation: Node | undefined): node is SelfReferenceLocation {
1160611607
switch (node.kind) {
11608+
case SyntaxKind.Parameter:
11609+
return !!lastLocation && lastLocation === (node as ParameterDeclaration).name;
1160711610
case SyntaxKind.FunctionDeclaration:
1160811611
case SyntaxKind.ClassDeclaration:
1160911612
case SyntaxKind.InterfaceDeclaration:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
noUnusedLocals_potentialPredicateUnusedParam.ts(1,40): error TS6133: 'a' is declared but its value is never read.
2+
3+
4+
==== noUnusedLocals_potentialPredicateUnusedParam.ts (1 errors) ====
5+
function potentialPredicateUnusedParam(a: unknown) {
6+
~
7+
!!! error TS6133: 'a' is declared but its value is never read.
8+
return !!Math.random();
9+
}
10+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//// [tests/cases/compiler/noUnusedLocals_potentialPredicateUnusedParam.ts] ////
2+
3+
=== noUnusedLocals_potentialPredicateUnusedParam.ts ===
4+
function potentialPredicateUnusedParam(a: unknown) {
5+
>potentialPredicateUnusedParam : Symbol(potentialPredicateUnusedParam, Decl(noUnusedLocals_potentialPredicateUnusedParam.ts, 0, 0))
6+
>a : Symbol(a, Decl(noUnusedLocals_potentialPredicateUnusedParam.ts, 0, 39))
7+
8+
return !!Math.random();
9+
>Math.random : Symbol(Math.random, Decl(lib.es5.d.ts, --, --))
10+
>Math : Symbol(Math, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
11+
>random : Symbol(Math.random, Decl(lib.es5.d.ts, --, --))
12+
}
13+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//// [tests/cases/compiler/noUnusedLocals_potentialPredicateUnusedParam.ts] ////
2+
3+
=== noUnusedLocals_potentialPredicateUnusedParam.ts ===
4+
function potentialPredicateUnusedParam(a: unknown) {
5+
>potentialPredicateUnusedParam : (a: unknown) => boolean
6+
> : ^ ^^ ^^^^^^^^^^^^
7+
>a : unknown
8+
> : ^^^^^^^
9+
10+
return !!Math.random();
11+
>!!Math.random() : boolean
12+
> : ^^^^^^^
13+
>!Math.random() : boolean
14+
> : ^^^^^^^
15+
>Math.random() : number
16+
> : ^^^^^^
17+
>Math.random : () => number
18+
> : ^^^^^^
19+
>Math : Math
20+
> : ^^^^
21+
>random : () => number
22+
> : ^^^^^^
23+
}
24+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// @strict: true
2+
// @noEmit: true
3+
// @noUnusedLocals: true
4+
// @noUnusedParameters: true
5+
6+
function potentialPredicateUnusedParam(a: unknown) {
7+
return !!Math.random();
8+
}

0 commit comments

Comments
 (0)