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?
codeoxlint test.tsWhat does your
.oxlintrc.json(oroxlint.config.ts) config file look like?jsonc// 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
tsconst foo = { KEY: 'token' }; declare function Inject(token: unknown): ParameterDecorator; export class C { constructor(@Inject(foo.KEY) private readonly foo: number) {} }Actual
code! 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 + does not report this — its scope analyser correctly counts the decorator expression as a use of the outer binding.
- Assistant
I'll start by exploring the repository structure and reproducing the reported
no-unused-varsfalse positive. - Assistant
I'll look at the project docs and locate the
no-unused-varsrule implementation. - Assistant
Next I'll inspect how scopes handle decorator references and related shadowing bugs.
- Assistant
The binding order around formal parameters and their decorators looks like the smoking gun — I'll read that path next.
- Assistant
Confirming visit order: bind_param runs before decorators — I’ll check related function/method visit order and the #20670 sibling fix.
- Assistant
Let me look at how class visit works - decorators are visited outside class scope. And for function parameters. Also look at binder for FormalParameter - when is the parameter bound relative to functi...
- Assistant
Next I’ll find existing decorator-scope tests and how references resolve under type-parameter shadowing.
- Assistant
Let me check git history for related fixes and also look at how generated visit does formal parameter, and convalescence tests for decorators.
- Assistant
There are decorator fixtures — I’ll inspect those and the reference snapshots next.
- Assistant
Let me look at the snapshots and also try to reproduce the issue.
- Assistant
I’ll confirm class decorator scope handling next, since parameter decorators may need the same treatment.
- Assistant
Class decorators are visited outside class scope. Method/property/parameter decorators are inside class (and method) scope based on when visit happens.
- Assistant
So method definitions use the default visitor which visits decorators first before the function. Let me check the generated visitor order.
- Assistant
Let me look at those walkers.
- Assistant
The default ast visitor already walks decorators first — the semantic builder override binds first, which is likely the bug. Checking the binder next.
- Assistant
I’m checking whether decorator resolution should exclude all parameters, not only the current one.
- Assistant
I need to understand when identifier references get resolved during parameter visits.
- Assistant
Let me read the resolve_references and visit_identifier_reference code carefully.
- Assistant
Let me continue reading resolve_references_for_current_scope and the identifier reference visit.
- Assistant
I'll look for existing temporary-scope / leave-scope patterns and how parameter property symbols are handled.
- Assistant
I'll check BindingIdentifier.bind to confirm parameters only get bound via FormalParameter.bind.
- Assistant
I'll check how nodes store scope and whether temporarily swapping
current_scope_idis safe. - Assistant
Let me look at create_ast_node and Reference::new - scopes stored on nodes and refs.
- Assistant
Let me read create_ast_node and reference_identifier
- Assistant
I'll reproduce the false positive, then file fixture testing.
- Assistant
I'll build and run the linter against the reproduction so we can watch the diagnostic change.
- Assistant
I'll inspect existing unused-vars class/decorator tests so the fix can land with matching coverage.
- Assistant
Implementing the fix: visit parameter decorators in the enclosing (class) scope so they don't resolve to parameter bindings.
- Assistant
I truncated
visit_formal_parameter_rest— restoring the rest of that function and adding the helper. - Assistant
I'll check
visit_decorators's signature and fix the blank line before the next function. - Assistant
Updating the helper to take
oxc_allocator::Vecand clean up spacing. - Assistant
Checking imports and whether we should use a type already in scope.
- Assistant
Verifying with a temporary script rather than adding repo tests.
- Assistant
Let me look at semantic example
- Assistant
Compiling and running the semantic example against the reproduction.
- Assistant
The helper can't live on the
Visitimpl — moving it ontoSemanticBuilder. - Assistant
I'll move the helper before the closing of SemanticBuilder impl at line 659.
- Assistant
Now remove the duplicate from Visit impl.
- Assistant
Cleaning up the corrupted section next.
- Assistant
Confirming
visit_formal_parameterlooks right, then rebuilding. - Assistant
Rebuilding and verifying reference resolution with the semantic example.
- Assistant
Scope resolution looks correct. Checking the linter next with the unused-vars rule.
- Assistant
All unused-vars tests passed. Running semantic decorator fixtures and a temporary oxlint check.
- Assistant
Decorator edge cases resolve correctly. Running semantic fixture tests next.
- Assistant
Hmm 0 tests - maybe the test name doesn't match. Let me check main.rs
- Assistant
The main distill uses insta on fixtures. Let me run it fully and see if snapshots fail.
- Assistant
Snapshots match the intended scope change. Building oxlint and running it on the reproduction.
- Assistant
The false positive on
foois gone. Checking the minimal reproduction in isolation: - Assistant
Fix verified. Formatting the change and running a few more related tests.
- Assistant
Integration tests passed. Checking the final diff and running cargo fmt.
- Assistant
Removing temporary snapshot artifacts so they aren't submitted.
- Assistant
gata ready to submit — verifying a clean status first.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.