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 incorrectly reports a false positive when a constructor parameter property shadows an imported/outer identifier that is used in the parameter's own decorator expression.The key insight from the issue is:
- When a TypeScript constructor parameter has a decorator that references an outer variable with the same name as the parameter property
- The decorator is evaluated at class-decoration time in the enclosing scope, BEFORE the parameter binding exists
- So references in the decorator should resolve to the outer binding, not the parameter
- But oxlint's scope analysis incorrectly thinks the outer binding is unused
Let me start by exploring the repository structure to understand how it's organized.
Assistant
Let me explore the repository structure more systematically.
Assistant
This is a Rust project. Let me explore the crates directory to find the linter-related code, especially the
no-unused-varsrule.Assistant
I see
oxc_linterandoxc_semanticcrates. The issue is related to theno-unused-varsrule in the linter, so I should look there. Let me explore the linter crate and find theno-unused-varsrule.Assistant
Let me look at the structure of the no_unused_vars rule:
Assistant
Let me first read the main mod.rs file to understand the structure of the no_unused_vars rule, and then look at the usage.rs file which likely handles how variable usages are tracked.
Assistant
Now let me look at the symbol.rs file and usage.rs to understand how usages are detected:
<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 fix my command - I need to use the bash_terminal tool properly.
Assistant
Now let me look at the usage.rs file which is the key file for understanding how variable usages are tracked:
Assistant
Now let me look at the semantic crate to understand how it handles scopes and references:
Assistant
Let me look at the binder.rs file to understand how decorators are handled in semantic analysis:
Assistant
Let me now understand the builder.rs file to see how decorators are handled in semantic analysis.
Assistant
Let me search for decorator-related code in the builder:
Assistant
Let me look at how decorators are visited in the builder. The key issue is that decorators on constructor parameters are evaluated in the enclosing scope before the parameter binding exists. Let me understand the code flow.
Assistant
Now I see how decorators on classes are visited - they're visited BEFORE the class scope is entered. This is important because decorators need to be evaluated in the outer scope.
Let me look at how decorator handling works for constructor parameters:
Assistant
Let me understand the scope structure for constructor parameters better:
Assistant
Let me first reproduce the issue to confirm the problem:
Assistant
Now let me build and run oxlint on this test file:
Assistant
Let me find the correct package name for oxlint.
Assistant
Let me explore the apps directory to find the oxlint binary.
Assistant
Let me check the Cargo.toml of the oxlint app to find the binary name.
Assistant
Let me build the oxlint binary.
Assistant
The build is taking too long. Let me try building in debug mode which is faster:
Assistant
Now let me run oxlint to reproduce the issue:
Assistant
I've reproduced the issue. The problem is in how the semantic builder handles decorators on constructor parameters. Let me investigate the scope structure.
Assistant
Let me understand the scope structure better by looking at how functions/constructors handle their parameter scopes:
Assistant
Let me write a small test program to understand the scope structure:
Assistant
Let me try a different approach - I'll write a smaller test script and run it using cargo run with a simple test.
Assistant
Let me explore the tests for the no_unused_vars rule:
Assistant
Let me look at the existing tests to understand the test format and then check for any decorator-related tests.
Assistant
Let me look at the relevant tests for decorators:
Assistant
Now I understand the issue. The problem is in
/oxc/crates/oxc_semantic/src/builder.rs. When visiting constructor/method parameters with decorators, the parameter is bound (which declares its symbol) BEFORE the decorators are visited. This causes references in the decorator expression to resolve to the parameter binding instead of the outer binding.The fix is to visit decorators BEFORE binding the parameter. Let me look at the exact lines to modify:
Assistant
Now I see the issue! In
visit_formal_parameter, the order is:param.bind(self)- binds the parameter nameself.visit_decorators(¶m.decorators)- visits decorators
But decorators should be visited BEFORE the parameter is bound, because decorator expressions are evaluated in the enclosing scope (before the parameter binding exists in that scope).
Let me look at how class decorators are handled for reference:
<tool_call> <function=bash_terminal> <parameter=command> sed -n '790,810p' /oxc/crates/oxc_semantic/src/builder.rs </tool_call>
Assistant
Let me correct the command call.
Assistant
Now I understand the issue better. Let me look at how reference resolution works:
Assistant
Let me look at the code that handles references in builder.rs to understand how references get resolved to symbols.
Assistant
Now I understand the issue. When visiting a formal parameter:
param.bind(self)is called first, which binds the parameter name in the current scope (function scope)- Then
self.visit_decorators(¶m.decorators)visits the decorator expressions
This means when the decorator expression contains a reference to a variable with the same name as the parameter, the reference resolves to the parameter binding instead of the outer scope's variable.
The fix is to visit decorators BEFORE binding the parameter, so that decorator references resolve correctly to the outer scope.
Let me verify the issue by looking at the order:
Assistant
Assistant
I need to fix my command formatting.
Assistant
Now I understand the issue. The problem is in
/oxc/crates/oxc_semantic/src/builder.rs. When visiting constructor/method parameters, the parameter is bound (creates a symbol binding) BEFORE the decorators are visited. This causes references in decorator expressions to resolve to the parameter's own binding instead of the outer scope's variable.According to TypeScript semantics, decorator expressions are evaluated in the enclosing scope (before the parameter binding exists), so references in decorators should resolve to the outer scope, not to the parameter itself.
Let me verify the issue is in the semantic builder by first checking the current test to ensure I understand the problem:
Assistant
Confirmed! The false positive is reported for
foo. Now let me apply the fix. The issue is that invisit_formal_parameterandvisit_formal_parameter_rest,param.bind(self)is called BEFOREself.visit_decorators(¶m.decorators), which causes decorator references to resolve to the parameter binding instead of the outer scope.Assistant
I'll apply the fix by moving
self.visit_decorators(¶m.decorators)beforeparam.bind(self)in bothvisit_formal_parameterandvisit_formal_parameter_rest. This ensures decorator expressions are evaluated before the parameter binding exists, so references in decorators correctly resolve to the outer scope.Assistant
Now I'll apply the fix by moving
self.visit_decorators(¶m.decorators)beforeparam.bind(self)in both methods:Assistant
The
edittool has a specific format requirement. Let me use the correct syntax with theeditcommand.Assistant
Let me try with the
--fileoption instead:Assistant
The edit tool is not recognizing my arguments. Let me check the proper format for the edit tool. It seems I need to use the command format properly.
Looking at the special commands documentation:
Usage: edit [OPTIONS] LINE_RANGE [REPLACEMENT_TEXT]Options: --file PATH The file to edit.
So the format should be:
edit --file PATH LINE_RANGE REPLACEMENT_TEXTBut the way I'm passing it isn't working. Let me try with the heredoc syntax.
Assistant
Let me use sed to make the changes directly:
Assistant
The edit tool isn't working as expected. Let me try using sed to make the changes directly.
Assistant
Let me try the edit command with the proper syntax using heredoc for the replacement text.
Assistant
Let me try the edit command with heredoc syntax properly:
Assistant
The edit tool seems to have issues with the parameter format. Let me use sed to make the changes directly.
Assistant
Let me use sed to make the changes. The fix requires moving
self.visit_decorators(¶m.decorators)beforeparam.bind(self)in both functions.Assistant
The changes have been applied. Let me verify the changes were made correctly by viewing the modified code.
Assistant
The changes look correct. Now let me build the project and test the fix:
Assistant
The build was successful. Now let me test the fix with the reproduction case:
Assistant
The issue is still occurring. The problem is more fundamental than I initially thought. My fix (moving
visit_decoratorsbeforeparam.bind) is necessary but not sufficient. The reference resolution still fails because references are resolved LATER (after all traversal), and by that time, the parameter binding exists in the scope.The real issue is that decorator expressions on parameters should be resolved starting from the CLASS scope (parent scope), not the FUNCTION scope. Let me investigate further.
Assistant
Assistant
I need to investigate how references are created in the builder and how the scope_id is set for them. Let me search for the relevant code.
Assistant
The issue is more fundamental. When visiting parameter decorators, the
current_scope_idis the function scope (constructor), but decorator expressions should be resolved in the enclosing scope (class scope). References created during decorator visiting get their scope_id set to the function scope, and whenwalk_up_resolve_referenceis called, it finds the parameter binding first and resolves incorrectly.Let me implement a fix that temporarily changes the scope before visiting decorators:
Assistant
The issue is that even though I moved
visit_decoratorsbeforeparam.bind(self), the reference resolution happens later inresolve_references_for_current_scope, which is called at the end of the function scope when ALL parameters have already been bound. At that point, when walking up from the function scope, the parameter binding is found first.The fix needs to ensure that decorator references are resolved in the parent scope (class scope), not the function scope. I need to temporarily change the
current_scope_idwhen visiting decorators.Let me update the fix:
Assistant
Let me verify the changes:
Assistant
Good, the changes look correct. Now let me build and test:
Assistant
Now let me test the fix:
Assistant
The fix works. Now let me test some edge cases to ensure the fix handles them correctly.
Assistant
The basic case works. Let me test more edge cases:
- Multiple decorators on the same parameter
- Decorator references to variables with different names than the parameter
- Parameter initializer that uses the parameter name (should still work)
- Non-decorated parameters (should still work correctly)
- NestJS-like pattern
- Regular method parameters (not just constructor)
- Rest parameters with decorators
- Accessor (getter/setter) parameters
- Method decorators (should not be affected)
- Class decorators (should not be affected)
Assistant
The fix is working for the basic cases. Let me test more edge cases to ensure nothing is broken.
Assistant
Let me fix the test case - method parameters can't have private/readonly modifiers:
Assistant
This is expected - the parameter
foois unused in the method body (no warning about the outerfoo). Let me test more edge cases:Assistant
NestJS-like pattern works. Let me test more edge cases:
Assistant
Good -
foois correctly flagged as unused in edge case 6 (since it's not used in any decorator), andargsis correctly flagged as unused parameter. The fix is working correctly.Let me test a few more edge cases:
Assistant
The fix is working correctly:
- Outer
foois correctly recognized as used (in decorator) - Parameter
foois correctly flagged as unused (in method body)
Let me now run the existing tests to make sure the fix doesn't break anything:
- Outer
Assistant
All 47
no_unused_varstests passed. Let me also run the semantic tests to ensure I didn't break anything:Assistant
The error is in an example file (
cfg.rs) that seems to have a pre-existing issue withmainfunction not found. Let me check if this is a pre-existing issue by looking at the file and checking if it's supposed to compile.Assistant
Let me check if this is a pre-existing issue by reverting my changes and running the test:
Assistant
All 11 semantic tests passed. The error with
cfg.rsis a pre-existing issue in an example file (not related to my changes) that only affects theexamples/directory.Let me run the full linter test suite to ensure nothing is broken:
Assistant
Excellent! All 1144 tests passed. Let me clean up the test files I created:
Assistant
Let me verify the final state of my changes:
Assistant
The changes look correct. Let me create a final test to verify the fix works for the exact issue described:
Assistant
The fix is working correctly. Let me verify the fix handles a few more edge cases and then submit.
Assistant
All edge cases are handled correctly:
- C1 (original issue): No warning for
foo- ✅ The fix works! - C2 (multiple params with decorators): No warnings for
configA/configB- ✅ - C3 (method param): Only the parameter
foois flagged as unused - ✅ (outerfoois correctly recognized as used) - C4 (param initializer referencing earlier param): Only
bis flagged - ✅ - C5 (accessor setter with underscore): No warnings - ✅
- C6 (regular unused param): Correctly flagged - ✅
Let me clean up the temp test files:
- C1 (original issue): No warning for
Assistant
All edge cases are handled correctly. Let me verify the final checklist:
-
✅ Reproduced the error: Ran
./target/debug/oxlint test_issue.tsbefore the fix and confirmed the false positive. -
✅ Confirmed the fix resolves the error: Ran
./target/debug/oxlint test_issue.tsafter the fix and confirmed "Found 0 warnings and 0 errors." -
✅ Edge cases handled:
- Original issue (constructor parameter property shadows outer binding): ✅
- Multiple decorators on same parameter: ✅
- Method parameters (not constructor): ✅
- Rest parameters: ✅
- Static method parameters: ✅
- Accessor (getter/setter) parameters: ✅
- Non-decorated parameters still checked correctly: ✅
- Parameter initializers referencing earlier parameters: ✅
- Multiple parameters with different outer bindings: ✅
-
✅ Existing tests pass:
- All 1144 linter tests pass
- All 11 semantic tests pass
- All 47 no_unused_vars tests pass
Now let me submit the changes:
-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.