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
I'll start by exploring the repository structure to understand the codebase and then work on reproducing and fixing the issue.
Assistant
This is the oxc (Oxidation Compiler) repository. The issue is about a false positive in the
no-unused-varslinter rule when a constructor parameter property shadows an imported identifier that's used in a decorator. Let me explore the structure more to find where the linter code is.Assistant
Now let me first create a test file to reproduce the issue:
Assistant
Now let me check if oxlint is available or needs to be built:
Assistant
I need to build the project. Let me check what build tools are available:
Assistant
Let me build oxlint:
Assistant
The build is taking too long. Let me try a debug build which is faster:
Assistant
The build completed. Now let me run oxlint on the test file to confirm the issue:
Assistant
I've confirmed the issue. The
foovariable is reported as unused even though it's used in the decorator@Inject(foo.KEY). Now let me explore the codebase to understand how theno-unused-varsrule works and where the scope analysis for decorators is handled.Assistant
Let me look at the
symbol.rsandusage.rsfiles to understand how symbol references are tracked:Assistant
Now let me check the usage.rs file to see how symbol usages are detected:
Assistant
Let me continue reading the usage.rs file:
Assistant
Now let me look at the semantic analysis to understand how scopes work for decorators. The issue mentions that decorators are evaluated in the enclosing class scope, before parameter bindings exist. Let me check the oxc_semantic crate:
Assistant
Let me search for how decorators are handled in the semantic builder:
Assistant
Let me look at how decorators are visited in the context of formal parameters:
Assistant
I see the issue! In
visit_formal_parameter, the code calls:param.bind(self)- which binds the parameter name to create the symbol- Then
self.visit_decorators(¶m.decorators)- which visits the decorators after the parameter is bound
This is wrong! According to the issue, decorators are evaluated at class-decoration time in the enclosing scope, before the parameter binding exists. So when visiting the decorators, the
fooreference should resolve to the outerconst foo, not the parameter.Let me check the binder to understand how parameters are bound:
Assistant
Now I understand the issue. In
visit_formal_parameter, the binding happens first (creating the symbol for the parameter), and then decorators are visited. When the decorator referencesfoo, the scoping resolves it to the newly created parameter symbol instead of the outer binding.The fix should be to visit the decorators before binding the parameter, so that references in decorators resolve to the outer scope bindings.
Let me also check how class decorators are visited:
Assistant
Good. For classes, decorators are visited at the class level scope (before entering the class scope). That's correct behavior.
The issue is with parameter decorators on formal parameters. Let me check what tests exist for this rule to understand the test patterns:
Assistant
Let me search for existing decorator tests:
Assistant
Let me look at the test around line 67 more closely - it has
APP_CONFIGwhich is an outer binding used in a parameter decorator:Assistant
I notice this test case has
APP_CONFIGused in a decorator, butAPP_CONFIGis not declared in the test - it's probably expected to be an unresolved global or imported. Let me search if APP_CONFIG is imported:Assistant
So
APP_CONFIGis not declared in that test, which means it's an unresolved reference (global). The issue is specifically when there's a shadowing situation - when an outer binding with the same name as the parameter exists.Let me now understand the problem better. The issue is that when:
const foo = { KEY: 'token' };is declared- In the constructor,
@Inject(foo.KEY) private readonly foo: numberis used
When
param.bind(self)is called, it creates a symbol for the parameterfoo. Then whenvisit_decoratorsis called and visitsfoo.KEY, the reference resolution finds the parameter symbolfoo(which shadows the outer one) instead of the outerconst foo.The fix is to visit decorators before binding the parameter. Let me verify my understanding by looking at the current code more carefully:
Assistant
Now I understand the fix. I need to move
self.visit_decorators(¶m.decorators);to beforeparam.bind(self);so that references in decorators are resolved before the parameter symbol is created.Let me make this fix:
Assistant
I need to use the correct syntax for the open command.
Assistant
Now I need to navigate to line 2358 to make the edit.
Assistant
Now I can see the code clearly. I need to move
self.visit_decorators(¶m.decorators);to beforeparam.bind(self);in bothvisit_formal_parameterandvisit_formal_parameter_restfunctions.Assistant
Good, the first function is fixed. Now let me fix the second function
visit_formal_parameter_rest:Assistant
Both functions are now fixed. Now let me rebuild and test:
Assistant
Build succeeded. Now let me test the fix:
Assistant
Hmm, the issue still persists. The fix I made was to the semantic builder, but it seems like the reference resolution is still not working as expected. Let me investigate further.
Let me check if the issue is with how references are resolved. Perhaps the issue is at a different layer - maybe in how references are looked up during resolution.
Let me check how references are resolved in the semantic builder:
Assistant
Let me look at the
reference_identifierfunction and how references get resolved:Assistant
Let me look at how the reference is resolved to a symbol. The issue might be in how references get resolved after being declared:
Assistant
Now I understand. The issue is that references are resolved by walking up the scope chain using
scope_idstored in the reference. When a reference is created, it stores the currentscope_id, and then resolution happens later by looking up bindings starting from that scope.The problem might be timing. Let me see when
resolve_all_referencesis called:Assistant
OK, so
resolve_all_references()is called at the end of visiting the program. That means all references are resolved at the end of the traversal. The issue is that the reference'sscope_idis stored when the reference is created, not when it's resolved.Wait, let me re-think the problem. When I visited the test case, I still got the error. Let me trace through more carefully:
const foois declared in the root/module scope- When we enter the constructor, we create a new scope for the function
- In
visit_formal_parameter:- Original code:
param.bind(self)creates a symbol for parameterfooin the function scope, thenvisit_decoratorsis called - After my fix:
visit_decoratorsis called first, thenparam.bind(self)creates the symbol
- Original code:
So after my fix, when
visit_decoratorsvisitsfoo.KEY, the reference should be created and then later resolved. At resolution time, it should find the outerfoobecause the parameter hasn't been bound yet.Assistant
I see! The function's scope is entered before visiting the parameters. Let me check where the function scope is created:
Assistant
I see! The function scope is entered before visiting the parameters. So when I visit the decorator, I'm still in the function's scope, but the parameter symbol hasn't been bound yet.
This should work because when resolving references later, the system walks up the scope chain:
- Start from function scope - no
foobinding there yet (since we visit before binding) - Walk up to enclosing scope (class scope or module scope) - find
foothere
But wait, the binding is created later in the same scope. The issue might be that when the reference is resolved later (at
resolve_all_references), the parameter binding will already exist in the function scope!Let me check the timing more carefully. The issue is:
- Start from function scope - no
Assistant
Let me check how TypeScript-eslint handles this. The issue description mentions that typescript-eslint correctly handles this. Let me look for any related tests or implementations:
Assistant
Interesting! I can see in this snapshot that the decorator references (
@decorator) havescope_id: 3which is the constructor function scope (ScopeFlags: StrictMode | Function | Constructor). Butdecoratoris defined at the top level (scope 0).Looking at the references, they reference the
decoratorfunction from the top-level scope even though theirscope_idis 3. This works because the reference resolution walks up the scope chain.So the current design is that references are resolved by walking up the scope chain, starting from their
scope_id. The issue is that when a parameter with the same name exists, it shadows the outer binding.Let me think about this differently. According to the ECMAScript/TypeScript specification:
- Parameter decorators are evaluated in a special evaluation context
- The decorator factory is called with the class prototype (for instance methods/constructors) or constructor function (for static members)
- At the time the decorator expression is evaluated, the parameter bindings don't exist yet
Assistant
I see. The approach should be to save the current scope before entering the function scope (for constructor parameters), and use the parent scope when resolving decorator references. But that's complex.
A simpler approach: when visiting decorators on a parameter, temporarily set the
current_scope_idto the parent scope. Let me implement this:Assistant
Now let me modify
visit_formal_parameterto temporarily use the parent scope when visiting decorators:Assistant
Now let me modify the
visit_formal_parameterfunction to use the parent scope when visiting decorators:Assistant
Now let me do the same for
visit_formal_parameter_rest:Assistant
Good, the changes look correct. Now let me rebuild and test:
Assistant
Build succeeded. Now let's test it:
Assistant
The fix works! The false positive is gone. Now let me test a few edge cases to make sure the fix is robust:
Assistant
There's a syntax error in my test (nested classes need
staticor to be in a method). Let me fix that:Assistant
The warnings are correct:
paramis unused (the method doesn't use it)restis unused (the method doesn't use it)xis unused (the outerconst x = 1is never used)
The important thing is that no false positives for
foo,A,B,token,restToken, orconfigwhich are all used in decorators. Let me verify by removing the unused vars test file and confirming:Assistant
All outer bindings used in decorators are now correctly recognized as used, and there are no false positives.
Now let me run the existing tests to make sure I didn't break anything:
Assistant
All semantic library tests passed! Now let me run the integration tests for semantic:
Assistant
All integration tests pass. Now let me run the linter tests for the no-unused-vars rule:
Assistant
Excellent! All 47 tests for the no_unused_vars rule pass! Now let me also run the broader semantic tests to make sure nothing is broken:
Assistant
This is an unrelated error in an example file. Let me run the tests more specifically:
Assistant
Let me check the decorator test snapshots - they might need updating since the scope_id of references has changed:
Assistant
The snapshot shows that the decorator references (
@decorator) havescope_id: 3which is the constructor function scope. With my fix, they should now havescope_id: 2(the class scope, which is the parent of the constructor scope).Let me run the snapshot tests to see if they fail:
Assistant
The snapshot tests show that the
scope_idof decorator references has changed from 3 (constructor scope) to 2 (class scope), which is exactly what we expected. This is the correct behavior because decorator expressions should resolve references in the class scope, not the constructor scope.Let me update the snapshots:
Assistant
Let me manually update the snapshot files since
cargo instais not available:Assistant
Now let me verify that the tests pass:
Assistant
All tests pass now. Let me also clean up the
.snap.newfiles:Assistant
Now let me also test some edge cases related to the fix. Let me test that when there are no decorators on a parameter, the behavior is unchanged:
Assistant
The outer
foois correctly reported as unused when there's no decorator using it. The parameter shadows the outer variable, and only the parameter is used.Let me also test the case where the decorator doesn't use the same name as the parameter:
Assistant
OUTERis correctly recognized as used. Now let me run a broader set of linter tests to ensure nothing is broken:Assistant
All 1144 tests passed in the linter! Let me verify the final state of my changes:
Assistant
Let me view the complete diff:
Assistant
The changes look good. Let me verify one more time that the original issue is fixed:
Assistant
The fix is working correctly. Let me summarize the changes I made and verify the checklist:
Summary of Changes
Problem
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-varsincorrectly reported the outer binding as declared but never used.This was 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.
Assistant
I need to submit my changes now since the issue has been resolved.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.