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
Let's inspect the reproduction file and prepare test while build runs.
Assistant
Reproduced. Now let's fix the ordering in
visit_formal_parameterandvisit_formal_parameter_rest.Assistant
Let's view the full context of these lines to edit precisely.
Assistant
Now let's modify this function to accept an optional skip scope, and add the new helper method for decorator resolution.
Assistant
Now update the other call site in
resolve_references_for_current_scope, and add the new helper method for decorator resolution.Assistant
Good,
ScopeIdis imported. Now let's updatevisit_formal_parameterandvisit_formal_parameter_rest.Assistant
Now let's check that
DecoratorandVectypes are already imported in builder.rs.Assistant
ast::*is glob-imported soDecoratortype is available. Let's checkVectype (oxc_allocator::Vec) is used elsewhere.Assistant
Need to import
oxc_allocator::Vecin builder.rs, or reuse fully qualified path. Let's check current imports.Assistant
Now let's build to check compile errors.
Assistant
There's another
impl<'a> SemanticBuilder<'a>block at 2703. Let's move the helper there instead.Assistant
Let's remove the function from its current location and add it into the inherent impl block at line 2703.
Assistant
Now let's also remove the doc-comment duplicate I left inline before (the one attached above
visit_formal_parametercall within trait impl was already just a call, no doc needed there). Let's check and compile.Assistant
Compiles cleanly. Let's rebuild oxlint and test the reproduction.
Assistant
The false positive is gone. Now let's test more edge cases to ensure correctness.
Assistant
All cases behave correctly: case 1 (fixed bug), case 2 (still detects genuinely unused var), case 3 (unused param still flagged), case 4 (sibling shadow correctly resolved, no warnings), case 5 (import correctly resolved, no warnings). Let's double check with
--tsx/regular ESM import resolution and also verify no double-counting of references (e.g., reference count forfooshould be exactly 1).Assistant
Confirmed pre-existing issue unrelated to my change. Let's run tests using
--lib --teststo skip examples.Assistant
All tests pass. Now let's run the linter's no_unused_vars tests and broader linter tests.
Assistant
All 47 relevant tests pass. Now let's run the broader oxc_linter test suite (might be big, but let's try in background with timeout awareness) to be safe against regressions in other rules relying on semantic scope resolution.
Assistant
Confirmed: these 4 fixer test failures are pre-existing and unrelated to my change (likely due to
debug_assert!not firing in release mode). Let's restore my changes.Assistant
Good. My changes are restored. Now let's view the final diff for review.
Assistant
There's a leftover stray doc comment before
visit_ts_module_declaration. Let's remove it since the real function now lives in the other impl block.Assistant
Good, compiles cleanly. Let's view the final diff again to confirm cleanliness.
Assistant
This is clean now. Let's also run clippy to check for lint issues introduced.
Assistant
No clippy warnings. Now let's clean up temp test files and re-verify final oxlint build works with the reproduction script one more time (already confirmed). Let's remove /tmp test files (not part of repo, fine to leave, but let's tidy).
Assistant
Only the intended file is modified. Let's do a final full re-verification build and re-test the exact reproduction from the issue.
Assistant
The fix works correctly. Let's clean up and do a final check of the whole diff.
Assistant
Summary
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.