ISSUE DESCRIPTION linter: no-unused-vars false positive when constructor parameter…
- User
ISSUE DESCRIPTION
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.
- Assistant
I’ll first inspect the repository structure and locate the TypeScript scope-analysis and
no-unused-varsimplementation/tests. Then I’ll reproduce the reported diagnostic with the project’s existing tooling before tracing how decorator references are bound. - Assistant
rgis unavailable, so I’ll usefind/grep. I’ll also read the repository guidance and identify the relevant semantic/scoping crates and linter fixture conventions. - Assistant
The semantic builder visits each formal parameter’s binding before its decorators, which is the likely cause: decorator references are then resolved against the newly declared parameter. I’ll inspect surrounding scope creation, declaration, and reference-resolution logic, plus existing semantic tests, to determine whether ordering alone is sufficient or decorators need an explicit enclosing-scope override.
- Assistant
Because references from parameters are deliberately deferred until all parameters are declared, merely visiting a decorator earlier would still bind it to the parameter during the later scope-resolution pass. The fix must distinguish parameter-decorator references from parameter initializer/type references and resolve them from the parent scope. I’ll inspect the unresolved-reference stack API and method/constructor traversal to implement this at the semantic layer without linter-specific workarounds.
- Assistant
The resolver records each reference’s originating scope and walks upward from there. A targeted approach is therefore possible: while visiting parameter decorators, create their references with the function scope’s parent as the origin, while preserving normal AST parentage. I’ll verify the reference creation API and whether a temporary
current_scope_idswap is an established pattern or whether a dedicated decorator visitor is cleaner. - Assistant
A temporary scope switch would correctly tag both decorator AST nodes and references with the enclosing class scope, matching how class decorators are already visited before entering class scope. Before editing, I’ll reproduce the failure through the linter example and inspect AST visitor ordering for method decorators so the change remains consistent for constructor and method parameter decorators (including rest parameters).
- Assistant
The example command completed but did not print diagnostics, likely because its CLI differs from
oxlint; I’ll inspect its usage and use the actual CLI package or focused rule test harness to get a definitive baseline. I’ll also inspect generated traversal (read-only) and the linter test helper’s TypeScript source-type detection. - Assistant
The linter example is intentionally not the real rule runner. I’ll run the
oxlintapp directly for reproduction, then inspect the exact method visitor. After that I’ll patchSemanticBuilderwith a small helper that visits parameter decorators in the parent scope, avoiding duplicated scope-switch code for ordinary and rest parameters. - Assistant
The baseline exactly reproduces the warning. Generated traversal confirms method decorators run in class scope, while parameter decorators currently run after entering the method’s function scope. I’ll add a semantic-builder helper that temporarily visits only parameter decorators in the function scope’s parent. This also handles forward outer declarations because unresolved references retain that parent origin for final resolution.
- Assistant
The source change is in place. I notice a minor style issue (missing blank line between methods), which I’ll clean up. Then I’ll rerun the exact reproduction and create a temporary edge-case fixture covering: an imported-like outer binding, a later declaration, multiple parameters shadowing the same outer name, a decorated regular method parameter, and a default initializer that must still resolve to parameter scope.
- Assistant
Compilation caught that generated visitors accept Oxc’s allocator-backed
Vec, not a slice. I’ll adjust the helper signature to the exact AST container type (already likely imported asVecor available fully qualified), then rerun both fixtures. - Assistant
Both the exact report and broader fixture now produce zero diagnostics. The initializer case also remains untouched because only decorator traversal changes scope. Next I’ll run focused semantic and
no-unused-varstest suites, then inspect the diff and run Clippy/checks relevant to the changed crate. - Assistant
Focused linter tests pass (47/47). The broad semantic command hit a repository feature-gating issue in the
cfgexample (“main function not found”), unrelated to this change; I’ll rerun semantic library/integration tests while excluding examples. I’ll also validate the literal import form from the report and run Clippy foroxc_semantic. - Assistant
The import case passes and Clippy is clean. Semantic snapshots detect the intended scope correction (decorator reference
scope_idmoves from function scope 3 to enclosing class scope 2); per the instruction not to modify tests/snapshots, I will remove generated.snap.newfiles rather than accept them. I’ll run the semantic library and integration targets separately to confirm all non-snapshot tests pass, then check for accidental files. - Assistant
Final review:
- Reproduced the reported error: yes; the original fixture produced one
eslint(no-unused-vars)warning for outerfoo. - Confirmed the fix resolves it: yes; both the exact
constfixture and animport { foo }fixture now produce zero warnings. - Considered edge cases: yes; verified a later outer declaration, multiple decorated constructor parameters, a regular method parameter, and that default initializers remain in parameter scope. Rest parameters use the same corrected helper.
- Ran affected existing tests:
no_unused_varstests pass 47/47, semantic library tests pass 11/11, semantic integration tests pass 60/60, and semantic Clippy passes with warnings denied. The semantic snapshot suite reports only the expected reference scope-ID changes; generated snapshot updates were removed in accordance with the instruction not to modify tests.
git diff --checkis clean and onlycrates/oxc_semantic/src/builder.rsis modified. I’ll submit the fix now. - Reproduced the reported error: yes; the original fixture produced one
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.