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
Let me analyze this issue. The problem is that oxlint's
no-unused-varsrule is incorrectly reporting a false positive when:- A constructor parameter property has the same name as an imported/bound identifier
- That outer binding is used in the parameter's own decorator expression (like
@Inject(foo.KEY))
The issue is that
fooin@Inject(foo.KEY)should resolve to the outer binding, not the parameter propertyfoo, because decorators are evaluated before the parameter binding exists.Let me first explore the repository structure to understand how the codebase is organized.
Assistant
This is a Rust-based linter (oxlint). Let me explore the structure to find where the
no-unused-varsrule is implemented.<tool_call> <function=bash_terminal> <parameter=command> find /oxc -type f -name "*.rs" | xargs grep -l "no-unused-vars" 2>/dev/null | head -20 </tool_call>
Assistant
Let me try a different approach to find the no-unused-vars rule.
Assistant
Let me explore the directory structure for no_unused_vars to understand the codebase better.
<tool_call> <function=bash_terminal> <parameter=command> ls -la /oxc/crates/oxc_linter/src/rules/eslint/no_unused_vars/ </tool_call>
Assistant
Let me try again - the tool call should be properly structured.
Assistant
Good, this looks like the main directory for the no_unused_vars rule. Let me first create a test file to reproduce the issue.
<tool_call> <function=bash_terminal> <parameter=command> cat > /tmp/test.ts << 'EOF' const foo = { KEY: 'token' };
declare function Inject(token: unknown): ParameterDecorator;
export class C { constructor(@Inject(foo.KEY) private readonly foo: number) {} } EOF cat /tmp/test.ts </tool_call>
Assistant
Let me retry the command - it seems like it didn't execute.
Assistant
Good, let me verify the test file was created.
<tool_call> <function=bash_terminal> <parameter=command> cat /tmp/test.ts </tool_call>
Assistant
It seems like the tool call wasn't executed. Let me try again with the command.
Assistant
Good, the test file was created. Now let me build and run oxlint to reproduce the issue.
Assistant
It timed out. Let me try a simpler build approach.
<tool_call> <function=bash_terminal> <parameter=command> cargo build -p oxlint 2>&1 | tail -20 </tool_call>
Assistant
The command didn't seem to execute. Let me try again with a clearer command.
Assistant
Good, the build succeeded. Now let me run oxlint on the test file to reproduce the issue.
<tool_call> <function=bash_terminal> <parameter=command> ./target/debug/oxlint /tmp/test.ts </tool_call>
Assistant
Let me try to run the command with a simpler approach.
Assistant
Excellent! I've successfully reproduced the issue. The linter incorrectly reports that
foois declared but never used, even though it's used in@Inject(foo.KEY).Now let me understand how the codebase handles this. I need to look at:
- How the
no_unused_varsrule detects uses of variables - How decorators are parsed and handled
- Why the decorator expression isn't being counted as a use of the outer binding
Let me examine the main source files for the
no_unused_varsrule.<tool_call> <function=bash_terminal> <parameter=command> cat /oxc/crates/oxc_linter/src/rules/eslint/no_unused_vars/mod.rs </tool_call>
- How the
Assistant
The command didn't execute. Let me try again to read the mod.rs file.
Assistant
The key part is where it determines if a symbol is used:
symbol.is_exported()- checks if exportedsymbol.has_usages(self)- checks for usages
Let me look at the
symbol.rsfile to see howhas_usagesis implemented.<tool_call> <function=bash_terminal> <parameter=command> cat /oxc/crates/oxc_linter/src/rules/eslint/no_unused_vars/symbol.rs </tool_call>
Assistant
Let me try to run a command to view the symbol.rs file again.
Assistant
Now let me look at the
usage.rsfile which is wherehas_usagesis likely implemented.<tool_call> <function=bash_terminal> <parameter=command> cat /oxc/crates/oxc_linter/src/rules/eslint/no_unused_vars/usage.rs </tool_call>
Assistant
The command seems to not execute again. Let me try a simpler approach to read the file.
Assistant
Let me look for the
has_usagesfunction:Assistant
Let me look at the
has_usagesfunction in context:<tool_call> <function=bash_terminal> <parameter=command> sed -n '90,150p' /oxc/crates/oxc_linter/src/rules/eslint/no_unused_vars/usage.rs </tool_call>
Assistant
The command isn't working. Let me try using the bash_terminal tool correctly.
Assistant
The issue is about decorators. The problem is that when we have a constructor parameter property with the same name as an outer binding, and the outer binding is used in the decorator, the outer binding should be considered as used.
Let me understand the issue better. The
fooidentifier in@Inject(foo.KEY)should be resolved to the outerconst foo = { KEY: 'token' }declaration, not to the parameter propertyfoo. This is because decorators are evaluated at class-decoration time in the enclosing class scope, before any parameter binding exists in the parameter scope.Let me look at how decorators are handled in the semantic analysis. First, let me look at how the semantic analysis works for decorators.
Assistant
Let me look at the semantic builder to understand how decorators are handled.
Assistant
This is the issue. When we have
@Inject(foo.KEY)in the decorator expression, thefooshould be resolved to the outer scope. But if the constructor parameter property has the same name, it might be shadowing the outer binding incorrectly.Let me look at the builder for the parameter to understand how the decorator is handled:
<tool_call> <function=bash_terminal> <parameter=command> sed -n '2355,2400p' /oxc/crates/oxc_semantic/src/builder.rs </tool_call>
Assistant
Let me try again with a simpler approach.
Assistant
I see the issue now. When visiting a formal parameter with decorators:
- First,
param.bind(self)is called - this binds the parameter symbol to the scope - Then,
self.visit_decorators(¶m.decorators)is called - Then,
self.visit_binding_pattern(¶m.pattern)is called
The problem is that when decorators are visited, the parameter symbol has already been bound. But decorators should be evaluated before the parameter binding exists (as they are evaluated at class-decoration time, not at function-call time).
Let me look at how the binding works in the semantic builder to understand the scoping.
Let me also check if there's any handling for decorators in the binding logic. First, let me look at the formal parameter binding.
<tool_call> <function=bash_terminal> <parameter=command> grep -n "bind" /oxc/crates/oxc_semantic/src/binder.rs | grep -i "parameter" | head -20 </tool_call>
- First,
Assistant
Let me try running the grep command again properly.
Assistant
The grep command exited with code 1 which means no matches were found. Let me look at the binder.rs file more directly to understand how parameters are bound.
Assistant
Let me search for how parameters bind.
<tool_call> <function=bash_terminal> <parameter=command> grep -n "FormalParameter" /oxc/crates/oxc_semantic/src/binder.rs | head -20 </tool_call>
Assistant
Let me try again with the grep command in a different way.
Assistant
Let me look at the FormalParameter binding implementation in more detail.
<tool_call> <function=bash_terminal> <parameter=command> sed -n '/^impl.*Binder.*for FormalParameter/,/^impl/p' /oxc/crates/oxc_semantic/src/binder.rs | head -100 </tool_call>
Assistant
Let me try to find the line numbers first, then view them.
Assistant
Let me view the FormalParameter binding implementation.
<tool_call> <function=bash_terminal> <parameter=command> sed -n '240,280p' /oxc/crates/oxc_semantic/src/binder.rs </tool_call>
Assistant
The command didn't execute properly. Let me try again.
Assistant
I see the issue now. When the FormalParameter is bound, it declares the symbol at the current scope. But when visiting a decorator on a parameter, the decorator should be evaluated before the parameter binding exists in the scope.
The key issue is in the order of operations in
visit_formal_parameter:param.bind(self)is called - this binds the parameter symbol to the scopeself.visit_decorators(¶m.decorators)is called - the decorator is visited at this point, but the parameter symbol has already been bound
But decorators should be evaluated before the parameter binding exists. The fix should be to visit decorators before binding the parameter.
Let me look at how this is done for class members:
<tool_call> <function=bash_terminal> <parameter=command> grep -n "visit_decorators" /oxc/crates/oxc_semantic/src/builder.rs </tool_call>
Assistant
Let me try the grep command again to find where decorators are visited.
Assistant
Let me see the context around each of these decorator visits.
<tool_call> <function=bash_terminal> <parameter=command> sed -n '795,810p' /oxc/crates/oxc_semantic/src/builder.rs </tool_call>
Assistant
Let me try again with the sed command.
Assistant
Interesting! For class decorators, the order is:
class.bind(self)- binds the classself.visit_decorators(&class.decorators)- visits decorators
But for parameter decorators (lines 2363 and 2379), the current order is:
param.bind(self)- binds the parameterself.visit_decorators(¶m.decorators)- visits decorators
The issue is that for parameter decorators, the decorator should be visited BEFORE the parameter is bound. Let me check how class decorators work. For classes, the class binding is done first because the class name is used in the decorator expression (like
@decorator(ClassName)).But for parameters, the parameter binding shouldn't affect the decorator expression - decorators are evaluated at class-definition time, not at function-call time.
Let me look at the full
visit_formal_parameterfunction:Assistant
I see the issue now. The fix should be to:
- First visit decorators
- Then bind the parameter
But there's a complication - when we visit the binding pattern, we need to visit it after binding. Let me check what
visit_binding_patterndoes and if it needs the binding to be set up first.Actually, I need to think about this more carefully. The
param.bind(self)function binds the parameter's binding identifier to the scope. But the decorators are evaluated before the parameter binding exists in the parameter scope.The fix should be to visit decorators before binding the parameter. Let me look at how the class decorator case is handled to understand the scoping.
Actually, looking at the class decorator case more carefully, I see that for class declarations, the class is bound first and then decorators are visited. This is because the class name is used in the decorator expression.
For parameters, decorators should NOT see the parameter binding. The fix should be:
Assistant
Let me run the sed command again to view the function.
Assistant
The issue is clear now. In both
visit_formal_parameterandvisit_formal_parameter_rest, the order is:param.bind(self)- binds the parameterself.visit_decorators(¶m.decorators)- visits decoratorsself.visit_binding_pattern(¶m.pattern)- visits the binding pattern (which sets upident.symbol_id)
The fix should be to move
self.visit_decorators(¶m.decorators)to BEFOREparam.bind(self). This way, when the decorator expression is visited, the parameter symbol has not been declared yet, and references in the decorator will correctly resolve to outer bindings.Let me also check if
visit_spanorvisit_binding_patternneed the parameter to be bound first. Looking at the code,visit_binding_patternseems to visit the binding pattern to resolve references, not to set up the binding. The binding is set up byparam.bind(self).Let me make the fix by reordering the calls in both functions.
<tool_call> <function=bash_terminal> <parameter=command> sed -n '2358,2385p' /oxc/crates/oxc_semantic/src/builder.rs </tool_call>
Assistant
Let me run the command to view the relevant lines.
Assistant
Now I understand the issue. The fix is to move
self.visit_decorators(¶m.decorators)to BEFOREparam.bind(self)in bothvisit_formal_parameterandvisit_formal_parameter_rest.However, there's a potential issue -
visit_binding_patternandvisit_binding_rest_elementneed to set up thesymbol_idfor the binding. These methods may rely on the symbol being declared first. Let me check thevisit_binding_patternandvisit_binding_rest_elementimplementations to see if they depend on the symbol being declared.Assistant
Let me search for where visit_binding_pattern is defined.
<tool_call> <function=bash_terminal> <parameter=command> find /oxc -name "*.rs" -exec grep -l "visit_binding_pattern" {} ; | head -5 </tool_call>
Assistant
Let me try the find command again properly.
Assistant
The command exited with code 123 (find error) but still returned results. Let me check the semantic builder to understand how visit_binding_pattern works.
Let me look at how the builder handles binding patterns and whether it needs the symbol to be declared first. Actually, I need to understand the purpose of
param.bind(self)vsvisit_binding_pattern.Looking at the code:
param.bind(self)- declares the symbol in the scopevisit_binding_pattern(¶m.pattern)- visits the binding pattern to resolve any references inside it
The issue is that when we visit the binding pattern, we're not looking up the symbol - we're just visiting the pattern. The symbol is used for resolving references, but the pattern itself doesn't need the symbol to be declared first.
Let me verify this by looking at what
visit_binding_patterndoes:<tool_call> <function=bash_terminal> <parameter=command> grep -n "visit_binding_pattern|visit_binding_rest_element" /oxc/crates/oxc_semantic/src/builder.rs | head -20 </tool_call>
Assistant
Let me try the grep command again to search for the visitor methods in the builder.
Assistant
Now let me understand the issue better. I need to see if
visit_binding_patternorvisit_binding_rest_elementneeds the symbol to be declared first. Let me look at the AST structure to understand what happens when we visit a binding pattern.Actually, I think I understand now. The binding pattern (
param.pattern) is what contains the binding identifier (ident). Whenvisit_binding_patternis called, it visits the binding identifier. The binding identifier already has itssymbol_idset byparam.bind(self)throughident.symbol_id.set(Some(symbol_id)).So if we move
visit_decoratorsbefore , then:Assistant
Now I understand the issue. The fix is to move
self.visit_decorators(¶m.decorators)to BEFOREparam.bind(self)in bothvisit_formal_parameterandvisit_formal_parameter_rest. This ensures that decorator expressions are evaluated before the parameter symbol is bound, so references in the decorator correctly resolve to outer bindings.Let me apply the fix:
Assistant
Let me apply the fix by editing the
visit_formal_parameterfunction first:Assistant
Now I need to apply the fix. The change is to move
self.visit_decorators(¶m.decorators)to beforeparam.bind(self). Let me use the edit command to make this change:Assistant
The fix has been applied. Now let me verify the changes were made correctly:
Assistant
The fix has been applied correctly. Now let me rebuild the project and test if the fix works:
Assistant
The build succeeded. Now let me test if the fix resolves the issue:
<tool_call> <function=bash_terminal> <parameter=command> ./target/debug/oxlint /tmp/test.ts </tool_call>
Assistant
Let me try running the command again properly.
Assistant
The issue is still occurring. The fix I made to the semantic builder didn't resolve the issue. Let me think about this more carefully.
The problem is that even though we visit decorators before binding the parameter, the reference in the decorator expression (
foo.KEY) might still be resolving incorrectly. Let me investigate the scoping behavior more carefully.Actually, I think I need to understand what's happening here. When we have:
const foo = { KEY: 'token' }; export class C { constructor(@Inject(foo.KEY) private readonly foo: number) {} }The
fooin@Inject(foo.KEY)should resolve to the outerconst foo. But it seems like it's not being resolved correctly.Let me check if there's something else going on with the scope chain. The issue might be that the scope for the constructor parameter is different from what I expect.
Assistant
Let me try running the grep command again to find the constructor and class-related code.
Assistant
Let me look at the visit_class_body function to understand how method/constructor scopes are set up.
Assistant
I see - the
visit_class_bodydoesn't seem to set up a new scope for each method/constructor. The class elements are visited in the class scope.Let me check how method parameters are handled. The issue might be that the constructor is a method, and methods have their own scope.
Assistant
Let me look at how function/method parameters are handled, including the scope setup:
Assistant
Let me look at the
visit_functionfunction to understand how function scopes are set up:Assistant
Now I understand the scope setup better. For functions, a new scope is entered after binding the function declaration. The
visit_formal_parametersis called within the function scope.Let me trace through what happens for a constructor:
- Class is bound (class declaration)
- Class decorators are visited (in the enclosing scope, e.g., file/module scope)
- Class scope is entered
- Class body is visited
- Constructor method is visited (in class scope)
- A function scope is entered for the constructor
- Constructor parameters are visited (in constructor scope)
- Constructor decorators are visited (should be in the class scope, not constructor scope!)
Wait, this is the key insight. The constructor decorators should be evaluated in the class scope, not the constructor function scope. But currently, when we visit
visit_formal_parameter, we're inside the constructor function scope.Let me check how method decorators are handled. There might be a similar issue with class method decorators.
<tool_call> <function=bash_terminal> <parameter=command> grep -n "visit_method_definition|MethodDefinition" /oxc/crates/oxc_semantic/src/builder.rs | head -20 </tool_call>
Assistant
Let me try the grep command again.
Assistant
The command didn't find anything. Let me search in the class body visitor to see how methods are handled.
Assistant
Let me look at how class elements are visited to understand how method definitions are handled.
<tool_call> <function=bash_terminal> <parameter=command> grep -n "visit_class_elements|ClassElement" /oxc/crates/oxc_ast/src/ast/js.rs | head -20 </tool_call>
Assistant
Let me try a different approach to understand how class elements are visited.
Assistant
I need to understand how the class element visitor is implemented. Let me check the AST to see the structure.
<tool_call> <function=bash_terminal> <parameter=command> grep -n "ClassElement" /oxc/crates/oxc_ast/src/ast/js.rs | head -20 </tool_call>
Assistant
Let me try running the grep command again.
Assistant
Let me look at the ClassElement enum to see what elements are included:
Assistant
Let me understand the issue better. I need to check how the MethodDefinition visitor handles decorators and parameters.
<tool_call> <function=bash_terminal> <parameter=command> grep -n "MethodDefinition" /oxc/crates/oxc_semantic/src/builder.rs | head -20 </tool_call>
Assistant
Let me try the grep command again to find how MethodDefinition is handled in the semantic builder.
Assistant
The grep returned exit code 0 but no output, meaning MethodDefinition is not explicitly handled in the builder. This is probably because it's auto-generated from the AST visitor pattern.
Let me understand the issue more clearly. The key insight is:
- Class decorators are evaluated in the enclosing scope (file scope)
- Method/constructor decorators should also be evaluated in the enclosing scope (class scope for methods, but... wait, that's not right)
Actually, looking at the test case again:
const foo = { KEY: 'token' }; export class C { constructor(@Inject(foo.KEY) private readonly foo: number) {} }The
fooin@Inject(foo.KEY)is a reference. The question is: in what scope should this reference be resolved?According to the JavaScript/TypeScript semantics, decorators are evaluated at class-definition time. For constructor decorators, the decorator is evaluated when the class is defined, not when an instance is created. At that point:
Assistant
The
barin Test 2 and Test 3 is correctly detected as used (no warning forbar). This shows that my fix didn't break the normal case. But the original issue withfooin the constructor decorator is still present.This is strange. My fix moved the decorator visit before the parameter bind, but the issue persists. Let me think about what else could be going on.
Assistant
Let me look at the existing test structure for the no-unused-vars rule:
Assistant
Let me check the TypeScript tests to see if there are any decorator-related tests:
Assistant
There are decorator-related tests. Let me check one of the constructor parameter decorator tests:
Assistant
I see! In these test cases, the decorator uses a different identifier than the parameter. For example:
@ConstructorParameterDecoratorFactory(APP_CONFIG)- theAPP_CONFIGis different from the parameterconfig@ConstructorParameterDecorator bar- the decoratorConstructorParameterDecoratoris different from the parameterbar
The issue in the bug report is specifically when the decorator argument has the same name as the parameter:
@Inject(foo.KEY) private readonly foo: numberHere,
fooin@Inject(foo.KEY)should resolve to the outerconst foo, not the parameterfoo.Let me search for any test cases that match this pattern:
Assistant
Now let me understand the actual issue better. The problem might be in how references are resolved during semantic analysis. When we have:
const foo = { KEY: 'token' }; export class C { constructor(@Inject(foo.KEY) private readonly foo: number) {} }When visiting the decorator expression
foo.KEY:- The
fooidentifier reference needs to be resolved - At this point, with my fix, the parameter
fooshould NOT be bound yet - So the reference should resolve to the outer
const foo
But maybe the issue is that the reference is still being resolved incorrectly. Let me check if there's a scope issue.
Actually, wait. Let me re-examine my fix. I moved before . But the question is: when we visit the decorator, what scope are we in?
- The
Assistant
Let me look at the resolve_references_for_current_scope function to understand how references are resolved:
Assistant
I need to understand how reference resolution works. Let me look at the
walk_up_resolve_referencefunction.Assistant
Let me look at the
walk_up_resolve_referencefunction to understand how references are resolved:Assistant
Now I understand the reference resolution logic better. The
walk_up_resolve_referencefunction walks up the scope chain from the reference's scope_id to find the symbol.The issue might be that when the decorator is visited, the reference's scope_id is set to the constructor scope (where the decorator is visited), and when walking up, the parameter symbol (if it's already bound) would be found.
With my fix, the parameter is NOT bound yet when the decorator is visited, so the parameter symbol should not be found in the constructor scope. But wait, the reference is added to the
unresolved_referenceslist when it's encountered, and it's associated with the current scope_id at that time.Let me check when references are added:
<tool_call> <function=bash_terminal> <parameter=command> grep -n "add_reference|unresolved_references" /oxc/crates/oxc_semantic/src/builder.rs | head -30 </tool_call>
Assistant
Let me search for where references are added to the unresolved_references list.
Assistant
Let me try searching for where references are created.
Assistant
Let me look at the context around line 516 to understand how references are added:
Assistant
Now I need to understand where
declare_referenceis called. Let me search for it:<tool_call> <function=bash_terminal> <parameter=command> grep -n "declare_reference" /oxc/crates/oxc_semantic/src/builder.rs | head -20 </tool_call>
Assistant
Let me try the grep command again to find where declare_reference is called.
Assistant
The grep command succeeded and found two occurrences of
declare_reference: one at line 510 (the definition) and one at line 2645 (a usage). Let me look at line 2645 to understand how references are declared.<tool_call> <function=bash_terminal> <parameter=command> sed -n '2630,2680p' /oxc/crates/oxc_semantic/src/builder.rs </tool_call>
Assistant
Let me try the grep command again to find where declare_reference is called.
Assistant
The grep command is having issues. Let me try a different approach. Let me look at the visitor implementation to see where references are declared.
<tool_call> <function=bash_terminal> <parameter=command> wc -l /oxc/crates/oxc_semantic/src/builder.rs </tool_call>
Assistant
The grep command is having issues, possibly due to some shell escaping problem. Let me try a simpler approach - I'll use bash to run the grep command.
Assistant
I see the builder.rs file has 2659 lines. The grep found a usage of declare_reference at line 2645, which is near the end of the file. Let me view around that area.
<tool_call> <function=bash_terminal> <parameter=command> sed -n '2635,2659p' /oxc/crates/oxc_semantic/src/builder.rs </tool_call>
Assistant
The sed command is not executing properly. Let me try a different approach - I'll just look at the last few lines of the file.
Assistant
I see that
reference_identifieris the function that declares a reference for anIdentifierReference. This is called when visiting identifier references in the AST.The key insight is that when
reference_identifieris called forfooin@Inject(foo.KEY), it creates a reference withself.current_scope_id. At that point, with my fix, the constructor scope is the current scope, and the parameterfoohas not been bound yet.So the reference should walk up the scope chain:
- Constructor scope - no symbol
foo(since it's not bound yet) - Class scope - no symbol
foo - File scope - find
foo(the const declaration)
Wait, but this should work! Let me double-check that the issue isn't something else.
- Constructor scope - no symbol
Assistant
The sed command is still not working. Let me try using head and tail to view specific lines.
Assistant
Assistant
Let me try a simpler approach to get the information I need.
Assistant
Let me think about the fix differently. The issue is that when we visit constructor decorators, we need to resolve references in the class scope (or enclosing scope), not the constructor scope.
One approach is to save the parent scope ID before entering the function scope, and then visit decorators in that scope. Let me look at the function visitor more carefully.
Assistant
Assistant
The command didn't execute properly. Let me try using the head/tail approach again to view the end of the file.
Assistant
Assistant
The grep command didn't produce output. Let me try a different approach to find the visit_formal_parameters function.
Assistant
The command exited with code 1 which means it didn't find the pattern. Let me search more broadly.
<tool_call> <function=bash_terminal> <parameter=command> grep -n "visit_formal_parameters" /oxc/crates/oxc_semantic/src/builder.rs </tool_call>
Assistant
Let me try running the command again properly.
Assistant
The
visit_formal_parametersis called from withinvisit_functionandvisit_arrow_function_expression. Let me look at where it's defined:<tool_call> <function=bash_terminal> <parameter=command> grep -n "fn visit_formal_parameters" /oxc/crates/oxc_ast/src/visit/ </tool_call>
Assistant
Let me try to search for the
visit_formal_parametersdefinition in the AST visitor.Assistant
Let me search for where the visitor methods are defined.
Assistant
The command returned exit code 0 but no output. Let me try a different approach. The visitor methods are likely generated, so let me check the semantic builder to see how parameters are visited.
Let me take a step back and think about the fix. The issue is that constructor decorators are visited in the constructor scope, but they should be evaluated in the enclosing class scope.
The simplest fix is to:
- Before entering the function scope, visit all parameter decorators
- Enter the function scope
- Continue with the rest of the function processing
Let me modify the
visit_functionfunction to do this:Assistant
I need to modify the
visit_functionfunction to visit parameter decorators before entering the function scope. This will ensure that decorator expressions are evaluated in the enclosing class scope, not the constructor scope.Let me first check the structure of the parameters to understand how to visit just the decorators.
<tool_call> <function=bash_terminal> <parameter=command> grep -n "visit_formal_parameters" /oxc/crates/oxc_semantic/src/builder.rs </tool_call>
Assistant
Let me take a different approach. I'll write a small script to view the
visit_formal_parametersimplementation.Assistant
Let me search for the definition of
visit_formal_parameters:Assistant
Let me check if the visitor is auto-generated:
Assistant
I see that the builder implements
Visitfromoxc_ast_visit. This means thevisit_formal_parametersmethod is likely auto-generated. Let me look for where the visitor methods are implemented manually for the builder.Actually, I think I need to take a different approach. Instead of trying to visit decorators before entering the scope, I can track whether we're visiting a decorator and use a different scope ID.
Assistant
Let me try to run the sed command again properly to view the code around line 1865.
Assistant
Now I can see the code around line 1865. Let me modify the
visit_functionfunction to visit parameter decorators before entering the function scope. I need to:- Save the scope ID before
enter_scope - Visit parameter decorators using the saved scope
- Then enter the function scope
Let me first check if there's a way to visit just the decorators from the function:
<tool_call> <function=bash_terminal> <parameter=command> grep -n "func.params|func.declarations" /oxc/crates/oxc_semantic/src/builder.rs | head -10 </tool_call>
- Save the scope ID before
Assistant
Let me try running the grep command again properly to search for parameter-related code.
Assistant
I need to modify the
visit_functionfunction to visit parameter decorators before entering the function scope. Let me implement a custom function to visit just the parameter decorators and then call it beforeenter_scope.The approach will be:
- Create a helper function to visit only parameter decorators
- Call this helper before
enter_scopeinvisit_function
Let me implement this fix.
Assistant
Assistant
The command didn't execute. Let me try again to apply the fix.
Assistant
Now let me verify the changes were applied correctly:
Assistant
Good, the changes look correct. Now let me also verify that the
visit_formal_parameterandvisit_formal_parameter_restfunctions have been updated correctly:Assistant
Good, the
visit_formal_parameterandvisit_formal_parameter_restfunctions have been updated correctly - they no longer visit decorators.Now let me rebuild and test the fix:
<tool_call> <function=bash_terminal> <parameter=command> cargo build -p oxlint 2>&1 | tail -30 </tool_call>
Assistant
The command didn't execute properly. Let me try again with a clearer approach.
Assistant
The build succeeded. Now let me test if the fix resolves the issue:
<tool_call> <function=bash_terminal> <parameter=command> ./target/debug/oxlint /tmp/test.ts </tool_call>
Assistant
The command didn't execute. Let me try running it again.
Assistant
The fix worked! The test case that previously reported a warning for the unused
foovariable now passes without any warnings.Let me also test with the more complex test file to make sure the fix doesn't break other cases:
<tool_call> <function=bash_terminal> <parameter=command> ./target/debug/oxlint /tmp/test2.ts </tool_call>
Assistant
The command didn't execute. Let me try again properly.
Assistant
The fix is working correctly:
- The original issue with
fooin the constructor decorator is fixed - no warning forfoo - The
barin Test 2 and Test 3 is correctly detected as used (no warning forbar) - The
testfunction is correctly detected as unused (warning fortest)
Now let me also think about edge cases. There might be other scenarios to consider:
- Method decorators (not just constructor) - should also be evaluated in the class scope
- Arrow functions - should also be handled
- Static method decorators
Let me create a test case for these scenarios:
<tool_call> <function=bash_terminal> <parameter=command> cat > /tmp/test3.ts << 'EOF' // Test case 1: Method decorator with same-named parameter const config = { NAME: 'test' }; declare function Inject(token: unknown): ParameterDecorator;
- The original issue with
Assistant
The command didn't execute properly. Let me try again with a clearer approach.
Assistant
The results are mostly good:
TestClass- no warning forconfigused in method and constructor decorators ✓TestArrow- warning for the class itself (not used) - but wait, thearrowConfigin the arrow function should be detected as used. Let me check if it was.unused- correctly reported as unused ✓TestImport- no warning forfooused in constructor decorator ✓
But I notice that the
arrowConfigin Test 2 might not be detected correctly. Let me check if there's an issue with arrow functions. The arrow function is defined as a class property, and the decorator is on the property, not the arrow function itself.Actually, looking at the test case, the decorator
@MethodDecorator()is on a class propertyarrow, not on the arrow function. The arrow function is just the value of the property. The arrow function body usesarrowConfig.VALUE, which should be detected as a use of .Assistant
The command didn't execute. Let me try running it again.
Assistant
Let me check the
visit_arrow_function_expressionfunction:Assistant
I see that
visit_arrow_function_expressionalso enters the scope before visiting parameters. I need to apply the same fix here for arrow functions with parameter decorators.But wait, looking at my test case, the arrow function doesn't have parameter decorators - it has a property decorator. Let me think about whether arrow functions with parameter decorators are a valid use case.
Actually, arrow functions can have parameter decorators in TypeScript. Let me create a test case for this and see if it works correctly.
<tool_call> <function=bash_terminal> <parameter=command> cat > /tmp/test_arrow.ts << 'EOF' // Test arrow function with parameter decorator const outerConfig = { KEY: 'test' }; declare function Inject(token: unknown): ParameterDecorator;
class TestArrow { method = (@Inject(outerConfig.KEY) x: string) => { console.log(x); }; } EOF ./target/debug/oxlint /tmp/test_arrow.ts </tool_call>
Assistant
Let me try again to run the test for arrow functions with parameter decorators.
Assistant
The test case syntax is not valid TypeScript. Arrow functions don't support decorators on their parameters in this way. The issue description specifically mentions constructor parameter properties, not arrow functions with parameter decorators.
Let me focus on verifying that the fix handles the original issue correctly and doesn't break other cases. Let me also check if there are any existing tests that might be affected by this change.
Assistant
All 47 tests passed for the
no_unused_varsrule. This is a good sign that the fix doesn't break existing functionality.Now let me also run the full test suite to make sure I haven't broken anything else:
Assistant
There's an error in the cfg example, but that seems to be an unrelated issue (the example file has an error about a missing
mainfunction). Let me run the tests without the examples:Assistant
All the semantic library tests pass. The cfg example error is unrelated to my changes - it seems to be a pre-existing issue with the example file.
Now let me also add a specific test case for the bug fix to ensure this issue doesn't regress. Let me check the test structure:
Assistant
Let me add a test case for the bug fix. I'll add it to the
oxc.rstest file since it's an oxc-specific issue.Assistant
Let me add a test case for the decorator shadowing fix:
<tool_call> <function=bash_terminal> <parameter=command> cat >> /tmp/oxc_test_addition.rs << 'EOF' // #5308 - decorator uses outer binding with same name as parameter ( " const foo = { KEY: 'token' }; declare function Inject(token: unknown): ParameterDecorator; export class C { constructor(@Inject(foo.KEY) private readonly foo: number) {} } ", None, ), // decorator in method with same name as outer binding ( " const config = { NAME: 'test' }; declare function Inject(token: unknown): ParameterDecorator; export class C { @Inject(config.KEY) method() {} } ", None, ), EOF cat /tmp/oxc_test_addition.rs </tool_call>
Assistant
Let me try the command again properly.
Assistant
Now let me add this test case to the oxc.rs file. First, let me see where I should add it:
Assistant
Let me look at the test_classes function to see where I can add the new test case:
Assistant
I can see that the
test_classesfunction has apassvector where I should add the test case. Let me find the end of thepassvector in this function and add the test cases:Assistant
I can see that the
passvector ends around line 1364 with the comment about class property initializers. Let me add the test case before thelet fail = vec!line. Let me find the exact line:Assistant
I see that the
passvector ends at line 1360 (the line with just];). I'll add the test case before thelet fail = vec![line. Let me use theeditcommand to add the test cases:Assistant
Let me verify that the test case was added correctly:
Assistant
I notice the sed command added multiple copies of the same test and the indentation is wrong. Let me fix this by resetting the file and applying the changes correctly.
<tool_call> <function=bash_terminal> <parameter=command> git checkout /oxc/crates/oxc_linter/src/rules/eslint/no_unused_vars/tests/oxc.rs </tool_call>
Assistant
The command didn't execute properly. Let me try to run the git checkout command again.
Assistant
Now let me add the test case properly using a Python script:
Assistant
Now let me verify the test case was added correctly:
Assistant
Now let me rebuild and run the tests to make sure everything works:
<tool_call> <function=bash_terminal> <parameter=command> cargo test -p oxc_linter -- no_unused_vars::tests::oxc::test_classes 2>&1 | tail -30 </tool_call>
Assistant
Let me run the test command again.
Assistant
I see the issue - the test format is wrong. Let me check the correct format for the test cases.
<tool_call> <function=bash_terminal> <parameter=command> sed -n '1350,1365p' /oxc/crates/oxc_linter/src/rules/eslint/no_unused_vars/tests/oxc.rs </tool_call>
Assistant
Let me try running the command again to view the file.
Assistant
I see the issue. The test format should not have the tuple format - the previous test cases don't use
None. Let me check the correct format by looking at another test case:Assistant
I see - the pass format for test_classes is just strings, not tuples. Let me fix this:
Assistant
Now let me add the test case with the correct format (just strings):
Assistant
Now let me verify and run the tests:
Assistant
The test cases look good. Now let me rebuild and run the tests:
Assistant
The test passes. Now let me run all the
no_unused_varstests to make sure I haven't broken anything:Assistant
All 47 tests pass. Let me also verify that the original issue is fixed by running the test again:
Assistant
The fix works correctly. Let me summarize what I've done:
Summary
Issue
The
no-unused-varsrule in oxlint incorrectly reported a false positive when:- A constructor parameter property has the same name as an outer binding (import or
const) - That outer binding is used in the parameter's own decorator expression (e.g.,
@Inject(foo.KEY))
Root Cause
Constructor decorators were being visited AFTER entering the constructor scope and AFTER binding the parameter. This caused references in the decorator expression (like
fooin@Inject(foo.KEY)) to incorrectly resolve to the parameter binding instead of the outer binding.According to TypeScript semantics, decorators are evaluated at class-definition time in the enclosing scope, not at function-call time. Therefore, decorator expressions should not see the parameter bindings.
- A constructor parameter property has the same name as an outer binding (import or
Assistant
All tests pass and the issue is fixed. Let me now submit the solution.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.