Repository navigation
Conversation
A class static block runs as part of class evaluation, so a reference to the class from its own static block is a real use. Stop classifying that case as a self reference while keeping references from method bodies unused, as before. Fixes microsoft#57620
There was a problem hiding this comment.
🟢 Approval recommended
No active review issues were identified for the change to count class references inside static blocks as uses.
No open issues 🎉
What changed in this PR
Addresses #57620 by counting class references inside static blocks as uses, preventing false noUnusedLocals diagnostics.
Changes:
- Updates name resolution while preserving unused-class diagnostics for method-only self-references.
- Adds regression coverage for direct and nested static-block references, with expected baselines.
| File | Description |
|---|---|
tsc/internal/binder/nameresolver.go |
Exempts static-block references from class self-reference handling. |
tsc/testdata/tests/cases/compiler/noUnusedLocals_selfReference_inStaticBlock.ts |
Tests static-block uses and method-only self-references. |
tsc/testdata/baselines/reference/compiler/noUnusedLocals_selfReference_inStaticBlock.types |
Records expected types. |
tsc/testdata/baselines/reference/compiler/noUnusedLocals_selfReference_inStaticBlock.symbols |
Records expected symbol resolution. |
tsc/testdata/baselines/reference/compiler/noUnusedLocals_selfReference_inStaticBlock.js |
Records expected JavaScript output. |
tsc/testdata/baselines/reference/compiler/noUnusedLocals_selfReference_inStaticBlock.errors.txt |
Expects only the method-only class to be reported unused. |
Review effort: Balanced
Mohamed Rafik (Mllkmoha)
left a comment
There was a problem hiding this comment.
The new check also treats references inside deferred function bodies as immediate uses. For example:
// Target (@target): esnext
// @noUnusedLocals: true
export {};
class C {
static {
const later = () => {
C;
};
void later;
}
}
The arrow function is never invoked, so its body does not execute when the static block runs. On the PR branch, this suppresses the TS6196 diagnostic for C; on the base branch, the diagnostic is correctly reported. Could this check account for nested function-like boundaries, and could a regression test be added for this case?
A reference to the class from a nested function inside its own static block is deferred like a method body, so it should still count as a self-reference. Only static block code that executes during class definition counts as a real use.
|
Good catch — you're right, and I reproduced this on the branch. The check now tracks whether the lookup crossed a deferred function boundary inside the static block: code that runs directly (including an IIFE) still counts as a use, while nested arrow/function/method bodies remain self-references. Added a regression for your case plus an IIFE case; the full test suite passes. |
Fixes #57620
This PR was put together by Qoder Agent , working under direct human direction.
Motivation & Guidance
Motivation:
We were looking through TypeScript issues for something worth fixing, and this one was a good fit. The false positive under
--noUnusedLocalsblocks otherwise valid builds, and the issue had no assignee or open PR.Guidance:
The user pointed me at this specific issue and asked for a fix; the behavior was reproduced and verified locally before submission.
Analysis
While resolving a name, the binder keeps track of self-reference locations. If the symbol it finds is the last self-reference location, it skips
SymbolReferenced, so the declaration still looks unused.isSelfReferenceLocationsays yes for everyClassDeclaration. That makes sense for a method body (it only runs when called), but not for a static block, which runs immediately as part of class evaluation:This is still reported as
'BluethootDevice' is declared but never used.Fix
For a class declaration reached from one of its own static blocks, we no longer treat it as a self reference. Methods and other class elements behave as before, so
class C { m() { C; } }is still reported unused.Tests
Added
noUnusedLocals_selfReference_inStaticBlock.ts. It checks three things:The existing
noUnusedLocals_selfReference.tsandnoUnusedLocals_selfReference_skipsBlockLocations.tsbaselines are unchanged. The fullnpx hereby testsuite passes (173832 tests).