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 how it's organized and find the relevant code for the
no-unused-varslint rule.Assistant
Let me search for the
no-unused-varsrule implementation in this codebase.Assistant
The main implementation seems to be in
./crates/oxc_linter/src/rules/eslint/no_unused_vars/mod.rs. Let me explore the structure of this rule.<tool_call> <function=bash_terminal> <parameter=command> ls -la ./crates/oxc_linter/src/rules/eslint/no_unused_vars/ </tool_call>
Assistant
I need to use the bash_terminal tool to run commands. Let me explore the no-unused-vars rule directory.
Assistant
This is a Rust codebase for the oxlint linter. Let me first create a test file to reproduce the issue and confirm the bug.
Assistant
Now let me try to run oxlint on this test file to confirm the issue.
Assistant
The compilation is in progress. Let me wait for it to complete.
Assistant
Now let me run oxlint on the test file to reproduce the issue.
<tool_call> <function=bash_terminal> <parameter=command> cd /oxc && ./target/debug/oxlint /tmp/test.ts </tool_call>
Assistant
I need to use bash_terminal for this command.
Assistant
Great! I've confirmed the bug. The issue is that oxlint reports
fooas unused even though it's used in the decorator@Inject(foo.KEY). Now let me explore the no-unused-vars implementation to understand how it handles decorator expressions.Assistant
The issue is that the no-unused-vars rule doesn't properly track decorator expressions as usage of variables. Let me look at the usage.rs file which likely contains the logic for detecting usages.
Assistant
This is a large file. Let me search for decorator-related code to see how decorators are handled.
Assistant
No decorators found. Let me check the mod.rs file for any decorator handling.
Assistant
So there are some tests with decorators but they don't test the specific case where a decorator uses an identifier with the same name as the decorated parameter. Let me check how the usage detection works. The issue mentions this is a shadowing case where the decorator expression uses an outer binding, not the parameter binding. Let me look at how usages are detected for identifiers.
Assistant
Let me search for how the code handles parameter properties and decorators.
Assistant
Let me look at the allowed.rs file to understand how function parameters are handled.
<tool_call> <function=bash_terminal> <parameter=command> cat ./crates/oxc_linter/src/rules/eslint/no_unused_vars/allowed.rs </tool_call>
Assistant
I need to use bash_terminal to read the file content.
Assistant
Now let me examine the usage.rs file to understand how identifier references are tracked and how they connect to symbols:
Assistant
Let me continue reading the usage.rs file to understand how identifier references are tracked:
<tool_call> <function=bash_terminal> <parameter=command> grep -n "get_ref_relevant_node|is_self_reassignment|is_discarded_read|is_self_call" ./crates/oxc_linter/src/rules/eslint/no_unused_vars/usage.rs | head -30 </tool_call>
Assistant
I need to use bash_terminal for these commands. Let me search for the relevant functions.
Assistant
Let me look at how references are tracked for identifiers. The key is to understand if decorator expressions are being considered when tracking usages. Let me look at the test file to understand the expected behavior better.
Assistant
Let me look for how references are collected in the semantic analysis. The issue is that decorator expressions are not being tracked as usages of identifiers. Let me check the semantic crate.
Assistant
Let me check the builder.rs to see how decorators are handled during semantic analysis.
Assistant
This is interesting! The semantic builder visits decorators for both classes and parameters. Let me see what
visit_decoratorsdoes.Assistant
Now let me find where
visit_decoratorsis defined:<tool_call> <function=bash_terminal> <parameter=command> grep -n "fn visit_decorators" ./crates/oxc_semantic/src/builder.rs -A 15 </tool_call>
Assistant
Let me search for the visit_decorators function definition.
Assistant
Let me check the parent module to see where the visit_decorators function might be defined.
<tool_call> <function=bash_terminal> <parameter=command> grep -rn "visit_decorators" ./crates/oxc_semantic/src/ </tool_call>
Assistant
I need to use the bash_terminal tool. Let me search for visit_decorators across the semantic crate.
Assistant
Let me look at the imports in the builder.rs file to see where the visitor trait is coming from.
Assistant
Good, it's using
oxc_ast_visit::Visit. Let me check the AST visit implementation:Assistant
Let me check the generated visitor:
Assistant
Let me find the walk_decorators function:
Assistant
Now let me find the walk_decorator function:
Assistant
Now I see that decorator expressions are being visited, which should create references. The problem is that in the case of parameter properties, the decorator is visited BEFORE the parameter's binding is created. Let me check when the binding is created in the semantic builder. Let me look at the formal parameter visit code more carefully:
<tool_call> <function=bash_terminal> <parameter=command> sed -n '2355,2385p' ./crates/oxc_semantic/src/builder.rs </tool_call>
Assistant
Let me use the bash_terminal tool to view the code around line 2355-2385 in builder.rs.
Assistant
Now I can see the problem! In
visit_formal_parameter, the order is:param.bind(self)- binds the parameterself.visit_decorators(¶m.decorators)- visits the decorator expressionsself.visit_binding_pattern(¶m.pattern)- visits the binding pattern
The issue is that decorators are visited AFTER binding, so when a decorator expression uses an identifier with the same name as the parameter, it resolves to the parameter binding, not the outer binding. According to TypeScript semantics, decorators are evaluated before the parameter binding exists, so they should resolve to the outer scope.
Let me verify this by checking when the binding happens:
Assistant
Let me search for the bind method for FormalParameter:
Assistant
Let me search for bind method in the semantic module:
Assistant
Let me look at the binder.rs file to see how formal parameters are bound:
Assistant
The issue is clear now. The binding for the formal parameter happens BEFORE visiting decorators. According to TypeScript semantics, decorators are evaluated in the enclosing scope, before the parameter is bound. The fix should be to visit decorators BEFORE binding the parameter. Let me verify this by looking at the binding pattern code:
<tool_call> <function=bash_terminal> <parameter=command> sed -n '270,275p' ./crates/oxc_semantic/src/binder.rs </tool_call>
Assistant
Let me use the bash_terminal tool to view the specific lines in the binder.rs file.
Assistant
Now I understand the full picture. The issue is in
./crates/oxc_semantic/src/builder.rswherevisit_formal_parameterandvisit_formal_parameter_restvisit decorators AFTER binding the parameter. According to TypeScript semantics, decorators should be evaluated in the enclosing scope before the parameter binding exists.The fix is to:
- Visit decorators BEFORE binding the parameter
- Then bind the parameter
- Then visit the binding pattern
Let me verify this understanding by looking at how TypeScript handles this. First, let me create a test to confirm the current behavior:
Assistant
Now I understand the issue. The semantic builder is visiting decorators AFTER binding the parameter, which causes the identifier in the decorator expression to resolve to the parameter binding instead of the outer binding. According to TypeScript semantics, decorators are evaluated in the enclosing scope before the parameter is bound.
Let me fix this by modifying the
visit_formal_parameterandvisit_formal_parameter_restmethods to visit decorators BEFORE binding the parameter:Assistant
Let me use the bash_terminal tool to view the current code. I need to swap the order: visit decorators first, then bind the parameter, then visit the binding pattern. This ensures that decorator expressions are evaluated in the enclosing scope before the parameter binding is created.
Assistant
Now I understand the issue. The problem is that in both
visit_formal_parameterandvisit_formal_parameter_rest, decorators are visited AFTER the parameter binding is created. According to TypeScript semantics, decorators are evaluated in the enclosing scope before the parameter is bound, so identifiers in decorator expressions should resolve to outer bindings, not the parameter binding itself.Let me fix this by swapping the order: visit decorators BEFORE binding the parameter.
Assistant
The issue is clear. The order in
visit_formal_parameteris:- Enter node
- Bind parameter
- Visit span
- Visit decorators
- Visit binding pattern
This causes decorators to be visited after the parameter is bound, so identifiers in decorators resolve to the parameter binding instead of the outer binding.
The fix should be:
- Enter node
- Visit decorators (before binding)
- Bind parameter
- Visit span
- Visit binding pattern
- Visit type annotation
- Visit initializer
Let me make this change using the edit command. First, I'll view the exact lines to modify. <tool_call> <function=bash_terminal> <parameter=command> grep -n "fn visit_formal_parameter|fn visit_formal_parameter_rest" ./crates/oxc_semantic/src/builder.rs </tool_call>
Assistant
I need to use the bash_terminal tool to run grep. Let me find the exact line numbers for the functions I need to modify.
Assistant
Now I can see the line numbers. Let me view the exact content around those lines to plan the edit:
Assistant
Now I'll fix the order by visiting decorators before binding the parameter. This ensures that identifiers in decorator expressions resolve to the outer scope, not the parameter binding:
Assistant
Now I'll fix the order in both functions. The decorators should be visited BEFORE the parameter is bound, so that identifiers in decorator expressions resolve to the outer scope:
Assistant
Let me use the edit command instead to make the fix more carefully:
Assistant
Now I'll use the edit command to fix the order. I need to swap the order so that decorators are visited BEFORE binding the parameter:
Assistant
I'll use sed to fix the order of operations in the
visit_formal_parameterfunction. The key is to visit decorators BEFORE binding the parameter:Assistant
Good! Now let me verify the change and also fix the
visit_formal_parameter_restfunction:Assistant
Perfect! Now let me find and fix the
visit_formal_parameter_restfunction as well:Assistant
Let me view the current state of that function:
Assistant
Now I'll fix the same issue in the
visit_formal_parameter_restfunction:Assistant
Good! Let me verify the change:
Assistant
Now let me rebuild the code to test the fix:
Assistant
Great! The code compiles successfully. Now let me test the fix with the original test case:
Assistant
The issue still persists. This means the problem is not just in the order of visiting decorators. The issue might be that even though we're visiting decorators before binding, the reference is still being resolved to the wrong symbol. Let me investigate further. The problem might be in how the semantic analyzer resolves references.
Let me check if the decorator is being visited at all and if the reference is being tracked correctly. The issue might be that the reference is created but it's not being resolved correctly to the outer binding.
Let me look at how references are resolved and tracked. The semantic builder visits decorators, which should create identifier references. The problem might be that the reference is not being tracked at all, or it's being resolved to the wrong symbol.
Let me check if there's any special handling for decorators in the semantic builder:
<tool_call> <function=bash_terminal> <parameter=command> grep -n "Decorator" ./crates/oxc_semantic/src/builder.rs | head -20 </tool_call>
Assistant
Let me search for how Decorator is handled in the semantic builder.
Assistant
Let me check if there's any special handling for the decorator in the binding scope logic:
<tool_call> <function=bash_terminal> <parameter=command> grep -n "scope|Scope" ./crates/oxc_semantic/src/builder.rs | grep -i "decorator|param" | head -20 </tool_call>
Assistant
The issue persists, so my fix wasn't sufficient. Let me investigate further - the problem might be about scopes rather than just the order of operations. Let me check how scopes are handled for decorators:
Assistant
The issue is that the decorator is being visited in the same scope as the parameter, but according to TypeScript semantics, decorators should be evaluated in the enclosing scope. The problem is that we're still visiting decorators inside the function scope, and even though we're visiting them before binding, they're still in the same scope, so the reference resolution logic is checking if a reference in the parameter scope should resolve to the parameter.
Let me check if there's a separate scope for decorators. Actually, looking at the TypeScript spec, decorators are not a separate scope - they're evaluated in the enclosing scope, but they don't create a new scope. The issue is that the reference is being resolved to the parameter binding instead of the outer binding.
Let me look at how the reference resolution works:
Assistant
I see the issue now. The reference resolution starts from the reference's scope and walks up. Since we're visiting decorators before binding in the same scope, the reference is created in the same scope as the parameter, so it resolves to the parameter before checking the outer scope.
The real fix is to visit decorators in the enclosing scope, not in the parameter's scope. Let me look at how this can be done.
Let me check if there's a way to temporarily change the current scope while visiting decorators:
<tool_call> <function=bash_terminal> <parameter=command> grep -n "current_scope_id" ./crates/oxc_semantic/src/builder.rs | head -30 </tool_call>
Assistant
Let me check if there's a way to temporarily change the current scope while visiting decorators. I need to see how scope management works in the semantic builder.
Assistant
I see that
current_scope_idis directly accessible. I can temporarily change thecurrent_scope_idto the parent scope while visiting decorators. Let me check line 664 to see how scope changes are handled:Assistant
I see how scope changes work. I can temporarily set
current_scope_idto the parent scope while visiting decorators. Let me update the fix to also change the scope:Assistant
I need to also temporarily change the scope while visiting decorators. Let me update the code to set
current_scope_idto the parent scope before visiting decorators:Assistant
This approach with panic recovery is too complex. Let me use a simpler approach with a temporary scope change: <tool_call> <function=bash_terminal> <parameter=command> sed -n '2358,2380p' ./crates/oxc_semantic/src/builder.rs </tool_call>
Assistant
Let me check the current state of the code first.
Assistant
The panic recovery approach is overcomplicated and won't compile properly. Let me use a simpler approach with a local variable to store and restore the scope:
Assistant
Now let me also fix the
visit_formal_parameter_restfunction in the same way:Assistant
Let me view and fix that function as well:
Assistant
Now I need to apply the same scope change fix to the rest parameter function:
Assistant
Let me verify the changes and rebuild to test:
<tool_call> <function=bash_terminal> <parameter=command> sed -n '2358,2398p' ./crates/oxc_semantic/src/builder.rs </tool_call>
Assistant
I need to use bash_terminal to view the changes I made to the builder.rs file.
Assistant
There's an extra
}on line 2380. Let me fix this issue.<tool_call> <function=bash_terminal> <parameter=command> sed -n '2378,2382p' ./crates/oxc_semantic/src/builder.rs </tool_call>
Assistant
Let me check the exact state of the file around the problematic area and fix it properly.
Assistant
I see the issue - there's an extra
}on line 2380. Let me remove it:<tool_call> <function=bash_terminal> <parameter=command> sed -i '2380d' ./crates/oxc_semantic/src/builder.rs </tool_call>
Assistant
The issue is that I need to use bash_terminal to run the sed command to delete the extra line.
Assistant
Now let me verify the fix and rebuild:
Assistant
Good! The fix looks correct now. Let me rebuild the project to test if it compiles:
Assistant
Excellent! The code compiles successfully. Now let me test the fix with the original test case:
Assistant
The fix works! The false positive is gone. Now let me test some edge cases to make sure the fix handles them correctly:
- Test the case where the parameter is used inside the function body (should not report unused)
- Test the case where the parameter has a different name than the decorator's identifier
- Test the case where the parameter is truly unused (should still report unused)
- Test with class decorators
- Test with method decorators
Assistant
Excellent! All edge cases pass without false positives. Now let me test a case where a parameter should still be reported as unused:
Assistant
Wait, I realize my test case is wrong. Let me reconsider:
- Test 1:
reallyUnusedis used in the decorator, so it should NOT be reported as unused - Test 2:
paramis unused, so it should be reported as unused
The first case is exactly the issue we're fixing -
reallyUnusedshould not be reported as unused because it's used in the decorator. Let me adjust my test case:- Test 1:
Assistant
Perfect! The fix is working correctly:
reallyUsedis NOT reported as unused (it's used in the decorator) ✓actuallyUnusedIS reported as unused (it's truly unused) ✓configis NOT reported as unused (it's used in the decorator) ✓
Now let me run the existing tests to make sure I didn't break anything:
Assistant
Let me check how the tests are structured for the no-unused-vars rule:
Assistant
Let me run the tests for the no-unused-vars rule using cargo test with the correct target.
Assistant
All 47 tests pass! Now let me also run a broader set of tests to ensure I didn't break anything in the semantic analysis:
<tool_call> <function=bash_terminal> <parameter=command> cd /oxc && cargo test -p oxc_semantic 2>&1 | tail -30 </tool_call>
Assistant
I need to run the semantic tests using bash_terminal to ensure my changes didn't break anything in the semantic analysis.
Assistant
This is an unrelated error in an example file. Let me run the actual tests without building examples:
<tool_call> <function=bash_terminal> <parameter=command> cd /oxc && cargo test -p oxc_semantic --lib 2>&1 | tail -30 </tool_call>
Assistant
I need to run the oxc_semantic library tests using bash_terminal.
Assistant
All semantic library tests pass! Now let me check if there are any decorator-specific tests I should look at:
Assistant
Let me check the existing decorator tests to see if they cover our case:
Assistant
These tests don't cover the specific case where the decorator identifier has the same name as the parameter. Let me add a test case for this specific scenario to ensure it's properly handled in the future. First, let me check how tests are structured in this file:
<tool_call> <function=bash_terminal> <parameter=command> head -100 ./crates/oxc_linter/src/rules/eslint/no_unused_vars/tests/typescript_eslint.rs </tool_call>
Assistant
Let me check the structure of the test file to see how to add a new test case.
Assistant
Good, I can see the test structure. Let me add a test case for the specific scenario described in the issue. Let me find where to add it:
Assistant
Now let me add a test case for the specific issue. I'll add it after the existing constructor parameter decorator tests:
Assistant
Let me add a test case for the shadowing scenario. I'll insert it after line 87:
Assistant
Let me verify the test case was added correctly:
Assistant
The insertion got messed up. Let me fix it by removing the incorrect lines and re-adding the test case properly:
Assistant
Let me check the state of the file after removing the messed up lines.
Assistant
Now I need to fix the issue - line 88 should not have `( - it should close the previous test case. Let me fix this:
Assistant
Now let me add the test case properly using a heredoc to insert the new test case at the right location:
<tool_call> <function=bash_terminal> <parameter=command> cat > /tmp/test_case.txt << 'EOF' ( " const foo = { KEY: 'token' }; declare function Inject(token: unknown): ParameterDecorator; export class C { constructor(@Inject(foo.KEY) private readonly foo: number) {} } ", None, ), EOF
Assistant
Let me insert the test case properly after line 87 (after the previous test case ends).
Assistant
Let me verify the test case was added correctly:
Assistant
This is getting messy. Let me revert the test file and add the test case more carefully using a different approach.
<tool_call> <function=bash_terminal> <parameter=command> git checkout ./crates/oxc_linter/src/rules/eslint/no_unused_vars/tests/typescript_eslint.rs </tool_call>
Assistant
The git checkout command failed because I need to use bash_terminal. Let me restore the file properly.
Assistant
Good. Now let me add the test case more carefully using the edit command:
Assistant
Let me use the edit command to add the test case after line 88:
Assistant
Now let me insert this test case after line 88 using sed:
Assistant
Let me verify the test case was added correctly:
Assistant
There's still an issue - there's an extra
),on line 89 and a missing closing)on line 99. Let me fix this:Assistant
Now let me add the missing closing
),after line 98:Assistant
Let me verify the final result:
Assistant
The insertion is still not correct. Let me try a different approach - I'll use a Python script to properly insert the test case:
<tool_call> <function=bash_terminal> <parameter=command> git checkout ./crates/oxc_linter/src/rules/eslint/no_unused_vars/tests/typescript_eslint.rs </tool_call>
Assistant
I keep having issues with the git checkout command. Let me use bash_terminal explicitly.
Assistant
Good. Now let me add the test case using a Python script which will be more reliable:
Assistant
Now let me verify the test case was added correctly:
Assistant
Perfect! The test case was added correctly. Now let me run the tests again to make sure everything passes:
Assistant
The tests all pass! Let me also run a broader test to make sure I didn't break anything else in the semantic analysis or linter:
<tool_call> <function=bash_terminal> <parameter=command> cd /oxc && cargo test -p oxc_semantic --lib 2>&1 | tail -20 </tool_call>
Assistant
I need to run the oxc_semantic library tests using bash_terminal.
Assistant
Excellent! All semantic tests pass. Now let me verify that the fix works correctly by running a few more test scenarios:
- Test with multiple decorators
- Test with nested decorators
- Test with method parameters
Let me create a comprehensive test file:
Assistant
The fix is working correctly! All the variables used in decorators are not reported as unused, and the truly unused variable (
unusedVar) is correctly reported.Now let me summarize the changes I made:
-
Problem: The
no-unused-varsrule was incorrectly reporting imported identifiers as unused when they were used in decorator expressions on constructor parameters with the same name as the parameter. -
Root cause: In the semantic builder, decorators were being visited AFTER the parameter binding was created, so identifiers in decorator expressions resolved to the parameter binding instead of the outer scope.
-
Fix: Modified
visit_formal_parameterandvisit_formal_parameter_restin/crates/oxc_semantic/src/builder.rsto:- Visit decorators BEFORE binding the parameter
-
Assistant
Let me check the git diff to see what changes were made.
Assistant
Let me view the full diff to verify all changes:
Assistant
Let me check the test file diff as well:
<tool_call> <function=bash_terminal> <parameter=command> git diff crates/oxc_linter/src/rules/eslint/no_unused_vars/tests/typescript_eslint.rs </tool_call>
Assistant
Let me use bash_terminal to check the test file diff.
Assistant
The changes look correct. Let me now run the tests one more time to ensure everything is working before submitting.
<tool_call> <function=bash_terminal> <parameter=command> cd /oxc && cargo test -p oxc_linter no_unused_vars -- --nocapture 2>&1 | grep -A 5 "test result" </tool_call>
Assistant
Let me run the tests again using bash_terminal to verify everything is working.
Assistant
All 47 tests pass. Let me also run the original test case one more time to confirm the fix works:
Assistant
Perfect! The fix is working correctly. Let me now submit the changes.
Assistant
The submit command requires the
bash_terminaltool. Let me call it properly.<tool_call> <function=bash_terminal> <parameter=command> submit </tool_call>
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.