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 reports a false positive when a constructor parameter property has the same name as an outer binding (like an import or const), and that outer binding is used in the parameter's own decorator expression.Let me start by exploring the repository structure to understand the codebase.
Assistant
Let me explore the crates directory to find the linter-related code, especially the
no-unused-varsrule.Assistant
Let me first create a test file to reproduce the issue, and then explore the relevant source code.
Assistant
I've successfully reproduced the issue. Now let me explore the source code to understand how the
no-unused-varsrule handles decorators and variable scoping.Assistant
Now let me look at the
usage.rsfile which handles detecting usages of variables:Assistant
Let me also look at the
symbol.rsfile to understand how symbols and references work:Assistant
Now I need to understand how references are resolved in the semantic analysis. The issue is about scoping - the
fooreference inside@Inject(foo.KEY)should be resolved to the outerconst foodeclaration, not to the constructor parameterprivate readonly foo. Let me check the semantic analysis to understand how decorators are handled:Assistant
Let me look at the builder.rs file to understand how decorators are visited in relation to parameter scoping:
Assistant
I see the issue. In
visit_formal_parameter, the decorators are visited afterparam.bind(self)which binds the parameter's identifier. So when we visit the decorator expression@Inject(foo.KEY), thefooreference resolves to the parameter binding instead of the outerconst foo.According to TypeScript semantics, parameter decorators are evaluated in the class scope (or enclosing scope), not in the parameter scope. The decorator should be visited before the parameter binding is created.
Let me check the order more closely:
Assistant
The problem is clear:
param.bind(self)is called on line 2361, which creates the symbolfoofor the parameter. Thenself.visit_decorators(¶m.decorators)on line 2364 visits the decorator expressions. Since thefoosymbol for the parameter already exists, the referencefooin@Inject(foo.KEY)resolves to the parameter instead of the outerconst foo.The fix should be to visit the decorators before calling
param.bind(self), since decorators are evaluated in the enclosing scope before the parameter binding exists.Let me also check
visit_formal_parameter_rest:Assistant
Same issue with
visit_formal_parameter_rest. Let me first verify the issue by checking the test files for any existing tests related to decorators:Assistant
Let me check the oxc.rs test file for more context about the test structure:
Assistant
Now let me understand the fix. The issue is that when visiting a formal parameter, the decorator expressions should be visited before the parameter binding is created. This is because parameter decorators are evaluated in the class scope (or the enclosing scope where the class is defined), not in the parameter's scope.
Let me make the fix in the builder.rs:
Assistant
<parameter=command> open /oxc/crates/oxc_semantic/src/builder.rs 2358 </tool_call>
Assistant
Let me try the correct syntax to open the file.
Assistant
Now I understand the issue. The fix is to move
self.visit_decorators(¶m.decorators);beforeparam.bind(self);so that decorator expressions are visited before the parameter binding is created. This way, references in decorators resolve to the outer scope correctly.Assistant
It looks like the tool didn't work as expected. Let me use the proper format for the edit command.
Assistant
Now let me also fix
visit_formal_parameter_restwith the same change:Assistant
Now let's test if the fix works by running oxlint on the test file again:
Assistant
The issue persists. Let me investigate further - the problem might be related to how scopes are handled. Let me check the scope structure more carefully:
Assistant
The issue is still happening. Let me think about this more carefully.
The problem is that
param.bind(self)binds the parameter to the current scope. When the decorator@Inject(foo.KEY)is visited, the referencefoois resolved. But the issue is:- Before my fix,
param.bind(self)was called beforeself.visit_decorators, so the parameterfooshadowed the outerfoo. - After my fix,
self.visit_decoratorsis called beforeparam.bind(self), so the decorator reference should resolve to the outerfoo.
But the warning is still happening. Let me debug this further.
Let me check if there's caching or if the binary wasn't properly rebuilt. Also, let me look at the reference resolution logic more carefully.
Actually, let me re-run the command to make sure the changes are applied:
- Before my fix,
Assistant
The build says it's finished without recompiling. Let me check if my changes were actually saved:
Assistant
The changes are there. Let me force a recompilation:
Assistant
The build completed. Let me run the test again.
Assistant
Let me investigate the scope handling more carefully. The issue might be that the decorator's reference is still recorded in the constructor's scope, and resolved later when the parameter binding already exists.
Assistant
The issue is that the reference resolution happens later, not during the visit. Let me check how references are resolved. The problem might be in the reference resolution phase, not the visit phase.
Let me look at the reference resolution logic more carefully. When a reference is created, it's associated with a scope_id. Later, when references are resolved, the scope_id is used to determine which binding the reference should resolve to.
So even if I visit the decorators before binding the parameter, the reference might still be recorded in the constructor's scope, and when it's resolved later, the parameter binding already exists in that scope.
Let me check where the reference resolution happens.
Assistant
Assistant
Now I understand the issue better. The problem is that
resolve_references_for_current_scopeis called after all parameters are visited, so by that time, all parameter bindings exist. Even though I visit decorators before binding the parameter, the resolution happens later.The fix needs to resolve references immediately after visiting decorators, before the parameter binding is created. Let me update the code:
Assistant
Let me check how the checkpoint mechanism works for
resolve_references_for_current_scope:Assistant
I need to understand how the checkpoint mechanism works. It looks like the checkpoint is set to the current position in the unresolved_references vector, and when
resolve_references_for_current_scopeis called, it only processes references added after the checkpoint.Let me look at how checkpoints are managed for function parameters.
Assistant
Now let me also update
visit_formal_parameter_restto have the same fix:Assistant
The changes look good. Now let me rebuild and test to see if the fix works.
Assistant
The fix works! The false positive is now gone. Let me verify by checking a few edge cases:
- When the decorator doesn't use the outer binding (should still report unused)
- When there are multiple parameters with decorators
- When using imports instead of const
- When the decorator uses a different variable
Let me test these edge cases.
<tool_call> <function=bash_terminal> <parameter=command> cat > /oxc/test_edge_cases.ts << 'EOF' // Test case 1: Outer binding used in decorator - should pass (no warning) const foo = { KEY: 'token' }; declare function Inject(token: unknown): ParameterDecorator; export class C1 { constructor(@Inject(foo.KEY) private readonly foo: number) {} }
// Test case 2: Outer binding NOT used in decorator - should still report unused const bar = { KEY: 'token' }; export class C2 { constructor(@Inject('some-token') private readonly bar: number) {} }
Assistant
The previous command had an error. Let me try again with the correct format.
Assistant
I need to test the edge cases now.
Assistant
The edge case tests show:
foo- No warning ✅ (used in decorator)bar- Warning ✅ (NOT used in decorator)someConfig- No warning ✅ (import used in decorator)config1- No warning ✅ (used in decorator)config2- Warning ✅ (NOT used in decorator)myConfig- No warning ✅ (used in decorator, different param name)
All edge cases are working correctly. Now let me run the existing tests to ensure nothing is broken.
Assistant
All no_unused_vars tests passed. Let me also run the semantic tests to make sure my changes to the semantic builder didn't break anything else.
Assistant
Wait, the error is in a different file (cfg.rs example) - it's not related to my changes. Let me run just the unit tests without examples.
Assistant
All semantic tests passed. Let me run a broader test to make sure nothing is broken.
Assistant
All semantic tests passed. Let me also run a broader test suite to ensure the changes don't break anything else.
Assistant
All tests are passing. Let me also run a broader linter test to make sure everything is working correctly.
Assistant
All 1144 linter tests passed. Let me clean up the test files I created and verify the fix one more time.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.