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.
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
Let me start by understanding the issue:
- There's a false positive in
no-unused-varsrule when a TypeScript constructor parameter property shadows an imported identifier used in its own decorator. - The example:
const foo = { KEY: 'token' };and thenconstructor(@Inject(foo.KEY) private readonly foo: number) {}-fooin the decorator resolves to the outerfoo, not the parameterfoo, because decorators are evaluated at class-decoration time before parameter bindings exist.
Let me explore the repository structure first.
- There's a false positive in
Assistant
Let me explore the relevant crates and find the
no-unused-varsrule implementationAssistant
The linter is in
oxc_linter. Let me find the no-unused-vars rule.Assistant
Hmm, no files found. Let me search more broadly.
Assistant
The
no_unused_varsrule doesn't have its own file. Let me search for the main rule implementationAssistant
Let me look for the main rule file.
Assistant
Let me look at the directory structure and the relevant source
Assistant
Let me first reproduce the issue, then explore the relevant code.
Assistant
Let me run oxlint on this file.
Assistant
Let me find the oxlint binary.
Assistant
That's a node wrapper. Let me check if there's a cargo-built binary. Let me also check what's available via cargo.
Assistant
Good, it built. Now let me run the test to reproduce the issue
Assistant
I've reproduced the issue. Now let me explore the relevant source code to understand how the analysis
Assistant
Let me use the correct command to open the file.
Assistant
Let me look at more of the file, especially the
runfunctionAssistant
Let me scroll down to see the Rule implementation
Assistant
Let me continue reading the file
Assistant
Let me look at
symbol.rsto understand how usages are trackedAssistant
Let me continue reading this file
Assistant
Let me see more of the file, specifically
has_usagesAssistant
Let me see the rest of the file
Assistant
I don't see
has_usagesin this file. Let me search for itAssistant
Let me look at
usage.rsAssistant
Let me continue reading
Assistant
Let me understand the issue better by looking at how the semantic analysis handles scoping for class constructor parameters with decorators. Let me check the semantic crate
Assistant
Let me look at the semantic builder, particularly how it handles scopes
Assistant
Let me look at the
visit_functionin builderAssistant
Let me look at how classes are handled
Assistant
Let me look at the
visit_classmethodAssistant
Let me look at
visit_formal_parametersand `visit_formal_Assistant
Let me look at the
visit_formal_parameterfunctionAssistant
Now I can see the issue. In
visit_formal_parameter, the parameter is bound first, then decorators are visited. But decorators should be evaluated in the enclosing scope before the parameter binding is created. Let me verify this by looking at how class elements/methods are visitedAssistant
Let me look at how class elements are visited
Assistant
Let me check how class elements are visited - maybe there's a generated visitor
Assistant
Let me check the AST visit crate for the generated
Assistant
Let me check how the semantic builder overrides the class element
Assistant
Now I understand the issue. In
visit_formal_parameter, the parameter is bound BEFORE decorators are visited. This means that when@Inject(foo.KEY)is visited,fooresolves to the just-created parameter binding, not the outer one. The fix should be to swap the order: visit decorators first, then bind the parameter.But first, let me verify my understanding by looking at how reference resolution works and whether the scope is already active when visiting parameters
Assistant
Let me look at
resolve_references_for_current_scopeAssistant
Let me look at
walk_up_resolve_referenceAssistant
Let me look at the
walk_up_resolve_referencefunctionAssistant
Now I understand the issue clearly. In
visit_formal_parameter,param.bind(self)is called beforeself.visit_decorators(¶m.decorators). This means when a decorator like@Inject(foo.KEY)referencesfoo, it resolves to the just-created parameter binding instead of the outer variable.The fix is to swap the order: visit decorators before binding the parameter. Let me make this change.
Assistant
Now let me make the fix. I'll swap the order of
param.bind(self)andself.visit_decorators(¶m.decorators)in bothvisit_formal_parameterandvisit_formal_parameter_rest.Assistant
Let me use the replace command with
--fileoptionAssistant
Now let me fix
visit_formal_parameter_restas wellAssistant
Good. Now let me rebuild and test the fix
Assistant
Now let me test with the reproduction case
Assistant
The fix didn't work because references are resolved deferredly - by the time
resolve_references_for_current_scopeis called, all parameters have already been bound. Let me investigate more carefullyAssistant
Let me look at the
walk_method_definitionfunctionAssistant
Let me look at walk_method_definition
Assistant
Let me look at how references are created and their scope IDs are set
Assistant
Let me look at these
Assistant
Let me look at
reference_identifierto understand how scope IDs are assigned toAssistant
Now I understand the core issue. The reference is created with
self.current_scope_idas its scope (line 2644). When visiting decorators insidevisit_formal_parameter, the current scope is the function scope. So decorator references are scoped to the function scope, and they resolve to the parameter binding instead of the outer one.I need a different approach: temporarily change the current scope to the parent scope while visiting parameter decorators. Let me implement this.
Assistant
Now I need to modify the approach. Instead of just swapping, I need to temporarily change the scope while visiting decorators. Let me implement this
Assistant
Now do the same for
visit_formal_parameter_restAssistant
Now let me build and test
Assistant
Let me test with the reproduction case
Assistant
The fix works! Now let me test with more edge cases to make sure nothing is broken
Assistant
Now let me actually think about this more carefully. Let me test the edge case file I created and also run the existing test suite to make sure I didn't break anything.
But first, let me also verify: does the
shadowedcase still report correctly? Let me update the test file:Assistant
Excellent! The fix works correctly:
fooused in@Inject(foo.KEY)→ no warning ✓barused in@Inject(bar.KEY)→ no warning ✓notUsedwith no decorator reference → correctly flagged as unused ✓
Now let me run the existing test suite to make sure nothing is broken
Assistant
That error is unrelated to our changes. Let me run the tests without examples
Assistant
Now let me run the linter tests, specifically for no_unused_vars
Assistant
All 47 no_unused_vars tests pass. Let me run a broader set of tests to ensure nothing is broken
Assistant
Let me run with all features that are needed
Assistant
There are snapshot test failures. Let me check what they are about
Assistant
The snapshot differences are exactly what we'd expect: the decorator reference's
scope_idchanged from 3 (function scope) to 2 (parent/class scope). This is the correct behavior. Let me update the snapshots and verify the test inputs.Assistant
Let me check the parameter-property test
Assistant
The snapshot changes are expected and correct. Let me accept the new snapshots
Assistant
Let me copy the new snapshots over the old ones
Assistant
Let me verify the updated snapshots look correct
Assistant
Let me re-run the tests
Assistant
Let me now run a broader set of tests to ensure nothing else is broken
Assistant
All 61 tests pass. Let me also run the linter tests again
Assistant
Let me also run the oxc_linter unit tests specifically
Assistant
All tests pass. Let me also search for any other places where parameter decorators are visited, to make sure I haven't missed any
Assistant
Good - only
visit_formal_parameterandvisit_formal_parameter_resthandle parameter decorators, and I've fixed both. Let me verify class/method decorators are already handled correctly by looking at the generated walk_method_definition againAssistant
Let me run a quick comprehensive test to ensure nothing is broken, including some more edge cases
Assistant
Let me fix the test
Assistant
Let me check if the
Hclass unused warning is pre-existing or caused by my changeAssistant
The
Hclass unused warning is a pre-existing issue (same behavior before and after my change). My change correctly fixes the reported issue. Let me now run the full test suiteAssistant
Let me also run the linter tests one more time
Assistant
All tests pass. Let me view the diff of my
Assistant
The diff looks clean and correct. Let me do a final verification with the original reproduction case
Assistant
Wait, the binary might not be rebuilt. Let me rebuild and test again
Assistant
The fix works correctly. Let me clean up the temporary files and do a final review.
Assistant
Let me do a final review of the changes
Assistant
Let me also run a broader linter test to make sure nothing else breaks
Assistant
Let me also test with an import case to make sure it works as well
Assistant
Let me verify the snapshots look good without the assertion_line additions
Assistant
Let me check if other snapshot files have the assertion_line too
Assistant
Let me check
Assistant
Let me check if the assertion_line is present in other unmodified snap
Assistant
Let me verify the tests pass one final
Assistant
All tests pass. Let me clean up and submit.
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
Qwen/Qwen3.6-27BCompare side by side →
Qwen/Qwen3.6-27BCompare side by side →
Qwen/Qwen3.6-27BCompare side by side →
Qwen/Qwen3.6-27BCompare side by side →
Qwen/Qwen3.6-27BCompare side by side →
- xiaomi/mimo-v2.5-proCompare side by side →
- xiaomi/mimo-v2.5-proCompare side by side →
- xiaomi/mimo-v2.5-proCompare side by side →
- gpt-5.6-solCompare side by side →
- gpt-5.6-solCompare side by side →
- gpt-5.6-solCompare side by side →
- gpt-5.6-solCompare side by side →