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
I'll start by exploring the repository structure to understand the codebase and locate the relevant code for the
no-unused-varsrule.Assistant
Let me explore the crates directory to find the relevant linting code for the
no-unused-varsrule.Assistant
Let me first create the test file to reproduce the issue:
Assistant
Now let me check if oxlint is built and available:
Assistant
I need to build the project first:
Assistant
Command timed out. Let me try a debug build which might be faster, or check if there's already a built binary somewhere.
Assistant
Let me look for existing test infrastructure for this rule:
Assistant
Let me read the mod.rs file to understand the structure of the no_unused_vars rule:
Assistant
I need to use the correct format for the open command - it's a special command, not a function call.
Assistant
Now let me look at the symbol.rs file to understand how references are tracked:
Assistant
Now let me look at the usage.rs file to understand how usages are detected:
Assistant
Let me search for how the references are resolved and how decorators are handled:
Assistant
Let me look at the tests to see if there are any related decorator tests:
Assistant
Let me look at the semantic module to understand how references are resolved:
Assistant
Let me look at the builder.rs to understand how scopes and symbols are built:
Assistant
Let me see the context around those decorator visits:
Assistant
I see the issue! When visiting a formal parameter, the order is:
param.bind(self)- binds the parameter to create the symbolself.visit_decorators(¶m.decorators)- visits decoratorsself.visit_binding_pattern(¶m.pattern)- visits the binding pattern
The problem is that when the decorator references a variable with the same name as the parameter, the parameter binding has already been created, so the reference in the decorator resolves to the parameter instead of the outer variable.
Let me verify this understanding by looking at the binder:
Assistant
Let me check how formal parameters are bound:
Assistant
Now I understand the issue better. The problem is that when visiting a formal parameter, the parameter is bound BEFORE the decorators are visited. So when the decorator expression references an identifier, it resolves to the newly bound parameter instead of the outer scope binding.
According to TypeScript semantics, parameter decorators are evaluated at class-declaration time, and the decorator expression is evaluated in the enclosing scope, not in the parameter scope. Therefore, the decorators should be visited BEFORE the parameter binding is created.
Let me check the scope structure to confirm:
Assistant
Let me check how function parameters handle their scope:
Assistant
Now I understand the scope structure better. The function enters a scope, then visits formal parameters. Let me look at how formal parameters are visited:
Assistant
Expressions can be
Assistant
Now I understand the issue better. Let me look at how the scope is structured for class constructors:
The issue is that when visiting a formal parameter:
param.bind(self)is called first - this creates the symbol for the parameterself.visit_decorators(¶m.decorators)is called second - at this point, the reference tofooin the decorator resolves to the newly bound parameter instead of the outer scope variable
The fix should be to visit decorators BEFORE binding the parameter, since decorators are evaluated in the enclosing scope, not in the parameter scope.
Let me check if this is the correct fix by looking at the TypeScript spec and how typescript-eslint handles this.
Let me also run the test suite to understand the current state:
Assistant
Let me look at the full
visit_formal_parameterandvisit_formal_parameter_restfunctions:Assistant
Now I understand the problem clearly:
- In
visit_formal_parameter, the order is:param.bind(self)- creates the symbol for the parameter (e.g.,foo)self.visit_decorators(¶m.decorators)- visits decorators, but nowfooin@Inject(foo.KEY)resolves to the parameter instead of the outer variable
The fix is to visit decorators BEFORE binding the parameter, so that references in decorator expressions resolve to the outer scope.
Let me make the fix:
- In
Assistant
Let me
Assistant
Now let me apply the fix by editing the file:
Assistant
I need to edit the file to move the
visit_decoratorscall beforeparam.bind(self):Assistant
Now let me verify the changes:
Assistant
Good, the fix is applied. Now let me run the tests for the
no_unused_varsrule to make sure the fix doesn't break any existing tests:Assistant
All tests pass. Now let me create a test file to verify that the fix works for the reported issue:
Assistant
Create a test that I'll run to confirm the fix works
Assistant
Actually, let me run a simpler test using the test infrastructure in the crate:
Assistant
Good, all tests pass. Now let me add a test case to the test file to ensure this specific issue is covered:
Assistant
Let me search for where test cases are defined to understand the test format better:
Assistant
Let me look at more of this file to understand the test pattern:
Assistant
Now I need to understand: does
APP_CONFIGexist in this test? Let me see the full test case context:Assistant
I see! In the existing test case,
APP_CONFIGis a global reference that's not defined locally (so it wouldn't trigger the issue). Let me add a specific test case for the issue to the test file:Assistant
Now let me check the end of the
passvector to find the right place to add a test case:Assistant
Now let me create a simple test script to confirm the fix works:
Assistant
Let me create a temporary test within the existing test structure and run it:
Assistant
Let me
Assistant
Let me try a different approach - I'll add a test case to the existing test file and run it:
Assistant
Let me look for an existing test for decorator shadowing scenarios:
Assistant
Now let me run the tests again and then also build a debug binary of oxlint to test directly:
Assistant
Now let me test the fix by running oxlint directly:
Assistant
The issue is still present! The fix I made to
visit_formal_parameterdidn't fully solve the problem. Let me investigate further.Looking at the code flow, I think the issue is that when
param.bind(self)is called, it creates the symbol for the parameter. But then when we look at the references, we're looking at the outerfoosymbol which has no resolved references to the decorator use.Let me check the scoping more carefully:
Assistant
I see. The issue is that reference resolution happens in a scope, and when I visit decorators before binding, the reference to
fooin the decorator might be getting resolved in a different scope.Let me look at how formal parameters are visited in the context of the function:
Assistant
I see the issue now! When visiting formal parameters:
- The function scope is entered first
- Then parameters are visited (including binding)
- References are resolved within this scope
The problem is that when I visit the decorator before binding, the reference to
foowill be added to the unresolved references list, but it won't be resolved untilresolve_references_for_current_scope()is called. At that point, thefooparameter binding has been created, so it resolves to the parameter instead of the outer scope variable.Let me look at how the scope lookup works:
Assistant
Now I understand the resolution mechanism. When
walk_up_resolve_referenceis called, it starts from the reference's scope and walks up the scope chain looking for a binding. The issue is that the reference's scope is set when the reference is created.Let me check when references are created and what scope they are assigned to:
Assistant
I see it now! When a reference is created, it is assigned to
self.current_scope_id. The key insight is:- When visiting the decorator, if I do it BEFORE binding the parameter, the
current_scope_idis the function's scope - When
resolve_references_for_current_scope()is called, it looks up from that scope - At that point, the
fooparameter has been bound to the function scope - So the reference resolves to the parameter, not the outer variable
The issue is that decorator expressions should be evaluated in the enclosing scope (the class scope or module scope), not the function's parameter scope.
Let me check how decorators are currently visited:
- When visiting the decorator, if I do it BEFORE binding the parameter, the
Assistant
Now I need to understand: when visiting a class method/constructor, what is the scope structure? Let me check:
Assistant
I see! For class decorators, they are visited BEFORE
enter_scope, so they are evaluated in the outer scope. This is the correct behavior.The problem is that for parameter decorators in a constructor, the decorators are visited inside the function scope. The fix should ensure that parameter decorators are evaluated in the class scope (or at least outside the parameter's binding scope).
Let me look at what TypeScript's actual behavior is. According to the TypeScript spec:
- Parameter decorators are evaluated in the enclosing class scope
- They are NOT evaluated in the function's parameter scope
So the reference
fooin@Inject(foo.KEY)should resolve to the outerfooconstant, not the parameterfoo.The fix should be: when visiting decorators of a constructor parameter, we should visit them in the class scope, not the function scope. Let me check how to implement this:
Assistant
My previous command failed. Let me search for how method definitions are handled to understand the scope structure better.
Assistant
My command failed. Let me search differently:
Assistant
Now I understand the scope structure better. The correct fix is:
For constructor parameter decorators, the decorator expressions should be evaluated in the class scope (before the function scope is entered), not in the function scope where the parameter would be bound.
Let me look at how the constructor is visited and find where we can save/restore the scope:
Assistant
Now let me check if we're inside a constructor and get the parent class scope:
Assistant
My current fix visits decorators before binding, but the issue is that the decorators are still being evaluated in the function scope, not the enclosing class scope. I need to change the approach:
Instead of just moving the decorator visit, I need to ensure that when visiting parameter decorators, the reference is created with the parent scope (class scope), not the function scope.
Let me look at the approach more carefully. The issue is:
- Function scope is entered
- Parameter is bound (which creates
fooin the function scope) - Decorator is visited, and
fooreference is created with function scope - When resolving,
fooin function scope is found first
The solution is to visit decorators before entering the function scope. But that's complicated because decorators are part of the parameter.
A better approach: when creating a reference for a decorator expression, we should use the parent scope instead of the current scope. Let me check how to implement this:
Assistant
I can temporarily set the scope to the parent scope when visiting decorators. Let me implement this fix properly:
Assistant
The helper method wasn't inserted. Let me find the correct insertion point:
Assistant
Now let me add the helper method in the right location:
Assistant
Now let me verify the changes and try to compile:
Assistant
I need to find the correct method name to check if a function is a constructor:
Assistant
Let me check what properties are available on the MethodDefinition struct:
Assistant
error
Assistant
Let me find how to check if a method is a constructor:
Assistant
Let me search in a different way:
Assistant
Now I found it! The
kindfield hasMethodDefinitionKindenum with aConstructorvariant:Assistant
Now I need to fix the helper method to use
kind == MethodDefinitionKind::Constructor:Assistant
Now let me build again:
Assistant
The semantic crate compiles. Now let me rebuild the linter and oxlint binary:
Assistant
Let me test the fix:
Assistant
The fix works! The false positive is gone. Now let me run the full test suite to make sure I didn't break anything:
Assistant
All tests pass. Let me also test some edge cases to make sure the fix is robust:
Assistant
error
Wait, there's an issue with test case 5. The rest parameter with decorator should work, but it seems like the rest parameter's binding (
foo) is being reported as unused. Let me check if the fix also applies tovisit_formal_parameter_rest:Assistant
Let me see the rest of
visit_formal_parameter_rest:Assistant
The fix is in place for
visit_formal_parameter_restas well. The warning we see for test case 5 is actually correct behavior - the rest parameterfooIS declared but never used in the constructor body. The decorator references the outerfoo, which is correct.Let me update the test case to verify:
Assistant
There's an issue! Test case 8 (static method with parameter decorator) is not handled correctly. The
configvariable should be used in the decorator, but it's being reported as unused.The issue is that my fix only handles constructor parameters. For static methods (and regular methods), the decorator should still reference the outer scope, not the parameter scope.
Wait, let me think about this more carefully. The TypeScript spec says:
- Parameter decorators are evaluated at runtime
- They are evaluated in the enclosing function/method scope
- But they should NOT resolve to the parameter itself if the parameter shadows an outer variable
Actually, looking at the TypeScript semantics more carefully, parameter decorators are evaluated before the function body runs, and references in decorators should resolve according to normal JavaScript scoping rules - meaning they should resolve to the outer binding, not the parameter.
Let me reconsider my fix. The issue is:
- For constructors: the decorator should be evaluated in the class scope
- For regular methods: the decorator should be evaluated in the class scope
- For standalone functions: the decorator should be evaluated in the outer scope
Assistant
Now let me rebuild and test:
Assistant
Let me test the fix again:
Assistant
All edge cases pass. Let me also test the original issue:
Assistant
Now let me run the full test suite to make sure nothing is broken:
Assistant
All tests pass. Let me also run the semantic tests to make sure I didn't break anything in the semantic analysis:
Assistant
That's an unrelated error in an example file. Let me just run the library tests:
Assistant
All tests pass. Now let me view the current diff to see the final changes:
Assistant
The changes look clean. Let me also run a few more tests to ensure edge cases are handled:
Assistant
error
Good, that's expected - decorators on function expression parameters are not valid. Let me fix the test:
Assistant
All edge cases pass. Now let me run the broader linter test suite to make sure nothing else is broken:
Assistant
All 1144 tests pass (with 1 ignored). The fix is complete. Let me verify the final changes one more time and provide a summary:
Assistant
Let me also run the original test file one more time to confirm the issue is fixed:
Assistant
The fix is working correctly. Let me verify the final state of the file to ensure everything is correct:
Assistant
The fix is complete. Let me provide a summary of what was done:
Summary
Issue
The
no-unused-varsrule was producing a false positive when a TypeScript constructor parameter property had the same name as an outer binding (import orconst), and that outer binding was used in the parameter's own decorator expression (e.g.,@Inject(foo.KEY)).Root Cause
In
SemanticBuilder::visit_formal_parameterandvisit_formal_parameter_rest, decorators were being visited after the parameter binding was created. This meant that when reference resolution occurred, the identifier in the decorator (e.g.,fooin ) resolved to the newly-bound parameter instead of the outer-scope variable.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.