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 issue:
-
In TypeScript, when a 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,no-unused-varsreports the outer binding as unused. -
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.
Let me first explore the repository structure to understand what we're dealing with.
The issue is in the linter, specifically in
no-unused-varsrule. Let me explore the codebase.-
Assistant
Let me explore the structure more, particularly around the linter rules
Assistant
The main rule implementation is in
crates/oxc_linter/src/rules/eslint/no_unused_vars/. Let me explore that directory.Assistant
Let me first create a reproduction script and then explore the relevant source code
Assistant
Let me create a test file to reproduce the issue
Assistant
Let me try running oxlint on this file. First, let me find the oxlint binary.
Assistant
Let me try running that binary on the test file.
Assistant
Let me find the actual binary
Assistant
Let me build the binary from source
Assistant
Let me try running the specific tests instead to understand the behavior.
Assistant
Good, the tests pass. Now let me understand the source code structure and the logic behind unused var detection, specifically for parameter properties with decorators
Assistant
Let me try using the open special command.
Assistant
Let me read more of this file and the usage.rs file
Assistant
Let me go further down to see the actual rule implementation.
Assistant
Let me look at usage.rs to understand how usages are tracked.
Assistant
Let me read more of usage.rs
Assistant
Let me look at the
symbol.rsfileAssistant
The issue is likely in the semantic scope analysis. Let me look at the scope analysis to see how parameter decorators are scoped.
Assistant
Let me look at the builder.rs and scoping.rs
Assistant
Let me look at the builder.rs around the parameter decorator visiting code.
Assistant
Let me find the relevant code around the parameter decorator handling:
Assistant
Let me look at
visit_formal_parametermore closely:Assistant
Now I see the issue! In
visit_formal_parameter,param.bind(self)(which creates the parameter binding in the current scope) is called BEFOREself.visit_decorators(¶m.decorators). This means when a decorator expression referencesfooandfoois also the parameter name, the scope resolution finds the parameter's binding first.In TypeScript, decorators on parameters are evaluated in the enclosing class scope BEFORE the parameter binding is introduced. So the fix is to visit decorators FIRST, then bind the parameter.
Let me verify this by checking the semantic scope resolution:
Assistant
Let me check the binder for formal parameter binding
Assistant
Let me look at the Binder for FormalParameter:
Assistant
Now I understand the issue. Let me verify my understanding by looking at how decorators are evaluated:
In TypeScript, parameter decorators are evaluated at class-definition time in the class scope, BEFORE the parameter binding is introduced. So
@Inject(foo.KEY)should resolvefooto the outer binding, not the parameter.The fix is to swap the order in
visit_formal_parameterandvisit_formal_parameter_restso that decorators are visited BEFORE the parameter binding is created.Let me first check if there are existing test cases related to this:
Assistant
Let me look at the specific test case around the constructor parameter decorator
Assistant
Let me understand the scope context better. I need to see how function scopes are created when visiting formal
Assistant
Let me look at how function scope is set up before parameters are visited
Assistant
Now I understand the issue clearly. In
visit_formal_parameter, the parameter binding (param.bind(self)) happens BEFORE the decorators are visited. This means when@Inject(foo.KEY)referencesfoo, it resolves to the parameterfooinstead of the outerfoo, causing the outerfooto be incorrectly flagged as unused.The fix: swap the order so that decorators are visited BEFORE the parameter binding.
Let me first create a reproduction test and look at the test structure
Assistant
Now let me implement the fix. The issue is in
crates/oxc_semantic/src/builder.rs. In bothvisit_formal_parameterandvisit_formal_parameter_rest, decorators are visited AFTER the parameter binding is created, which causes references in decorators to resolve to the parameter instead of the outer scope.Assistant
That edited the wrong file. Let me reopen the builder.rs and fix it there
Assistant
Good, now I'm in the correct file. Let me first revert the accidental edit to the test file, then make the correct edits
Assistant
Now let me edit
visit_formal_parameter:Assistant
Now let me fix
visit_formal_parameter_rest:Assistant
Let me verify the changes:
Assistant
Good. Now let me run the tests to see if this fix works. Let me start with the no_unused_vars tests since that's the rule being affected:
Assistant
All linter tests pass. Let me also run the semantic tests and broader test suites to make sure nothing is broken:
Assistant
There's a pre-existing error in an example. Let me run just the tests, not the examples
Assistant
All tests pass. Let me now verify the fix by adding a test case to the oxc-specific tests for no_unused_vars and running it:
Assistant
Let me check the test structure around lines 1318-1370 to understand how tests are formatted:
Assistant
Let me add a test case to the oxc-specific tests to verify the fix. Let me find the right place to add it:
Assistant
Let me add a test case to the
test_classestests. Let me look at the full test_classesAssistant
Let me add test cases for the decorator shadowing issue. I'll add them to the
test_classespass vector:Assistant
I see there's an extra empty line. Let me fix that:
Assistant
I messed up the edit. Let me restore the file and redo this properly.
Assistant
Let me first run the existing tests to confirm nothing is broken, then I'll add the test case properly
Assistant
Let me add the test case properly. Let me view the exact lines I need to edit:
Assistant
Let me add the test case line properly. I'll replace line 1359 with two lines:
Assistant
Now let me run the tests again to verify:
Assistant
The test still fails! This means the fix at the builder level alone isn't sufficient. Let me investigate further. The issue might be deeper - perhaps references in decorators are resolved after the function scope is built, regardless of binding order.
Let me look at how reference resolution works:
Assistant
Let me look at the reference resolution logic:
Assistant
I see - the issue is more subtle. The reference resolution happens AFTER all parameters are bound, so even visiting decorators before binding doesn't help. Let me look at how method visits handle decorators:
Assistant
Let me look at how method definitions are visited:
Assistant
Let me check how the Visit trait handles method definitions. It's likely auto-generated.
Assistant
Let me see the walk_method_definition function:
Assistant
I see the problem more clearly now. For method decorators (like
@MethodDecoratoron a method), they're visited BEFOREvisit_functionis entered, so they resolve in the class scope. But for parameter decorators, they're visited INSIDE the function scope. Even movingbindafter decorators doesn't help because the reference resolution happens later, after all parameters are bound.The real fix: parameter decorators should be visited in the parent scope, not the function scope. Let me implement this:
Assistant
Let me look at how we can get the parent scope and temporarily switch:
Assistant
Let me check the scoping module to understand the parent scope API:
Assistant
Now I understand the real issue. I need to visit parameter decorators in the parent scope (class scope), not the function scope. Let me implement this
Assistant
Now let me also fix
visit_formal_parameter_rest:Assistant
Now let me run the tests to verify this fix:
Assistant
All linter tests pass! Let me also run the semantic tests:
Assistant
There are snapshot test failures. Let me look at what changed:
Assistant
The snapshot changes confirm the fix is working correctly - the
scope_idfor decorator references changed from3(function scope) to2(parent/class scope), which is exactly what we want. Let me update the snapshots:Assistant
Let me manually update the snapshots:
Assistant
Now let me re-run the semantic tests:
Assistant
All semantic tests pass. Let me run the linter tests again:
Assistant
All tests pass. Let me also run a broader set of tests to make sure nothing is broken:
Assistant
All 1144 linter tests pass! Let me check the overall diff:
Assistant
The diff looks clean and correct. Let me run both test suites one final time to confirm everything passes:
Assistant
Let me run them separately:
Assistant
All tests pass! Let me clean up and verify:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.