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?
codeoxlint test.tsWhat does your
.oxlintrc.json(oroxlint.config.ts) config file look like?jsonc// 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
tsconst foo = { KEY: 'token' }; declare function Inject(token: unknown): ParameterDecorator; export class C { constructor(@Inject(foo.KEY) private readonly foo: number) {} }Actual
code! 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 + does not report this — its scope analyser correctly counts the decorator expression as a use of the outer binding.
Assistant
Let me analyze the issue. The problem is that oxlint's
no-unused-varsrule incorrectly reports a false positive when a constructor parameter property shadows an imported (or outer) identifier that is used in the parameter's own decorator expression.The key insight is:
- Parameter decorators are evaluated at class-decoration time in the enclosing class scope, before any parameter binding exists
- So when
@Inject(foo.KEY)is used in a decorator,fooshould resolve to the outer binding, not the parameter property - The current implementation doesn't account for this, so it thinks the outer
foois unused
Let me start by exploring the repository structure to understand how it's organized and find the relevant code.
Assistant
The repository has a Rust codebase (oxc). Let me explore the structure more to find the linter-related code, specifically the
no-unused-varsrule.Assistant
I need to look at the
oxc_lintercrate to find theno-unused-varsrule. Let me explore that.Assistant
I need to find the
no-unused-varsrule in the oxc_linter crate. Let me search for it.Assistant
Let me explore the no_unused_vars directory structure.
Assistant
The user wants me to continue exploring the repository to find the no-unused-vars rule code. I was trying to find the files related to no_unused_vars. Let me run the command again.
Assistant
Now I can see the no_unused_vars rule structure. Let me explore the main module and understand how it handles scope resolution.
Assistant
Let me read the main mod.rs file to understand the structure of the rule.
Assistant
Let me use the proper tool call format to open the file.
Assistant
Now I need to understand how usages are detected. Let me look at the symbol.rs and usage.rs files.
Assistant
Now I need to look at the usage.rs file which likely contains the
has_usagesmethod that determines whether a symbol is used.Assistant
Now let me understand how the semantic analyzer handles decorators. The issue is that parameter decorators should reference the outer scope, not the parameter's own scope.
Assistant
The user wants me to continue exploring the repository to understand how decorators work and how scopes are handled. Let me look at the semantic analyzer to see how it handles decorator scopes, and also look at the parser to understand how parameter decorators are parsed.
Assistant
Let me look at how the semantic builder handles decorators, particularly for class methods and parameter properties. I need to understand how scopes are assigned to decorator expressions.
Assistant
Let me explore the semantic builder more to understand how scopes and decorators are handled.
Assistant
Let me look at the code around line 2363 to understand how parameter decorators are handled.
Assistant
Let me continue exploring to understand how the scope is entered for formal parameters and how decorators are visited.
Assistant
Let me understand the scope handling for formal parameters better. I need to look at how scopes are entered for functions that contain formal parameters, especially in the context of class methods.
The key question is: when we have:
tsconst foo = { KEY: 'token' }; export class C { constructor(@Inject(foo.KEY) private readonly foo: number) {} }The
foo.KEYreference in the decorator@Inject(foo.KEY)should resolve to the outerfoovariable, not the parameter propertyfoo. This is because decorators are evaluated at class decoration time (before the parameter binding exists).Let me look at how scopes are set up for functions (including constructors).
Assistant
Let me look at the visit_function to understand how scopes are set up for functions.
Assistant
Let me understand the issue better by looking at how scopes are set up for formal parameters and decorators.
Assistant
Now I understand the issue better. Let me create a test file to reproduce the issue first.
Assistant
Now let me build and run oxlint to reproduce the issue.
Assistant
The user seems to be waiting for me to continue. The cargo build timed out, so let me try building in debug mode instead or check if there's already a built binary.
Assistant
Good, the build is complete. Now let me run oxlint to reproduce the issue.
Assistant
I've confirmed the issue. Now let me understand the root cause. The problem is in how the semantic builder handles parameter decorators. Let me look at how
param.bind(self)and decorator visiting interact.Assistant
Let me continue exploring to understand the root cause. I need to see how the formal parameter binding interacts with the decorator scope.
Assistant
Now I need to understand how
bindworks on formal parameters and how it interacts with decorator visiting. Let me look at the trait definition.Assistant
Assistant
The user wants me to continue investigating. Let me look at the semantic example to understand how to debug the issue.
Assistant
Let me run the semantic example to see how references are being resolved for our test case.
Assistant
This is exactly the root cause I suspected. The output shows:
- The outer
foo(fromconst foo = { KEY: 'token' }) has no references. - The parameter
foo(fromprivate readonly foo: number) has a reference to it fromfoo.KEYin the decorator.
The semantic builder is incorrectly resolving the reference
foo.KEYin the decorator to the parameterfooinstead of the outerfoo. This is becauseparam.bind(self)is called BEFOREvisit_decorators(¶m.decorators).Let me look at the
visit_formal_parameterfunction more closely to implement the fix.- The outer
Assistant
I've confirmed the root cause. The issue is in
visit_formal_parameterandvisit_formal_parameter_restin/oxc/crates/oxc_semantic/src/builder.rs. The parameter is bound BEFORE its decorators are visited, which means references in the decorator expression resolve to the parameter binding instead of the outer scope.Looking at the code:
rustfn visit_formal_parameter(&mut self, param: &FormalParameter<'a>) { ... param.bind(self); // <-- binds parameter first self.visit_decorators(¶m.decorators); // <-- decorators visited after ... }The fix is to visit decorators BEFORE binding the parameter, because decorators are evaluated at class decoration time (before parameter binding exists).
Let me also check if there are similar issues with decorators on class methods:
Assistant
Let me also check the class decorator handling to see if the order is correct there:
Assistant
The user wants me to continue. I've identified the root cause: in
visit_formal_parameterandvisit_formal_parameter_rest, the parameter is bound BEFORE decorators are visited, causing decorator expressions to incorrectly resolve to the parameter binding.Now I need to fix this. The fix is to swap the order: visit decorators BEFORE binding the parameter. Let me also check if there are similar issues with decorators on methods (which might be visited in class body).
Assistant
Let me continue. I need to check if there are similar issues with method decorators in class bodies, then implement the fix.
Assistant
The user wants me to continue. I need to check if there are similar issues with method decorators and then implement the fix for the parameter decorator issue. Let me look at how class elements are visited.
Assistant
Let me check how class elements and method definitions are visited in the AST visit crate.
Assistant
Let me check the generated visit.rs to see how method definitions handle decorators and parameters:
Assistant
Let me continue exploring to see if the semantic builder overrides method definition handling and understand the full picture before implementing the fix.
Assistant
Good - the semantic builder doesn't override
visit_method_definition, so method decorators work correctly (thevisit_functionis called for method bodies, and within that, the parameter binding happens after decorators are visited throughvisit_formal_parameter).Wait, that's not right. Let me re-check. The
visit_formal_parameteris called withinvisit_functionviavisit_formal_parameters. And the order invisit_formal_parameteris:enter_nodeparam.bind(self)visit_spanvisit_decoratorsvisit_binding_pattern
The issue is that
param.bind(self)is called BEFOREvisit_decorators. This means when decorator expressions are visited, the parameter binding is already in scope, so references resolve to the parameter.The fix is to swap the order: visit decorators BEFORE binding the parameter. Let me also check what
param.bind(self)does:Assistant
I need to see the Binder implementation for FormalParameter and FormalParameterRest to understand what param.bind(self) does.
Assistant
Now I fully understand the issue. Let me implement the fix by swapping the order in
visit_formal_parameterandvisit_formal_parameter_restso that decorators are visited BEFORE the parameter binding is added.Assistant
Now I understand the issue completely. Let me implement the fix. The issue is in
visit_formal_parameterandvisit_formal_parameter_restin/oxc/crates/oxc_semantic/src/builder.rs. The parameter is bound BEFORE decorators are visited, causing decorator expressions to incorrectly resolve to the parameter binding.Assistant
Now I'll fix the issue by reordering the operations in
visit_formal_parameterandvisit_formal_parameter_restso that decorators are visited BEFORE the parameter binding is added to the scope. This ensures that references in decorator expressions resolve to the outer scope, not to the parameter binding.Assistant
Let me try the edit again with the correct format.
Assistant
Let me try the edit again with the correct tool call format:
Assistant
Let me try a different approach - use sed to make the replacement in-place.
Assistant
Let me continue fixing the issue by editing the file to swap the order of
param.bind(self)andself.visit_decorators(¶m.decorators).Assistant
I need to edit the file to swap the order of
param.bind(self)andself.visit_decorators(¶m.decorators)in thevisit_formal_parameterfunction, and similarly forvisit_formal_parameter_rest. Let me use a sed command to make this change.Assistant
Let me check if the sed command worked correctly by examining the modified file.
Assistant
The sed command partially worked but left some empty lines. Let me clean this up properly and also fix the
visit_formal_parameter_restfunction.Assistant
Let me continue by cleaning up the empty lines in visit_formal_parameter and also fixing visit_formal_parameter_rest.
Assistant
The sed command left some empty lines. Let me revert the changes and make a clean edit.
Assistant
Let me use the edit command properly to make the fix:
Assistant
Let me try with the proper heredoc syntax:
Assistant
Good, the first fix is applied. Now I need to apply the same fix to
visit_formal_parameter_rest. Let me update that function too:Assistant
Good, the changes have been applied to both functions. Let me verify the complete diff and then rebuild to test the fix.
Assistant
The diff looks correct. Both
visit_formal_parameterandvisit_formal_parameter_restnow visit decorators before binding the parameter. Let me rebuild and test.Assistant
Good, the build succeeded. Now let me test if the fix resolves the issue:
Assistant
The user wants me to continue. The fix didn't work as expected. The issue is more complex than just reordering the visit. Let me investigate further.
Assistant
I see the issue now. My initial fix (swapping order) didn't work because the reference resolution happens later via
resolve_references_for_current_scope(). At that point, the parameter binding has already been added, so references are still matched to it.The real fix is to visit decorator expressions in the parent scope (class scope) instead of the function scope. This matches TypeScript semantics where decorators are evaluated at class decoration time, before any parameter binding exists.
Let me look at how scope switching works:
Assistant
I see the problem now. The issue is more complex than just reordering - the reference resolution happens later via
resolve_references_for_current_scope(), so simply reordering doesn't help because the parameter binding is already added before the resolution step.The real fix is to visit decorator expressions in the parent scope (class scope) instead of the current function scope. This matches TypeScript semantics where decorators are evaluated at class decoration time.
Assistant
Let me look at how
resolve_references_for_current_scope()works to understand why my fix didn't work:Assistant
I need to understand why my fix didn't work. Let me look at
resolve_references_for_current_scope():Assistant
I see the issue now. Simply reordering the visit doesn't help because
resolve_references_for_current_scope()resolves references in the function scope, and by that point, the parameter binding has already been added.The real fix is to visit decorator expressions in the parent scope (the scope before the function scope), so references resolve to the outer scope. Let me revert my changes and implement a proper fix.
Assistant
Assistant
Let me look at
walk_up_resolve_referenceto understand how references are resolved:Assistant
Let me look at
walk_up_resolve_referenceto understand how references are resolved:Assistant
Now I understand the issue. The
walk_up_resolve_referencefunction walks up the scope chain starting from the reference'sscope_id. If the reference is in the function scope and the parameter binding is also in the function scope, the reference will be resolved to the parameter binding.The fix is to visit decorator expressions in the parent scope (class scope), so references will be created with the parent scope_id and will be resolved to outer bindings, not the parameter binding.
Assistant
Now I understand the issue completely. The
walk_up_resolve_referencefunction starts from the reference'sscope_idand walks up the scope chain. If the reference is created in the function scope and the parameter binding is also in the function scope, the reference will be resolved to the parameter binding.The fix is to visit decorator expressions in the parent scope (class scope), so references will be created with the parent scope_id. When
walk_up_resolve_referenceis called, it will start from the parent scope and find the outer binding.Assistant
Let me try the edit command again with the proper format.
Assistant
I see the edit left some broken code. The
visit_formal_parameter_restfunction got corrupted. Let me fix this.Assistant
I see the edit left some broken code. Let me revert and try again.
Assistant
Let me continue investigating. The edit left the file in a broken state. Let me verify the file is clean and understand the issue better.
Assistant
Good, the file is clean. Now let me verify the current line numbers for both functions I need to edit.
Assistant
Let me understand the issue better by looking at the exact line ranges.
Assistant
Now I have a clear picture of the code. The fix is to visit decorators in the parent scope so that references resolve to outer bindings. Let me implement this properly.
Assistant
Let me verify the changes look correct.
Assistant
Good, the diff looks correct. Now let me rebuild and test.
Assistant
Build succeeded. Now let me test if the fix resolves the issue:
Assistant
Excellent! The fix works! The false positive is gone. Let me verify with the semantic example to confirm references are resolved correctly:
Assistant
The fix works correctly. Now the outer
foohas a reference fromfoo.KEYin the decorator, while the parameterfoohas no references. This is exactly the correct behavior.Let me now test some edge cases:
Assistant
Good - the "Decorators are not valid here" error is expected (decorators are only valid on class method parameters, not regular function parameters). Let me fix the edge case test file and verify:
Assistant
The fix is working for the main issue. The
...restparameter warning is a separate, pre-existing issue (rest parameters being reported as unused). Let me remove that edge case and run the existing tests to ensure my fix doesn't break anything:Assistant
All edge cases pass. Now let me run the existing tests to make sure my fix doesn't break anything.
Assistant
Let me run the tests for the linter specifically, focusing on the no_unused_vars rule:
Assistant
All 47 no_unused_vars tests pass. Let me also run the semantic tests:
Assistant
All tests pass. Let me also run some broader tests to make sure the fix doesn't break other functionality.
Assistant
All 1144 linter tests pass. Now let me add a test case for this specific issue to prevent regression:
Assistant
Let me look at the test file structure to understand where to add a test case:
Assistant
Let me search for how tests are organized and find a good place to add a test case for this specific bug.
Assistant
Let me look at the oxc.rs test file to find the right place to add a test case:
Assistant
Let me look at the structure of the oxc.rs test file to understand where to add a test:
Assistant
Let me add a test case for this specific issue to prevent regression:
Assistant
Let me add a test case for this specific issue. I'll add it to the typescript_eslint.rs test file since it's related to TypeScript parameter decorators.
Assistant
Let me add a test case for the parameter decorator shadowing issue:
Assistant
The user wants me to continue testing my fix. I've added a test case for the parameter decorator shadowing issue. Now I need to run the tests to make sure they pass.
Assistant
The test failed because NoUnusedVars requires fix cases when using
test(). Let me fix the test to use a different approach:Assistant
Let me look at how other tests handle this and fix my test:
Assistant
Let me fix the test by adding a fail vector (can be empty):
Assistant
Let me continue by checking the tester API and fixing the test properly:
Assistant
Let me look at the tester.rs to understand how to fix the test:
Assistant
Let me look at how to properly handle the fix test:
Assistant
The user wants me to continue. I need to fix the test properly. The issue is that NoUnusedVars has a fixer, so the tester requires fix test cases. Let me update the test to not require fix test cases by using a different approach.
Assistant
The user wants me to continue. I need to fix the test by using
intentionally_allow_no_fix_tests()method. Let me update the test file.Assistant
Let me continue by running the test to see if it passes now:
Assistant
The test passes. Let me run all no_unused_vars tests to make sure everything still works:
Assistant
Excellent! All 48 no_unused_vars tests pass. Let me run the full linter test suite to make sure nothing is broken:
Assistant
All 1145 linter tests pass. Let me also clean up by removing the test files I created:
Assistant
Let me verify the final state of my changes:
Assistant
The user wants me to submit my changes. I've successfully fixed the issue and all tests pass. Let me submit the changes.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.