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
Looking at this issue, I need to understand how oxlint's scope analyzer handles decorator expressions on parameters. The problem is that when a parameter has a decorator that references an outer binding with the same name as the parameter property, the scope analyzer incorrectly thinks the outer binding is unused.
Let me start by exploring the repository structure.
Assistant
Let me explore the crate structure to understand where the scope analyzer is.
Assistant
This is the oxc repository. Let me look at the crates directory to find the linter and scope analyzer.
Assistant
The issue is about the
no-unused-varsrule in oxlint, which is inoxc_linter. The scope analyzer is likely inoxc_semantic. Let me explore these crates.Assistant
Let me search for the no-unused-vars rule.
Assistant
The no-unused-vars rule is in a directory. Let me explore it.
Assistant
Let me also check the semantic analysis related to decorators. The issue is about scope analysis - when a decorator on a parameter references an outer binding with the same name as the parameter, the outer binding should be marked as used.
Assistant
Let me first reproduce the issue to confirm it exists.
Assistant
Now let me build and run oxlint on this file to reproduce the issue:
Assistant
Let me check if there's already a built binary or try a debug build:
Assistant
Let me check what's in the bin folder - it might be an executable that I can use.
Assistant
Let me check what that bin/oxlint file is - it might be a shell script or an executable wrapper.
Assistant
It's a Node wrapper. Let me check if there's a pre-built binary or if we need to build from source.
Assistant
Let me start the build but also study the code in parallel. Let me look at the scope analysis for decorators:
Assistant
Let me wait a moment for the build to start and check the output.
Assistant
Good, oxc_semantic built successfully. Now let me explore how decorators on parameters are handled in scope analysis.
Assistant
The issue is about scope analysis for decorators on constructor parameters. When a parameter has a decorator that references an outer variable with the same name as the parameter property, the scope analyzer doesn't properly count the usage in the decorator.
Let me search more specifically for how parameter decorators are handled in the scope analysis.
Assistant
Let me understand the flow: The issue is about when a decorator on a constructor parameter references an outer binding with the same name as the parameter property. The scope analyzer should record the reference to the outer binding but doesn't.
Let me look at these lines more closely and understand how scopes work for decorators.
Assistant
Let me explore the scope analysis more carefully. The issue is that when we have:
const foo = { KEY: 'token' }; export class C { constructor(@Inject(foo.KEY) private readonly foo: number) {} }The decorator
@Inject(foo.KEY)referencesfoo, which is an outer binding. But the parameter also declares a local bindingfoo. The scope analyzer should properly:- Create a scope for the parameter's decorator
- Record that
fooin the decorator is a reference to the outerfoo, not the parameterfoo
Let me look at how scopes are created for parameters and their decorators.
Assistant
The issue is that the decorator on a parameter is visited BEFORE the parameter binding is created (via
param.bind(self)). Let me see the order of operations:fn visit_formal_parameter(&mut self, param: &FormalParameter<'a>) { let kind = AstKind::FormalParameter(self.alloc(param)); self.enter_node(kind); param.bind(self); // <-- Binding is created here self.visit_span(¶m.span); self.visit_decorators(¶m.decorators); // <-- Decorators are visited AFTER binding self.visit_binding_pattern(¶m.pattern); ... }Assistant
Let me look at how the binder works for formal parameters.
Assistant
Assistant
Let me use proper tool call format.
Assistant
Now I understand the issue. Let me look at the relevant code in the semantic builder:
Assistant
I've analyzed the issue. The problem is in
/oxc/crates/oxc_semantic/src/builder.rs. In bothvisit_formal_parameterandvisit_formal_parameter_rest, the parameter binding is created BEFORE visiting decorators. This means when a decorator expression references an identifier with the same name as the parameter, it incorrectly resolves to the parameter binding instead of the outer binding.The fix is simple: swap the order of
param.bind(self)andself.visit_decorators(¶m.decorators)so that decorators are visited BEFORE the parameter binding is created.Let me implement the fix:
Assistant
Now I understand the issue clearly. The problem is in
/oxc/crates/oxc_semantic/src/builder.rs. In bothvisit_formal_parameterandvisit_formal_parameter_rest, the parameter binding is created viaparam.bind(self)BEFORE visiting decorators. This means when a decorator expression references an identifier with the same name as the parameter, it incorrectly resolves to the parameter binding instead of the outer binding.According to TypeScript semantics, decorator expressions on parameters are evaluated at class-decorator time in the enclosing scope, before parameter bindings exist. So
fooin@Inject(foo.KEY)should resolve to the outerfoo, not the parameterfoo.The fix: move
visit_decoratorsto be called BEFOREparam.bind().Assistant
The edit command failed because I didn't follow the proper format. Let me try again with the correct format.
Assistant
Let me check the current state and make the proper edit.
Assistant
Now I need to properly fix both
visit_formal_parameterandvisit_formal_parameter_restby movingvisit_decoratorsbeforeparam.bind. Let me do this with sed:Assistant
I need to properly fix the file. Let me use a Python script to do the swap cleanly.
Assistant
Let me carefully swap the lines. I need to move
visit_decoratorsbeforeparam.bindandvisit_span.Assistant
The first swap worked. Now let me do the same for
visit_formal_parameter_rest:Assistant
Good, the line numbers are still the same. Let me check the current state of
visit_formal_parameter_rest:Assistant
Now let me apply the same swap to
visit_formal_parameter_rest:Assistant
Both functions have been fixed. Now let me build oxc_semantic to verify the code compiles:
Assistant
The code compiles. Now let me build oxlint to test the fix. First, let me check if there's a faster way to test:
Assistant
The build succeeded. Now let me find the oxlint binary and test the fix:
Assistant
Now let me test the fix with the reproduction case:
Assistant
The fix didn't resolve the issue. Let me think more deeply about this. The problem might be more subtle - it's not just about the order of visiting, but about how scopes are structured and how references are resolved.
Let me check how the scope analyzer works and how references are recorded:
Assistant
Assistant
I need to look at how references store their scope information and how resolution works. Let me check the reference module.
Assistant
Let me look at the scoping.rs to understand how references store scope information.
Assistant
Let me look at how the Reference struct stores scope information and how the resolution works.
Assistant
Let me see the full Reference struct to understand how scope_id is stored:
Assistant
Assistant
Let me search for how references are created when visiting identifiers:
Assistant
So the issue is more nuanced than I initially thought. Let me understand the reference resolution process:
- When visiting an identifier reference, a Reference is created with the current scope_id
- Later, resolve_all_references walks up from the reference's scope_id to find the binding
The problem is:
- The decorator expression
foo.KEYis visited in the function scope - A reference
foois created with scope_id = function scope - The parameter binding
foois also in the function scope - Resolution finds the parameter binding
The fix needs to make the reference's scope_id be the parent of the function scope (where the outer
foois).Now, how can we change the scope when visiting decorators? One approach:
Before visiting decorators on a parameter, temporarily "move" the current scope to the parent scope. Then after visiting, restore the original scope.
Let me look at how scope tracking works:
<tool_call> <function=bash_terminal> <parameter=command> grep -n "current_scope_id" /oxc/crates/oxc_semantic/src/builder.rs | head -20 </tool_call>
Assistant
Let me look at how references are created in the builder:
Assistant
Now I understand the issue better. The key insight is:
- When
visit_identifier_referenceis called, it creates a reference withself.current_scope_id(line 2644) - The
current_scope_idis used to track which scope we're currently in - When we visit a decorator on a parameter, the current scope is the function scope
- The reference created for
fooin the decorator has scope_id = function scope - Later, resolution walks up from function scope and finds the parameter binding in the same scope
The fix is: when visiting decorator expressions on a parameter, we need to temporarily change
self.current_scope_idto the parent scope (the scope containing the function). This way, the reference will be created with the parent scope_id, and resolution will find the outer binding.Let me look at how to get the parent scope:
- When
Assistant
I now understand the issue. The problem is not just the order of operations, but that the reference created in the decorator expression uses the function scope as its
scope_id. When resolving, it finds the parameter binding in the same scope instead of walking up to find the outer binding.The fix needs to change the current scope to the parent scope when visiting decorator expressions on parameters. Let me look at the current code and implement this properly:
Assistant
Let me understand how scope_parent_id works and then implement a fix that temporarily uses the parent scope when visiting parameter decorators:
Assistant
Looking at the output, the fix I made (reordering
visit_decoratorsbeforeparam.bind) didn't solve the issue because the real problem is more subtle. The issue is that when creating a reference forfooin the decorator, the reference is created with the function/method scope as itsscope_id. When resolving, it finds the parameter binding in the same scope instead of walking up to the outer scope.The fix needs to temporarily change
current_scope_idto the parent scope when visiting decorator expressions on parameters. This way, references in decorators will be created with the parent scope_id, and resolution will correctly find the outer binding.Assistant
I now understand the issue better. The problem is that when visiting the decorator expression, the reference is created with
current_scope_id(which is the function/method scope). The parameter binding is also in this scope. So when resolving, the reference finds the parameter binding in the same scope instead of walking up to find the outer binding.The fix needs to temporarily change
current_scope_idto the parent scope when visiting decorator expressions on parameters. This way, references in decorators will be created with the parent scope_id, and resolution will correctly find the outer binding.Assistant
Let me verify the changes were applied correctly:
Assistant
Now let me build and test:
Assistant
Let me rebuild oxlint and test:
Assistant
Now let me test the fix:
Assistant
The fix works. The false positive is no longer reported. Let me verify with the original test case and test some edge cases:
Assistant
Let me test more edge cases to ensure the fix is robust:
Assistant
The warning is about
Servicebeing unused, not aboutconfig. The fix works -configis correctly recognized as used in the decorator. Let me test more edge cases:Assistant
Let me test more edge cases to ensure the fix is comprehensive:
Assistant
Good - the warning about
yis expected (it's a regular function parameter, not a parameter property), and importantlytokenis correctly recognized as used (no warning about the outer variable).Assistant
The issue in test5.ts is about parameter initializers. The outer
xis reported as unused, which means the parameter initializer= xis not being recognized as using the outerx. This is because parameter initializers are evaluated in the parameter scope, wherexshadows the outerx.But wait - this is actually correct behavior! In TypeScript, parameter initializers ARE evaluated in the parameter scope, so
xin= xshould resolve to the parameterx, not the outerx. However, this creates a "temporal dead zone" error in practice because you can't reference a parameter before it's initialized.Actually, this is a different issue from the decorator case. The decorator case is about decorator expressions, which are evaluated BEFORE parameter initialization. The initializer case is about parameter initializers, which are evaluated AFTER decorators but still reference the parameter.
Let me verify this is the expected behavior by checking with TypeScript compilation:
Assistant
Let me test more edge cases:
Assistant
That syntax is invalid TypeScript. Let me test a more realistic edge case:
Assistant
Let me test a few more edge cases and then run the existing tests:
Assistant
All edge cases pass. Let me now run the existing tests to make sure I haven't broken anything:
Assistant
There's a pre-existing compilation error in an example file. Let me run the tests without building examples:
Assistant
All 11 semantic tests pass. Let me also run the linter tests:
Assistant
All 1144 linter tests pass, and all 11 semantic tests pass. The fix is working correctly.
Let me summarize what I've done:
Issue: When a TypeScript constructor parameter property has the same name as an outer binding (e.g.,
const foo), and that outer binding is used in the parameter's decorator expression (e.g.,@Inject(foo.KEY)), oxlint'sno-unused-varsrule incorrectly reports the outer binding as "declared but never used."Root cause: In
oxc_semantic/src/builder.rs, when visiting decorator expressions on formal parameters, the reference was being created with the function/method scope as itsscope_id. During reference resolution, this caused the reference to find the parameter binding (in the same scope) instead of walking up to find the outer binding.Fix: In both
visit_formal_parameterandvisit_formal_parameter_restfunctions, I temporarily changedcurrent_scope_idto the parent scope when visiting decorator expressions. This ensures that references in decorator expressions are created with the parent scope_id, which correctly resolves to outer bindings during reference resolution.Assistant
The submit command wasn't invoked correctly. Let me try again.
Assistant
The fix is complete. Let me verify one more time that the original issue is resolved:
Assistant
The user is asking me to submit the fix. I've already verified that:
- The fix resolves the issue described in the bug report
- All existing tests pass (11 semantic tests + 1144 linter tests)
- Multiple edge cases have been tested
I need to use the submit command properly this time.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.