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 reported
no-unused-varsfalse positive. - Assistant
Next I'll locate the
no-unused-varsrule and related scope analysis code. - Assistant
I'll dig into how parameter decorators resolve identifiers in the semantic analyzer.
- Assistant
Let me look at the builder.rs around parameter decorator visiting - especially how scopes are set up.
- Assistant
The bug is clear: parameters are bound before their decorators are visited, so decorator refs resolve to the parameter. I'll check how function/method scopes wrap parameters next.
- Assistant
Let me look at visit_function and how parameters are handled.
- Assistant
I'll check related issues and existing tests for parameter shadowing next.
- Assistant
I'll inspect the related type-param shadowing fix and how unresolved references are resolved for parameters.
- Assistant
There are related commits around decorators and shadowing — I'll inspect those and the surrounding logic.
- Assistant
Let me understand the TypeScript scoping for parameter decorators better. Looking at the TypeScript compiler and ESLint scope manager behavior:
- Assistant
Let me look at how MethodDefinition is visited - it probably uses rustc Visit trait default which visits children.
- Assistant
I'll check the generated visitor and the Visit trait to see how method definitions are walked.
- Assistant
Let me look at the Visit trait used by semantic - how method definitions are walked by default.
- Assistant
Let me look at the visit for formal parameters and method definitions.
- Assistant
Let me read the walk_method_definition and walk_formal_parameter functions.
- Assistant
I'll check when parameters get bound relative to decorator evaluation.
- Assistant
Let me look at FormalParameter binding
- Assistant
I'll inspect how
leave_scopeand unresolved-reference resolution work. - Assistant
I'll check Reference creation and whether initializer scope handling differs from decorators.
- Assistant
I'll search the repo and related fixes for type-parameter shadowing next.
- Assistant
I'll review the typescript-eslint tests that cover constructor parameter decorators.
- Assistant
I'll confirm how nodes record their scope and whether changing
current_scope_idwould affect them. - Assistant
I'll look for patterns that resolve references from a parent scope, then try to reproduce.
- Assistant
I'll reproduce the false positive with a minimal example.
- Assistant
The example may not enable that rule by default — I'll check how it's wired.
- Assistant
Reproduced. Next I’ll inspect semantic reference resolution and existing tests so we can fix this correctly.
- Assistant
I'll inspect semantic symbol tests and any previous shadowing-related fixes for reference.
- Assistant
I'll review SymbolTester and how parameter properties are bound.
- Assistant
Implementing the fix: resolve parameter decorator references in the parent scope.
- Assistant
I'll check TypeScript's name resolution when a parameter decorator references an earlier parameter.
- Assistant
npx not available. Let me try other ways.
- Assistant
Updating parameter visitors so decorators resolve in the enclosing scope.
- Assistant
I'll add the
visit_parameter_decoratorshelper next to the other reference helpers. - Assistant
I'll add the helper method in the SemanticBuilder impl block.
- Assistant
Rebuilding and verifying the reproduction.
- Assistant
I'll adjust the helper to take the correct decorator vector type.
- Assistant
Vec is oxc_allocator::Vec. I'll change the signature to use
&Vec<'a, Decorator<'a>>. - Assistant
Need to检查 if Vec is imported - it's oxc_allocator::Vec via ast prelude probably.
- Assistant
Vecis std::Vec because oxc_allocator::Vec wasn't imported. Need to use full path. - Assistant
Rebuild and test.
- Assistant
Original case is fixed. Next I'll cover edge cases with temporary scripts.
- Assistant
Edge cases look right. I'll run the related semantic and linter tests next.
- Assistant
I'll double-check import shadowing and that method-parameter type annotations still resolve type params.
- Assistant
I'll remove the temporary test file and use the semantic example for verification instead.
- Assistant
Root
foocorrectly has one read. I'll format the change and run a final verification. - Assistant
I'll check
git diffto ensure the patch is clean and nothing unexpected changed. - Assistant
I'll check whether FormalParameter appears in type-only contexts like call signatures.
- Assistant
There are TSFunctionType, TSCallSignature, etc. that have FormalParameters. Do those hop over forward visit_formal_parameter? Several may enter a scope first.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.