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
The linter example didn't flag it — probably rule config. Let me run the actual oxlint binary:
- Assistant
Reproduced. Now let's find where semantic resolves decorator references:
- Assistant
Now add the helper method in the
SemanticBuilderimpl block (before theVisitimpl): - Assistant
Fixed. Now run the semantic test suite to check for regressions:
- Assistant
Pre-existing example failure (needs a feature flag). Run tests excluding examples:
- Assistant
Now add pass cases to the
no-unused-varsrule tests: - Assistant
The Tester handles TS sources. Add a regression test fn in
oxc.rs: - Assistant
That's just the new snapshot needing acceptance (the fail case correctly emits a diagnostic). Accept it:
- Assistant
Conformance submodules aren't available in this environment (
justisn't installed and submodules are empty), so I can't run those. The transformer's legacy-decorator transform depends on semantic scoping, so run its tests: - Assistant
Transformer unit tests pass; Babel conformance also requires missing submodules. Run remaining checks — clippy and fmt:
- Assistant
All pre-existing. Run clippy on lib + tests (excluding the broken example):
- Assistant
Clean. Let me sanity-check a few more edge cases through oxlint:
- Assistant
All edge cases behave correctly. Quick check that the decorator transform still works (it consumes semantic scoping):
- Assistant
All pass. Final review of the change:
- Assistant
The fix is complete. Summary:
Root cause
SemanticBuildervisited TS parameter decorators inside the function scope, after the parameter had been bound. Since reference resolution walks up from the reference's recorded scope,fooin@Inject(foo.KEY)resolved to the same-named parameter property instead of the outer binding — leaving the outerconst/import with zero references, whichno-unused-varsthen flagged.Fix —
crates/oxc_semantic/src/builder.rsAdded
visit_parameter_decorators, which visits parameter decorators withcurrent_scope_idtemporarily set to the function scope's parent (the class scope). This matches TS semantics — parameter decorators are evaluated at class-decoration time in the enclosing class scope, before any parameter binding exists — and matches typescript-eslint's scope manager. Bothvisit_formal_parameterandvisit_formal_parameter_restnow use it.Tests
- New semantic fixture
crates/oxc_semantic/tests/fixtures/oxc/ts/decorators/parameter-shadowed-outer-binding.ts+ snapshot — confirms the decorator reference attaches to the outerfoo(class scope) and the parameter symbol has no references. - New linter test
test_ts_parameter_decoratorsinno_unused_vars/tests/oxc.rs— passes for the issue repro and the NestJS@Inject(fooConfig.KEY)import pattern; still fails (correctly) when the decorator doesn't reference the outer binding. - Updated 2 existing snapshots (
typescript-eslint/decorators/parameter{,-property}.snap) — only change is decorator references now reporting the class scope id instead of the function scope id.
Verification
- Repro now clean:
oxlint test.ts→ 0 warnings; the non-use case still warns. - Edge cases checked manually: method (non-constructor) param decorators, arrow closures inside decorators, no-outer-binding case.
- Legacy decorator transform output verified: hoisted
Inject(foo.KEY)correctly references the outerfoo. cargo testgreen foroxc_semantic(72),oxc_linter(1149),oxc_transformer,oxc_traverse,oxc_minifier,oxc_isolated_declarations,oxc_codegen; clippy clean;cargo fmtapplied.
Two pre-existing failures unrelated to this change (verified against the clean tree via
git stash): theoxc_semanticcfgexample doesn't compile, andoxc_codegen'ssourcemap::stacktrace_is_correctfails. Conformance suites (cargo coverage, transform conformance) couldn't run here — git submodules are empty andjustisn't installed — so those should be exercised in CI. - New 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 →
- fable-5Compare side by side →