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
I’ll first inspect the repository structure and locate the TypeScript scope-analysis and decorator handling code, then reproduce the diagnostic with the project’s available test/CLI workflow before making a focused fix.
- Assistant
rgis unavailable, so I’ll usefind/grep. The likely implementation is inoxc_semantic; I’ll inspect scope-building code and relevant fixtures/tests there and in the linter. - Assistant
The semantic builder explicitly visits formal-parameter decorators, making visit order/scope the prime suspect. I’ll inspect parameter, function, and class traversal plus existing semantic scope tests to determine whether decorators should be visited before entering parameter scope or require an explicit outer-scope reference.
- Assistant
param.bind(self)declares the parameter before its decorator expression is resolved, and all of this occurs inside the function scope. Merely swapping decorator and bind order may not suffice because deferred reference resolution still sees the function-scope symbol. I’ll inspect how function scopes are entered and how references can be assigned/resolved against a specific scope, looking for analogous handling of class decorators or computed keys. - Assistant
The builder resolves all parameter-region references immediately against the function scope after declarations, explaining the false binding. To preserve AST parentage, the cleanest fix may be to visit decorator nodes normally but temporarily record their identifier references in the enclosing scope. I’ll inspect reference creation and unresolved-reference bookkeeping to ensure such a scope switch is safe and to identify any existing helper pattern.
- Assistant
References directly store
current_scope_id, so a temporary scope-ID switch during decorator traversal should work without disturbing AST node parentage. However, parameter decorators apply only to methods/constructors (not arbitrary functions), and the immediate parent scope could vary. I’ll inspect AST structure/traversal and TypeScript semantics/tests, then reproduce via the linter test harness or CLI to validate exact current bindings. - Assistant
I’ll now reproduce the issue with a temporary file using the local
oxlintbinary target. This establishes the baseline without modifying repository tests (as required). - Assistant
The baseline reproduces exactly. I’ll implement a small semantic-builder helper that visits parameter decorators with
current_scope_idtemporarily set to the function scope’s parent, restoring it afterward. This applies generally to constructor and method parameter decorators (including rest parameters), matching their class-evaluation environment while preserving AST parent relationships and ordinary parameter initializer/type resolution. - Assistant
The focused change is in place. I’ll rerun the exact reproduction and additional temporary edge cases: a regular method parameter, a parameter still used in its body, and a decorator referencing another same-named parameter, ensuring decorator references resolve outward while body references remain local. Then I’ll run the affected semantic and
no-unused-varstest suites. - Assistant
Compilation exposed that generated visitor APIs require the allocator-backed AST
Vec, not a slice. I’ll adjust the helper signature to the exact decorator collection type already used by the AST, then repeat verification. - Assistant
I’ll import the allocator vector under an alias to avoid changing existing standard
Vecuses, update the helper, format, and rerun the same command sequence. - Assistant
The reproduction and edge cases now pass, and all 60 semantic integration tests pass. The attempted name filter selected zero linter tests because this project’s rule tests are generated/organized under a separate integration target. I’ll identify and invoke the correct linter test target, while also inspecting whether the scope-switch helper should be made panic-safe or constrained by scope flags.
- Assistant
The linter does contain 1,145 library tests; my prior filter string was simply too specific and did not match Rust’s module path. I’ll run the
no_unused_varssubset correctly, then run clippy/check-style validation and inspect the final diff/status. - Assistant
The targeted linter suite (47 tests), semantic integration suite, all-feature semantic compilation, formatting, and diff checks pass. I’ll perform two final behavioral checks: an actual imported binding and a nested decorator-local parameter that legitimately shadows the outer binding. The latter ensures the scope correction does not falsely count unrelated outer variables as used. I’ll also run the complete
oxc_semantictest package. - Assistant
The import reproduction is fixed, and the nested local-shadow case correctly still reports the truly unused outer
foo, confirming proper nested-scope behavior. Running barecargo test -p oxc_semanticattempted to compile a feature-gated example with nomain; this is an existing target-configuration issue unrelated to the change. I’ll instead run all semantic test targets explicitly (--tests) and Clippy for the changed crate with all features, then do a final clean diff review. - Assistant
All ordinary semantic tests passed, while the fixture snapshot test detected the intentional semantic change: parameter decorator references now have class scope ID 2 instead of function scope ID 3. Per the instruction not to modify tests, I will remove only the generated
.snap.newartifacts and leave existing snapshots untouched; this mismatch is expected because those fixtures encode the buggy scope assignment. I’ll run Clippy separately and verify no artifacts remain. - Assistant
Strict Clippy reached an unrelated pre-existing
clippy::redundant_pub_cratewarning inoxc_parser; it did not identify an issue in the changed file. I’ll run Clippy without globally promoting dependency warnings, then inspect the exact final diff once more before submission.
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 →