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 then locate the relevant code for the
no-unused-varslint rule.Assistant
Let me explore the structure more and look for the
no-unused-varsrule specifically.Assistant
There's a lot of output. Let me search more specifically for the no-unused-vars rule implementation.
Assistant
I found it. The
no_unused_varsdirectory is at/oxc/crates/oxc_linter/src/rules/eslint/no_unused_vars. Let me explore that directory.Assistant
Now let me first reproduce the issue by creating a test file and running oxlint on it.
Assistant
Now let me find where the oxlint binary is or how to build and run it.
Assistant
This is a Node.js wrapper. Let me check if there's a Rust binary that I can build and run directly:
Assistant
Let me check the justfile to see how to build and run the project:
Assistant
Let me build oxlint and run it on the test file:
Assistant
Good, the build succeeded. Now let me run oxlint on the test file:
Assistant
I've successfully reproduced the issue. Now let me explore the codebase to understand how the
no-unused-varsrule works and where the bug might be. Let me look at the test files first to see if there are any decorator-related tests:Assistant
Let me look at the main implementation file:
Assistant
Now let me look at the
usage.rsfile, which likely contains the logic for determining if a symbol is used:Assistant
Let me continue reading the usage.rs file:
Assistant
Let me search for any code related to decorator or parameter handling in the usage logic:
Assistant
Now let me look at the
symbol.rsfile to see how references are collected:Assistant
The issue is about scope analysis in decorators. The problem is that when a reference to
fooappears in a decorator (@Inject(foo.KEY)), the scope analyzer is incorrectly resolving it to the parameterfooinstead of the outerconst foo. Let me look at how the semantic analysis handles this:Assistant
Let me look at the semantic builder to understand how scopes are created for decorators and parameters:
Assistant
This is interesting. In
visit_formal_parameter, the parameter is bound first (param.bind(self)) and then decorators are visited. The issue is that when decorators are visited, the parameter binding has already been added to the scope. Let me see how scopes work for formal parameters:Assistant
...
Assistant
I need to continue investigating the issue. Let me look at how the formal parameters are visited and how scopes work for decorators.
Assistant
Let me look at the AST visitor trait:
Assistant
I found it! The key is in
builder.rsat lines 2358-2372. Let me trace the issue more carefully:In
visit_formal_parameter:param.bind(self)- binds the parameter to the current scopeself.visit_decorators(¶m.decorators)- visits decorators AFTER the parameter is boundself.visit_binding_pattern(¶m.pattern)- visits the binding pattern
The problem is that decorators are visited after the parameter is bound. This means when the decorator references
foo, it resolves to the parameterfooinstead of the outer binding.According to TypeScript semantics, parameter decorators are evaluated in the enclosing class scope, not the parameter scope. So decorators should be visited before the parameter binding is added to the scope.
Let me verify this understanding by checking the test cases. )
<tool_call> <function=bash_terminal> <parameter=command> grep -n "visit_formal_parameter\b" /oxc/crates/oxc_ast_visit/src/generated/visit.rs </tool_call>
Assistant
Let me search for the visit_formal_parameter function in the AST visitor generated code.
Assistant
Now I understand the problem better. The issue is in the semantic builder (
/oxc/crates/oxc_semantic/src/builder.rs). When visiting a formal parameter:param.bind(self)is called first - this binds the parameter name to the scope- Then
self.visit_decorators(¶m.decorators)is called - this visits the decorators
The problem is that decorators are visited AFTER the parameter is bound. So when a decorator references an outer variable with the same name as the parameter, the reference resolves to the parameter instead of the outer binding.
According to TypeScript semantics, parameter decorators are evaluated in the enclosing scope (class scope for constructor parameters), NOT in the parameter scope. So decorators should be visited BEFORE the parameter binding is added to the scope.
Let me verify this by looking at the code more carefully and then fix it. )
<tool_call> <function=bash_terminal> <parameter=command> sed -n '2358,2385p' /oxc/crates/oxc_semantic/src/builder.rs </tool_call>
Assistant
The shell is executing a command, let me wait for the response.
Assistant
Now I can see the problem. In
visit_formal_parameter, the order is:param.bind(self)- binds the parameter to the scopeself.visit_decorators(¶m.decorators)- visits decorators
According to the issue description, parameter decorators are evaluated in the enclosing class scope, before any parameter binding exists. So decorators should be visited before the parameter is bound.
Let me fix this by swapping the order of visiting decorators and binding the parameter:
Assistant
I need to use the correct format. Let me try a different approach - use the edit command directly.
Assistant
Let me try using sed to make the fix:
Assistant
I can see the issue. The
param.bind(self)is called beforeself.visit_decorators(¶m.decorators). This means when the decorator referencesfoo, it resolves to the parameterfooinstead of the outer binding.The fix is to visit decorators BEFORE binding the parameter. I need to swap the order so that:
- First visit decorators (while the outer
foois still in scope) - Then bind the parameter (so
foonow refers to the parameter)
Let me create a sed command to fix this.
- First visit decorators (while the outer
Assistant
Let me verify if the change was applied correctly:
Assistant
The sed command went wrong. Let me revert the changes using git:
Assistant
Let me use a Python script to make the edit more reliably.
Assistant
The Python script was executed and the file was modified. Let me verify the changes were applied correctly by looking at the modified sections.
Assistant
Good! The changes look correct. Now I've swapped the order so that:
self.visit_decorators(¶m.decorators)is called beforeparam.bind(self)
This means decorators will be visited while the outer binding is still in scope, and the reference will correctly resolve to the outer
fooinstead of the parameterfoo.Let me rebuild and test:
Assistant
The build succeeded. Now let me run oxlint on the test file to see if the fix works:
Assistant
The fix didn't work yet. Let me investigate further. The issue might be more subtle. Let me look at how scopes work for parameters and decorators.
Assistant
The fix I made didn't work. Let me investigate further to understand how scopes work in this case.
The issue is that even though I'm visiting decorators before binding the parameter, the scope might still be set up in a way that causes the reference to resolve incorrectly.
Let me look at how scopes are entered for function/class parameters and where decorator references are resolved. )
<tool_call> <function=bash_terminal> <parameter=command> cd /oxc && grep -n "enter_scope" crates/oxc_semantic/src/builder.rs | head -50 </tool_call>
Assistant
The command seems to have executed but there's no output. Let me check the scope structure more carefully. The issue might be that even though I'm visiting decorators before binding the parameter, the reference resolution is happening at a different time (during semantic analysis resolution phase).
Let me look at how references are resolved and where the scope chain is set up.
Assistant
Let me understand the scope structure better. The issue is that parameter decorators should be evaluated in the class scope, not in the parameter scope. Let me look at how the class scope is entered and where formal parameters are visited.
Assistant
Let me look at how the class body and constructors are visited:
Assistant
Let me understand the scope binding mechanism better:
Assistant
I need to understand how the binding works and where the scope is created for formal parameters. Let me look at the FormalParameter bind implementation.
Assistant
Let me trace through the scope handling for functions and constructors to understand where the issue lies:
Assistant
The issue is becoming clearer now. Looking at the code flow:
- In
visit_function, the function scope is entered first - Then
visit_formal_parametersis called - For each parameter,
visit_formal_parameteris called - Inside
visit_formal_parameter:- The parameter is bound with
param.bind(self)- this adds the symbol to the (which is the function scope)
- The parameter is bound with
- In
Assistant
The command executed successfully. Now let me run the test again to see if the fix works.
Assistant
Actually, let me check if the reference resolution is working correctly. The issue might be that even though I changed the order, the reference is still not being resolved to the outer binding. Let me investigate how references are resolved.
Assistant
Let me understand the reference resolution mechanism. When a reference is created, it's initially unresolved. Then
walk_up_resolve_referenceis called to resolve it by walking up the scope chain.The key insight is that
walk_up_resolve_referencewalks up from the current scope to find the symbol. If the parameterfoohas already been bound to the constructor scope, the reference will resolve to that parameter instead of the outerconst foo.My change visits decorators BEFORE binding the parameter. So when the decorator is visited:
- The reference
foois created walk_up_resolve_referenceis called- It walks up from the current scope (constructor scope)
- At this point,
foo(the parameter) has NOT been bound yet - So it should continue walking up and find the outer
const foo
But the error is still occurring. Let me verify my change was applied correctly and check if there's something else going on.
Let me look at the current state of the builder.rs file to make sure my change is there.
- The reference
Assistant
The change is there - decorators are visited before binding. But the error still occurs. Let me debug this further by creating a simple test to see what's happening with the scope analysis.
Wait, I think I understand the issue now. Looking at the code more carefully:
In
visit_function:self.enter_scope(...)is called - this enters the function/constructor scope- Then
self.visit_formal_parameters(&func.params)is called - For each parameter,
visit_formal_parameteris called - Inside
visit_formal_parameter:self.visit_decorators(¶m.decorators)is called- The decorator references are resolved in the CURRENT scope (which is the constructor scope)
- At this point,
param.bind(self)hasn't been called yet, sofoois NOT in the constructor scope - Therefore, the reference should walk up and find the outer
const foo
But the error is still happening! Let me check if there's something else going on...
Actually, let me look at how
visit_decoratorsworks and when references are resolved:Assistant
...
Assistant
Let me search for the visit_decorators function definition:
Assistant
The
visit_decoratorsfunction is probably inherited from the AST visitor trait. Let me check how references are resolved when visiting expressions:Assistant
...
Assistant
... Let me understand the reference resolution mechanism better. When a reference is created:
declare_referenceis called- The reference is added to
unresolved_references - Later,
resolve_all_referencesorresolve_references_for_current_scopeis called walk_up_resolve_referencewalks up the scope chain starting from the reference's scope_id
The key is the
scope_id()of the reference. This is set when the reference is created. Let me look at how this is done: )<tool_call> <function=bash_terminal> <parameter=command> cd /oxc && grep -n "reference.*scope_id|scope_id.*reference|Reference::new|create_reference" crates/oxc_semantic/src/builder.rs | head -20 </tool_call>
Assistant
The command was successful. Let me trace through the reference resolution flow to understand when and how references are resolved.
The issue might be that:
- The reference's scope_id is set when the reference is created (during AST traversal)
- At that point, the parameter
foomight already be in the current scope - Even if I visit decorators before binding the parameter, the parameter might still be visible in the scope
Wait, let me re-read my change. I swapped the order so that decorators are visited BEFORE
param.bind(self)is called. So when the decorator is visited and the referencefoois created, the parameterfooshould NOT be bound yet in the constructor scope.But the reference resolution might happen later, after all parameters are bound. Let me look at when
resolve_references_for_current_scopeis called.Assistant
Let me understand the reference resolution mechanism better. Looking at line 2644, I can see that when a
Referenceis created, it getsself.current_scope_idas its scope. Then in line 558,walk_up_resolve_referencestarts from that scope and walks up the scope chain.The key insight is that the reference is created with the current scope at the time of visiting the identifier. So if I visit decorators before binding the parameter, the reference should be created with the constructor scope, but the parameter
fooshould NOT be bound yet in that scope.Let me verify that my change is working by checking if the reference is being resolved correctly. The issue might be that
resolve_references_for_current_scopeis called AFTER all parameters are bound, which would mean the parameterfoois already in the scope when resolution happens.Wait, let me check line 1915 and 2007 where
resolve_references_for_current_scopeis called.Assistant
Let me check when reference resolution happens in the function visit flow:
Assistant
I see the issue now. The reference resolution happens AFTER all parameters are bound (at line 1916), so even though I visit decorators before binding the parameter, when resolution occurs, the parameter is already in the scope.
Let me look at how the constructor and its scope are handled:
Assistant
Assistant
Assistant
The previous command successfully printed the grep output showing how
unresolved_referencesandunresolved_references_checkpointare used throughout the builder. I can see the pattern used for checkpointing:- Save the current checkpoint:
let saved_checkpoint = self.unresolved_references_checkpoint;
- Save the current checkpoint:
Assistant
The Python script executed successfully. Let me verify the changes were applied correctly by looking at the modified code.
Assistant
The previous Python script successfully modified the
builder.rsfile. I can see the changes were applied correctly:- In
visit_formal_parameter: Added checkpointing around decorator visiting and immediate resolution - In
visit_formal_parameter_rest: Same pattern applied
The key change is:
- Before binding the parameter, we visit decorators and immediately resolve their references
- This ensures decorator references are resolved before the parameter is bound
- The walk-up resolution will then find the outer binding instead of the parameter
Now I need to rebuild and test if this fixes the issue.
- In
Assistant
The build was successful! Now let me test if the fix works by running oxlint on the test file.
Assistant
Excellent! The fix is working! The false positive warning is no longer reported. The output shows "Found 0 warnings and 0 errors."
Now let me run the existing tests to make sure I didn't break anything:
Assistant
All 47 no_unused_vars tests passed. Now let me also run the semantic tests to make sure the scope analysis changes didn't break anything:
Assistant
The error is in an example file (
cfg.rs), not in the tests. This is likely a pre-existing issue. Let me run the tests excluding examples:Assistant
All 11 semantic library tests passed! The error in the example file is unrelated to my changes. Let me also run the integration tests for semantic:
Assistant
Excellent! All semantic tests pass (60 tests). Now let me test some edge cases to make sure my fix handles them correctly:
Assistant
The error is expected - the syntax
private readonly ...restis invalid. Let me fix the test case and try again:Assistant
The previous test passed. All edge cases are working correctly:
- Multiple parameters with decorators shadowing outer bindings - works
- Multiple parameters referencing the same outer binding - works
- Nested decorator expressions - works
- Parameters without decorators are still checked for unused vars (note:
unusedin C5 is considered used because it's a parameter property withprivate readonly, which is a valid use case) - Imports used in decorators - works
Let me also verify that the original test case from the issue works correctly:
Assistant
The original issue from the bug report is now fixed. Let me also run a broader test suite to ensure I haven't broken anything else:
Assistant
Let me check the full test output to see if there are any failures:
Assistant
All 1144 linter tests passed! The fix is working correctly. Let me verify my changes one more time and then submit:
Assistant
The changes look correct. Let me verify the fix one more time by running the original test case:
Assistant
I have successfully resolved the issue. Let me provide a summary of what was done:
Checklist Verification
-
✅ If an error was reported in the issue description, I have successfully reproduced it.
- Created a test file with the exact code from the issue
- Confirmed the false positive warning was being reported
-
✅ If an error was reported in the issue description, I have confirmed that my fix resolves the error.
- Applied the fix to
crates/oxc_semantic/src/builder.rs - Verified the warning is no longer reported
- Applied the fix to
-
✅ I have thought about edge cases and made sure my fix handles them as well.
-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.