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 exploring the repository structure to understand the codebase.
Assistant
Let me explore the linter rules and find no-unused-vars:
Assistant
Let me reproduce the issue first:
Assistant
Building the release version takes too long. Let me try debug or use cargo's offline check first:
Assistant
Good, I've reproduced the issue. Now let me understand how the semantic analyzer handles TypeScript parameter property decorators:
Assistant
Now I see the issue. In
visit_formal_parameter, the decorators are visited AFTERparam.bind(self). Theparam.bind(self)likely binds the parameter property, which means when the decorator is visited, the parameter already exists in scope, andfooin@Inject(foo.KEY)resolves to the parameter property.Let me check the binder for FormalParameter:
Assistant
Now let me check what the visit_decorators does:
Assistant
Let me check the structure of FormalParameter:
Assistant
Let me look at the visit_function more carefully, and look for how the parameter reference is resolved:
Assistant
Let me look at how TypeScript handles this:
Assistant
Let me look at the current scope of resolution:
Assistant
Let me check what the typescript-eslint project does for parameter properties:
Assistant
Let me check the related GitHub issues mentioned in the bug report:
Assistant
Let me look at the actual semantic issue. The fix needs to be in the semantic builder, since the ordering is wrong:
Assistant
Let me check how the current code handles the scope for resolution:
Assistant
Let me look at
walk_up_resolve_reference:Assistant
So we have
walk_up_resolve_referencewhich walks up scopes and tries to resolve each scope's binding. The references are stored inself.scoping.references[reference_id], and we can determine which scope the reference was made in viareference.scope_id().So the fix could be:
- When visiting a parameter property's decorator, store the parameter property in a list of "currently binding parameters" or some such.
- When resolving references, if the reference was made in a parameter property's decorator, skip bindings that match the parameter property name in the current function scope.
But actually, a simpler approach is to just visit the decorator BEFORE binding the parameter. If we do that, when the decorator's expression references the name (e.g.,
foo), the reference is added to the unresolved list with its scope_id set to the current function scope (where the parameter would be). But since the parameter hasn't been declared yet, the parameter binding doesn't exist, sowalk_up_resolve_referencewould walk up and find the outerfoo.Wait, let me check whether visiting the decorator after the parameter binding matters. Let me check
visit_identifier_reference:Assistant
Let me look at the ReferenceFlags and understand what flags exist:
Assistant
Let me look at how
Referenceis structured and check Resolution flow:Assistant
Let me first understand how the issue should be resolved. Let me look at the related issue mentioned (#20670):
Assistant
Let me check the existing scope analyzer for parameter handling. Let me look at how it handles function parameter resolution now:
Assistant
Now let me understand the fix approach. Looking at the bug more carefully:
The issue is that
visit_formal_parameterbinds the parameter BEFORE visiting the decorator. We need to swap the order so that parameter property decorators are visited BEFORE the parameter is bound, and then immediately resolve the decorator's references so they don't accidentally resolve to the parameter being bound.Let me look at how to do this. First, let me check what happens with related cases:
Assistant
The issue also exists when the parameter is NOT a parameter property. So this is a general issue with parameter shadowing in decorators, not just parameter properties.
Let me also check if there are any related issues in tsc/eslint:
Assistant
Let me search for how typescript-eslint handles this to confirm:
Assistant
Let me check the existing fix for the related issue:
Assistant
Let me look at how the unresolved references are managed:
Assistant
Let me look at
visit_binding_identifierandparam.bind:Assistant
Let me check how methods are visited:
Assistant
Let me check no-unused-vars options:
Assistant
Now let me implement the fix. Let me first take a look at the relevant functions and understand all the existing behavior I need to consider:
Assistant
Let me look at the relevant parts more carefully to understand the structure:
Assistant
Let me check declare_reference:
Assistant
Now let me implement the fix. I'll modify both
visit_formal_parameterandvisit_formal_parameter_rest:Assistant
Now let me make the edit:
Assistant
Let me verify the change:
Assistant
Now let me build and test:
Assistant
Build succeeded. Now let me test the reproduction:
Assistant
Excellent! The fix is working. Let me test more cases including ones that should still work correctly:
Assistant
Now let me run the existing test suite to see if my change breaks anything:
Assistant
Just an example error, not a test failure. Let me try running with --no-run or check this example first:
Assistant
Let me see all the failed snapshot tests:
Assistant
I see that the snapshot tests are failing because my fix changes the resolution behavior for parameter default values that reference later parameters. I need to be more careful and only resolve the decorator's references, not all unresolved references. Let me update the fix:
Assistant
Now let me build and test:
Assistant
All test cases work correctly. Now let me run the existing tests to make sure they all pass:
Assistant
The cfg example error is pre-existing (unrelated to my fix). Let me run the semantic tests:
Assistant
All integration tests pass. Now let me run the linter tests:
Assistant
All 1144 linter tests pass. Let me also run the broader test suite to make sure nothing's broken:
Assistant
All tests pass. Let me also run a broader test:
Assistant
Good - the test failure is pre-existing. Let me restore my changes:
Assistant
The suppression tests have tests that delete some of these files. Looks like the test failure in apps/oxlint was related to those tests, and the tests are designed to handle missing files. But the deletion of those files is normal test behavior.
Let me restore from git stash:
Assistant
Good - my only change is in
crates/oxc_semantic/src/builder.rs. Let me run the failing oxlint test to confirm it's pre-existing:Assistant
The linter library tests all pass. Now let me also check the relevant tests in the linter to make sure nothing has changed:
Assistant
All 47 no-unused-vars tests pass. Let me also look at the snapshot tests related to decorators:
Assistant
Let me make sure my test reproduces correctly. The original test case:
Assistant
Original bug case is now fixed (no warnings). Let me also test additional cases:
Assistant
The fix works correctly across multiple test cases. Let me also check some transformer tests:
Assistant
All tests pass. Let me run a broader check including the snapshot tests:
Assistant
Let me also run a couple of specific snapshot tests to verify nothing else is broken:
Assistant
Let me also look at the snapshot test more carefully:
Assistant
Let me also test more edge cases:
Assistant
The edge case works correctly. The
Injectandbar(in the decorator) andfoo(parameter) are all correctly resolved.Let me also run more tests:
Assistant
The fix correctly handles multiple decorators.
CONFIG_Ais correctly identified as used in the decorator, butCONFIG_Bis correctly reported as unused (since it's not used anywhere).Let me test more cases:
Assistant
Rest parameter works correctly. Let me also test static method:
Assistant
The complex real-world case works perfectly! Only the unused import
Moduleis reported. Let me also test a TypeScript file with some other patterns:Assistant
OK so destructured parameter properties are a TS error (1187). So I don't need to worry about them.
Let me also test with auto-accessors (TS 4.9+):
Assistant
Let me also run a quick check to ensure the changes don't break anything in the wider codebase:
Assistant
The suppression test failure is pre-existing (it's about test cleanup in destructors). This isn't related to my fix. Let me confirm by running just specific tests:
Assistant
All tests pass. Now let me run oxlint tests but only specific ones (avoid the pre-existing suppression test failures):
Assistant
Confirmed - this is a pre-existing failure unrelated to my fix. Let me restore my changes:
Assistant
Let me verify everything works correctly one more time:
Assistant
The bug is fixed. Let me also do a final test by running the entire semantic test suite once more:
Assistant
All tests pass. Now let me also double check the
transformer/legacy-decoratorworks correctly by running a few specific tests:Assistant
All decorator tests pass. Let me also run a broader test to make sure I didn't miss any snapshot updates:
Assistant
No new snapshots. Let me verify the fix one more time by running my reproduction test:
Assistant
The fix is working correctly. Let me also run a few specific edge case tests to ensure the fix handles them well:
Assistant
All comprehensive tests pass. Each test produces exactly the expected diagnostics:
- Test 1 (parameter property decorator with same-named outer binding): No false positive
- Test 2 (method parameter with same-named outer binding): No false positive
- Test 3 (parameter WITHOUT decorator): Correctly reports
configCas unused (no usage) - Test 4 (multiple parameters with decorators): All correctly identified as used
- Test 5 (decorator with default value): Correctly handled
- Test 6 (static method parameter): Correctly identified as used
- Test 7 (forward reference to later parameter): Still works correctly
- Test 8 (rest parameter with decorator): Correctly identified as used
Let me also run a few more relevant tests:
Assistant
Let me look at the snapshot tests once more to ensure no regressions:
Assistant
No new snapshots and no modified snapshot files. The fix is clean and doesn't introduce any snapshot changes.
Let me verify the final state of the code changes:
Assistant
The diff is clean and focused. Let me also clean up my temporary test files:
Assistant
All temp files are cleaned up. Let me do one more final test to verify the fix:
Assistant
Assistant
My only change is to
crates/oxc_semantic/src/builder.rs. The fix is minimal, focused, and well-commented. Let me submit it:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.