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 trace the no-unused-vars scope/reference handling for TypeScript parameter decorators, reproduce the reported case with the project’s tests or CLI, then make the smallest scope-analysis fix and add no repository tests per instruction. I’ll verify with targeted existing tests and a temporary reproduction, inspect the diff, and submit only once the behavior is confirmed.
- Assistant
rgis unavailable in this environment, so I’ll usegrep/findinstead. The repository is the Oxc monorepo; next I’m locating the lint rule implementation and its scope analyzer dependencies. - Assistant
The semantic builder currently binds each formal parameter before traversing its decorators, while parameter references are deferred until all parameters are declared. Reordering alone would not be sufficient because deferred resolution would still see the parameter binding. I’m checking the resolver’s intended early-resolution mechanism; the likely targeted fix is to traverse a parameter decorator and resolve its references against the enclosing scope before binding that parameter. I’ll now reproduce and inspect existing decorator semantic fixtures before editing.
- Assistant
The resolver supports exactly this pattern via a checkpoint: collect only the decorator’s newly created references, resolve them immediately while the parameter has not yet been declared, then bind and process the rest of the parameter normally. I’ll implement that narrowly for formal and rest parameters, preserving AST traversal order and existing deferred handling for type annotations/initializers.
- Assistant
The semantic crate now compiles. I’ll run the actual linter against the minimal reproduction, then run the existing semantic and linter test suites (without modifying tests). I’ll also inspect whether the change affects rest parameters and multiple parameters with same-name decorators.
- Assistant
The reproduction now reports zero diagnostics, the semantic fixture test passes, and all 1,145 linter library tests pass. The diff is limited to the semantic builder and has no whitespace errors. I’m doing a final formatting/diff review and one broader semantic test invocation before submission.
- Assistant
Final review is clean: rustfmt made no unrelated changes,
git diff --checkpasses, the minimal CLI reproduction is fixed, semantic tests pass, and all linter tests pass. The implementation also covers rest parameters with decorators and preserves deferred resolution for parameter bindings, annotations, and initializers. I’ll submit the single-file change now.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.