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?
codeoxlint test.tsWhat does your
.oxlintrc.json(oroxlint.config.ts) config file look like?jsonc// 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
tsconst foo = { KEY: 'token' }; declare function Inject(token: unknown): ParameterDecorator; export class C { constructor(@Inject(foo.KEY) private readonly foo: number) {} }Actual
code! 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 + does not report this — its scope analyser correctly counts the decorator expression as a use of the outer binding.
- Assistant
I’ll trace the
no-unused-varsscope/reference handling for TypeScript parameter decorators, reproduce the reported case with the existing test or a temporary fixture, then make the smallest scope-analysis fix and run targeted tests plus relevant checks. I’ll avoid modifying tests per the repository instructions. - Assistant
The semantic builder currently binds each formal parameter before walking its decorators. Because reference resolution uses the current function scope, a same-named parameter shadows the outer binding during decorator traversal. The correct fix is to walk all parameter decorators before entering the function scope (and therefore before any parameter bindings), while retaining normal parameter binding/type/initializer traversal afterward. I’ll implement that in both ordinary functions/methods and arrows, and remove the now-late decorator visits.
- Assistant
The first compile check caught that the helper cannot be declared inside the generated
Visittrait implementation; I moved it to the builder’s inherent implementation. The semantic crate now compiles, and running the actual Oxlint binary on the reproduction reports zero diagnostics. I’ll run the focused linter tests and inspect the final diff for unintended changes.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.