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
I'll start by exploring the repository to understand its structure and locate the relevant code for the
no-unused-varsrule. Let's first check the current directory and see what's there. - Assistant
Let me explore the repository structure more broadly.
- Assistant
This is the OxLint/OXC repository (Rust-based linter/formatter). I need to find the
no-unused-varsrule implementation and understand how it handles decorator expressions in TypeScript constructors.Let me explore the crates directory to find the relevant linter crate.
- Assistant
Great. The linter crate is at
crates/oxc_linter. Now I need to find theno-unused-varsrule implementation. This might be in a plugin or module within the linter crate. Let's search recursively for "no-unused-vars" or "unused_vars". - Assistant
Let me find the no_unused_vars rule implementation.
- Assistant
Let me first create a test file to reproduce the issue.
- Assistant
Let me understand the codebase structure better and look at how the no_unused_vars rule tracks usages.
- Assistant
Let me first try to build and run the linter to reproduce the issue.
- Assistant
Let me look at how to run tests and understand the code flow instead.
- Assistant
Let me look at the usage tracking logic, specifically how the rule determines if a symbol is "used". The issue is about constructor parameter properties with decorators that reference outer scope variables. Let me look at the usage module and understand how references are resolved.
- Assistant
Let me understand the issue better. The problem is about constructor parameter properties in TypeScript that have decorators. When a constructor parameter property has the same name as an outer binding, and that outer binding is used in the decorator, the linter incorrectly reports the outer binding as unused.
Let me trace through the code to understand how the linter processes this.
- Assistant
Let me look at how references are tracked and how the linter determines if a symbol is "used".
- Assistant
Let me use scroll_down or just read the file with head/tail.
- Assistant
Let me look at the semantic analysis and scope handling. The issue is likely in how the semantic analyzer resolves references in decorator expressions.
- Assistant
Let me look at the semantic builder to understand how decorators are handled.
- Assistant
Let me look at the context around these decorator visits.
- Assistant
Let me look at how
param.bind(self)works. This is critical to understand the scoping issue. - Assistant
Let me search for the bind implementation:
- Assistant
Let me check the FormalParameter bind implementation:
- Assistant
Let me look at the binder.rs more carefully to see the FormalParameter binding:
- Assistant
Now I understand the issue. Let me look at the order of operations in the semantic builder when visiting formal parameters. The problem is that
param.bind(self)is called beforeself.visit_decorators(¶m.decorators), which means when the decorator is visited, the parameter binding already exists in scope. Let me examine this more carefully. - Assistant
I found the issue. The problem is in the order of operations in
visit_formal_parameter. The parameter is bound (creating the symbol) BEFORE decorators are visited. This means when@Inject(foo.KEY)is processed, thefooidentifier resolves to the newly-created parameter symbol instead of the outerconst foo.Let me check how decorators should be handled in TypeScript/ECMAScript semantics and look at the current test files for the no-unused-vars rule:
- Assistant
Let me look at how the visitor pattern resolves identifier references and understand the scope issue:
- Assistant
Let me look at the reference resolution logic in the semantic builder:
- Assistant
Now I understand the issue! In the
visit_formal_parametermethod,param.bind(self)is called BEFOREself.visit_decorators(¶m.decorators). This means when the decorator@Inject(foo.KEY)is processed, the parameterfoois already bound in scope, sofooin the decorator resolves to the constructor parameter instead of the outerconst foo.The fix is to visit decorators BEFORE binding the parameter, so that the decorator expression resolves to the correct outer scope. Let me verify this is the correct approach by checking how TypeScript handles this:
- Assistant
Now I understand the issue clearly. In
visit_formal_parameter, the parameter is bound (creating the symbol) BEFORE decorators are visited. This means thefooidentifier in@Inject(foo.KEY)resolves to the constructor parameterfooinstead of the outerconst foo.Let me check the related issue #20670 mentioned in the bug report:
- Assistant
Let me search for issues related to shadowing or decorator handling:
- Assistant
Now let me look at the test files to understand what tests exist for the no-unused-vars rule:
- Assistant
Let me look at the test file to see if there are any existing tests for this case:
- Assistant
Let me look at the
visit_formal_parameterand related methods more closely to understand the scope issue. The key question is: whenparam.bind(self)is called, does it put the symbol into the function's scope, and then when the decorator visitsfoo, does it see this parameter'sfooinstead of the outerconst foo? - Assistant
Now I understand the issue completely. The fix is in
visit_formal_parameterincrates/oxc_semantic/src/builder.rs. The problem is:param.bind(self)creates the parameter symbol in scope- Then
self.visit_decorators(¶m.decorators)visits decorators - When
@Inject(foo.KEY)is visited,fooresolves to the parameterfooinstead of the outerconst foo
The fix is to visit decorators BEFORE binding the parameter. Let me check if there are any other considerations:
- Assistant
Now I understand the issue completely. In
visit_formal_parameter, the order is:param.bind(self)- binds the parameter name (creating the symbol)self.visit_decorators(¶m.decorators)- visits decorators
This means when
@Inject(foo.KEY)is visited, thefooidentifier resolves to the constructor parameterfooinstead of the outerconst foo. The fix is to visit decorators BEFORE binding the parameter.Let me also check the
reference_identifiermethod to understand how references are resolved: - Assistant
Let me look at the reference_identifier method to understand how identifiers are resolved:
- Assistant
Let me check if there are any related tests or issues that might give insight into the expected behavior:
- Assistant
Let me look at how
declare_referenceresolves references: - Assistant
Let me look at the
declare_referencemethod: - Assistant
Now I understand the issue completely. The problem is in
visit_formal_parameterin the semantic builder. The parameter is bound (creating the symbol in scope) BEFORE the decorators are visited. This means when@Inject(foo.KEY)is processed, thefooidentifier resolves to the constructor parameter instead of the outerconst foo.According to TypeScript/ECMAScript semantics, parameter decorators are evaluated at class-definition time, in the enclosing scope, BEFORE the parameter binding exists. So the fix is to visit decorators BEFORE binding the parameter.
Let me make the fix:
- Assistant
Now let me verify the fix by running the existing tests. First, let me find the test files and run them:
- Assistant
Let me wait for this to complete:
- Assistant
I introduced an error -
FormalParameterRestdoesn't have aninitializerfield. Let me fix that: - Assistant
I need to remove the
initializerfield from thevisit_formal_parameter_restmethod sinceFormalParameterRestdoesn't have it: - Assistant
Now let me run the semantic tests to see if this change breaks anything:
- Assistant
The build error is unrelated to my change - it's about a missing main function in an example file. Let me try running just the tests:
- Assistant
Good, the semantic tests pass. Now let me run the linter tests:
- Assistant
Great! All existing tests pass. Now let me also check if there are any snapshot tests that might need updating. Let me look for semantic snapshot tests:
- Assistant
Let me run the semantic tests to check if any snapshot tests fail:
- Assistant
Let me try running the integration tests instead.
- Assistant
Good, all integration tests pass. Now let me check if there are any decorator-related tests in the semantic tests:
- Assistant
Let me check the decorator-related test files, especially the parameter-property.ts file which seems most relevant to our issue:
- Assistant
Let me check if there's a snapshot for this test:
- Assistant
Let me check the snapshot to understand how it's currently handling the parameter-property case:
- Assistant
Now I need to understand the issue better. Let me look at the parameter.ts test:
- Assistant
Now let me run all the semantic tests to check if any snapshots need updating:
- Assistant
The example file has an error, let me try to run the tests specifically:
- Assistant
Good, the semantic tests pass. Now let me run the linter tests to make sure the no-unused-vars tests pass:
- Assistant
Now let me write a test to verify that the issue is fixed. First, let me add a test case to the typescript_eslint.rs file:
- Assistant
Let me look at the end of the pass vector to add a test case:
- Assistant
Let me look at how the test file is structured:
- Assistant
Let me look for the structure of the test file to see where I should add the test case:
- Assistant
Let me look at the end of the pass vector to add a test case:
- Assistant
Now let me add a test case for the issue. I'll add it to the pass vector:
- Assistant
Now let me run the test to see if it passes:
- Assistant
The test is still failing. My fix doesn't seem to be working. Let me investigate further. The issue might be deeper than just the ordering of
param.bind()andvisit_decorators. Let me look at how scope resolution works: - Assistant
Let me look at how references are resolved to understand why my fix doesn't work:
- Assistant
Let me check if there's a caching issue and make sure my changes are being picked up:
- Assistant
My fix isn't working. The issue is that the references are resolved later (by
resolve_all_references), not at the time of visiting. Let me look at howresolve_references_for_current_scopeworks: - Assistant
I see - the references are resolved later, not at visit time. The issue is that when decorators are visited, the reference to
foois created in the constructor scope, where the parameterfoois also declared. The reference resolver finds the parameterfoofirst.The real fix needs to happen at a different level. According to TypeScript semantics, parameter decorators are evaluated in the enclosing class scope, not the constructor scope. Let me look at how to properly handle this:
- Assistant
Now I understand the issue better. The
walk_up_resolve_referencewalks up the scope chain from the reference's scope. When the decorator@Inject(foo.KEY)is visited, the reference tofoois created in the constructor scope. When it walks up, it finds the constructor parameterfoofirst (in the same scope), so it resolves to that instead of the outerconst foo.My fix of visiting decorators before binding the parameter doesn't help because the references are resolved later, at which point the parameter is already bound.
The real fix needs to ensure that the decorator's reference to
foois resolved in the class scope, not the constructor scope. Let me look at how to properly handle this by examining how the constructor scope is set up: - Assistant
Let me look at how
visit_functionis implemented to understand how the scope is set up: - Assistant
Now I understand the issue! The key is in the
visit_functionmethod. After visiting all formal parameters,resolve_references_for_current_scope()is called. At that point, ALL parameters are already bound, so the reference tofoofrom the decorator resolves to the constructor parameterfooinstead of the outerconst foo.The fix needs to ensure that decorator references are resolved BEFORE the parameter is bound. Let me look at how to do this properly:
- Assistant
I see the issue now. The problem is that
resolve_references_for_current_scope()is called after ALL parameters are visited. So even though I visit decorators before binding the parameter, the reference tofoois still resolved after the parameter is bound.The fix needs to resolve decorator references BEFORE the parameter is bound. Let me implement a more targeted fix:
- Assistant
Now I understand the issue better. The problem is that even though I visit decorators before binding the parameter, the references are resolved later by
resolve_references_for_current_scope(). At that point, the parameter is already bound.The fix needs to resolve decorator references BEFORE the parameter is bound. Let me implement a more targeted fix:
- Assistant
Now let me apply the same fix to
visit_formal_parameter_rest: - Assistant
Now let me check if the
checkpoint()method exists onunresolved_references: - Assistant
Let me also look at the
resolve_references_for_current_scopemethod to understand how it handles checkpoints: - Assistant
Now let me run the test to see if my fix works:
- Assistant
Excellent! The test passes now! Let me run all the no-unused-vars tests to make sure I didn't break anything:
- Assistant
All tests pass! Now let me run the semantic tests to make sure I didn't break anything:
- Assistant
Let me run the semantic integration tests as well:
- Assistant
Let me run the main semantic test which includes snapshot tests:
- Assistant
All tests pass! Let me also run a broader set of linter tests to make sure I didn't break anything:
- Assistant
Excellent! All 1144 linter tests pass! Let me now review the changes I made to make sure they are complete and correct:
- Assistant
The changes look good. Let me verify the fix one more time by running a quick test with the exact reproduction case from the issue:
- Assistant
Let me run the test without the grep to see the output:
- Assistant
All tests pass. Let me now run a broader set of tests to make sure I didn't break anything else in the codebase. Let me run all the linter tests and semantic tests:
- Assistant
Let me also run the semantic tests again to make sure everything is still passing:
- Assistant
The error is about a pre-existing issue with an example file, not about my change. Let me run just the tests:
- Assistant
All tests pass. Let me now run a comprehensive test to make sure I haven't broken anything else:
- Assistant
Let me also check if there are any other similar patterns in the codebase that might need fixing:
- Assistant
Let me check if class decorators have the same issue:
- Assistant
Class decorators are already handled correctly - they're visited before entering the class scope. Let me verify that the fix works with imports as well:
- Assistant
Let me add another test case to verify that the fix works with imports as well:
- Assistant
Let me add another test case for the import scenario:
- Assistant
Let me run the test again to make sure the new test case passes:
- Assistant
All tests pass. Let me now run the complete test suite one more time to make sure everything is working correctly:
- Assistant
Let me also run the semantic tests one more time to make sure everything is working correctly:
- Assistant
All tests pass. Let me now review the final changes and submit them:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.