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 trace semantic scope construction around TypeScript parameter decorators, then add a focused regression test and run the smallest relevant semantic/linter test set. Because this touches a linter rule implementation, I’m also loading the repository’s performance guidance first.
- Assistant
The advertised structural-search binary isn’t available in this checkout, so I’m falling back to targeted text search. I’m keeping the investigation centered on semantic reference binding; if the defect is there, the linter rule itself should not need a workaround.
- Assistant
The reference is being created after the constructor’s function scope is entered, so it starts resolution at the parameter binding and never reaches the outer
foo. I’m correcting the semantic scope assigned while visiting parameter decorators, and I’ll cover both the symbol-resolution contract and the user-visible lint result. - Assistant
The fix and regressions are in place. This environment also lacks the advertised
justcommand, so I’m using the equivalent Cargo formatter and crate-specific test commands directly. - Assistant
Both focused regressions now pass: semantic analysis binds the decorator’s
footo the outer symbol, andno-unused-varsemits no diagnostic. I added an empty-decorator fast path so ordinary parameters do not pay for the scope adjustment; I’m doing the final formatter and broader semantic checks now. - Assistant
The broad default semantic command tries to compile a feature-gated
cfgexample without its required feature and fails because that example has no activemain; this is unrelated to the change. I’m rerunning the complete semantic library/test targets while excluding examples. - Assistant
The broader semantic suite confirms the intended scope change in two existing decorator snapshots. I’m using the repository’s
insta-snapshotsworkflow now to update those expected scope IDs safely; it explicitly governs snapshot changes here. - Assistant
The snapshot helper CLI is also absent despite the repository setup notes. I won’t hand-edit generated snapshots: after reviewing the two
.snap.newfiles directly, I’ll use Insta’s non-interactive test-update mode to accept only these semantic fixtures.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.