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 to understand the codebase.
Assistant
This is the oxc repository. The issue is about the
no-unused-varslinter rule and scope analysis with constructor parameter properties and decorators. Let me look at the relevant crates.Assistant
Let me first reproduce the issue.
Assistant
Let me check if there's a built oxlint binary or build one.
Assistant
Let me look at the scope builder code while building the binary.
Assistant
Let me look at the
visit_formal_parametermethod and the binder more carefully.Assistant
Let me look at how the function/method scope and parameters interact.
Assistant
Let me look at how formal parameters bind and how the scope flags work.
Assistant
Let me look at how classes and constructors are visited, and how scope is set up.
Assistant
Let me understand the reference resolution mechanism.
Assistant
Now let me look at
walk_up_resolve_referenceto understand how references resolve.Assistant
Let me look at how references get their scope_id, and how identifiers are visited.
Assistant
Let me look at how method definitions and their decorators are handled.
Assistant
The
visit_decoratorsis the default visit. Let me look at howMethodDefinitionandPropertyDefinitionare visited. They're probably using the default visitor fromVisit. Let me check the AST visitor.Assistant
Let me look at the walk functions for method definitions and formal parameters.
Assistant
Let me look at the related issue #20670 (sibling bug) for the approach used.
Assistant
Let me look at the grandparent lookup commit which may be related to the sibling bug.
Assistant
Not related. Let me search the git log for the sibling bug fix related to "type param shadowed by function arg".
Assistant
Let me look at the no-unused-vars rule implementation.
Assistant
Let me look at the default walk for method definitions and property definitions.
Assistant
Let me look at the start of
visit_functionin the builder.Assistant
Let me look at existing tests for decorators and scope analysis.
Assistant
Let me look at how the snap tests work and whether they're auto-generated.
Assistant
Let me look at the method and class-property decorator snaps.
Assistant
Let me check the AGENTS.md and CLAUDE.md for guidance on testing.
Assistant
Let me look at
walk_formal_parametersand the FormalParameters visit.Assistant
Let me check the formal_parameter_list visit and how the builder handles it.
Assistant
I now have a clear understanding. Let me look at the current
visit_formal_parameterandvisit_formal_parameter_restonce more, then implement the fix.Assistant
Let me check the scoping field access.
Assistant
Let me check the
visit_decoratorssignature and imports in builder.rs.Assistant
Now let me implement the fix. I'll edit
visit_formal_parameter.Assistant
Let me verify
Decoratoris imported in builder.rs.Assistant
oxc_ast::{AstKind, ast::*}is imported, soDecorator(fromoxc_ast::ast) is in scope. Good.Vecis fromoxc_allocator(the allocator Vec), also re-exported via ast::* likely. ActuallyVec<'a, Decorator<'a>>— theVechere isoxc_allocator::Vec. Let me check it's in scope. The walk function usesVec<'a, Decorator<'a>>and builder.rs usesVecin many places (e.g.,allocreturns). Let me check thatVecresolves to the allocator Vec. The builder.rs hasuse oxc_allocator::Address;but notVec. Let me check how other code refers to Vec.Actually, the existing code in the visit trait uses
Vec<'a, ...>so it must be imported. Let me check.Assistant
Hmm,
Vec<'a, ...>isn't directly used elsewhere in builder.rs. Let me check how the existing visit_decorators is called — it's a trait methodself.visit_decorators(¶m.decorators). The type ofparam.decoratorsisoxc_allocator::Vec<'a, Decorator<'a>>. So in my helper signature I used&Vec<'a, Decorator<'a>>. I needVecto resolve tooxc_allocator::Vec.Let me check if
oxc_allocator::Vecis imported or re-exported viaoxc_ast::ast::*. Let me check.Assistant
Two issues:
Vecresolves to std's Vec, and the helper must be in an inherent impl, not the Visit trait impl. Let me fix both. First, let me find where inherent impls are.Assistant
Let me remove the misplaced helper from the Visit impl and inline the logic instead to avoid the
Vectype issue.Assistant
Let me move the helper to the inherent impl block (near line 627). First, remove the helper from here.
Assistant
Now let me add the helper to the inherent impl block before
resolve_references_for_current_scope.Assistant
It compiles. Now let me build oxlint to test the reproduction.
Assistant
The false positive is fixed. Let me test some edge cases.
Assistant
All edge cases behave correctly. Now let me run the existing semantic tests to see what snaps change.
Assistant
The example failure is unrelated. Let me run just the test binary.
Assistant
Let me see which snapshots changed (the full failure list).
Assistant
Only two snaps changed, both decorator-related. Let me examine the
parameter.snapchanges.Assistant
Both snap changes are the intended correct behavior (parameter decorator references now resolve in the class scope, consistent with method decorators). Per the guidance, I'll leave the snaps unchanged and clean up the
.snap.newfiles. But first, let me check whether any linter tests are affected (which would indicate incorrect behavior).Assistant
Let me run the linter tests for
no-unused-varsto ensure correctness.Assistant
Let me look at the relevant linter test cases for parameter decorators.
Assistant
Let me look at the linter test cases at lines 1208-1263 (decorator edge cases).
Assistant
Let me find how to run the no-unused-vars tests.
Assistant
These are unit tests run via
cargo test. Let me run the no-unused-vars unit tests.Assistant
All no-unused-vars linter tests pass. Let me run the full oxc_linter lib tests to ensure no other rule broke.
Assistant
All linter unit tests pass. Let me run the linter integration tests.
Assistant
Let me verify there are no other snapshot changes by running once more and checking for any new
.snap.newfiles.Assistant
Only 2 decorator snaps change, and the changes are correct (consistent with method decorators). Per the guidance, I'll leave the existing snaps unchanged. Let me clean up the
.snap.newfiles.Assistant
Now let me check whether the transformer (which handles decorators) depends on these scope IDs, to ensure I haven't broken anything there.
Assistant
Let me run the transformer tests to ensure the scope change doesn't affect decorator transformation.
Assistant
Transformer tests pass. Let me test a few more scenarios with the built oxlint to be thorough.
Assistant
All scenarios behave correctly. Let me re-confirm the exact issue reproduction is clean.
Assistant
The issue is fixed. Let me review my final change.
Assistant
Let me verify that visiting decorators (entering/leaving decorator nodes) restores
current_node_idbeforeparam.bindruns.Assistant
Let me check whether any checker relies on the parameter being bound before decorator nodes are checked.
Assistant
This checker is about
eval/argumentsbinding names, runs on binding identifier enter. Not related to decorators.Let me check the Decorator checker at line 1044.
Assistant
Let me check for any tests involving
superin decorators that might be affected.Assistant
Let me check whether conformance
.failsnapshots exist for the two affected decorator fixtures.Assistant
No conformance failures. Let me do a final review of the complete change and verify git status.
Assistant
Let me check the idiomatic way
oxc_allocator::Vecis referenced elsewhere in the codebase.Assistant
Let me run clippy on the semantic crate to ensure code quality.
Assistant
No clippy warnings. Let me run the broader workspace check to ensure dependent crates still compile.
Assistant
Let me verify there are no other places in the semantic builder that handle formal parameter decorators.
Assistant
Let me check the other
param.bind(self)calls at lines 2247 and 2498.Assistant
The fix covers all formal-parameter decorator cases. Let me run the formatter and minifier unit tests to ensure the scope change doesn't break them.
Assistant
Both pass. Let me do a final review of the complete change and verify the reproduction one final time.
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.