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 identifier that is used in the parameter's own decorator expression.Let me start by exploring the repository structure to understand how it's organized and find the relevant code.
Assistant
Let me explore the directory structure more to understand the project layout.
Assistant
The issue is related to the
no-unused-varslinter rule in the oxc_linter crate. Let me explore that crate and find the relevant code.Assistant
Let me first create a test file to reproduce the issue and understand it better.
Assistant
Let me check if there's a prebuilt binary or use a faster build:
Assistant
This is a Node.js wrapper. Let me check if there's a native binary or if I can run tests directly:
Assistant
Let me look at the no_unused_vars rule source code first to understand how it works:
Assistant
Now let me look at the
usage.rsfile which handles how variable usages are detected:Assistant
Now let me look at the
symbol.rsfile to understand how references are collected:Assistant
Let me look at the semantic crate to understand how references are resolved for decorators:
Assistant
The issue is about how the scope analyzer resolves references in decorator expressions. The decorator
@Inject(foo.KEY)should resolvefooto the outer binding, not to the parameter property. Let me look at how the builder handles decorators:Assistant
Let me see the context around these lines in builder.rs to understand how decorators are visited:
Assistant
I see the issue now. In
visit_formal_parameter, the code callsparam.bind(self)before visiting the decorators. This means the parameter binding is created in scope before the decorator expressions are evaluated. So whenfoois referenced in@Inject(foo.KEY), it resolves to the parameterfooinstead of the outerconst foo.The fix should be to visit the decorators before binding the parameter, so that references in decorator expressions resolve to the outer scope.
Let me also check the binder to understand how binding works:
Assistant
Now I understand the issue. In
visit_formal_parameter, the binding happens before visiting decorators. Let me look at how this should be ordered. According to the TypeScript specification and ECMAScript semantics, parameter decorators are evaluated at class decoration time, which means they should be evaluated in the enclosing scope, not in the parameter scope.Let me verify by looking at the existing test files for this rule:
Assistant
Let me look at the test file to understand the test format and see if there are similar tests:
Assistant
Let me look at the test structure more closely:
Assistant
I can see the test structure. Now let me look at the issue more carefully. The issue is that when a constructor parameter has a decorator that references an outer variable, and the parameter shadows that outer variable, the outer variable is reported as unused because the reference in the decorator resolves to the parameter instead of the outer binding.
The fix should be in the semantic builder to visit decorators before binding the parameter. Let me look more closely at the visit_formal_parameter function. ) => { this.title = config.title; } } ", None, ), ( " import { ConstructorParameterDecorator } from 'decorators'; export class Foo { constructor(@ConstructorParameterDecorator bar) { this.bar = bar; } } ", None, ), ( " import { ParameterDecoratorFactory } from 'decorators'; export class Qux { bar(@ParameterDecoratorFactory(true) baz: number) { console.log(baz); } } ", None, ), ( " import { ParameterDecorator } from 'decorators'; export class Foo { static greet(@ParameterDecorator name: string) { return name; } } ", None, ), ( " import { Input, Output, EventEmitter } from 'decorators'; export class SomeComponent { @Input() data; @Output() click = new EventEmitter(); } ", None,
Assistant
It seems my previous command had an issue. Let me try again.
Assistant
Now I can see the issue clearly. In
visit_formal_parameter, the binding (param.bind(self)) happens before visiting the decorators (self.visit_decorators(¶m.decorators)). This means whenfoois referenced in@Inject(foo.KEY), it resolves to the parameterfooinstead of the outerconst foo.The fix is to visit the decorators BEFORE binding the parameter. Let me make the fix:
Assistant
I need to use the correct format for the open command. Let me try again with the proper tool call format.
Assistant
I can see the issue clearly. In
visit_formal_parameterandvisit_formal_parameter_rest, the parameter binding (param.bind(self)) happens before visiting decorators (self.visit_decorators(¶m.decorators)). This causes references in decorator expressions to resolve to the parameter binding instead of the outer scope binding.The fix is to visit decorators before binding the parameter. Let me apply the fix:
Assistant
Now I need to also fix the
visit_formal_parameter_restfunction which has the same issue:Assistant
The changes look good. Now let me verify by running the tests. First, let me build and run the relevant tests:
Assistant
Let me continue verifying the fix by creating a test script to reproduce the original issue:
Assistant
The fix I made didn't resolve the issue. Let me investigate further. The problem might be that decorators are being visited inside the wrong scope. Let me check how scopes are handled for formal parameters:
Assistant
Let me look at the walk_formal_parameters function to understand how the scope is entered:
Assistant
I see. The
walk_formal_parameterinoxc_ast_visitvisits decorators before binding pattern. But inoxc_semantic/src/builder.rs, there's a customvisit_formal_parameterthat overrides this behavior. Let me check where the scope for formal parameters is entered:Assistant
I can see the issue now. In
visit_function, the scope is entered at line 1866 (self.enter_scope(...)) and thenvisit_formal_parametersis called at line 1902. This means the function scope (which includes parameters) is already entered when visiting parameters.However, the issue is that when visiting decorators, the reference to
fooshould be in the outer scope (before entering the function scope), not the parameter scope.Let me check if this is really the issue by looking at how references are resolved:
Assistant
Let me understand how references are resolved in more detail:
Assistant
Let me look more closely at how references are created and which scope they're associated with:
Assistant
The issue is now clear. When visiting a reference, it creates a
Referencewithself.current_scope_id. The current scope ID is set when entering a scope viaenter_scope. So when we're inside a function'svisit_formal_parameters, the scope is the function scope, and all references are associated with that scope.Let me check how references are resolved:
Assistant
I see! The
walk_up_resolve_referencefunction walks up the scope chain starting from the reference's scope. So if the reference is in the function scope and there's a parameter namedfooin that scope, it will resolve to the parameterfooinstead of the outerconst foo.The fix I made (visiting decorators before binding) should have worked. Let me check if it was applied correctly and if there are other issues:
Assistant
The fix looks correct. But the issue is that even though we're visiting decorators before binding, the reference is still in the function scope, not the outer scope. The parameter binding hasn't happened yet, but the reference is being created in the function scope. When
walk_up_resolve_referenceis called, it starts from the function scope and walks up. If there's no binding forfooin the function scope yet, it should walk up to the parent scope and find the outerconst foo.Wait, let me re-check the output. The issue might be in how references are resolved. Let me add some debugging or check the reference resolution more carefully.
Assistant
The issue persists. Let me investigate by creating a test to understand how references are being resolved:
Assistant
Let me check how the semantic tests work to create a proper test:
Assistant
...
Assistant
I see the issue now. Even though decorators are visited before binding, the reference resolution happens AFTER all parameters are bound (at line 1915). At that point, the parameter binding exists, so the reference resolves to the parameter instead of walking up to the outer scope.
The fix should be to resolve decorator references immediately after visiting them, before binding the parameter. Let me update the code:
Assistant
I made a mistake in the tool call format. The
editcommand doesn't takeargas a parameter. Let me use the correct format with just the line range.Assistant
Now let me test the fix:
Assistant
The fix works. The warning is no longer reported. Now let me run the existing tests to make sure I haven't broken anything:
Assistant
All 47 tests pass. Now let me also run the semantic tests to make sure the changes in builder.rs don't break anything:
Assistant
The error is unrelated to my changes - it's an example file issue. Let me run only the tests:
Assistant
There's a test failure. Let me look at the failing test to understand what's happening:
Assistant
Assistant
I see. The
resolve_references_for_current_scopefunction processes all unresolved references. My fix resolves all references after each parameter, which breaks the case where later parameter initializers reference subsequent parameters.I need a more targeted approach. Instead of resolving all references, I should only resolve references that come from decorator expressions. Let me think about how to do this...
One approach is to save the checkpoint before visiting decorators, resolve only those new references, then restore the checkpoint. But that's complex.
Another approach is to mark decorator references specially so they can be resolved differently. But that's also complex.
A simpler approach might be to not call
resolve_references_for_current_scope()but instead resolve decorator references directly. Let me see how references are added:Assistant
...
<tool_call> <function=bash_terminal> <parameter=command> sed -n '510,550p' /oxc/crates/oxc_semantic/src/builder.rs </tool_call>
<tool_call> <function=bash_terminal> <parameter=command> sed -n '510,550p' /oxc/crates/oxc_semantic/src/builder.rs </tool_call>
Assistant
It seems the command was cut off. Let me try again.
Assistant
Let me look at how class scopes are handled:
Assistant
Assistant
I need to see my current changes and then implement the correct fix.
Assistant
Let me revert my changes and implement a cleaner approach that visits decorators in the parent scope.
Assistant
Let me now implement the correct fix. The approach is to visit decorators in the parent scope (class scope for constructor parameters) so that references in decorator expressions resolve to outer scope bindings, not the parameter scope.
The key insight is:
- Decorator references should be created in the parent scope (not the function scope)
- When resolved, they walk up from the parent scope and find outer bindings
- Parameter references remain in the function scope and can reference later parameters
I need to:
- Temporarily switch
current_scope_idto the parent scope before visiting decorators - Visit decorators
- Restore
current_scope_idto the function scope - Continue with binding and pattern visiting as before
Assistant
The issue is that decorator references are being resolved to the parameter binding instead of the outer scope binding. The fix should visit decorators in the parent scope so references resolve correctly.
Let me implement the fix by temporarily switching to the parent scope when visiting decorators.
Assistant
Now I need to apply the same fix to
visit_formal_parameter_rest:Assistant
The user's last message shows an error from a tool call that failed. I need to use the correct tool call format. Let me use the bash_terminal command with the edit command properly.
Assistant
Now let me test the fix:
Assistant
The fix works for the main test case. Now let me run the semantic tests to ensure I haven't broken anything:
Assistant
The snapshot tests are failing because the scope_id of decorator references changed from the function scope (scope_id: 3) to the parent scope (scope_id: 2). This is the expected behavior change from my fix - decorator references should be in the parent scope.
Let me first verify that the functional tests pass:
Assistant
The integration tests pass! Now let me also run the no_unused_vars tests to make sure everything is working correctly.
Assistant
All functional tests pass. The only failing tests are snapshot tests that check the exact
scope_idof references. Since my fix intentionally changes decorator references from the function scope to the parent scope, I need to update these snapshots. Let me do that:Assistant
Let me accept the snapshot changes since my fix intentionally changes the scope_id of decorator references:
Assistant
The user's message shows that I didn't make a tool call. I need to continue verifying that my fix is correct and run the tests again to confirm everything passes.
Assistant
All semantic tests pass. Let me also test some edge cases and run a broader test suite:
Assistant
Let me test the edge cases to make sure my fix handles them correctly.
Assistant
The warnings about unused parameters
restandparamare expected (those parameters are declared but not used in their function bodies). The important thing is thatfoo,bar,qux, andconfig(the outer variables used in decorators) are NOT reported as unused anymore - which confirms the fix is working correctly.Let me run the full linter test suite to make sure I haven't broken anything:
Assistant
All 1144 linter tests pass. Let me verify my changes by checking the git diff:
Assistant
I need to continue verifying the fix and ensure it's robust. Let me test a few more edge cases and then submit the solution.
Assistant
The fix is working correctly. Let me verify one more time and then submit.
Assistant
All tests pass. Let me provide a summary of what I did and submit the solution.
Summary
Issue
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 decorator expression (e.g.,@Inject(foo.KEY)), oxlint'sno-unused-varsincorrectly reports the outer binding as "declared but never used".Root Cause
In the semantic builder's
visit_formal_parameterandvisit_formal_parameter_restfunctions, decorators were being visited in the function scope. When references in decorator expressions were resolved, they would find the parameter binding (which shadows the outer binding) instead of the intended outer scope binding.Fix
Modified
visit_formal_parameterandvisit_formal_parameter_restin/oxc/crates/oxc_semantic/src/builder.rsto temporarily switch to the parent scope when visiting decorators. This ensures that:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.