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
Let me first reproduce the issue.
- Assistant
Reproduced. Now let me look at how the semantic builder handles parameter decorators.
- Assistant
Let me look at how
visit_functionand method visiting work in the builder. - Assistant
Now update the two visit sites:
- Assistant
Now let me test more variants:
- Assistant
These are oxc's own snapshots of its scoping data. Let me see the full diffs before accepting:
- Assistant
The diffs are exactly the intended change. Accept them:
- Assistant
The snapshot confirms the outer
const foonow owns the decorator reference. Promote it and run the remaining test suites: - Assistant
Semantic tests pass. Now add a regression test to
no-unused-vars: - Assistant
I can replicate the clones with plain git:
- Assistant
These are pre-existing known-failure entries whose details shifted. Let me check the context to verify the changes are improvements:
- Assistant
Only the expected snapshot changed. Now run the transformer conformance (decorator transforms rely on semantic scoping) and remaining unit tests:
- Assistant
The total test count changed (1165 → 1000) — I skipped
just update-transformer-fixtures. Let me check that recipe: - Assistant
Now check the decorator legacy transform end-to-end with the shadow case, since emitted code must reference the outer binding:
- Assistant
Transform output is correct. Now run formatting and clippy:
- Assistant
Let me run the remaining hygiene checks (
typos, and a broader compile check): - Assistant
The fix is complete. Summary:
Root cause
no-unused-varssits on top ofoxc_semantic's reference resolution, and the semantic builder was visiting parameter decorators inside the function scope (visit_formal_parameterruns aftervisit_functionenters the function scope). Sofooin@Inject(foo.KEY)resolved to the same-named parameter property instead of the outerconst/import — leaving the outer binding with zero references.Fix —
crates/oxc_semantic/src/builder.rsAdded
visit_parameter_decorators, which temporarily rebasescurrent_scope_idto the function scope's parent (the class scope) while visiting a parameter's decorators, then restores it. Bothvisit_formal_parameterandvisit_formal_parameter_restnow use it. This matches:- Runtime semantics — parameter decorators are evaluated at class-decoration time in the enclosing scope, before any parameter binding exists
- typescript-eslint's scope manager, which visits parameter decorators before nesting the function scope
- tsc's own expectations — the
decoratorReferences.tscompiler test literally asserts "decorator y should be resolved at the class declaration, not the parameter", and the conformance diff moved in exactly that direction
Tests
- New semantic fixture
crates/oxc_semantic/tests/fixtures/oxc/ts/decorators/parameter-decorator-shadow.ts+ snapshot — confirms the outerconst fooowns the decorator reference and the parameter has none - Two new
no-unused-varspass cases (const-shadow and import-shadow, referencing issue #22687) intests/oxc.rs - Updated 2 existing semantic snapshots (decorator refs now recorded in class scope) and
semantic_typescript.snap(one pre-existing mismatch eliminated)
Verification
cargo test -p oxc_semantic --all-features— all passcargo test -p oxc_linter— 1144 pass,cargo test -p oxc_transformer— all pass- Full
cargo coverage(test262/babel/typescript/estree across parser, semantic, codegen, formatter, transformer, minifier) — no regressions; only the expectedsemantic_typescript.snapimprovement - Legacy decorator transform emits correct output for the shadow case:
Inject(foo.KEY)outside the constructor referencing the outerfoo - Decorator referencing its own parameter (
@Inject(p) p) now correctly reportsno-undef, matching typescript-eslint
One note: I could not regenerate
tasks/transform_conformanceBabel fixtures (requires node +@babel/core, unavailable here), so I reverted the invalid snapshot that run produced; transformer correctness is covered by the unit tests andcargo coveragetransformer suites instead.
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 →