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 inspect the repository and locate the
no-unused-varsscope/reference handling for TypeScript parameter properties and decorators. I’ll first reproduce the reported case using the project’s existing build/test tooling if practical, then make the smallest source change, add no repository tests per instruction, and run targeted existing tests plus formatting/build checks. - Assistant
rgis unavailable in this container, so I’ll usegrep/findinstead. The repository is clean; next I’m narrowing the search to the Rust linter and semantic-analysis crates where variable references and decorator traversal are implemented. - Assistant
The likely defect is in semantic traversal rather than the linter rule:
no-unused-varsconsumes resolved symbol references, so a decorator reference resolving to the parameter-property symbol would explain the outer symbol being unused. I found the relevantSemanticBuilder::visit_formal_parameter; I’m inspecting its scope-entry/binding order and binder behavior now. - Assistant
The traversal confirms the bug:
param.bind(self)runs beforevisit_decorators, so the constructor parameter is visible while its decorator reference is collected. I’ll preserve normal parameter binding for defaults/types, but evaluate parameter decorators under the enclosing scope and resolve only references collected by that decorator visit. This avoids broad changes to symbol accounting and also handles multiple parameters consistently. - Assistant
The semantic crate now compiles. The helper had to live in the builder’s inherent implementation (not the
Visittrait implementation), and it uses the allocator-backed AST vector type used by this repository. I’ll now run the actual linter against the minimal reproduction and targeted no-unused-vars tests to verify both the fix and regressions. - Assistant
The reproduction now reports zero diagnostics, and all 47 targeted
no_unused_varsunit tests pass. I’m doing a final edge-case check to ensure an undecorated shadowing parameter still leaves the outer binding unused, while a decorator reference marks it used, then I’ll review the diff and submit.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.