From 27ad6f180c141301fa218675d6a41d4e09073c8b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20Burzy=C5=84ski?= Date: Mon, 13 May 2024 09:59:59 +0200 Subject: [PATCH 1/3] Fixed a regression with reporting unused parameters in potential predicates --- src/compiler/utilities.ts | 12 +++++++--- ...s_potentialPredicateUnusedParam.errors.txt | 10 ++++++++ ...cals_potentialPredicateUnusedParam.symbols | 13 ++++++++++ ...Locals_potentialPredicateUnusedParam.types | 24 +++++++++++++++++++ ...sedLocals_potentialPredicateUnusedParam.ts | 8 +++++++ 5 files changed, 64 insertions(+), 3 deletions(-) create mode 100644 tests/baselines/reference/noUnusedLocals_potentialPredicateUnusedParam.errors.txt create mode 100644 tests/baselines/reference/noUnusedLocals_potentialPredicateUnusedParam.symbols create mode 100644 tests/baselines/reference/noUnusedLocals_potentialPredicateUnusedParam.types create mode 100644 tests/cases/compiler/noUnusedLocals_potentialPredicateUnusedParam.ts diff --git a/src/compiler/utilities.ts b/src/compiler/utilities.ts index 655c35eacf576..e6c128257ce95 100644 --- a/src/compiler/utilities.ts +++ b/src/compiler/utilities.ts @@ -11360,11 +11360,15 @@ export function createNameResolver({ location = root.parent; } break; - case SyntaxKind.Parameter: + case SyntaxKind.Parameter: { + const param = location as ParameterDeclaration; + if (lastLocation === param.name) { + lastSelfReferenceLocation = param; + } if ( lastLocation && ( - lastLocation === (location as ParameterDeclaration).initializer || - lastLocation === (location as ParameterDeclaration).name && isBindingPattern(lastLocation) + lastLocation === param.initializer || + lastLocation === param.name && isBindingPattern(lastLocation) ) ) { if (!associatedDeclarationForContainingInitializerOrBindingName) { @@ -11372,6 +11376,7 @@ export function createNameResolver({ } } break; + } case SyntaxKind.BindingElement: if ( lastLocation && ( @@ -11538,6 +11543,7 @@ export function createNameResolver({ } type SelfReferenceLocation = + | ParameterDeclaration | FunctionDeclaration | ClassDeclaration | InterfaceDeclaration diff --git a/tests/baselines/reference/noUnusedLocals_potentialPredicateUnusedParam.errors.txt b/tests/baselines/reference/noUnusedLocals_potentialPredicateUnusedParam.errors.txt new file mode 100644 index 0000000000000..929eb5a565a75 --- /dev/null +++ b/tests/baselines/reference/noUnusedLocals_potentialPredicateUnusedParam.errors.txt @@ -0,0 +1,10 @@ +noUnusedLocals_potentialPredicateUnusedParam.ts(1,40): error TS6133: 'a' is declared but its value is never read. + + +==== noUnusedLocals_potentialPredicateUnusedParam.ts (1 errors) ==== + function potentialPredicateUnusedParam(a: unknown) { + ~ +!!! error TS6133: 'a' is declared but its value is never read. + return !!Math.random(); + } + \ No newline at end of file diff --git a/tests/baselines/reference/noUnusedLocals_potentialPredicateUnusedParam.symbols b/tests/baselines/reference/noUnusedLocals_potentialPredicateUnusedParam.symbols new file mode 100644 index 0000000000000..3decedaf23158 --- /dev/null +++ b/tests/baselines/reference/noUnusedLocals_potentialPredicateUnusedParam.symbols @@ -0,0 +1,13 @@ +//// [tests/cases/compiler/noUnusedLocals_potentialPredicateUnusedParam.ts] //// + +=== noUnusedLocals_potentialPredicateUnusedParam.ts === +function potentialPredicateUnusedParam(a: unknown) { +>potentialPredicateUnusedParam : Symbol(potentialPredicateUnusedParam, Decl(noUnusedLocals_potentialPredicateUnusedParam.ts, 0, 0)) +>a : Symbol(a, Decl(noUnusedLocals_potentialPredicateUnusedParam.ts, 0, 39)) + + return !!Math.random(); +>Math.random : Symbol(Math.random, Decl(lib.es5.d.ts, --, --)) +>Math : Symbol(Math, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --)) +>random : Symbol(Math.random, Decl(lib.es5.d.ts, --, --)) +} + diff --git a/tests/baselines/reference/noUnusedLocals_potentialPredicateUnusedParam.types b/tests/baselines/reference/noUnusedLocals_potentialPredicateUnusedParam.types new file mode 100644 index 0000000000000..a745d58782cc4 --- /dev/null +++ b/tests/baselines/reference/noUnusedLocals_potentialPredicateUnusedParam.types @@ -0,0 +1,24 @@ +//// [tests/cases/compiler/noUnusedLocals_potentialPredicateUnusedParam.ts] //// + +=== noUnusedLocals_potentialPredicateUnusedParam.ts === +function potentialPredicateUnusedParam(a: unknown) { +>potentialPredicateUnusedParam : (a: unknown) => boolean +> : ^ ^^ ^^^^^^^^^^^^ +>a : unknown +> : ^^^^^^^ + + return !!Math.random(); +>!!Math.random() : boolean +> : ^^^^^^^ +>!Math.random() : boolean +> : ^^^^^^^ +>Math.random() : number +> : ^^^^^^ +>Math.random : () => number +> : ^^^^^^^^^^^^ +>Math : Math +> : ^^^^ +>random : () => number +> : ^^^^^^^^^^^^ +} + diff --git a/tests/cases/compiler/noUnusedLocals_potentialPredicateUnusedParam.ts b/tests/cases/compiler/noUnusedLocals_potentialPredicateUnusedParam.ts new file mode 100644 index 0000000000000..f711939d6d7b9 --- /dev/null +++ b/tests/cases/compiler/noUnusedLocals_potentialPredicateUnusedParam.ts @@ -0,0 +1,8 @@ +// @strict: true +// @noEmit: true +// @noUnusedLocals: true +// @noUnusedParameters: true + +function potentialPredicateUnusedParam(a: unknown) { + return !!Math.random(); +} From 3d0b2b8342d3d16a1d0e5c334eae91a8bbf619b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20Burzy=C5=84ski?= Date: Mon, 10 Jun 2024 10:13:10 +0200 Subject: [PATCH 2/3] address PR feedback --- src/compiler/utilities.ts | 14 ++++++-------- ...nusedLocals_potentialPredicateUnusedParam.types | 4 ++-- 2 files changed, 8 insertions(+), 10 deletions(-) diff --git a/src/compiler/utilities.ts b/src/compiler/utilities.ts index 62fce5ddefac0..c15679f9c6ae8 100644 --- a/src/compiler/utilities.ts +++ b/src/compiler/utilities.ts @@ -11418,14 +11418,10 @@ export function createNameResolver({ } break; case SyntaxKind.Parameter: { - const param = location as ParameterDeclaration; - if (lastLocation === param.name) { - lastSelfReferenceLocation = param; - } if ( lastLocation && ( - lastLocation === param.initializer || - lastLocation === param.name && isBindingPattern(lastLocation) + lastLocation === (location as ParameterDeclaration).initializer || + lastLocation === (location as ParameterDeclaration).name && isBindingPattern(lastLocation) ) ) { if (!associatedDeclarationForContainingInitializerOrBindingName) { @@ -11466,7 +11462,7 @@ export function createNameResolver({ } break; } - if (isSelfReferenceLocation(location)) { + if (isSelfReferenceLocation(location, lastLocation)) { lastSelfReferenceLocation = location; } lastLocation = location; @@ -11608,8 +11604,10 @@ export function createNameResolver({ | TypeAliasDeclaration | ModuleDeclaration; - function isSelfReferenceLocation(node: Node): node is SelfReferenceLocation { + function isSelfReferenceLocation(node: Node, lastLocation: Node | undefined): node is SelfReferenceLocation { switch (node.kind) { + case SyntaxKind.Parameter: + return !!lastLocation && lastLocation === (node as ParameterDeclaration).name; case SyntaxKind.FunctionDeclaration: case SyntaxKind.ClassDeclaration: case SyntaxKind.InterfaceDeclaration: diff --git a/tests/baselines/reference/noUnusedLocals_potentialPredicateUnusedParam.types b/tests/baselines/reference/noUnusedLocals_potentialPredicateUnusedParam.types index a745d58782cc4..090e1ae0fb609 100644 --- a/tests/baselines/reference/noUnusedLocals_potentialPredicateUnusedParam.types +++ b/tests/baselines/reference/noUnusedLocals_potentialPredicateUnusedParam.types @@ -15,10 +15,10 @@ function potentialPredicateUnusedParam(a: unknown) { >Math.random() : number > : ^^^^^^ >Math.random : () => number -> : ^^^^^^^^^^^^ +> : ^^^^^^ >Math : Math > : ^^^^ >random : () => number -> : ^^^^^^^^^^^^ +> : ^^^^^^ } From 0846b3620ab011b357f3b34d77d30c9992bcd185 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20Burzy=C5=84ski?= Date: Mon, 10 Jun 2024 19:13:13 +0200 Subject: [PATCH 3/3] remove leftover formatting change --- src/compiler/utilities.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/compiler/utilities.ts b/src/compiler/utilities.ts index c15679f9c6ae8..1a20f59e2cb1e 100644 --- a/src/compiler/utilities.ts +++ b/src/compiler/utilities.ts @@ -11417,7 +11417,7 @@ export function createNameResolver({ location = root.parent; } break; - case SyntaxKind.Parameter: { + case SyntaxKind.Parameter: if ( lastLocation && ( lastLocation === (location as ParameterDeclaration).initializer || @@ -11429,7 +11429,6 @@ export function createNameResolver({ } } break; - } case SyntaxKind.BindingElement: if ( lastLocation && (