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.
Assistant
Reproduced. Now let's implement the fix.
Assistant
Need to add closing brace for the function before
pub(crate) fn add_redeclare_variable.Assistant
Now let's update the two visit_formal_parameter(_rest) functions to call this new helper instead of visit_decorators directly.
Assistant
Now let's check that
Decoratortype andoxc_allocator::Vecare already imported/available in this file.Assistant
Good,
Decoratorcomes fromoxc_ast::ast::*.oxc_allocator::Vecfully qualified path should work fine even without explicit import since we use full path. Now let's build.Assistant
Compiles. Now let's run the reproduction again.
Assistant
The false positive is fixed. Now let's test edge cases: decorator that legitimately references another parameter's name that's NOT shadowed, decorator using a preceding param value (should now fail to resolve - but that was likely already invalid in TS/JS semantics anyway), function-scope var used in decorator, arrow function params, class getter/setter, and the sibling issue #20670 (type param shadowed by function arg).
Assistant
That's expected (function f unused, unrelated to our fix - it's a top-level unused function). The default parameter case works fine (no complaints about x). Let's remove that unrelated one and check the import case doesn't break (bar import unused since ./bar doesn't exist, but that's fine for unused-var purposes). Let's check no other warnings appeared for our shadowing cases - none did. Good.
Now let's test the case where a parameter decorator references a genuinely undeclared name (should just be unresolved, not crash):
Assistant
Now let's run the existing test suites for oxc_semantic and oxc_linter to make sure nothing broke.
Assistant
That example error is unrelated to our change (pre-existing, feature-gated code). Let's verify it exists on a clean checkout too.
Assistant
Confirmed pre-existing issue unrelated to our change. Now let's run the linter test suite (specifically no-unused-vars tests) and semantic snapshot tests.
Assistant
All pass. Now let's run the broader oxc_semantic and oxc_linter full test suites, plus check the parser/semantic conformance test suite (test262/typescript conformance) if present, to be safe.
Assistant
All linter unit tests pass. Now let's also check semantic conformance tests that might exercise scope resolution broadly (e.g., test262 conformance suite in tasks/ or a dedicated semantic conformance test crate).
Assistant
Same pre-existing unrelated error. Let's clippy just the lib target.
Assistant
Clean. Now let's view final diff.
Assistant
Good — the outer
fooconst is correctly resolved as used (no warning on it), and the unused-parameter warning is unrelated and correct (the rest paramfoogenuinely isn't used in the body). This confirms the fix works forFormalParameterResttoo.Now let's clean up temp files and do a final full check.
Assistant
All tests pass. Let's view final diff once more for review.
Assistant
Now let's go through the checklist explicitly.
Checklist review:
- Reproduced the reported error: Yes — I created
/tmp/repro/test.tsmatching the minimal reproduction from the issue and confirmedoxlintproduced the false-positiveno-unused-varswarning onfoobefore my fix. - Confirmed the fix resolves the error: Yes — after the fix, running
oxlinton the same file producesFound 0 warnings and 0 errors. - Thought about edge cases: Yes — I tested: multiple shadowed params in the same constructor, decorator referencing an import, decorator referencing an undeclared/global identifier (no crash, still unresolved as expected), method (non-constructor) parameter decorators, default parameter values still correctly referencing earlier parameters (unaffected regression check), and rest-parameter decorators (). All behaved correctly — the outer binding used in the decorator is treated as "used", while genuinely-unused parameters are still flagged appropriately.
- Reproduced the reported error: Yes — I created
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.