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 locating the no-unused-vars implementation to reproduce this false positive.
- Assistant
Next I'll check the project docs and search for the no-unused-vars rule and related scope handling.
- Assistant
Let me find the no-unused-vars rule and semantic analysis related to parameter decorators.
- Assistant
This looks like a semantic/scoping issue around decorator resolution. I'll inspect symbol references and parameter decorator binding.
- Assistant
Let me look at how decorators are handled in the semantic builder and dig into tests related to this issue.
- Assistant
Let me look at the builder.rs around those lines for formal parameters and decorators.
- Assistant
The parameter is bound before its decorators are visited — that's likely the bug. Checking function/method scope entry next.
- Assistant
Let me look at visit_function fully.
- Assistant
I'll look for existing tests and any related handling of decorator scope or outer bindings.
- Assistant
I'll inspect sibling issues and existing decorator fixtures to mirror the correct fix.
- Assistant
I'll inspect that related commit and how references resolve for type params and params.
- Assistant
I'll reproduce the issue with a quick test file and the linter example.
- Assistant
Need to confirm whether the example enables
no-unused-varsand dig into how references are counted. - Assistant
Reproduced. Next I'll inspect how unresolved references and identifier resolution work so we can fix decorator scoping correctly.
- Assistant
Let me look at the resolve_references_for_current_scope and visit_decorator, and how leave_scope resolves things.
- Assistant
Checking how method and property decorators are visited relative to scope entry.
- Assistant
There's no custom override for visit_method_definition - so it uses the default which visits decorators before the function. Let me check the default.
- Assistant
Let me look at the walk function for formal_parameter
- Assistant
Let me read those sections.
- Assistant
I'll search the codebase for prior art on temporarily switching scope while visiting decorators.
- Assistant
I'll inspect semantic fixtures and check how Parameter initializers vs decorators are ordered in symbols snapshots.
- Assistant
I'll read how formal parameters are bound in the binder.
- Assistant
I'll check how type-parameter shadowing is handled and whether related tests already exist.
- Assistant
Checking whether nodes store scope IDs and whether changing scope for decorators could break that.
- Assistant
Checking TypeScript emit for class expression vs. constructor parameter decorator scoping.
- Assistant
I'll check what symbols live in the clas scope and whether one hop to the parent is enough.
- Assistant
I'll checking no-unused-vars tests structure and how Tester works so I can validate with a temporary test script.
- Assistant
I'll impl parent-scope resolution for param decorators and apply it on FormalParameter and FormalParameterRest.
- Assistant
I'll extract a small helper and update both formal-parameter visit methods.
- Assistant
Adding the
visit_parameter_decoratorshelper next to the other private helpers. - Assistant
I need to add the helper. It's called from Visit impl, so it can be on SemanticBuilder.
- Assistant
I'll check imports and how other methods type decorator vectors to keep this consistent.
- Assistant
Looking at Visit trait for visit_decorators signature.
- Assistant
The Visit trait uses
Vecfrom somewhere - probably through re-export. Let me check oxc_ast_visit imports. - Assistant
Building and verifying the fix against the reproduction case.
- Assistant
Semantic built cleanly — rebuilding oxlint and re-running the reproduction.
- Assistant
Main case is fixed. I'll verify edge cases next: previous-param shadowing, imports, rest params, and initializers that should still see parameters.
- Assistant
Edge cases look good. Running the existing no-unused-vars and semantic tests next.
- Assistant
All no_unused_vars tests passed. Checking semantic fixtures next.
- Assistant
There's an unrelated example issue. Let me run just the lib and integration tests.
- Assistant
Snapshots correctly reflect parent-scope decorator references; I'll update those fixtures.
- Assistant
I'll install cargo-insta or update snapshots via INSTA_UPDATE.
- Assistant
Good. Let me also run integration tests for semantic.
- Assistant
I'll double-check symbol resolution points at the outer binding with a temporary script.
- Assistant
Adjusting the temporary check example to match the current references API.
- Assistant
Cleanup: remove the temporary example and format the changed code.
- Assistant
Simplifying the helper signature to match crate style.
- Assistant
Is ArenaVec used elsewhere? Maybe just keep fully qualified path without import to minimize change. Or use the ArenaVec.
- Assistant
Need to use goto first or --file with the replace.
- Assistant
Need to goto the line first.
- Assistant
I'll drop the ArenaVec import and keep the fully-qualified path to minimize the diff.
- Assistant
Need to scroll to top.
- Assistant
I'll switch the helper signature over to
ArenaVecso the import is used. - Assistant
Final verification of the original case and a clean git status.
- Assistant
Removing leftover
.snap.newfiles from the snapshot update.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.