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 start by exploring the repository structure and reproducing the false positive with the minimal test case.
- Assistant
Let me check the project docs and locate the no-unused-vars implementation.
- Assistant
rg is not found, let me use grep or the ast-grep.
- Assistant
The root cause is likely in semantic scope analysis for decorators. I'll inspect related issues and the semantic/linter code next.
- Assistant
Let me look at the builder.rs around how constructor parameters and decorators are handled, and also look at issue #20670 related sibling bug.
- Assistant
Let me read the visit_formal_parameter and function-related visitor methods more carefully.
- Assistant
Looking at the formal parameter visitor:
- Assistant
I'll dig into how references and bindings are resolved for parameters and any similar prior fixes.
- Assistant
Let me understand the resolve mechanism better - when are references resolved and when is binding done:
- Assistant
I need to understand the resolve_references_for_current_scope better and how references get their scope_id.
- Assistant
I'll check how references record their scope_id when created.
- Assistant
I'll look for related fixes and existing tests for type params and decorator shadowing.
- Assistant
Let me check type parameter + parameter shadowing (issue #20670) and also how class property decorators work.
- Assistant
I'll first create a minimal reproduction and try running the linter against it.
- Assistant
Let me check if there's a built binary or use cargo run with the example.
- Assistant
I'll look at existing tests for no_unused_vars to add a case there, and also fix the semantic builder. Let me first reproduce with load test via cargo test in the no_unused_vars tests.
- Assistant
Let me write a quick semantic dump to see where
fooresolves. There's probably a semantic example or test utility. - Assistant
Let me run the semantic example on the reproduction.
- Assistant
Confirmed the false positive. I'll check how method and property decorators handle scopes so we can mirror that.
- Assistant
Let me check method definition visitor - does it handle method decorators specifically?
- Assistant
Method definitions use the default Visit trait methods from oxc_ast_visit. Let me check those.
- Assistant
Let me read those visit methods.
- Assistant
Let me look at walk_method_definition and walk_formal_parameter
- Assistant
Let me read the walk implementations.
- Assistant
I'll inspect FormalParameter binding next.
- Assistant
Let me look at FormalParameter binder more carefully - does it also bind pattern separately?
- Assistant
I'll search git history and code for similar shadowing fixes to follow the same pattern.
- Assistant
There's a test for decorated parameter renames. Let me look at that.
- Assistant
I'll verify TypeScript/ESLint handling of earlier parameters used inside later parameter decorators.
- Assistant
Checking semantic integration tests andготов potential snapshots for reference resolution.
- Assistant
There's a fixture for parameter-property and parameter decorators! Let me look at those.
- Assistant
I'll check how these semantic fixtures are used and whether matching typescript-eslint is expected.
- Assistant
Implementing the fix: parameter decorator references should resolve in the parent (enclosing) scope.
- Assistant
I'll add a helper and update both formal-parameter visit methods so decorators resolve in the enclosing scope.
- Assistant
Adding the helper that switches to the enclosing scope while visiting parameter decorators.
- Assistant
Fixing a typo I introduced and checking that Decorator is imported.
- Assistant
Checking how other methods type decorator vectors.
- Assistant
visit.rsusesXVec' as imported. Looking at the爱国主义 imports for Visit: - Assistant
I'll import Vec from oxc_allocator and use it for consistency.
- Assistant
The first replace for import may not have worked - only the second worked. Let me check imports.
- Assistant
There's already Uses of
Vecfor diagnostics — I'll use the full path for the allocator Vec type. - Assistant
Need to goto that line first.
- Assistant
Let me edit the line properly.
- Assistant
Verifying the fix against the reproduction case:
- Assistant
Outer
fooresolves correctly. Adding no-unused-vars tests and updating semantic snapshots. - Assistant
Let me add a test case near test_classes or create a costal new test for parameter decorator shadowing. Looking at test_classes.
- Assistant
I'll add pass cases for the NestJS pattern in test_classes.
- Assistant
I introduced bad typos while editing — cleaning those up now.
- Assistant
I'll add a semantic fixture for the repro and update the decorator snapshots.
- Assistant
Let me run semantic fixture tests with UPDATE and linter tests.
- Assistant
Filter didn't match. Let me see the test name.
- Assistant
Just one test main.
- Assistant
Snapshots look correct. Checking the new fixture and running the linter tests next.
- Assistant
Running related tests and checking remaining painter snapshots.
- Assistant
Checking whether scope-sensitive checkers run while temporarily in the parent scope:
- Assistant
Running rustfmt and a final review of the changes.
- Assistant
Need cargo fmt instead.
- Assistant
The checklist can't ask for new tests, so I'll keep only the semantic fix and revert the test/fixture/snapshot changes.
- Assistant
Re-verifying mau the fix with a temporary script, then submitting.
- Assistant
I'll run a quick temporary check that the linter no longer flags the case.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.