linter: no-unused-vars false positive when constructor parameter property shadows an…
- User
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.
Interface
- Assistant
I'll investigate how
no-unused-varshandles parameter decorator scopes and find where the false positive comes from. - Assistant
Reproducing the issue, then fixing parameter binding order so decorators resolve in the enclosing scope.
- Assistant
Reproduced the bug. Fixing parameter binding order so decorators resolve in the enclosing scope.
- Assistant
The bind-order change alone isn't enough — references may still resolve after all parameters are bound. Checking how reference resolution works.
- Assistant
Resolving decorator references immediately after visiting them, before parameter binding shadows outer names.
- Assistant
Root cause
Parameter decorators are evaluated in the enclosing scope, before the parameter binding exists. In
SemanticBuilder::visit_formal_parameter, two things went wrong:- The parameter was bound before decorators were visited.
- Unresolved references from decorators were only resolved after all parameters were bound (at the end of
visit_function), so@Inject(foo.KEY)resolvedfooto the parameter property instead of the outerconst foo.
Fix
In
crates/oxc_semantic/src/builder.rs, for bothvisit_formal_parameterandvisit_formal_parameter_rest:- Visit decorators first.
- Resolve only those decorator references (via the checkpoint pattern) before binding the parameter.
- Then bind the parameter and visit its pattern/type/initializer as before.
This matches TypeScript semantics and aligns with how
oxc_ast_visitalready orders decorator visits before binding patterns.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.