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 see what's in the root directory.
- Assistant
Let me explore the repository structure more broadly to understand what kind of project this is.
- Assistant
This is the OXC repository (Oxlint). Let me explore the structure to understand how the linter works and find the
no-unused-varsrule. - Assistant
The issue is about the
no-unused-varsrule in oxlint. The rule is incrates/oxc_linter. Let me search for the relevant rule and understand the issue.The issue is about constructor parameter properties in TypeScript - when a constructor parameter property shadows an imported identifier used in its own decorator, the linter incorrectly reports the outer variable as unused.
This is a scope analysis issue. The decorator expression should be evaluated in the outer scope (class scope), not in the parameter scope. So when we have:
tsconst foo = { KEY: 'token' }; declare function Inject(token: unknown): ParameterDecorator; export class C { constructor(@Inject(foo.KEY) private readonly foo: number) {} }The
fooin@Inject(foo.KEY)should reference the outerfoo, not the constructor parameter propertyfoo. This means the scope analysis is incorrectly handling where decorators are evaluated.Let me look at the semantic analysis code, particularly around how scopes are created for constructor parameters with decorators.
- Assistant
Let me look at the no-unused-vars rule implementation to understand how it handles constructor parameter properties and decorators.
- Assistant
Let me look at the scope builder and how it handles parameter decorators. The issue is likely in how scopes are built for constructor parameters - decorators on parameters should be resolved in the enclosing scope (the class body scope), not in the function parameter scope.
- Assistant
Let me look at the scope builder code. The issue is that when a constructor parameter has a decorator, the decorator expression should be resolved in the enclosing scope (class body scope), not in the function parameter scope.
Let me search for how scopes are built for constructors and how decorators are handled.
- Assistant
Let me understand the scope analysis and how decorators are handled. First, let me look at the builder.rs to see how decorators are visited and how scopes are created for function parameters.
- Assistant
Let me reproduce the issue first.
- Assistant
Now let me build and test with oxlint to reproduce the issue.
- Assistant
Let me try a faster approach - run just the specific tests for the no-unused-vars rule:
- Assistant
Good, the tests pass. Now let me understand the issue better. The problem is in the scope analysis - when a constructor parameter property has a decorator, the decorator expression should be resolved in the enclosing scope (before the parameter scope is created). Let me look at how the semantic builder creates scopes for function parameters and decorators.
Let me look at the builder.rs file to understand how scopes are handled for constructor parameters and their decorators.
- Assistant
Let me look at how
visit_formal_parametersandvisit_decoratorswork in relation to scope creation. The issue is that decorators on constructor parameters should be evaluated in the class scope, not the constructor parameter scope. - Assistant
Let me look at the full visit_formal_parameter implementation.
- Assistant
The key issue is in the
visit_formal_parametermethod. It's callingself.visit_decorators(¶m.decorators)AFTERself.enter_node(kind)but BEFOREself.visit_binding_pattern(¶m.pattern).The problem is that decorators should be evaluated before the parameter's binding pattern is resolved. But more importantly, the decorator expressions need to be evaluated in the scope that's active BEFORE the function parameter scope is entered.
Let me look at how the function body visits formal parameters to understand the scope hierarchy.
- Assistant
Now I see the structure. Let me check what
param.bind(self)does: - Assistant
Let me look at the FormalParameter bind implementation.
- Assistant
I see the issue now! In the
visit_formal_parametermethod,param.bind(self)is called beforeself.visit_decorators(¶m.decorators). This means the parameter binding is registered in the scope before the decorator is visited, causing the decorator'sfooto resolve to the parameter binding instead of the outerconst foo.The fix should be to visit decorators BEFORE binding the parameter. Let me also look at how the class decorator handling works to understand the pattern better.
- Assistant
Now let me look at the test file to understand how to add a test case:
- Assistant
Now I understand the problem. In
visit_formal_parameter,param.bind(self)is called BEFOREself.visit_decorators(¶m.decorators). This means the parameter name (e.g.,foo) is bound in the scope before the decorator expression is visited, so the decorator'sfooreference resolves to the parameter itself rather than the outerconst foo.The fix is to visit decorators BEFORE calling
param.bind(self), so the decorator is evaluated in the enclosing scope without the parameter binding yet.Let me first write a test to reproduce the issue:
- Assistant
Let me look at the oxc.rs test file to understand the test structure better.
- Assistant
Let me look at the classes test section.
- Assistant
Let me look at the test_classes function to understand the test structure better.
- Assistant
Let me first look at the no_unused_vars rule to understand how it handles decorator usages.
- Assistant
Let me now understand the core issue better. Looking at the builder, in
visit_formal_parameter, the parameter is bound before decorators are visited: - Assistant
The issue is clear. In
visit_formal_parameter:param.bind(self)creates the parameter bindingfooin the current scopeself.visit_decorators(¶m.decorators)then visits@Inject(foo.KEY)- but at this point,fooalready resolves to the parameter binding, not the outerconst foo
The fix is to visit decorators BEFORE binding the parameter. This matches how class decorators are handled (visited before entering class scope).
- Assistant
Now let me fix both functions by moving
self.visit_decoratorsbeforeparam.bind(self): - Assistant
Let me navigate to the
visit_formal_parameterfunction and fix it. - Assistant
Now I understand the issue. The fix is to move
self.visit_decoratorsbeforeparam.bind(self)in bothvisit_formal_parameterandvisit_formal_parameter_rest. This ensures that decorator expressions are resolved in the enclosing scope before the parameter's own name is bound. - Assistant
I see there's a duplicate
}on line 2373. Let me fix that and also update thevisit_formal_parameter_restfunction. - Assistant
Good, the changes look correct. Now let me verify the changes compile and run the existing tests:
- Assistant
There's a pre-existing issue with the cfg example, not related to my change. Let me run just the tests for oxc_semantic (not examples):
- Assistant
Good. Now let me also run the integration tests:
- Assistant
Now let me run the linter tests:
- Assistant
All existing tests pass. Now I need to add a test case for this specific issue. Let me first look at the test file to see where to add the test case:
- Assistant
Let me look at the test_classes function to see where to add the test case:
- Assistant
Now let me add test cases for the issue. I'll add them to the
passlist intest_classes: - Assistant
The edit seems to have introduced a syntax issue. Let me check the file and fix it:
- Assistant
I see the test now has duplicate entries. Let me remove the duplicates:
- Assistant
I see the issue - the original duplicate lines need to be removed. Let me fix this:
- Assistant
Let me remove the duplicated original lines and check the file:
- Assistant
I see - my replacement removed the first 3 "let a" lines. Let me add them back:
- Assistant
I see a duplicate comment. Let me fix that:
- Assistant
Good, the file looks correct now. Let me run the tests to see if they pass:
- Assistant
The issue is with the
r#"string literal. Let me check the actual file content: - Assistant
The content looks fine. Let me check how other tests in the file use raw strings:
- Assistant
Let me check the exact bytes around the raw string:
- Assistant
The file looks correct. Let me check if there's a compilation issue elsewhere. Let me try compiling just the linter crate:
- Assistant
The check passes. Let me try the test again:
- Assistant
Hmm,
cargo checkpasses butcargo testfails? Let me look at the exact content: - Assistant
This looks correct. Let me check if there are other
r#"in the file that might interfere. The error is strange. Let me try running the test compilation with more details. - Assistant
Let me look at the first occurrence at line 447 more carefully:
- Assistant
Let me look at the file from the beginning to check if there's a raw string that doesn't close properly:
- Assistant
Let me check the raw strings more carefully:
- Assistant
Let me check if there's a
"#somewhere before line 1362 that could prematurely close the raw string: - Assistant
Let me look at the exact bytes:
- Assistant
The bytes look correct. The raw strings have proper
r#"and"#,delimiters. Let me check if the issue is specific to the test compilation: - Assistant
So
cargo check --testsfails. The raw strings look correct to me. Let me look at the surrounding context more carefully: - Assistant
Let me look at the exact bytes around the problematic raw string:
- Assistant
The raw string looks correct. Let me check if there's an unterminated string or raw string before line 1361 that's causing the issue:
- Assistant
I see there's a multiline string starting at line 1342 but I can't see the closing quote. Let me check the git diff to see what changed:
- Assistant
I see the problem! The closing
",for the multiline string was removed by my edit. Let me fix this: - Assistant
Let me look at the current state around line 1353:
- Assistant
I see the problem! The closing
",for the multiline string that starts at line 1342 was removed. I need to add it back: - Assistant
Good, now the closing
",is back. Let me run the test again: - Assistant
- Assistant
All 3 test cases are still failing. My fix in the semantic builder didn't help. Let me think about what's happening.
The issue is that the decorator expression references
foobut the semantic analysis doesn't consider this as a "use" of the outerfoo. Let me think about why...Actually, let me look at how the semantic builder handles identifier references to understand why the decorator's
fooisn't resolving to the outer binding. - Assistant
Let me understand the reference resolution better:
- Assistant
Let me look at
resolve_references_for_current_scopeto understand how references are resolved. - Assistant
Now I understand the deeper issue. The problem is that
resolve_references_for_current_scope()resolves all references accumulated since the checkpoint against the current scope (function scope), where parameter bindings already exist. Even though decorators are visited beforeparam.bind(self), the resolution happens later when all parameter bindings are already in scope.Let me look at how function visit handles the full flow:
- Assistant
I need to understand how
walk_up_resolve_referenceworks: - Assistant
Let me understand the issue better. The problem is that
resolve_references_for_current_scope()is called after ALL parameters are bound, so even though decorators are visited beforeparam.bind(), the resolution still happens after binding. Let me look at howvisit_identifier_referencecreates references: - Assistant
Let me understand the full flow better. Let me look at the function visit code more carefully:
- Assistant
I see - the real problem is that
visit_formal_parameters(including decorators) is called inside the function scope. Decorators should be evaluated in the enclosing scope. Let me look at how the function visit works: - Assistant
Let me understand
walk_up_resolve_reference: - Assistant
Now I understand the core issue. The problem is that even though I moved
visit_decoratorsbeforeparam.bind(), theresolve_references_for_current_scope()is called AFTER all parameters have been bound. So the decorator reference still resolves to the parameter binding.The fix needs to be at a different level. The decorator references need to be resolved against the parent scope (the class scope), not the function scope. Let me think about this differently.
The key insight is that parameter decorators are evaluated at class-definition time, in the enclosing class scope. So the references in decorators should be resolved before the function scope is even entered.
Let me look at how this should be implemented:
- Assistant
There's no
visit_formal_parametersmethod in the builder - it must be the default implementation from theVisittrait. The actual visitor methods that are overridden arevisit_formal_parameterandvisit_formal_parameter_rest.The issue is that even if I move
visit_decoratorsbeforeparam.bind(self)withinvisit_formal_parameter, the resolution still happens in the function scope after all params are bound.I need a different approach. Let me think...
The fundamental problem is:
- Decorator expressions are visited in the function scope
- When
resolve_references_for_current_scope()is called, it walks up from the function scope and finds the parameter bindingfooin the function scope before finding the outerconst foo
What I need to do is ensure that decorator references are resolved against the parent scope of the function, not the function scope itself.
- Assistant
Let me look at how
reference_identifierworks - this is where references are created: - Assistant
Let me look at how references are created and resolved. The key is that
walk_up_resolve_referencestarts from the reference's scope_id and walks up. So even though we visit decorators before binding, the reference is still in the function scope. When resolve_references_for_current_scope runs, it walks from the function scope up, and finds the parameter binding in the function scope. - Assistant
Now I understand the fundamental issue. The reference is created with
self.current_scope_id(the function scope), and whenresolve_references_for_current_scoperesolves it, it finds the parameter binding in the function scope first.The fix needs to ensure that decorator references are created with the PARENT scope's ID, not the function scope's ID. Let me implement this properly.
- Assistant
Now I understand the core problem. The reference is created with
self.current_scope_id(the function scope). Whenresolve_references_for_current_scoperuns, it finds the parameter binding in the function scope first.The fix needs to ensure that decorator references on constructor parameters are resolved in the parent scope (the class scope), not the function scope. I need to change the approach - instead of just reordering bind/decorators within
visit_formal_parameter, I need to resolve decorator references against the parent scope.Let me revert my earlier change and implement a proper fix:
- Assistant
The real issue is that
resolve_references_for_current_scope()is called after ALL parameter bindings exist. The decorator referencefooresolves to the parameterfoobecause it was created with the function scope ID.I need a fundamentally different approach. Let me look at how references are scoped:
- Assistant
The real fix needs to be at the scope level. When visiting decorators, the references need to be created with the parent scope, not the function scope.
Let me think about this differently. The most natural fix would be to visit parameter decorators before entering the function scope, just like class decorators are visited before entering the class scope.
Let me look at the full function visit flow:
- Assistant
I need a different approach. The fundamental issue is that decorator references need to be resolved in the enclosing scope (parent scope), not the function scope. Let me implement this by temporarily changing the scope when visiting decorators in formal parameters.
In
visit_formal_parameter, before visiting decorators, I'll temporarily setcurrent_scope_idto the parent scope, then restore it after. This way, the decorator references are created with the parent scope's scope_id.Let me also revert the original change (reorder decorators/bind) since it doesn't actually fix the issue.
- Assistant
Now let me run the tests to see if this fix works:
- Assistant
Excellent! The test passes now! Let me run all the no-unused-vars tests to make sure I haven't broken anything:
- Assistant
All 47 tests pass. Now let me run the broader semantic tests and other linter tests to make sure nothing else is broken:
- Assistant
Good. Let me run the integration tests:
- Assistant
There are snapshot assertion failures, which is expected since we changed scope behavior. Let me look at what snapshots failed:
- Assistant
The snapshot changes are exactly what I expect - the
scope_idfor decorator references changed from3(the function/constructor scope) to2(the class/enclosing scope). This is the correct behavior - decorators should reference identifiers in the enclosing scope.Let me look at the test fixtures to understand:
- Assistant
The snapshot changes show that decorator references now resolve to scope_id 2 (the class scope) instead of scope_id 3 (the function/constructor scope). This is correct behavior - decorators are evaluated in the enclosing scope. Let me update the snapshots:
- Assistant
Good, the snapshots have been updated. Now let me run the full test suite for both the linter and semantic crates:
- Assistant
All 1144 linter tests pass. Now let me also run the semantic integration tests again to confirm:
- Assistant
Let me also run the semantic conformance tests:
- Assistant
That's a pre-existing issue with the
cfgexample, not related to my change. Let me run just the tests (not examples): - Assistant
All tests pass. Let me also run a broader set of linter tests to make sure nothing else is broken:
- Assistant
All tests pass. Let me now look at the changes I've made to ensure they're correct:
- Assistant
Let me look at the full diff to verify the changes:
- Assistant
The changes look correct. Let me verify all tests pass one more time by running a broader set of tests:
- Assistant
Let me also verify the semantic tests pass:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.