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
I'll start by exploring the repository structure to understand the codebase.
Assistant
Let me explore the repository structure first.
Assistant
This is the oxc repository. The issue is about the
no-unused-varslinter rule having a false positive with constructor parameter properties and decorators. Let me explore the relevant code.Assistant
I need to remember to make only one tool call at a time. Let me search for decorator references in the semantic crate.
Assistant
Let me look at the context around line 2363 and 2379 in builder.rs, where parameter decorators are visited.
Assistant
I can see the issue now. In
visit_formal_parameter,param.bind(self)is called BEFOREself.visit_decorators(¶m.decorators). This means the parameter bindingfoois in scope when the decorator is visited, sofoo.KEYin the decorator resolves to the parameterfooinstead of the outerfoo. Let me look at how binding works.Assistant
Let me look at how formal parameters and their scope are set up.
Assistant
Let me look at how the visit_formal_parameters is defined. It's probably in the generated visitor code. Let me look at the visit_function and visit_method_definition.
Assistant
Now let me reproduce the issue first. Let me find how to run the linter.
Assistant
No node available. Let me build the Rust binary. Let me check the structure.
Assistant
Let me build the oxlint binary in release mode for faster speed, but first let me check the build.
Assistant
Now let me run the linter to reproduce the issue.
Assistant
The
--rulesflag shows the list. Let me run it differently.Assistant
I've reproduced the issue. Now let me understand the fix. The problem is in
visit_formal_parameter— the parameter binding is created before the decorators are visited. Let me look at the relevant code again and understand the scope model.Assistant
Let me understand how reference resolution works, especially the checkpoint and
resolve_references_for_current_scope.Assistant
Let me look at how references are added and resolved.
Assistant
Let me look at the area around line 2230 which has a checkpoint mechanism.
Assistant
Let me look at how
IdentifierReferencereferences are created and resolved.Assistant
Let me find how
MethodDefinitionis visited, since the constructor is a method.Assistant
The visit_method_definition is in the default Visit trait implementation. Let me check the oxc_ast_visit crate for the default visit_method_definition.
Assistant
Let me understand the scope resolution better. The key issue is that decorator references resolve to the parameter binding instead of the outer binding. Let me check how
try_resolve_referenceworks and look at the related issue #20670 fix.Assistant
Let me look at the git history for related fixes, especially #20670 mentioned in the issue.
Assistant
Let me look at the no-unused-vars rule and how it determines references.
Assistant
Let me look at how
current_scope_idis defined and accessed, and check thevisit_decoratorsmethod.Assistant
Let me look at
visit_classto understand the scope structure.Assistant
Let me check
create_ast_nodeto see if nodes track scope, which matters for my approach.Assistant
Let me look at existing semantic tests related to decorators to understand expectations.
Assistant
Let me look at the parameter-property snapshot, which is closer to the bug.
Assistant
Let me check how the semantic tests work and whether snapshots can be regenerated.
Assistant
Let me look at the generated
walk_formal_parametersto understand the structure.Assistant
I now understand the issue. Let me check the
scope_parent_idmethod signature and theReferencetype.Assistant
Let me look at the imports and the area around the formal parameter methods to add a helper.
Assistant
Let me view the exact current code for both methods to edit them.
Assistant
Let me check where
reference_identifieris defined to understand the impl structure.Assistant
Let me view the end of the file to find where to add the helper.
Assistant
Now let me implement the fix. First, I'll add the helper method at the end of the
SemanticBuilderimpl block:Assistant
Now let me add the helper method after
resolve_reference_usages:Assistant
Now let me update
visit_formal_parameterandvisit_formal_parameter_restto use the helper:Assistant
Let me navigate to the lines and use the edit command with line ranges.
Assistant
I moved
param.bindafter the decorator visit. Let me verify thatvisit_binding_identifierdoesn't depend on the bind happening in a specific order relative to the decorator visit. Let me check the binding identifier visit and the binder interaction.Assistant
Now let me build to check for compilation errors.
Assistant
The
Vecneeds to beoxc_allocator::Vec. Let me check how it's referenced elsewhere.Assistant
I need to use the full path for
Vec. Let me update the helper:Assistant
It compiles. Now let me test the reproduction case:
Assistant
The false positive is fixed. Now let me test edge cases. Let me create a comprehensive test script.
Assistant
The edge cases work correctly. Now let me run the semantic tests to see which snapshots are affected.
Assistant
The snapshot tests fail because parameter decorator references now correctly have
scope_id: 2(class scope) instead ofscope_id: 3(function scope) — this is the intended, correct behavior change matching TypeScript semantics. Only 2 decorator snapshots are affected. Let me verify there are no other unexpected failures and check the conformance snapshots.Assistant
Let me verify the
.snap.newfiles show only the expectedscope_idchange.Assistant
Let me reconsider whether I can avoid the snapshot change. Let me check what
visit_binding_identifierdoes and whether binding must precede it.Assistant
The snapshot change is the intended, correct behavior (parameter decorator references now correctly belong to the class scope, matching TypeScript semantics). Per the instructions, broken snapshots that need updating will be ignored by the evaluation. Let me first clean up the insta artifacts, then run the linter tests for
no-unused-varsto ensure no regressions there.Assistant
The artifacts are cleaned up. Now let me run the
no-unused-varslinter tests.Assistant
Let me run the
no-unused-varslinter tests to ensure they pass.Assistant
All
no-unused-varstests pass. Let me run the broader linter test suite to check for regressions, particularly rules that depend on scope resolution.Assistant
Let me see the summary of the test results to confirm there were no failures in the main test runs.
Assistant
All 1144 linter tests pass. Let me also run the semantic library unit tests and check for any other regressions.
Assistant
Let me test the rest parameter decorator case and a few more variations to be thorough.
Assistant
The syntax
...@Inject(foo.KEY) restisn't valid TS syntax. Rest parameter decorators have a different syntax. Let me check the correct syntax. Actually, in TS, decorators on rest parameters... let me check. The commit "parse decorators on rest parameters (#18938)" — let me check the syntax. Actually, I think the syntax is@Inject(...args) ...restor the decorator goes before.... Let me check the FormalParameterRest structure.Actually, let me not worry about the exact rest syntax. Let me check the AST for FormalParameterRest to understand where decorators go.
Assistant
So the syntax is
@Inject(...) ...rest(decorators before...). Let me test that.Assistant
The rest parameter decorator case works correctly. Let me verify
visit_spanisn't affected by the reordering (it moved beforebind). Let me check if the builder overridesvisit_span.Assistant
walk_spanis a no-op, so the reordering is safe. The Decorator nodes remain children of the FormalParameter node (only theirscope_idchanges to the parent). Let me run the transformer tests since they consume semantics and may involve decorators.Assistant
All transformer tests pass. Let me also verify the codegen and check for any other snapshot-based tests that might reference parameter decorator scopes.
Assistant
All tests pass. Let me do a final review of my complete change.
Assistant
Let me check whether the repo's recent semantic changes updated snapshot files, to understand the convention.
Assistant
The repo convention is to update snapshots, but the evaluation instructions explicitly say not to modify tests and that broken tests needing updates will be ignored. I'll follow the evaluation instructions and leave the snapshots unchanged. Let me verify my working tree only contains the intended code change and no stray artifacts.
Assistant
Only the intended file is modified. Let me double-check no
.snap.newartifacts remain anywhere in the repo.Assistant
The snapshot breakage is purely a cosmetic correctness improvement (parameter decorator references now correctly record the class scope), not a bug — resolution targets are unchanged. Let me run a few more scope-consuming crates (minifier, mangler) to be thorough about regressions.
Assistant
Let me run the minifier's full test suite (including exec/snapshot tests) since they heavily use scoping.
Assistant
All minifier tests pass. Let me re-run the semantic test suite to confirm ONLY the 2 expected decorator snapshots fail (and capture the complete failure list).
Assistant
Only the 2 expected decorator snapshots fail, with the correct
scope_id: 3 → 2change. Let me clean up the regenerated.snap.newartifacts.Assistant
I've settled the snapshot decision: per the evaluation instructions, I will NOT modify the existing snapshot test files (they break due to a correct behavior change and will be ignored). Let me do a final verification with the exact issue reproduction plus realistic NestJS-style variants.
Assistant
Both cases work correctly. Let me also verify
no-shadow(a scope-related rule) behaves correctly with the fix, since parameter properties now legitimately shadow outer bindings.Assistant
Let me enable
no-shadowvia a config file to verify it behaves correctly with the fix.Assistant
With
no-shadowenabled, it correctly reports the parameter property shadows the outerconst foo(legitimate shadow). Let me confirm thisno-shadowbehavior is unchanged by my fix (i.e., I didn't alter shadow detection) by comparing against the original code.Assistant
The stash test ran against my already-compiled binary. Let me properly rebuild after stashing to test the original behavior.
Assistant
Excellent! Clear comparison confirms the fix:
- Original:
no-shadow(correct) +no-unused-vars(false positive) = 2 warnings - My fix:
no-shadow(correct, unchanged) + no false positive = 1 warning
The
no-shadowbehavior is unchanged, and the false positive is fixed. Let me rebuild with my fix restored.- Original:
Assistant
The fix is verified. Let me run the scope-related linter rule tests one final time to confirm no regressions.
Assistant
All 1144 linter tests pass. Let me do a final review of the complete diff before submitting.
Assistant
The diff is clean. Let me test one more edge case: a parameter decorator on a non-constructor method (not just constructors).
Assistant
The method case works too, and the method body's
return foostill correctly resolves to the parameter (only decorator references move to the parent scope). Let me check whether thetasks/coverageconformance snapshots (which the related #17929 fix touched) might also be affected by my scope change.Assistant
The
oxc_coverageis a separate binary that runs the TypeScript conformance suite and compares snapshots. This is run viajusttasks, not the standardcargo test. It requires the actual TypeScript test suite files to be present. Let me check if running it is feasible/fast, and whether it's part of the standard test suite.Let me check if
cargo test --package oxc_coverageruns these snapshot comparisons. Let me look at the test/binary structure.Actually, the coverage binary likely needs to download the TypeScript test suite. It might not be runnable in this environment without the test files. Let me check if there's a test that runs it.
Let me check the lib.rs around line 334-361 for the test runner.
Assistant
The coverage tool runs the semantic analysis on the TypeScript/test262/babel test suites and snapshots the results. It needs the actual test files (TYPESCRIPT_PATH, etc.), which are likely git submodules or downloaded. Let me check if those paths exist.
Let me check if the test data exists. If not, I can't run it. Let me check the paths.
Assistant
The TypeScript/Babel/test262 test data directories (submodules) aren't present in this environment, so the coverage conformance tool can't run here. Let me confirm.
Assistant
The conformance test data submodules aren't present, so the coverage tool can't run here (and likely not in the evaluation either). Let me verify what the conformance snapshots actually record, to understand if my scope_id change would even affect them.
Assistant
The conformance snapshots only record pass/fail results (whether semantic builds succeed), not scope details — so my
scope_idchange wouldn't affect them (it doesn't cause panics or build errors). My change only affects the 2 oxc_semantic decorator snapshots (the expected, correctscope_id: 3 → 2). Let me run the coverage crate's own unit tests to be safe.Assistant
No tests there. Let me check the semantic checker's decorator validation to ensure my scope switch doesn't interfere with syntax/scope checks.
Assistant
This
superchecker uses node ancestors (notscope_id), and my change preserves the node tree (Decorator nodes remain children of FormalParameter). Let me check the checker for any decorator validation using scope.Assistant
The checker's decorator handling only involves
superlegality via node ancestors (which my change preserves), so it's unaffected.I've thoroughly verified the fix. Let me do a final confirmation of the working tree state and the reproduction before submitting.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.