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
No new interfaces are introduced.
- Assistant
I’ll trace the semantic scope construction for TypeScript parameter decorators, add a focused regression test, and run the narrow semantic/linter test suites. Since this touches a linter rule implementation, I’m also using the performance-lint-rules guidance.
- Assistant
The performance guidance applies only if the fix lands inside a rule; the symptom looks more likely to come from shared semantic scope resolution, so I’ll keep the fix at the ownership layer rather than adding a rule-specific workaround.
- Assistant
The bug is confirmed in semantic traversal: constructor parameters are bound in the method’s function scope before their decorators are visited, so the decorator reference incorrectly resolves to that parameter. I’m adding the reproduction to the existing TypeScript
no-unused-varspass corpus before changing traversal. - Assistant
The regression test is in place and the initial linter build is still compiling the workspace. The planned semantic change is small: visit parameter decorators with the function’s parent scope as their lexical scope, while preserving their AST parentage under the parameter.
- Assistant
The new test now passes with the semantic fix. I’m extending coverage to the title’s imported-binding form as well, then I’ll format and run both the semantic crate tests and the focused linter tests.
- Assistant
The repository’s documented
just fmtcommand is unavailable in this environment (justis not installed), so I’m using the equivalent Rust formatter directly for the two Rust files changed. - Assistant
The broad
cargo test -p oxc_semanticcommand hits an unrelated repository configuration issue: thecfgexample has nomainwithout its feature enabled. I’m narrowing it to the crate’s library and integration tests, which exercises the semantic implementation without building that feature-gated example. - Assistant
The semantic tests exposed two expected scope snapshot changes: existing parameter-decorator references move from the method function scope to the enclosing class scope. I’m using the repository’s
insta-snapshotsworkflow now to review and update only those two snapshots. - Assistant
cargo-instais also absent despite the repository prerequisites. I reviewed both pending files directly; their only behavioral changes are the expectedscope_id: 3 → 2moves (plus Insta’s generated assertion-line metadata). I’ll use Insta’s built-in noninteractiveINSTA_UPDATE=alwaysmode to accept them, then verify the resulting diffs. - Assistant
Both regression forms now pass, and the semantic suite confirms all existing parameter-decorator references are assigned to the enclosing class scope. I’m doing the final formatting/diff checks and a narrow Clippy pass on the semantic crate.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Identical first prompt
- composer-2.5Compare side by side →
- composer-2.5Compare side by side →
- composer-2.5Compare side by side →
- composer-2.5Compare side by side →
- composer-2.5Compare side by side →
- gpt-5.6-solCompare side by side →
- gpt-5.6-solCompare side by side →
- gpt-5.6-solCompare side by side →
- gpt-5.6-solCompare side by side →
- fable-5Compare side by side →
- fable-5Compare side by side →