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?
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
@typescript-eslint/parser+ does not report this — its scope analyser correctly counts the decorator expression as a use of the outer binding. - Assistant
Investigating the
no-unused-varsfalse positive for constructor parameter decorators that reference shadowed outer bindings. - Assistant
Reproducing the issue, then fixing the semantic builder's parameter decorator visit order.
- Assistant
Reproduced. Visiting parameter decorators before binding the parameter in the semantic builder.
- Assistant
The fix didn't resolve it — digging into how references are resolved and counted.
- Assistant
Parameter decorator references are deferred until after all parameters bind. Resolving them immediately after visiting each parameter's decorators.
- Assistant
Fix confirmed. Adding tests and running the test suite.
- Assistant
Fixed a false positive in
no-unused-varswhen a constructor parameter property shadows an outer binding used in its own decorator (e.g. NestJS@Inject(foo.KEY)withprivate readonly foo).Root cause
Parameter decorator references were collected as unresolved and only resolved after all parameters were bound. So
fooin@Inject(foo.KEY)resolved to the parameterfooinstead of the outerconst foo, leaving the outer binding with no references.Fix
In
oxc_semantic’sSemanticBuilder, parameter decorator references are now resolved immediately after visiting each parameter’s decorators, before binding the parameter pattern:2358:2372:crates/oxc_semantic/src/builder.rs
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.