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 and locate relevant files for the no-unused-vars rule. Let's first check the root directory.
- Assistant
Let me look for the no-unused-vars rule implementation.
- Assistant
Let me first explore the structure of the no-unused-vars rule and understand the codebase.
- Assistant
Let me first create a reproduction script to verify the issue.
- Assistant
Let me find where the linter binary is.
- Assistant
Let me check if there's a cargo binary or build directory.
- Assistant
Let me look at the semantic analysis and how decorators are handled in the scoping/resolution. The issue is that when a constructor parameter property has the same name as an outer binding, and that outer binding is used in the parameter's own decorator, the outer binding is reported as unused. This suggests the scope analysis might not properly track uses in decorators.
Let me first look at the semantic analysis and scope code.
- Assistant
Let me look at how the semantic analyzer resolves references, specifically around decorators and constructor parameter properties.
- Assistant
The issue is about how the linter handles constructor parameter decorators when a parameter property shadows an outer variable that's used in the decorator. Let me understand the problem better:
- There's an outer variable
foo - There's a constructor parameter
private readonly foowith a decorator@Inject(foo.KEY) - The
fooin@Inject(foo.KEY)should refer to the outerfoo, not the parameter property - But oxlint's
no-unused-varsreports the outerfooas unused
This is a scope resolution issue. The decorator expression should be evaluated in the outer scope (before the parameter binding is created), so
fooin the decorator should resolve to the outerfoo, not the constructor parameter.Let me look at the semantic analysis code to understand how scopes work for constructor parameters and decorators.
- There's an outer variable
- Assistant
Let me look at the semantic builder to understand how decorators and scopes are handled.
- Assistant
Let me look at how the semantic builder handles decorators in the context of constructor parameters.
- Assistant
I see! In
visit_formal_parameter, the code callsparam.bind(self)BEFOREself.visit_decorators(¶m.decorators). This means the parameter binding (includingprivate readonly foo) is created in the scope before the decorator expressions are visited. This causes the decorator's reference tofooto resolve to the parameter'sfoobinding instead of the outerfoo.Let me check how the
bindmethod works and what scope it creates. - Assistant
Let me look at where bind is defined.
- Assistant
Let me look at the
FormalParameterbind implementation and understand the ordering issue. - Assistant
Let me look at the FormalParameter bind implementation more closely.
- Assistant
Now I understand the issue. In
visit_formal_parameter:fn visit_formal_parameter(&mut self, param: &FormalParameter<'a>) { let kind = AstKind::FormalParameter(self.alloc(param)); self.enter_node(kind); param.bind(self); // This declares the parameter's binding (e.g., `foo`) self.visit_span(¶m.span); self.visit_decorators(¶m.decorators); // This visits decorators AFTER the binding is declared self.visit_binding_pattern(¶m.pattern); ... self.leave_node(kind); } - Assistant
Now I see the issue. In
visit_formal_parameter, the order is:param.bind(self)— binds the parameter namefooin the current (constructor) scopeself.visit_decorators(¶m.decorators)— visits the decorators, but by nowfooalready resolves to the newly-created binding
This means the decorator expression
@Inject(foo.KEY)resolvesfooto the constructor parameter, not the outerconst foo. And since the outerfooisn't referenced by anything else, it appears "unused".The fix is to visit decorators before calling
param.bind(self). Let me check the regular visitor to see what order it uses: - Assistant
Let me look at the generated visit for formal_parameter to see the default visit order.
- Assistant
Let me look at the walk function:
- Assistant
Let me look at the walk function to see the expected order:
- Assistant
Let me look at the walk function to see the expected order:
- Assistant
Now I can see the issue clearly. The standard
walk_formal_parametervisits decorators BEFORE the binding pattern. But in the semantic builder'svisit_formal_parameter,param.bind(self)is called BEFORE visiting decorators. This means the constructor parameter binding (e.g.,private readonly foo) is created in the scope before the decorator expressions are visited.So when visiting
@Inject(foo.KEY),fooalready resolves to the newly created parameter binding instead of the outerconst foo. This makes the outerfooappear unused.The fix is simple: move
self.visit_decorators(¶m.decorators)BEFOREparam.bind(self)invisit_formal_parameter.Let me also check for the same issue in
visit_formal_parameter_rest: - Assistant
Now I can see the issue clearly. In
visit_formal_parameter:fn visit_formal_parameter(&mut self, param: &FormalParameter<'a>) { let kind = AstKind::FormalParameter(self.alloc(param)); self.enter_node(kind); param.bind(self); // ← binds the parameter name (e.g., `foo`) into the scope FIRST self.visit_span(¶m.span); self.visit_decorators(¶m.decorators); // ← then visits decorators self.visit_binding_pattern(¶m.pattern); ... }The issue is that
param.bind(self)creates a binding for the parameter (e.g.,foo) in the current scope BEFORE visiting the decorators. So when the decorator@Inject(foo.KEY)is visited,fooresolves to the newly-created constructor parameter binding, not the outerconst foo. This means the outerfooappears unused.The fix is simple: visit decorators BEFORE calling
param.bind(self). Let me also checkvisit_formal_parameter_rest: - Assistant
Let me now check how
visit_decoratorsis handled. Do decorators create any bindings or reference resolutions? - Assistant
Now let me look at how the formal parameters scope is entered for the function/constructor.
- Assistant
Now I understand the problem. The issue is in
visit_formal_parameterin the semantic builder. The order of operations is:param.bind(self)— This creates a binding for the parameter (e.g.,foo) in the function scopeself.visit_decorators(¶m.decorators)— This visits the decorators
But decorators should be resolved in the scope before the parameter binding exists, because decorators are evaluated at class-definition time, not when the constructor is called. The
@Inject(foo.KEY)decorator should resolvefooto the outerconst foo, not to the parameterfoo.The fix is to visit decorators before calling
param.bind(self).Let me first look at the related issues mentioned to understand the pattern:
- Assistant
Let me check the existing tests for the no-unused-vars rule to understand the test patterns:
- Assistant
Now I understand the issue clearly. In
visit_formal_parameter(line 2358-2370 of builder.rs),param.bind(self)is called beforeself.visit_decorators(¶m.decorators). This means the constructor parameter's name (e.g.,foo) is bound in the function scope before the decorator expressions are visited. So whenfoois used in@Inject(foo.KEY), the scope resolver finds the parameter bindingfooinstead of the outerconst foo.The fix is to visit decorators before binding the parameter. Let me apply the fix:
- Assistant
Now I'll fix both
visit_formal_parameterandvisit_formal_parameter_restby movingvisit_decoratorsbeforeparam.bind(self): - Assistant
Now let me fix
visit_formal_parameter_restas well: - Assistant
Now let me build the project and run the tests to verify the fix. First, let me check if there's a way to build:
- Assistant
The build succeeded. Now let me test with the minimal reproduction case from the issue:
- Assistant
Hmm, the fix didn't work. The issue is deeper - it's not just about the order of operations. The problem is about which scope the decorator expression is resolved in. Let me understand the scope management better.
- Assistant
Let me look at how the function scope works. The issue is that the decorator expression
@Inject(foo.KEY)is on the constructor parameter, but thefooidentifier inside it resolves to the constructor parameterfooinstead of the outerconst foo. This is because the decorator is being visited in the constructor's scope, where the parameterfoois already bound.Wait, but I already moved the decorator visit before the bind. Let me check if the issue is that the function scope has already been entered at this point, and the parameter binding is part of the function scope.
Let me look at how visit_function works and when the function scope is entered.
- Assistant
I see the issue now. The
visit_functionenters the function scope BEFORE visiting formal parameters. So even though I movedvisit_decoratorsbeforeparam.bind, the function scope is already entered at that point. The parameter binding will be in the function scope. But the key issue is that when the decorator expression referencesfoo, the scope resolution should be looking for it in the outer scope, not the function scope.Let me look at how identifier references are resolved in the context of decorators. The key is in how
visit_identifier_referenceworks. - Assistant
Let me look at
visit_identifier_reference: - Assistant
Let me look at
reference_identifierto understand how references are resolved: - Assistant
Let me look at how
resolve_references_for_current_scopeworks: - Assistant
I see the problem now. Looking at the
visit_functioncode,resolve_references_for_current_scope()is called at the END of formal parameters (after all params are visited). So even though my change movedvisit_decoratorsbeforeparam.bind(self), the references from decorators are only resolved after all parameters are bound.Let me look at how references are collected:
- Assistant
Let me look at how references are stored and resolved.
- Assistant
Let me look at
declare_reference: - Assistant
Let me look at
declare_reference: - Assistant
Let me see the rest of
walk_up_resolve_reference: - Assistant
I see now. The issue is that in
visit_function,resolve_references_for_current_scope()is called after all formal parameters are visited. So even though I movedvisit_decoratorsbeforeparam.bind, the references from decorators are resolved AFTER all parameters are bound.Let me look at the
visit_functioncode more carefully: - Assistant
I see now. The problem is more fundamental. Even with my fix, the resolution happens in
resolve_references_for_current_scope()which is called after ALL parameters are bound. So the decorator's reference tofoowill still resolve to the parameter bindingfoo.Let me think about this differently. The issue is that decorators on constructor parameters should be evaluated in the class scope (enclosing scope), not the constructor's function scope. Let me look at how classes and their decorators are handled:
- Assistant
Let me look at the visit_class method and understand the class scope:
- Assistant
Let me understand the flow better. The issue is that even though we visit decorators before binding the parameter, the actual reference resolution happens later in
resolve_references_for_current_scope()at the end of all params. So the decorator reference gets stored as unresolved, but when it's resolved, the parameter is already bound.The key insight is: decorator references for formal parameters need to be resolved immediately (before the parameter binding is created), not deferred. Let me implement this approach:
- Assistant
- Assistant
Now I see the full picture. The issue is:
- In
visit_function, the function scope is entered, thenvisit_formal_parametersis called - In
visit_formal_parameter,param.bind(self)creates the parameter binding (e.g.,foo) in the function scope - Then decorators are visited, creating references that are added to
unresolved_references - After ALL parameters are visited,
resolve_references_for_current_scope()resolves all references - At this point, the parameter
foois already bound in the function scope, so the decorator'sfooreference resolves to it instead of the outerconst foo
Even with my change (visiting decorators before binding), the references are still deferred and resolved later. The key issue is that
resolve_references_for_current_scope()is called after ALL parameters are bound, so the parameter binding is found before the outer binding.The fix is to resolve decorator references immediately after visiting decorators, before binding the parameter. This way, the decorator's
fooreference resolves to the outerconst foobefore the parameterfoois bound. - In
- Assistant
Now let me update
visit_formal_parameter_restin the same way: - Assistant
Now let me rebuild and test:
- Assistant
Good, it compiles. Now let me test with the reproduction case:
- Assistant
The false positive is gone! Now let me run the existing tests to make sure nothing is broken:
- Assistant
The error is about an example, not the tests. Let me try running just the tests:
- Assistant
Good, the semantic tests pass. Now let me run the linter tests:
- Assistant
All 47 no-unused-vars tests pass. Let me also run a broader set of tests to make sure nothing else is broken:
- Assistant
All 1144 linter tests pass. Let me also run the semantic tests more broadly:
- Assistant
The cfg example has a pre-existing issue (no main function). Let me run just the lib tests:
- Assistant
There's a test failure. Let me look at the failing test:
- Assistant
Let me look at the test to understand what it expects.
- Assistant
The test expects
b = cto reference the third parameterc, not the rootc. But with my change, the reference fromb = cis resolved before parametercis bound (because I callresolve_references_for_current_scope()before binding each parameter). This causes thecreference to resolve to rootcinstead of parameterc.The issue is that I'm resolving ALL references from the checkpoint, including those from parameter initializers. I need to be more targeted: only resolve decorator references before binding, not initializer references.
Let me fix this by using a temporary checkpoint for decorator resolution:
- Assistant
Now let me also update
visit_formal_parameter_restsimilarly: - Assistant
Now let me rebuild and run the tests:
- Assistant
Now let me test the reproduction case:
- Assistant
Good, the reproduction case still passes. Now let me run the semantic tests:
- Assistant
All 60 semantic tests pass. Let me also run the linter tests:
- Assistant
All 1144 linter tests pass. Let me also test with some additional edge cases to make sure the fix is robust:
- Assistant
All edge cases pass. Let me also test with a rest parameter with a decorator:
- Assistant
Good -
argsis correctly flagged as unused (it's never used), butfoois NOT reported as unused. This is correct.Let me also run more comprehensive tests to ensure nothing is broken:
- Assistant
All 60 integration tests pass. Let me also check if there are any other tests that might be affected:
- Assistant
Let me also look at the existing decorator tests in the
no_unused_varsto make sure we haven't broken anything: - Assistant
Let me check if there are any tests for the specific pattern where a decorator references an outer variable that has the same name as the parameter property:
- Assistant
Let me also verify the fix works with the import case mentioned in the issue:
- Assistant
Let me also verify the fix works with the import case where
fooitself is imported: - Assistant
Let me also check if there are any snapshot tests related to scopes that might be affected:
- Assistant
Let me run the snapshot tests to see if they are affected:
- Assistant
All tests pass. Let me also verify by looking at the diff of my changes:
- Assistant
The diff looks clean and correct. Let me run a few more comprehensive tests to make sure everything is fine:
- Assistant
Let me also run the semantic coverage tests to make sure nothing is broken there:
- Assistant
Let me also verify the fix with a more comprehensive test to make sure it handles edge cases correctly:
- Assistant
The warnings are correct:
testandtest2are flagged as unused functions (correct, they're never called)foo,bar,bazare NOT flagged as unused (correct, they're used in decorators)- The forward reference in
b = astill works correctly
Let me also verify that the forward reference in initializers still works correctly with a targeted test:
- Assistant
The behavior is correct. Root
chas 0 references becausea = candb = cresolve to the parameterc(which is bound later), not the rootc. This matches the expected JavaScript/TypeScript semantics where parameter names are all bound before any default values are evaluated.Let me now review the complete changes and make sure everything is in order:
- Assistant
The fix is clean and minimal. Let me run a comprehensive test suite one more time to ensure everything is working correctly:
- Assistant
All 60 semantic tests pass. Let me also run the linter tests one more time:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.