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.
Interface
No new interfaces are introduced.
ADDITIONAL ADVICE
Since you are given a git repository, you can use git commands to simplify your work. For example, if you made a mistake and want to revert your changes, you can use
git checkout <file>to restore the file to its original state. You can also reset all changes in the repository usinggit reset --hardcommand. Additionally, you can usegit stashandgit stash popto temporarily save your changes and restore them later. Finally, you can see the changes that you've made (which will be submitted when you callsubmit) usinggit statusorgit diffcommands. However you don't need to usegit addorgit commitbefore submitting your changes. In fact,submitonly submits currently unstaged changes because it usesgit diffto get the changes that need to be submitted.CHECKLIST
Before submitting your solution, please go over the following checklist and make sure you've done everything:
- If an error was reported in the issue description, I have successfully reproduced it.
- If an error was reported in the issue description, I have confirmed that my fix resolves the error.
- I have thought about edge cases and made sure my fix handles them as well.
- I have run existing tests in the repository that might have been affected by the change I made and confirmed that they pass. I want you to list every bullet from this checklist and write your reasoning for why you think you did it or didn't need to.
Repository has been uploaded and your shell is currently at the repository root. Time to solve the issue!
(Current directory: /oxc, current file: none) bash-$
- Assistant
I'll start by exploring the repository to understand its structure and locate relevant files for the
no-unused-varsrule. Let's first get a high-level view. - Assistant
Let me explore the repository structure more broadly.
- Assistant
Let me look for the no-unused-vars rule in the linter crate.
- Assistant
Let me look at the main module for no-unused-vars rule.
- Assistant
Let me first create a reproduction script to understand the issue.
- Assistant
Let me look at the
usage.rsfile, which likely handles how usages are tracked: - Assistant
Let me look at how decorators are handled in the scope analysis:
- Assistant
Let me look at the
mod.rsfile which is the main entry point for the no-unused-vars rule: - Assistant
Let me see the rest of the file.
- Assistant
Now let me understand the issue better. The problem is that when a constructor parameter property has the same name as an outer binding, and the outer binding is used in the decorator, the scope analysis might be resolving the decorator reference to the parameter property instead of the outer binding.
Let me look at how the semantic analysis handles scopes, especially for decorators and constructor parameter properties.
- Assistant
Let me look at the scope/resolver to understand how decorators are handled:
- Assistant
Let me search for how decorators are visited in scope analysis:
- Assistant
Let me look at the builder.rs file to understand how decorators are handled:
- Assistant
Let me look at the visit_formal_parameter implementation:
- Assistant
I can see the issue! In
visit_formal_parameter,param.bind(self)is called beforeself.visit_decorators(¶m.decorators). This means the parameter's binding is already in scope when decorators are visited. But decorators on parameters should be evaluated in the enclosing scope, not in the constructor's scope.Let me look at how the resolver handles this:
- Assistant
Let me check the resolver to understand how reference resolution works for decorators:
- Assistant
Let me look at how the scope analysis creates scopes for constructor parameters:
- Assistant
Let me search for the
bindmethod for FormalParameter to understand how it creates the binding: - Assistant
Let me search for
bindin the codebase. It's a trait method likely. - Assistant
Let me look at the binder for FormalParameter:
- Assistant
Let me look at how class decorators are handled in the same file:
- Assistant
Let me look at the visit_class implementation:
- Assistant
Now I understand the problem. In
visit_formal_parameter, the parameter binding is created (param.bind(self)) BEFORE the decorators are visited (self.visit_decorators(¶m.decorators)). This means the parameter bindingfooshadows the outerconst foobefore the decorator@Inject(foo.KEY)is resolved.The fix is to visit decorators before binding the parameter, so the decorator expression resolves in the enclosing scope (as it should per TypeScript semantics).
Let me first create a reproduction test to confirm the bug:
- Assistant
Let me check if there are existing tests for decorators in this rule:
- Assistant
Let me look at the test around line 67 to see if there's already a test for constructor parameter decorators:
- Assistant
Let me look at the Binder impl for FormalParameter:
- Assistant
Now I understand the issue clearly. In
visit_formal_parameter,param.bind(self)creates the binding for the constructor parameter property BEFORE visiting the decorators. This means the decorator@Inject(foo.KEY)sees the constructor parameterfoo(which shadows the outerconst foo), so the outerfoois never referenced.The fix is to visit decorators BEFORE binding the parameter, just like how class decorators are visited before entering the class scope.
Let me make the fix:
- Assistant
Now I understand the issue clearly. In
visit_formal_parameter,param.bind(self)is called beforeself.visit_decorators(¶m.decorators). This means when the decorator@Inject(foo.KEY)is being resolved, thefooidentifier in the decorator already resolves to the constructor parameterfoo(which shadows the outerconst foo), rather than the outer binding.The fix is to visit decorators BEFORE binding the parameter, just like how class decorators are visited before entering the class scope.
Let me apply the fix:
- Assistant
Now let me also fix
visit_formal_parameter_restfor consistency: - Assistant
Now let me verify this change compiles and test it. First, let me check the existing tests:
- Assistant
Let me wait for the test to finish:
- Assistant
Good, the code compiles. Now let me run the linter tests for no_unused_vars:
- Assistant
All 47 no-unused-vars tests pass. Let me also run the broader semantic tests to ensure nothing is broken:
- Assistant
The example file seems to be broken but that's pre-existing. Let me run just the lib tests:
- Assistant
Now let me build the linter and test it with the reproduction case:
- Assistant
Now let me test with the reproduction case:
- Assistant
The issue is still reproduced. The fix wasn't enough. Let me investigate more deeply. Let me look at how the resolver handles references:
- Assistant
Let me look at how references are resolved. The issue is that reference resolution is deferred - references are resolved when leaving scopes:
- Assistant
Let me look at how
resolve_references_for_current_scopeis called: - Assistant
Let me see the context for line 2238:
- Assistant
Let me look at
resolve_references_for_current_scopemore carefully: - Assistant
Let me look at how function parameters and scope resolution work:
- Assistant
Let me look at the visit_function method to understand how formal parameters are visited and when references are resolved:
- Assistant
I see! The issue is more subtle. Even with decorators visited first, the
resolve_references_for_current_scope()is called later (at line 1915), AFTER all parameter bindings have been created. So the decorator reference tofooresolves to the constructor parameterfooat that point.The fix needs to ensure decorator references are resolved BEFORE the parameter binding is created. Let me look at the pattern used in
visit_catch_parameterwhich has similar behavior: - Assistant
Now I understand the full picture. The issue is:
- In
visit_function, a checkpoint is saved (line 1894), then parameters are visited (line 1902), thenresolve_references_for_current_scope()is called (line 1915). - By the time
resolve_references_for_current_scope()runs at line 1915, all parameter bindings have already been created. So decorator references resolve to the parameter binding, not the outer scope.
The fix: In
visit_formal_parameter, we need to use the same pattern asvisit_catch_parameter- save a checkpoint, visit decorators, resolve those references immediately (before binding), then restore the checkpoint. This ensures decorator references resolve to the enclosing scope, not the parameter binding. - In
- Assistant
Now let me also update
visit_formal_parameter_restwith the same pattern: - Assistant
Now let me build and test with the reproduction case:
- Assistant
Now let me test with the reproduction case:
- Assistant
The fix works! No more false positive. Now let me run all the existing tests to make sure nothing is broken:
- Assistant
All 47 tests pass. Let me also run the broader semantic tests and full linter tests:
- Assistant
Let me also run the full linter test suite:
- Assistant
All 1144 tests pass! Let me also run the semantic checker tests to verify snapshot tests:
- Assistant
That's a pre-existing issue with the example file. Let me run the semantic tests specifically:
- Assistant
Let me try to run the integration tests.
- Assistant
All 60 semantic integration tests pass. Let me also run the main test:
- Assistant
Excellent! Let me also test some more edge cases to make sure the fix is robust:
- Assistant
The warnings in cases 3 and 4 are correct - they're about the parameter
fooitself being unused in the constructor body (which is a separate valid warning). The important thing is that the outerconst foois NOT falsely reported as unused. Let me verify the import case more carefully: - Assistant
The import case also works correctly - no false positive. Let me also run the full linter test suite to make sure nothing is broken:
- Assistant
All tests pass. Let me also verify the diff of my changes:
- Assistant
Let me also run the parser tests to make sure nothing is broken:
- Assistant
Let me also run the semantic checker tests which might have snapshot tests affected by our change:
- Assistant
Let me also check if there are any other test suites that might be affected by this change:
- Assistant
All tests pass. Let me do a final review of the changes and run a comprehensive test to make sure everything is working:
- Assistant
Let me do a final comprehensive test with the original reproduction case and a few more edge cases:
- Assistant
The fix works perfectly for all edge cases. Let me clean up the temporary test files:
- Assistant
Let me review the final diff:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Identical first prompt
claude-sonnet-5Compare side by side →
claude-sonnet-5Compare side by side →
claude-sonnet-5Compare side by side →
claude-sonnet-5Compare side by side →
claude-sonnet-5Compare side by side →
Qwen/Qwen3.6-35B-A3BCompare side by side →
Qwen/Qwen3.6-35B-A3BCompare side by side →
Qwen/Qwen3.6-35B-A3BCompare side by side →
Qwen/Qwen3.6-35B-A3BCompare side by side →
Qwen/Qwen3.6-35B-A3BCompare side by side →
Qwen/Qwen3.5-35B-A3BCompare side by side →
Qwen/Qwen3.5-35B-A3BCompare side by side →