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
I'll start by exploring the repository structure to understand what we're working with. Let's first look at the root directory.
- Assistant
This appears to be the OXC (Oxlint) repository. The issue is about a false positive in the
no-unused-varsrule when a constructor parameter property has the same name as an outer binding that's used in the parameter's decorator.Let me explore the repository structure to understand where the linter rules are implemented.
- Assistant
Let me first understand the issue better and create a test file to reproduce it.
- Assistant
Let me look at the no-unused-vars implementation to understand how it handles decorators and constructor parameters.
- Assistant
Good, I've reproduced the issue. Now let me look at the
no_unused_varsrule implementation to understand how it handles this case. - Assistant
Now let me look at the usage module to understand how references are tracked:
- Assistant
Now let me look at the semantic model to understand how references in decorators are resolved:
- Assistant
Let me look at the semantic builder to understand how references in decorators are resolved:
- Assistant
Let me look at the builder.rs to understand how decorator references are resolved.
- Assistant
Let me look at the
visit_formal_parameterfunction in the semantic builder: - Assistant
Now I can see the issue! In
visit_formal_parameter,param.bind(self)is called beforeself.visit_decorators(¶m.decorators). This means the constructor parameter (e.g.,private readonly foo) gets bound to the scope before the decorators are visited. So when@Inject(foo.KEY)is evaluated,fooinside the decorator resolves to the constructor parameter instead of the outerconst foo.Let me check how
bindworks: - Assistant
Let me look for the
bindmethod in the semantic module: - Assistant
Let me look at the binder implementation.
- Assistant
Let me look at the binder.rs file more carefully to understand how parameters are bound.
- Assistant
Let me look at how class decorators are handled:
- Assistant
I can see that for classes, decorators are visited before entering the class scope. Let me look at the
visit_formal_parameterfunction again more carefully: - Assistant
Let me also look at the
visit_formal_parameter_restfunction: - Assistant
Let me first understand the issue fully:
- We have a constructor parameter with a decorator that references an outer variable .
- Assistant
Now I understand the issue. In
visit_formal_parameter,param.bind(self)is called beforeself.visit_decorators(¶m.decorators). This means the parameter's symbol (e.g.,foo) is declared in the scope before the decorators are visited. So when@Inject(foo.KEY)is resolved,fooin the decorator resolves to the parameter's binding instead of the outerconst foo.Let me check how decorators are handled in the tests:
- Assistant
Let me check what tests already exist for decorators in the no_unused_vars tests.
- Assistant
Let me look at the test cases around line 67:
- Assistant
Now I need to understand the core issue. The problem is in the semantic builder's
visit_formal_parameter:rustfn visit_formal_parameter(&mut self, param: &FormalParameter<'a>) { let kind = AstKind::FormalParameter(self.alloc(param)); self.enter_node(kind); param.bind(self); // <-- Binds the parameter name self.visit_span(¶m.span); self.visit_decorators(¶m.decorators); // <-- Decorators visited AFTER binding self.visit_binding_pattern(¶m.pattern); ... }The issue is that
param.bind(self)is called beforeself.visit_decorators(¶m.decorators). In TypeScript, parameter decorators are evaluated at class decoration time, before any parameter bindings exist. So@Inject(foo.KEY)should resolvefooto the outerconst foo, not the parameter propertyprivate readonly foo.Let me look at the
visit_formal_parameter_restas well to see if it has the same issue: - Assistant
Let me look at the full visit_formal_parameter method:
- Assistant
Let me look at how
enter_nodeandbindinteract more carefully: - Assistant
Let me look at how identifier references are resolved:
- Assistant
Let me look at the
visit_identifier_referencefunction: - Assistant
Let me look at the
reference_identifierfunction: - Assistant
Now I understand the root cause. The issue is in
visit_formal_parameter:rustfn visit_formal_parameter(&mut self, param: &FormalParameter<'a>) { let kind = AstKind::FormalParameter(self.alloc(param)); self.enter_node(kind); param.bind(self); // <-- This binds the parameter name BEFORE visiting decorators self.visit_span(¶m.span); self.visit_decorators(¶m.decorators); // <-- Decorators visited AFTER binding ... }When
@Inject(foo.KEY)is visited, thefooidentifier reference resolves to the constructor parameterfoo(which was just bound), not the outerconst foo. According to TypeScript semantics, parameter decorators are evaluated at class decoration time, before any constructor parameters exist, so they should see the outer scope.Let me check how
visit_formal_parameter_restworks: - Assistant
The fix is to visit decorators before binding the parameter. In both
visit_formal_parameterandvisit_formal_parameter_rest, we need to moveself.visit_decorators(¶m.decorators)beforeparam.bind(self).Let me make this fix:
- Assistant
I see there's an extra closing brace at line 2377. Let me check the actual file content:
- Assistant
I see there's an extra closing brace. Let me fix that:
- Assistant
Now let me also fix
visit_formal_parameter_restto visit decorators before binding, and add a blank line between the two functions: - Assistant
Good, the changes look correct. Now let me build and test with the reproduction case:
- Assistant
The fix didn't work. Let me understand the reference resolution better:
- Assistant
The issue is that reference resolution happens after bindings are created. Let me look at how references are resolved:
- Assistant
I see the issue. The reference resolution happens later, not during the visit. Even though we visit decorators first, the reference for
fooin the decorator gets added to the same scope where the parameter binding will be. Let me look at how references are collected: - Assistant
Let me look at how the reference identifier is stored and how the scope for a reference is determined:
- Assistant
Let me look at the
reference_identifierfunction to understand how references are stored: - Assistant
Now I need to understand the scoping better. Let me check how the function scope is set up:
- Assistant
I need to understand the full reference resolution flow better. Let me check when references are resolved during the function visit:
- Assistant
The issue is that even though we visit decorators first, references are resolved later. Let me understand the scoping mechanism better:
- Assistant
Let me look at how
enter_nodeandleave_nodework: - Assistant
Let me understand the scope resolution better. The key issue is that references in the decorator are being resolved in the function scope, but they should be resolved in the class scope:
- Assistant
Let me understand the scope hierarchy better. The issue is that even though I moved decorators before binding, the reference is still created with the function scope, and the parameter binding happens in the same scope. Let me look at the scope hierarchy:
- Assistant
I see the issue now. Even though I moved decorators before binding, the reference resolution happens later. Let me understand the full flow better:
- Assistant
Let me look at how the function parameters are visited:
- Assistant
Let me look at the
visit_classmethod more carefully: - Assistant
I think the issue is that the
fooreference in the decorator is resolving to the constructor parameterfoobecause the reference scope is the function scope where the parameter is bound. Let me understand how the scope resolution works: - Assistant
The issue is more subtle than just reordering. Let me understand the flow better. Even though I moved decorators before binding, the reference resolution happens later. Let me look at how
resolve_references_for_current_scopeis called: - Assistant
I see the issue now. The
resolve_references_for_current_scope()is called AFTER all parameters have been visited and bound. So even though I moved decorators before binding, the references are still resolved later when the parameter binding is already in scope.The real issue is that the decorator's
fooreference is being resolved in the function scope, but it should be resolved in the outer scope. In TypeScript, parameter decorators are evaluated at class decoration time, before the constructor is even called. So they should see the class scope, not the function scope.Let me look at how the scope is managed during the function visit:
- Assistant
I see the issue now. The
resolve_references_for_current_scope()is called after all parameters have been visited and bound. So even though I moved decorators before binding, the references are still resolved later when the parameter binding is already in scope.The real issue is that the decorator's
fooreference is being resolved in the function scope, but it should be resolved in the outer scope. In TypeScript, parameter decorators are evaluated at class decoration time, before the constructor is even called. So they should see the class scope, not the function scope.Let me look at how the scope is managed during the function visit:
- Assistant
The real issue is that
resolve_references_for_current_scope()is called after all parameters are visited and bound. By that point, the parameterfooexists in the function scope, so the decorator'sfooreference resolves to it.I need to ensure that decorator references are resolved in the class scope, not the function scope. The key is that the decorator expression's
fooreference gets thecurrent_scope_idat the time it's created. Let me look at how the parent scope can be used: - Assistant
I think I now understand the real issue. The decorator's
fooreference is created with the function scope as itsscope_id, but it should use the class scope. The decorator expression's references need to be resolved in the enclosing class scope, not the constructor's function scope.Let me look at how the class enters the scope:
- Assistant
Wait, I need to rethink this. Even with decorators visited before binding, the reference resolution still happens after all parameters are bound. Let me understand the scope resolution flow better:
- Assistant
The key issue is that the decorator's
fooreference is created withself.current_scope_id(the function scope) as the reference's scope. Even though we visit decorators before binding, the resolution happens later and finds the parameterfooin the same scope.I need to make the decorator references use the parent scope instead. Let me look at how to get the parent scope:
- Assistant
The real fix needs to change the scope of the decorator references. Let me update
visit_formal_parameterto temporarily set the scope to the parent scope when visiting decorators: - Assistant
The issue is more subtle than I initially thought. Even though we visit decorators before binding, the reference resolution happens later (in
resolve_references_for_current_scope()), which is called after ALL parameters have been visited and bound. At that point, the parameter binding is already in scope.The correct fix is to make the decorator references use the parent scope (the class scope or whatever is enclosing the constructor) instead of the function scope. Let me look at how the scope chain works:
- Assistant
The problem is that the
fooreference in the decorator gets created withself.current_scope_id(the function scope), and later whenresolve_references_for_current_scope()is called, it finds the parameterfooin the same scope.The correct fix is to make decorator references use the parent scope (the class/module scope) since decorators are evaluated at class-decoration time, before the constructor runs. Let me update the fix:
- Assistant
Now let me test the fix:
- Assistant
The fix works! Now let me also apply the same fix to
visit_formal_parameter_restfor consistency: - Assistant
Now let me verify the fix still works:
- Assistant
Now let me also test with some additional edge cases:
- Assistant
The edge cases look correct:
- ✅ Original issue - no false positive
- ✅ Import case - no false positive
- ✅ Multiple decorators - no false positive
- ✅ Method parameter with decorator - correctly reports unused parameter (not used in method body)
- ✅ Variable used in both decorator and body - correctly not reported
- ✅ Unused variable - correctly reported
Now let me run the existing tests to make sure nothing is broken:
- Assistant
All existing tests pass. Let me also run the oxc_semantic tests:
- Assistant
There's a compilation error in the example, but it's not related to my changes. Let me run just the tests:
- Assistant
Good, the semantic tests pass. Let me run the full linter test suite to ensure nothing is broken:
- Assistant
All linter tests pass. Let me also run the parser tests:
- Assistant
All parser tests pass. Let me also run the full oxc_semantic test suite:
- Assistant
Let me run the semantic tests more broadly:
- Assistant
The cfg example has a compilation error, but that's unrelated to my changes. Let me verify the full linter test suite passes:
- Assistant
Let me also test some additional edge cases to make sure we're not breaking anything:
- Assistant
All edge cases pass. Let me also test the import case more specifically:
- Assistant
Let me also check that the original issue case works:
- Assistant
Let me review the final changes:
- Assistant
The changes look clean and correct. Let me run the full test suite one more time to ensure everything is working:
- Assistant
All 1144 linter tests pass. Let me also verify the semantic tests one more time:
- Assistant
Let me also run the parser tests:
- Assistant
All tests pass. Let me also check if there are any snapshot tests related to this:
- Assistant
Let me check if there are any other tests that might be affected:
- Assistant
All tests pass. Let me run one more comprehensive test:
- Assistant
Let me also run the codegen tests:
- Assistant
All tests pass. Let me review the final diff one more time:
- Assistant
Let me run one final test with the exact example from the issue:
- Assistant
The fix works correctly. Now let me run one more comprehensive test with all the tests:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.