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 start by exploring the repository structure to understand the codebase.
Assistant
This is the Oxc project. Let me explore the structure to find the relevant files.
Assistant
Let me first reproduce the issue using a test file.
Assistant
Let me look at how oxlint is structured to see how to run a single linter check.
Assistant
Let me dig into the semantic builder to understand how parameter property binding and decorators interact.
Assistant
Wait, this is for FormalParameter, not for TSParameterProperty. Let me check how TSParameterProperty is built.
Assistant
Let me trace through what
binddoes for parameters:Assistant
So in
visit_class:visit_decorators(&class.decorators)is called BEFOREenter_scope
This is interesting. Decorators of a class should be evaluated in the enclosing scope.
For method/constructor within a class - let me check how parameters work.
Now I want to look at how visit method works. Let me search for that.
Assistant
Let me check how
visit_functionhandles constructors:Assistant
Now let me check the existing tests for parameter property decorators:
Assistant
Let me trace through the issue. Let me build a quick reproduction script:
Assistant
Now I've reproduced the issue. Let me look at how
no-unused-varsactually detects usage.Assistant
The bug is in the semantic builder. Let me look at how parameter scopes are handled and how the parameter's binding affects decorator references.
Assistant
Let me explore the structure of references and resolution:
Assistant
Let me look more carefully at the AST structure:
Assistant
Interesting! There is no
TSParameterPropertyin the AST kinds. So in oxc, TSParameterProperty is just treated as a regularFormalParameterwith decorators that have accessibility (private/public/protected/readonly/etc.) modifiers.Let me look at how
FormalParameteris structured and where accessibility modifiers come in:Assistant
Let me look at how walk_formal_parameters works:
Assistant
The default
walk_formal_parametervisits decorators BEFORE visiting the binding pattern. So in the default walker, the parameter binding isn't created before the decorator is visited.But in oxc's semantic builder,
visit_formal_parameteris overridden. Let me look at it more carefully:Assistant
Let me look at how visit_binding_pattern works:
Assistant
Let me look at how
accessibilityis used:Assistant
Let me look at the
no_unnecessary_parameter_property_assignmentrule:Assistant
Let me explore the structure of
bound_namesto understand the binding logic:Assistant
Now I have a clear picture. Let me look at the relevant code one more time to understand the change:
Assistant
Let me look at existing tests for parameter properties with decorators:
Assistant
Let me check if any of the existing test cases use parameter properties with decorators:
Assistant
Let me check what's allowed in no-unused-vars:
Assistant
Let me check how unresolved references are resolved:
Assistant
Let me look at the existing resolve mechanism:
Assistant
Now I understand the issue completely. Let me also check
visit_formal_parameter_rest:Assistant
Let me check the
unresolved_referencesinterface:Assistant
Now I have a complete understanding of the issue. Let me implement the fix.
The issue is in
crates/oxc_semantic/src/builder.rs:visit_formal_parameter(line 2357) binds the parameter BEFORE visiting its decorators.- This means any reference to the parameter name in a decorator resolves to the parameter (shadowing outer bindings), instead of the outer binding.
The fix is to visit the decorators first, resolve their references immediately (so they don't see the parameter binding), and then bind the parameter.
Let me first look at the entire
visit_formal_parameterand related code:Assistant
Now let me look at where
unresolved_references_checkpointis defined:Assistant
Now let me implement the fix:
Assistant
Now let me apply the fix. First, I'll add a helper method, then modify
visit_formal_parameterandvisit_formal_parameter_rest:Assistant
Let me look at
FormalParameterRest:Assistant
Let me first look at what other places might use unresolved_references or affect the resolution flow:
Assistant
Now let me implement the fix:
Assistant
Let me look at where to add the new helper method. I'll add it just before
add_redeclare_variable:Assistant
Let me check the result:
Assistant
The bash event expansion messed up my command. Let me revert and try again with a different approach:
Assistant
Let me write the changes to a temporary file first then apply them:
Assistant
Let me verify the change is correct:
Assistant
Now let me modify
visit_formal_parameterandvisit_formal_parameter_rest:Assistant
Let me verify the changes:
Assistant
Now let me build to confirm the fix compiles:
Assistant
Good, it compiles. Now let me build the oxlint binary and test:
Assistant
Now let me test the fix:
Assistant
The fix works. Let me test more edge cases:
Assistant
Let me test more edge cases:
Assistant
Let me test cases that should still report errors (genuinely unused variables):
Assistant
Let me run the existing tests to ensure I didn't break anything:
Assistant
All 47 no-unused-vars tests pass. Let me also run semantic tests:
Assistant
Let me see what's going on with the cfg example - that seems to be a pre-existing issue unrelated to my fix.
Assistant
The cfg example is gated by feature flag. Let me try with the right feature:
Assistant
All semantic tests pass. Now let me run the linter tests:
Assistant
These are pre-existing failures unrelated to my changes. Let me verify by reverting my changes and running the same tests:
Assistant
These failures are pre-existing and unrelated to my fix. Let me restore my changes:
Assistant
Let me verify the changes are still present:
Assistant
Good, changes are still there. Let me also run the existing typescript_eslint tests to make sure they all pass:
Assistant
All 4 typescript_eslint tests passed. Let me also look at more places that might interact with the parameter or decorator binding:
Assistant
All 47 no_unused_vars tests pass. Let me also think about other test areas that might be impacted:
Assistant
All parameter property tests pass. Let me also check the explicit_member_accessibility tests:
Assistant
Let me also test
no_unnecessary_parameter_property_assignment:Assistant
All tests pass. Let me also run the entire typescript test suite to make sure nothing is broken:
Assistant
All 58 TypeScript tests pass. Let me also test some transformer-related areas:
Assistant
Let me run the transformer tests to check:
Assistant
All 41 transformer tests pass. Let me also check minifier and other tests:
Assistant
All 504 minifier tests pass. Let me also test the isolated_declarations crate, which depends on the AST structure:
Assistant
All isolated_declarations tests pass. Let me also check language_server and traversal:
Assistant
All tests pass. Let me now also think about the fix more comprehensively and check the documentation:
Assistant
Let me also do a final verification on the original issue:
Assistant
The original issue is fixed. Let me also test more complex edge cases:
Assistant
Let me also verify the existing tests still pass after my fix:
Assistant
The regression test confirms the fix works correctly. Let me also verify that other parts of the codebase that may use parameter binding still work correctly:
Assistant
Let me also test setter parameters with decorators:
Assistant
Setter works. Let me also check the language server tests:
Assistant
Let me verify the existing typescript_eslint tests still pass with the fix:
Assistant
Let me run the broader semantic tests:
Assistant
All semantic tests pass! Let me also do a quick test for some other crates that might depend on this:
Assistant
All tests pass. Now let me think about whether I should add additional tests to ensure the fix is preserved. Let me check existing tests to see what's already covered:
Assistant
Let me run a more comprehensive check to verify the fix is working:
Assistant
Let me also test with exported classes:
Assistant
Let me look at the final diff:
Assistant
Let me clean up the test files and run the final verification:
Assistant
The changes are clean. Let me also do a final verification by checking the original issue once more:
Assistant
Let me clean up:
Assistant
The fix is minimal and targeted. Let me write a summary of what I did:
Summary
Assistant
Now let me submit the fix:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.