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 first inspect the repository structure and locate the TypeScript scope-analysis and lint-rule tests, then reproduce the reported behavior with the project’s existing test tooling.
- Assistant
rgis unavailable, so I’ll usegrep/findto identify the rule implementation, semantic scope builder, and relevant fixtures. - Assistant
The diagnostic likely reflects an incorrect reference-to-symbol resolution in
oxc_semantic, rather than logic specific tono-unused-vars. I’ll inspect parameter/decorator traversal and existing scope tests before changing anything. - Assistant
The semantic builder explicitly visits a parameter’s decorators before its pattern, but both may still be under the function parameter scope. I’ll inspect scope entry timing and unresolved-reference resolution to determine whether decorator references need a different scope assignment.
- Assistant
Binding occurs before decorator traversal, so the reference resolves to the parameter symbol in the current function scope. However, merely visiting decorators before binding could break duplicate/declaration semantics and later resolution may still see the eventual binding. I’ll inspect reference resolution and function scope structure to implement an explicit enclosing-scope lookup for parameter decorators.
- Assistant
A clean fix is to traverse parameter decorator expressions with the function scope’s parent as
current_scope_id, matching runtime lexical evaluation and the treatment of method decorators. Before doing that, I’ll verify how methods enter function scope and whether scope switching has established patterns/helpers. - Assistant
The reference records its scope at creation, so temporary scope switching is sufficient and robust even though resolution happens later. I’ll inspect scope entry/exit and AST visitor defaults to ensure node scope IDs and decorator nesting remain consistent.
- Assistant
The generated visitor shows whether method decorators are traversed before calling
visit_function; this will confirm the intended enclosing-scope model and delimit the fix to formal-parameter decorators. - Assistant
I’ll read the generated walk order and then reproduce through the linter CLI or a targeted rule test command before editing.
- Assistant
The method decorator is indeed visited in class scope before function scope, while parameter decorators are currently the exception. I’ll use the repository’s
oxlintbinary package to reproduce the exact diagnostic in a temporary file (outside tracked tests). - Assistant
The exact warning reproduces. I’ll add a small semantic-builder helper that visits formal-parameter decorators in the parent of the current function scope, restoring the function scope afterward. This also handles method (not only constructor) parameter decorators and all identifier uses nested in decorator expressions.
- Assistant
The implementation is localized and formatted. I’ll now rerun the exact reproduction plus variants covering a same-named import, a later method parameter, and ordinary default initializers (which must continue resolving to parameter scope). Then I’ll run the relevant linter and semantic test suites.
- Assistant
Compilation exposed two structural issues: helper methods cannot be declared inside the
Visittrait implementation, and unqualifiedVecthere is the standard vector rather than the allocator vector. I’ll move the helper into the preceding inherentSemanticBuilderimpl and accept a slice, which both AST allocator vectors coerce to. - Assistant
visit_decoratorsrequires the allocator vector exactly rather than a slice. I’ll inspect imports and use the fully qualified allocator-vector type to avoid thestd::vec::Vecambiguity. - Assistant
I’ll update only the helper signature to
oxc_allocator::Vec, then compile and rerun both reproductions. - Assistant
The minimal case now has zero diagnostics; the variant confirms the imported
foois treated as used, while unrelated unused declarations still report. The default initializer’sfooalso remains a parameter-scope use (the parameter property is not reported), so the scope switch is correctly limited to decorators. I’ll run focused and broader tests now. - Assistant
Focused linter tests passed (47/47), and the semantic unit groups passed, but the semantic fixture snapshot suite correctly detected that parameter decorator references now carry the enclosing class scope ID instead of the function scope ID. Per the instruction not to modify tests, I’ll remove the generated
.snap.newartifacts and leave those expected snapshot updates untouched. Then I’ll run Clippy and inspect the final diff/status. - Assistant
Clippy reached an unrelated pre-existing
oxc_parserlint (redundant_pub_crate) before checking the target fully; this file itself compiles cleanly undercargo check. I’ll run the full linter library suite and the semantic library tests once more, then verify no temporary/generated files remain.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.