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 me start by understanding the repository structure and the issue. The issue is about
no-unused-varslinter rule producing a false positive when a constructor parameter property shadows an imported identifier used in its own decorator.Let me first explore the repository structure and then reproduce the issue.
The repository seems to be "oxc" (which is probably oxlint - the Rust-based linter). Let me explore.
Assistant
Let me explore the crates directory to find the linter code, specifically the no-unused-vars rule.
Assistant
Let me find the no-unused-vars rule in the oxc_linter crate.
Assistant
Let me look at the no_unused_vars rule implementation.
Assistant
Let me first create a reproduction script and confirm the issue.
Assistant
Let me check if there's an oxlint binary I can use.
Assistant
I've confirmed the issue. Now let me understand the rule implementation to find where to apply the fix.
Assistant
Let me scroll down and see the rest of the module and particularly how it handles variable usage tracking.
Assistant
Let me view the mod.rs file more to understand how it works.
Assistant
Let me look at the
symbol.rsandusage.rsfiles to understand how symbol creation and usage trackingAssistant
Let me continue viewing symbol.rs to understand the key methods like `has_us
Assistant
Now let me look at the
usage.rsfile:Assistant
Let me continue reading usage.rs to understand how references are checked
Assistant
Let me understand the scoping issue better by looking at the AST for the given code. Let me check how the semantic analysis resolves references and the scope hierarchy for decorators
Assistant
Let me explore how semantic analysis handles scoping for decorators. Let me look at the scope tree and reference binding
Assistant
Let me look at the binder.rs to understand how symbols and scopes are created for decorators:
Assistant
Let me look at how the builder creates scopes for classes and decorators:
Assistant
Let me search for how decorators are handled in scope creation and reference resolution:
Assistant
Let me examine the class body handling and formal parameter handling more
Assistant
Let me now look at how formal parameters create their own scope and how decorators are handled for them.
Assistant
Let me see how formal parameters scope is created:
Assistant
Let me look at how visit_function handles this, and also where the scope for the function is created:
Assistant
Let me look at how visit_function creates the scope. Let me see earlier in the visit_function method.
Assistant
Let me understand how reference resolution works. Let me check the
resolve_references_for_current_scopemethodAssistant
Let me look at the scoping module to understand how references are resolved:
Assistant
Let me look at the
walk_up_resolve_referenceandresolve_references_for_current_scopein detailAssistant
Let me check how class decorators are handled compared to formal parameter decorators:
Assistant
Let me check what
FormalParameter::binddoes by looking at the binder:Assistant
Let me look at the FormalParameter binder
Assistant
Let me check what
visit_binding_patternandvisit_binding_identifierdoAssistant
Let me look at
visit_binding_identifier:Assistant
Let me search for
visit_binding_identifierin the whole project:Assistant
Let me look at the scopes_collector implementation which is likely the relevant one for understanding how traversal works
Assistant
Let me find
visit_binding_identifierin the semantic builderAssistant
Let me look at how
visit_binding_patternis implemented in the builder:Assistant
Let me check if visit_binding_pattern is auto-generated by looking at the visit trait:
Assistant
Let me look at the generated visit_binding_pattern:
Assistant
Now I understand the issue. The fix should be in the
visit_formal_parameterandvisit_formal_parameter_restmethods in the semantic builder. The decorators should be visited before the parameter is bound. Let me check if there are any existing tests related to this.Assistant
Let me explore the tests directory for no-unused-vars
Assistant
Let me look at the typescript_eslint tests for related test cases:
Assistant
Let me look at the specific tests related to constructor parameter decorators:
Assistant
Let me look at the namespace-shadowed-by-type-parameter tests:
Assistant
Let me understand how decorators are visited:
Assistant
Let me search for how decorators are visited in the AST visit trait:
Assistant
Let me check how class elements with decorators are handled:
Assistant
Now let me check how class elements / methods handle decorators relative to binding. Let me look at the
visit_class_elementmethod or similar:Assistant
Let me check the generated walk functions for how methods with decorators are handled:
Assistant
Let me check the generated walk for formal parameters:
Assistant
Let me check the generated walk for formal parameter rest:
Assistant
Now I understand the issue. The fix is in
crates/oxc_semantic/src/builder.rsin thevisit_formal_parameterandvisit_formal_parameter_restmethods. Theparam.bind(self)call (which creates the parameter's symbol) is called BEFOREself.visit_decorators(¶m.decorators), which means references in decorators resolve to the parameter's symbol instead of the outer scope.The generated walk function already orders decorators before binding patterns. The semantic builder just needs to bind the parameter symbol AFTER visiting decorators.
Let me apply the fix:
Assistant
Now let me apply the fix to both methods. First, `visit_formal_parameter
Assistant
Now let me fix
visit_formal_parameter_rest:Assistant
Now let me rebuild and test the fix:
Assistant
Let me run without filtering to see if the build was triggered
Assistant
Let me verify the changes are correct:
Assistant
Let me force a rebuild by touching the changed
Assistant
Let me test again:
Assistant
The fix is incomplete. The problem is that even though I moved decorator visiting before parameter binding, the reference resolution happens later (after all parameters are bound) in
resolve_references_for_current_scope. Let me study the checkpoint/resolution mechanism more carefullyAssistant
Let me understand how references are created and assigned a scope:
Assistant
Let me look at
visit_identifier_reference:Assistant
Let me look at the
reference_identifiermethod:Assistant
Let me look at
reference_identifier:Assistant
Let me check the class visiting code to understand the scope hierarchy:
Assistant
I need a different approach. The issue is that reference resolution happens after all parameters are bound. I need to make decorator references resolve against the parent (enclosing) scope, not the function scope. Let me modify the approach to temporarily switch
current_scope_idto the parent scope while visiting decorators.Assistant
Now let me also update
visit_formal_parameter_restwith the same logic:Assistant
Let me verify that
scope_parent_idexists:Assistant
Now let me rebuild and test:
Assistant
Excellent! The fix works! Let me now test with more edge cases:
Assistant
Let me run the test:
Assistant
The main issue is fixed. The constants are now correctly recognized as used (in their decorators). The remaining errors are about unused parameters which is correct behavior.
Let me now run the existing tests to ensure nothing is broken:
Assistant
All 47 no-unused-vars tests pass. Let me also run the semantic tests to make sure nothing is broken:
Assistant
That error is unrelated to my changes (it's in the cfg example). Let me run just the tests without examples:
Assistant
Let me also run a broader set of linter tests to make sure nothing is broken:
Assistant
Let me also check if there are any decorator-related tests in the broader test suite that might be affected:
Assistant
Let me check available tests for semantic:
Assistant
Let me run the integration tests for the linter more broadly:
Assistant
Let me also check if there's a similar issue with class property decorators. Let me look at how class properties handle decorators:
Assistant
Let me check if there's an override for property_definition in the builder:
Assistant
Let me check how class elements are handled:
Assistant
Let me check the generated walk_class_element:
Assistant
Assistant
Let me test the edge case with class static property reference in a parameter decorator:
Assistant
Good, no errors. Let me test with a non-exported class to make sure the decorator reference counts as a usage:
Assistant
Let me verify the original issue description pattern more thoroughly:
Assistant
Let me also test the import case mentioned in the issue:
Assistant
Let me also verify that truly unused variables are still reported:
Assistant
Good - truly unused variables are still correctly reported. Let me also run the full linter test suite once more and also run some broader tests to make sure I haven't broken anything:
Assistant
All tests pass. Let me also validate there are no issues with the broader test suites:
Assistant
Let me verify how
enter_scopeworks to make sure the scope switch doesn't cause issues:Assistant
Let me review the final changes:
Assistant
Let me do one final verification with the exact reproduction case from the issue:
Assistant
Let me check if there are any existing decorator-related tests in the semantic module that might need updating:
Assistant
Let me find semantic tests:
Assistant
Let me run the full linter test suite one final time to make sure everything is clean:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.