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
That example is a toy. Let me run the real oxlint binary.
- Assistant
Reproduced. Now let's determine whether the reference
fooinside the decorator resolves to the parameter binding (semantic bug) or the outerconst(linter bug). - Assistant
Now add the helper method in the private
impl SemanticBuilderblock at the end: - Assistant
The false positive is gone. Now run the semantic test suite and check for regressions:
- Assistant
Pre-existing issue — the
cfgexample needs thecfgfeature. Let me check howjust testinvokes it: - Assistant
Snapshot confirms correct resolution. Now run the full semantic test suite and the linter's
no-unused-varstests: - Assistant
Now add a regression test to the
no-unused-varsrule. Let me find where its TS pass cases live: - Assistant
All pass. Now run broader regression checks — full linter suite, semantic conformance coverage, and transformer conformance (the decorator transforms rely on this scoping):
- Assistant
No regression (2671/4773 both before and after; the diff removes one mismatch in an already-failing case). Now check the transformer, since decorator transforms consume this scoping:
- Assistant
I'll compare baseline vs. changed runs with the same fixture set:
- Assistant
Now format and lint the changes:
- Assistant
Pre-existing
oxc_parserwarnings only — nothing from my changes. Let me review the final diff: - Assistant
One more sanity pass over neighboring crates that consume semantic scoping:
- Assistant
All checks pass. The fix is complete.
Summary
Root cause — a semantic-analysis bug, not a linter bug:
SemanticBuilder::visit_formal_parametervisited parameter decorators inside the function scope, after the parameter was bound. So inconstructor(@Inject(foo.KEY) private readonly foo: number), thefooreference in the decorator resolved to the parameter symbol instead of the outer binding, leaving the outerconst foowith zero references — whichno-unused-varsthen correctly (per its data) but wrongly (per language semantics) reported.Fix (
crates/oxc_semantic/src/builder.rs): parameter decorators are evaluated at class-decoration time in the scope enclosing the method — before any parameter binding exists. The newvisit_parameter_decoratorshelper visits them withcurrent_scope_idtemporarily set to the function scope's parent (the class scope), so references inside them skip the function's own bindings during resolution. Bothvisit_formal_parameterandvisit_formal_parameter_restnow use it. This mirrors the existing treatment of class decorators, which are already visited before entering the class scope.Tests added:
- Semantic fixture
crates/oxc_semantic/tests/fixtures/oxc/ts/decorators/parameter-decorator-shadowed-binding.ts+ snapshot — verifies the decorator reference resolves to the outerconst foofrom the class scope. no-unused-varspass case intests/oxc.rs(test_classes) with the exact issue repro.- Two existing typescript-eslint decorator snapshots updated: decorator references now carry the class scope id (2) instead of the method scope id (3) — the intended behavior change.
Verification:
oxlinton the repro (plus the NestJS import variant and an arrow-inside-decorator variant): 0 warnings; parameter-shadow usage inside the method body still resolves to the parameter.cargo test -p oxc_semantic --all-features— all pass.cargo test -p oxc_linter— all 1144+ pass, including the 47no_unused_varstests.cargo coverage -- semantic— identical pass rates to baseline (typescript 2671/4773 before and after); thesemantic_typescript.snapdiff only changes details within already-failing cases and removes one mismatch.- Transformer conformance output byte-identical to baseline;
oxc_transformer,oxc_traverse,oxc_isolated_declarations,oxc_minifiertests all green. cargo fmt+ clippy clean for the touched crates.
Note: one pre-existing environment quirk —
cargo test -p oxc_semanticwithout--all-featuresfails to compile thecfgexample (needs thecfgfeature), unrelated to this change. - Semantic fixture
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 →
- gpt-5.6-solCompare side by side →
- fable-5Compare side by side →