ISSUE DESCRIPTION linter: no-unused-vars false positive when constructor parameter…
- User
ISSUE DESCRIPTION
linter: no-unused-vars false positive when constructor parameter property shadows an imported identifier used in its own decorator
What version of Oxlint are you using?
1.65.0 (also reproduces on 1.30.0)
What command did you run?
oxlint test.tsWhat does your
.oxlintrc.json(oroxlint.config.ts) config file look like?// default (no config file)What happened?
When a TypeScript constructor parameter property has the same name as an outer binding (import or
const), and that outer binding is used in the parameter's own decorator expression (e.g.@Inject(foo.KEY)), oxlint'sno-unused-varsreports the outer binding as declared but never used.This is a false positive. Parameter decorators are evaluated at class-decoration time in the enclosing class scope, before any parameter binding exists in the parameter scope. So
fooinside@Inject(foo.KEY)resolves to the outer binding, not to the same-named parameter property.This pattern is extremely common in NestJS code, where
@Inject(someConfig.KEY)is used to injectregisterAs(...)-style typed configs and the constructor parameter property is typically named after the config. The false positive forces a churn-y workaround: either alias the import (import { foo as _foo }) or rename the parameter property.Minimal reproduction
const foo = { KEY: 'token' }; declare function Inject(token: unknown): ParameterDecorator; export class C { constructor(@Inject(foo.KEY) private readonly foo: number) {} }Actual
! eslint(no-unused-vars): Variable 'foo' is declared but never used. Unused variables should start with a '_'. ,-[test.ts:1:7] 1 | const foo = { KEY: 'token' }; : ^|^ : `-- 'foo' is declared hereExpected
No diagnostics —
foois used in@Inject(foo.KEY).ESLint with
@typescript-eslint/parser+@typescript-eslint/eslint-plugindoes not report this — its scope analyser correctly counts the decorator expression as a use of the outer binding.Notes / related issues
- #20670 — sibling bug (type param shadowed by function arg), same family.
- #17927 — closed namespace-shadow case (
namespaceshadowed by generic). - #2180 — typescript-eslint umbrella tracker.
- Assistant
I’ll first inspect the repository structure and locate the TypeScript scope-analysis and decorator handling code, then reproduce the diagnostic with the project’s existing test tooling before making a targeted fix.
- Assistant
ripgrepis unavailable, so I’ll usefind/grep. The likely subsystem isoxc_semantic, where references are assigned to bindings and scopes; I’ll inspect that crate and its tests. - Assistant
The semantic builder explicitly overrides parameter visitation, including separate decorator visits. I’ll inspect those overrides, the function-scope setup, and the fixture harness to understand whether decorators are currently traversed after parameter bindings are declared.
- Assistant
The snapshot confirms parameter decorators are currently recorded in the constructor’s function scope (
scope_id: 3), which causes the shadowing. Correct behavior requires evaluating decorator expressions in the enclosing class scope while retaining their AST parent relationship. I’ll inspect function/method traversal and scope-reference APIs to choose between changing traversal order or explicitly assigning decorator references to the parent scope. - Assistant
I’ll read the full function traversal and low-level reference creation. This will reveal ordering constraints (binding before decorator visit, type parameters, defaults) and whether temporarily switching
current_scope_idis safe for unresolved-reference bookkeeping. - Assistant
A minimal, structurally safe fix is to keep decorators nested under each
FormalParameterAST node but temporarily set semantic reference scope to the function scope’s parent while visiting only the decorator list. I’ll verify resolution starts from each reference’s stored scope (not the builder’s current scope), and inspect method/accessor scopes and TypeScript decorator semantics to ensure this applies to all decorated parameters, including ordinary method parameters and rest parameters. - Assistant
The prior combined command truncated the relevant resolution implementation, so I’ll inspect it directly along with available CLI binaries and test conventions. Then I’ll reproduce the exact report using either the built CLI or a focused linter test invocation without modifying tests.
- Assistant
Resolution indeed begins at the reference’s stored scope, so temporary scope assignment is compatible with deferred and checkpoint resolution. Before editing, I’ll reproduce the issue with the repository’s
oxlintpackage and a temporary/tmpinput, keeping the repository unchanged. - Assistant
The exact warning is reproduced. I’ll implement a small semantic-builder helper used by both normal and rest formal parameters. It will visit decorator expressions from the enclosing scope while preserving the parameter node as AST parent; initializer and type annotation traversal will remain in function scope, since those have different name-resolution semantics.
- Assistant
The change is formatted. I’ll now compile the semantic crate and rerun the exact reproduction. I’ll also test edge cases in a temporary file: a same-named ordinary method parameter, a decorator-contained closure (whose scope should parent to the enclosing class), and a parameter initializer (which must still resolve to the parameter rather than the outer binding).
- Assistant
Compilation exposed only a type-name mismatch: this crate imports the allocator vector as
oxc_allocator::Vec, notArenaVec. I’ll inspect imports and generated visitor signatures, then correct the helper signature to the repository’s established type. - Assistant
Because
std::Vecis already intentionally used for diagnostics, I’ll importoxc_allocator::Vecunder theArenaVecalias rather than changing existingVecresolution, then rerun compilation and both diagnostic checks. - Assistant
The minimal reproduction now has zero diagnostics, and the broader temporary case reports only the intentionally unused class—not either outer decorator binding—while parameter defaults remain valid. Next I’ll run the semantic test suite. Existing decorator scope snapshots may intentionally differ because their old
scope_idencoded the bug; per instructions I will not update test fixtures, but I’ll distinguish expected snapshot deltas from genuine failures. - Assistant
The broad
cargo testtarget is blocked by an unrelated feature-gated example that has nomainwithout its feature. I’ll run only library and integration-test targets. Before that, I’ll simplify the helper to accept a slice and visit each decorator, avoiding a new allocator-type import and keeping the diff focused. - Assistant
The semantic library’s 71 unit/conformance tests pass. The integration snapshot runner reports exactly two expected deltas: parameter-decorator references move from constructor/method scope 3 to enclosing class scope 2; there are no other changes. I’ll remove the generated
.snap.newartifacts (not update tests, as instructed), add an empty-list fast path to avoid overhead for undecorated parameters, then run CFG-enabled compilation and focused linter tests. - Assistant
CFG/linter/serialization compilation succeeds, and all 47 focused
no_unused_varstests pass. I’ll perform final verification: clippy on the changed crate, rerun the exact issue input after the optimization, test an imported shadowed identifier specifically, and inspect the final diff/status for unintended files.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.