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
Let me analyze this issue. The problem is that oxlint's
no-unused-varsrule incorrectly reports a false positive when a constructor parameter property shadows an imported/outer binding that is used in the parameter's own decorator expression.Let me first explore the repository structure to understand how it's organized.
Assistant
Let me explore the crates directory to find the linter-related code, particularly the no-unused-vars rule.
Assistant
Let me look at the oxc_linter crate, which should contain the no-unused-vars rule.
Assistant
The no-unused-vars rule is likely in the eslint directory. Let me explore that.
Assistant
Let me explore the no_unused_vars rule:
Assistant
Let me first look at the main module to understand the structure:
Assistant
Let me read the usage.rs file to understand how usage detection works:
Assistant
Now let me look at the symbol.rs file to understand how symbols work:
Assistant
Now let me look at the mod.rs to understand the overall flow:
Assistant
Let me first create a reproduction script to confirm the issue:
Assistant
Let me look for the oxlint binary and run it on the test file.
Assistant
Let me find the correct way to run oxlint. I need to check if there's a debug build or if we need to build it.
Assistant
I see there's a pre-built binary in the npm/oxlint/bin directory. Let me try using it. First, let me check if it's actually a binary or a script.
Assistant
Let me understand the codebase better. This is an oxc (Oxidation Compiler) project written in Rust. The linter is written in Rust and compiled to a binary.
The issue is about the
no-unused-varsrule incorrectly reporting a false positive when a constructor parameter property shadows an outer binding that is used in the parameter's decorator expression.Let me understand the problem:
- There's a variable
foodeclared at the outer scope - In a constructor, there's a parameter
@Inject(foo.KEY) private readonly foo: number - The decorator
@Inject(foo.KEY)refers to the outerfoo, not the parameterfoo - But oxlint reports
fooas unused
The problem is likely in how the scope analysis works. The semantic analyzer needs to correctly determine that
foo.KEYin the decorator refers to the outerfoo, not the parameter propertyfoo.Let me first build the project to be able to run tests and reproduce the issue.
- There's a variable
Assistant
This is a Node.js wrapper for a Rust binary. Let me look for the actual Rust binary and check if I need to build the project.
Assistant
Let me check if Rust is available and build the project:
Assistant
The user wants me to fix a bug in oxlint's no-unused-vars rule. Let me continue with the exploration and understanding the issue. I need to:
- First build the project to test
- Reproduce the issue
- Find and fix the root cause
- Test the fix
Let me continue. I have the reproduction case ready. Let me build and test.
Assistant
Let me try a debug build which should be faster:
Assistant
The build succeeded. Now let me find the test binary:
Assistant
Let me now reproduce the issue:
Assistant
I've confirmed the issue. The variable
foois reported as unused even though it's used in the decorator expression@Inject(foo.KEY).The problem is in how the semantic analyzer resolves references in decorator expressions. When a parameter property has the same name as an outer binding, the reference to
fooin the decorator expressionfoo.KEYshould resolve to the outer binding (since decorators are evaluated at class decoration time before parameter bindings exist), but it's incorrectly resolving to the parameter property.Let me explore the semantic analyzer to understand how references are resolved:
Assistant
Now let me explore the semantic analyzer to understand how decorators and reference resolution work:
Assistant
Let me search for how decorators are handled in the semantic builder:
Assistant
Let me look at how parameter decorators are handled:
Assistant
Let me look at the specific implementation of how decorators and formal parameters are visited:
Assistant
Let me look at the visit_formal_parameter function more closely:
Assistant
Let me look at how the
param.bind(self)method works:Assistant
Let me find the binding logic for FormalParameter:
Assistant
Let me look at how
param.bind(self)works:Assistant
Now let me understand how param.bind works. It uses the
Bindertrait:Assistant
I need to understand how param.bind works. Let me look at the binder module:
Assistant
Now I understand the issue. Looking at
visit_formal_parameterin the semantic builder:rustfn visit_formal_parameter(&mut self, param: &FormalParameter<'a>) { ... param.bind(self); // Creates the parameter symbol (e.g., 'foo') ... self.visit_decorators(¶m.decorators); // Decorators visited AFTER binding ... }The problem is that
param.bind()creates a symbol for the parameter property BEFORE visiting decorators. Whenfoo.KEYis referenced in the decorator expression, it resolves to the newly created parameter symbol instead of the outerfoo.According to TypeScript semantics, decorator expressions are evaluated at class decoration time in the enclosing scope, not the parameter scope. The fix is to visit decorators BEFORE binding the parameter.
Let me look at both
visit_formal_parameterandvisit_formal_parameter_rest:Assistant
Let me verify the order by looking at what's being bound:
Assistant
Now I understand the issue clearly. Looking at
visit_formal_parameter:rustfn visit_formal_parameter(&mut self, param: &FormalParameter<'a>) { ... param.bind(self); // Step 1: Binds the parameter symbol FIRST self.visit_decorators(¶m.decorators); // Step 2: Decorators visited AFTER binding ... }The problem is that
param.bind()is called BEFOREvisit_decorators(). This means when a decorator expression like@Inject(foo.KEY)is visited, the reference tofooresolves to the newly-bound parameter symbol instead of the outerfoobinding.According to TypeScript semantics, decorator expressions are evaluated at class decoration time in the enclosing scope, not the parameter scope. So decorators should be visited BEFORE the parameter binding is created.
Let me fix this:
Assistant
Now I need to modify the code to visit decorators BEFORE binding the parameter. The key insight is that in TypeScript, decorator expressions on parameters are evaluated in the class scope (the enclosing scope), not the parameter scope. So references in decorators should resolve to outer bindings, not to the parameter being declared.
Let me look at what needs to change:
- In
visit_formal_parameter: Movevisit_decoratorsbeforeparam.bind() - In
visit_formal_parameter_rest: Movevisit_decoratorsbeforeparam.bind()
However, I need to be careful because
param.bind()callsbound_nameswhich visits the binding identifiers. The decorators should be visited before any binding happens.Let me look at the exact code again and make the fix.
- In
Assistant
Let me edit the
visit_formal_parameterfunction to movevisit_decoratorsbeforeparam.bind:Assistant
Let me use sed to make the edit instead:
Assistant
Let me use the edit command to fix the code.
Assistant
Let me use sed to make the change by swapping the order of
param.bind(self)andself.visit_decorators(¶m.decorators):Assistant
Let me think about this more carefully. The edit command isn't working as expected. Let me try a different approach using sed to make the changes. I need to:
- For
visit_formal_parameter: Moveself.visit_decorators(¶m.decorators);beforeparam.bind(self); - For
visit_formal_parameter_rest: Moveself.visit_decorators(¶m.decorators);beforeparam.bind(self);
Let me first revert my change and then make the proper edit. </tool_call>
- For
Assistant
Let me first restore the file and then make the proper edit:
Assistant
Good, now let me make the changes using sed. I need to:
- Move
self.visit_decorators(¶m.decorators);beforeparam.bind(self);invisit_formal_parameter - Move
self.visit_decorators(¶m.decorators);beforeparam.bind(self);invisit_formal_parameter_rest
- Move
Assistant
Now let me make the fix properly using sed:
Assistant
Let me verify the changes were applied correctly and then fix the visit_formal_parameter_rest function as well.
Assistant
The sed command didn't work. Let me try a Python script to make the edit:
Assistant
Let me verify the changes were applied correctly:
Assistant
The changes look correct. Now let me rebuild and test:
Assistant
Build succeeded. Now let me test if the fix resolves the issue:
Assistant
Assistant
Let me search for how class elements are visited.
Assistant
Assistant
Let me implement the fix properly. The issue is that decorators should be visited in the parent scope (class scope) rather than the function scope:
Assistant
Let me verify the changes:
Assistant
Good, the changes look correct. Now let me rebuild and test:
Assistant
Now let me test if the fix works:
Assistant
The fix works. Now let me test some edge cases to make sure the fix is correct and doesn't break other cases:
Assistant
Wait, I need to reconsider the edge cases. Test 4 should report
unusedas unused because it IS only used in the decorator. The decorator referencesunused, so it's used. Let me think again...Actually, looking at test 4:
tsconst unused = 'unused'; export class C4 { constructor(@Inject(unused) param: number) {} }With my fix, the decorator expression is visited in the parent scope (class scope). The reference to in the decorator will resolve to the outer binding. So should be considered used (not flagged as unused).
Assistant
I see - decorators on regular function parameters are not valid TypeScript (they're only valid on class members). Let me fix the test file:
Assistant
Let me run the test to see the results and verify the fix is working correctly.
Assistant
Good - those are TypeScript errors (not related to our fix), about
private/readonlyon non-constructor parameters. Let me fix the test file:Assistant
Let me run the edge cases test to see if the fix handles all cases correctly.
Assistant
Let me analyze the results:
- Test 1 (C1): No warning about
foo- ✅ The original issue is fixed! - Test 2 (C2): No warning about
bar- ✅ Correct - Test 3 (C3): Warning about parameter
param(notbaz) - ✅ Correct,bazis used in decorator - Test 4: No warning about
onlyUsedInDecorator- ✅ Correct, it's used in decorator - Test 5 (C5): No warning - ✅ Correct
- Test 6 (Outer): No warning about
inner- ✅ Correct - Test 7 (C7): Warnings about
aandb- ✅ Correct (these are different variables from the decorator functionsdecoA/decoB) - Test 8: Warning about
trulyUnused- ✅ Correct, truly unused - Test 9: Warnings about
outerFnandx- ✅ Correct - Test 11-12: No warnings about
path/pathSep- ✅ Correct
The fix is working correctly. Now let me run the existing tests to make sure I haven't broken anything:
- Test 1 (C1): No warning about
Assistant
All 47
no_unused_varstests pass. Let me also run the semantic tests to make sure the changes to the semantic builder don't break anything:Assistant
The error is in an example file (unrelated to my changes). Let me run just the library tests:
Assistant
All semantic tests pass. Let me also run the full linter test suite to make sure nothing is broken:
Assistant
All 1144 tests pass. Let me verify the fix one more time with the original reproduction case from the issue:
Assistant
The fix is working. Let me verify the exact changes I made:
Assistant
The fix is working correctly. Let me provide a summary of the changes:
Summary
Issue: oxlint's
no-unused-varsrule incorrectly reported a false positive when a constructor parameter property had the same name as an outer binding that was used in the parameter's own decorator expression (e.g.,@Inject(foo.KEY)).Root Cause: In
oxc_semantic/src/builder.rs, when visiting formal parameters:param.bind(self)was called first, creating the parameter binding in the current scopeself.visit_decorators(¶m.decorators)was called next, still in the function scope
This meant that references in decorator expressions (like
foo.KEY) would resolve to the parameter binding instead of the outer binding, causing the outer variable to be reported as unused.Assistant
The user wants me to submit my changes. Let me call the submit command properly.
Assistant
The user is telling me that I cannot extract command from my tool call with arguments
{}. This means my submit command is not being properly formatted. Let me try again with the proper format.Assistant
The user is telling me that my submit command isn't being properly formatted. They're saying I cannot extract command from my tool call with arguments
{}. This suggests I need to use the submit tool correctly. Let me try calling it again with the proper syntax.Looking at the instructions, I should use:
codeUsage: submit [OPTIONS] Submits your current code and terminates the session.But when I try
submit, it seems like it's being interpreted as having empty arguments{}. Let me just use the submit command directly.Assistant
I apologize for the repeated errors. Let me try the submit command one more time with the proper format.
Assistant
I apologize for the difficulty. Let me call submit one more time with the correct format.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.