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
Let me start by understanding the issue:
-
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 because 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.
Let me first explore the repository structure and then reproduce the issue.
-
Assistant
Let me create a test file to reproduce the issue
Assistant
Now let's run oxlint to confirm the false positive:
Assistant
I've confirmed the false positive. Now let me explore the relevant code.
Assistant
Let me look at the relevant rule files and the semantic analysis code
Assistant
Let me look at the main module and the symbol/usage files to understand the logic. The key thing to understand is how the linter tracks usage of symbols, particularly in decorators.
Assistant
Now let me look at the
usage.rsfile which is the main logic for determining if a symbol is used:Assistant
Let me explore how the semantic analysis handles decorators and parameter shadowing:
Assistant
Let me look at the builder to understand how scopes are created:
Assistant
Let me look at the relevant sections in the builder to understand the scope creation for parameters and their decorators:
Assistant
I found the issue! In
visit_formal_parameter,param.bind(self)is called BEFOREself.visit_decorators(...). This means when the decorator is visited, the parameter symbol is already in scope, causing references inside the decorator to resolve to the parameter instead of the outer binding.Let me verify by looking at what
param.bindAssistant
Let me search for the
bindtrait implementations:Assistant
Let me understand how scopes work for formal parameters - does enter_node create a new
Assistant
Let me look at how formal parameters scoping is handled:
Assistant
Let me look at how function scopes and formal parameters are visited:
Assistant
Now I understand the issue. Let me look at how other similar cases (like class decorators) are handled for reference
Assistant
Let me check how method decorators are handled:
Assistant
Let me search for where decorators on methods are visited:
Assistant
Let me look for method definition visiting and see if there are decorators there
Assistant
Maybe method definitions are handled through a different visitor. Let me search for where decorators on methods are visited in the AST visit code.
Assistant
Let me look at the generated visit code to see how method definitions with decorators are visited:
Assistant
Let me look at the generated visit.rs for MethodDefinition handling:
Assistant
Let me look at the walk_method_definition function
Assistant
Let me check if
visit_method_definitionis overridden in the builderAssistant
Now I have a clear picture. The fix is to swap the order of
param.bind(self)andself.visit_decorators(¶m.decorators)in bothvisit_formal_parameterandvisit_formal_parameter_rest. This way, decorators are visited before the parameter binding exists in the scope, which is correct because parameter decorators are evaluated at class definition time, before parameters exist.Let me implement the fix:
Assistant
Let me fix the tool call:
Assistant
Now I need to swap the order: move
visit_decoratorsbeforeparam.bind(self)in both methods. Let me fixvisit_formal_parameterfirstAssistant
Let me use the correct edit syntax:
Assistant
I lost the
visit_spancall. Let me fix that:Assistant
Now let me fix
visit_formal_parameter_restthe same wayAssistant
Both functions are now fixed. Let me rebuild and test:
Assistant
The fix isn't working yet. Let me investigate how references are resolved to understand the root cause better
Assistant
Let me look at
visit_identifier_reference:Assistant
Let me look at
reference_identifier:Assistant
Let me look at how references are resolved in the scoping module
Assistant
Let me look at how references are resolved:
Assistant
Let me look at
resolve_references_for_current_scopeand `declare_reAssistant
Let me look at `resolve_references_for_current_scope
Assistant
Let me look at
walk_up_resolve_referenceAssistant
Let me look at `walk_up_resolve_re
Assistant
Let me create a more comprehensive test to understand the issue
Assistant
Let me look at how
visit_decoratorsworksAssistant
Let me check if the builder overrides
visit_decoratorsAssistant
Let me look at the generated
visit_decoratorsandwalk_decoratorAssistant
Let me look at
walk_decorator:Assistant
Let me look at
walk_decoratorAssistant
I see - my fix didn't work because the reference resolution happens later, after all params (including the current one) are bound. I need to resolve references from decorators immediately after visiting them and before binding the parameter.
Let me update the fix
Assistant
Now update
visit_formal_parameter_restsimilarly:Assistant
Now let me rebuild and test:
Assistant
The fix works! Let me now test edge cases
Assistant
Let me fix the test and run
Assistant
Let me also test that unused variables are still correctly reported
Assistant
Good, unused variables are still correctly reported. Now let me run the existing tests for the affected modules
Assistant
That error is in an example file, not related to my changes. Let me run the lib tests only
Assistant
All semantic tests pass. Now let me run the no-unused-vars tests:
Assistant
All no-unused-vars tests pass (47 tests). Let me also run the full linter test suite to make sure nothing else is
Assistant
All 1144 tests pass. Let me verify my changes with a
Assistant
The diff looks good. Let me also run the broader test suite to ensure nothing is broken:
Assistant
All tests pass. Let me also run the integration test:
Assistant
Let me also run the full semantic test suite, including the snapshot tests that test reference resolution:
Assistant
The only error is in an example file (pre-existing, unrelated). Let me run the lib tests and snapshot tests:
Assistant
All tests pass. Let me also check for any snapshot tests that might be affected:
Assistant
Let me check the semantic snapshots:
Assistant
Let me check for any decorator-related integration tests in semantic:
Assistant
Let me check if there are decorator-related tests in symbols or scopes:
Assistant
The fix looks good. Let me do one final comprehensive test:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.