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 a false positive in
no-unused-varsrule when a TypeScript constructor parameter property shadows an imported identifier used in its own decorator.Let me first explore the repository structure and find the relevant code.
Assistant
Let me first create a reproduction script to confirm
Assistant
Let me find the oxlint binary and run it on the test file.
Assistant
There's a prebuilt binary. Let me run it on the test file.
Assistant
The oxlint binary is a node script. Let me check what it is.
Assistant
Let me check for a Rust binary or build from source.
Assistant
Let me check for a pre-built Rust binary in the cargo target directory.
Assistant
Let me build oxlint from source.
Assistant
Good, I can build from source. But first, let me understand the codebase structure and find the relevant files.
Assistant
Let me explore the no-unused-vars rule implementation.
Assistant
Let me look at the usage tracking and symbol code.
Assistant
Let me look at the symbol.rs to understand how symbols are tracked.
Assistant
Let me look at the usage.rs more thoroughly, particularly around how references are resolved and how decorators are handled.
Assistant
Let me read the full usage.rs file to understand the usage detection logic.
Assistant
Let me read the usage.rs in sections.
Assistant
Now let me look at the main mod.rs to understand the full symbol processing flow.
Assistant
The issue is likely in the semantic/scope analysis layer, not the no-unused-vars rule itself. Let me look for how decorators and formal parameters are handled in scope
Assistant
The binder is where symbols and scopes are created. Let me look at it, especially for how parameters with decorators are bound
Assistant
Let me look at the scoping.rs and the builder for how scopes and references are resolved.
Assistant
Let me look at how the binder processes formal parameters and decorators.
Assistant
Let me look at the binder more closely around parameter binding and decorators
Assistant
Let me look at the builder.rs to understand how AST nodes are visited, especially decorators and parameters.
Assistant
Let me look at the relevant sections around formal parameters in builder.rs
Assistant
Now I understand the issue. In
visit_formal_parameter, the parameter is bound (param.bind(self)) BEFORE the decorators are visited (self.visit_decorators). This causes the decorator to see the parameter binding, which shadows the outer binding. The fix is to visit decorators before binding the parameter.Let me verify this understanding and look at the
visit_decoratorsfunction.Assistant
Let me search more broadly for visit_decorators to find where it's
Assistant
Let me find where
visit_decoratorsis defined - it might be autoAssistant
Let me look at the
visit_binding_patternfunction to understand if it requires the parameter to be bound first.Assistant
Let me search for visit_binding_pattern definition.
Assistant
Let me look at how class decorators are visited vs class binding to confirm the pattern.
Assistant
Now I understand the issue clearly. The problem is in
visit_formal_parameterandvisit_formal_parameter_restwhereparam.bind(self)is called BEFOREself.visit_decorators(¶m.decorators). This means the parameter binding is created before decorators are visited, causing references in decorators to resolve to the parameter instead of the outer binding.For TypeScript parameter decorators, the decorator expression is evaluated in the enclosing class scope BEFORE the parameter binding exists. So decorators should be visited before the parameter is bound.
Let me make the fix:
Assistant
Now let me apply the fix - moving
param.bind(self)to after the decorator visit:Assistant
Let me open the file first.
Assistant
Let me try directly with the open command.
Assistant
Let me use the edit command with the --file option.
Assistant
Now let me fix the
visit_formal_parameter_restfunction similarly:Assistant
Now let me check if there's a similar pattern in the scopes_collector.rs in oxc_traverse that also needs fixing.
Assistant
Let me check the scopes_collector.rs version to see if it has the same pattern.
Assistant
Good, the scopes_collector already visits decorators before binding pattern. Let me check the minifier version too.
Assistant
Both the scopes_collector.rs files already have the correct order. Only the builder.rs in oxc_semantic needed fixing. Now let me build and test.
Assistant
Build succeeded! Now let me run the test to verify the fix works
Assistant
Let me find the built binary.
Assistant
Let me build the oxlint binary crate.
Assistant
Let me build the oxlint binary
Assistant
The full binary build timed out. Let me run the existing tests instead to verify.
Assistant
Semantic tests pass. Now let me run the no-unused-vars tests
Assistant
All 47 no-unused-vars tests pass. Let me also run the full semantic test suite and the full linter test suite related to parameter/decorator handling.
Assistant
Let me run only the unit tests excluding examples.
Assistant
Let me run the full linter test suite (unit tests only).
Assistant
All 1144 tests pass. Let me also check if there are any other places that need the same fix, like the auto-generated visitors.
Assistant
Let me check the auto-generated visit.rs for formal_parameter.
Assistant
Let me find and check
walk_formal_parameter.Assistant
Let me check the walk functions.
Assistant
The walk functions in the generated code already have the correct order. The fix is only needed in builder.rs where the semantic builder overrides these methods.
Let me also check visit_mut.rs for completeness.
Assistant
Let me check the existing no-unused-vars tests to see how they test decorator-related scenarios.
Assistant
Let me look at the existing constructor parameter decorator test to understand the pattern.
Assistant
Let me understand the test structure.
Assistant
Let me search the tests for the specific shadowing scenario.
Assistant
Let me look at the shadowing-related tests around line 1412.
Assistant
Let me try a faster build - only the changed crate.
Assistant
Let me check if the linter already has the modifications compiled and try building oxlint.
Assistant
Build succeeded. Let me find and test the oxlint binary.
Assistant
Now let me run oxlint on the reproduction test case
Assistant
The false positive is still there! My fix didn't work. Let me investigate more deeply. The issue might be more subtle than just the binding order.
Assistant
Let me look at how functions and their parameters set up scopes.
Assistant
Let me look at the
visit_functionimplementation.Assistant
Let me debug the scope analysis more carefully. Let me check if the reference is correctly resolved by dumping symbols.
Assistant
Let me test both cases.
Assistant
The issue is that when the parameter name shadows the outer name, the reference in the decorator is being incorrectly resolved to the parameter. My fix of swapping bind/visit order wasn't sufficient. Let me investigate how scope resolution works.
Assistant
Let me look at how references are resolved in the builder.
Assistant
Let me look at the reference resolution flow.
Assistant
Let me look at how identifier references are handled.
Assistant
Let me look at the reference handling.
Assistant
Let me check when
resolve_references_for_current_scopeis called.Assistant
Let me look at lines around 1915 to see when resolution happens in visit_function.
Assistant
The issue is deeper - the reference resolution happens AFTER all parameters are bound, so the decorator reference gets resolved to the parameter. Let me look at how class body and constructor visiting works.
Assistant
Let me look at
visit_class_bodyand around it.Assistant
Let me look at how
declare_referenceworks.Assistant
Let me look at
declare_reference.Assistant
I think the real fix is to use the parent scope when visiting parameter decorators. Let me look at how
current_scope_idis used.Assistant
Let me look at how
enter_scopeandleave_scopework.Assistant
Now I understand the real fix. I need to temporarily change the scope to the parent scope when visiting parameter decorators. This way, decorator references will be resolved in the correct scope (class scope), not the function scope where the parameter shadows things.
Let me restore my previous change and implement the correct
Assistant
Let me check how to get the parent scope ID
Assistant
Now let me implement the correct fix - temporarily switching to the parent scope when visiting parameter decorators.
Assistant
Let me verify that
self.scopingis accessible in this context.Assistant
Let me verify
scope_parent_idis accessible.Assistant
Good,
scope_parent_idis public. Let me rebuild and test.Assistant
Build succeeded. Let me test with the reproduction case.
Assistant
The false positive is gone! Let me test more edge cases.
Assistant
Let me test more edge cases.
Assistant
Let me test that the parameter still correctly shadows the outer binding inside the function body.
Assistant
Let me run the full test suite to make sure nothing is broken.
Assistant
Let me run the linter tests.
Assistant
All tests pass. Let me also run the parser and other related crate tests.
Assistant
Let me check the full workspace compiles.
Assistant
Let me verify arrow function and accessor cases work correctly.
Assistant
Let me check if there are other decorator visit sites that have similar issues - class decorators, method decorators, property decorators, etc
Assistant
The fix looks correct. Let me run the broader test suite to make sure nothing is broken.
Assistant
All tests pass. Let me verify the final state of my changes.
Assistant
The changes are clean and focused. Let me also run a broader set of tests to be thorough.
Assistant
Let me verify the linter tests also ran.
Assistant
All tests pass. Let me do one final verification of the reproduction case.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.