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 the codebase.
Assistant
Let me look at the binder.rs to understand how parameter properties and scopes are handled.
Assistant
Let me see the rest of the FormalParameter bind function, since parameter properties are what matter here.
Assistant
Now I understand the binding. The FormalParameter bind doesn't deal with TypeScript parameter properties specially - the parameter property is bound as a function-scoped variable. But there's a separate mechanism for declaring parameter properties as class members. Let me search for where parameter properties (accessibility modifiers like
private/readonly) are turned into class properties.Assistant
I see the issue now. In
visit_formal_parameter,param.bind(self)is called BEFOREvisit_decorators(¶m.decorators). So the parameter symbolfoois declared before the decorator@Inject(foo.KEY)is visited, causingfooin the decorator to resolve to the parameter binding instead of the outerconst foo. Let me verify by reproducing.Assistant
I've reproduced the issue. Now let me understand the scoping better. Let me look at how the function scope and parameter scope are created, and how references are resolved.
Assistant
So
visit_formal_parametersis called within the function scope. Let me look atvisit_formal_parametersandresolve_references_for_current_scope.Assistant
The default
walk_formal_parametervisits decorators BEFOREvisit_binding_pattern, but the SemanticBuilder override callsparam.bind(self)(which declares the symbol) BEFOREvisit_decorators. Let me look at the reference resolution logic to understand the full picture.Assistant
Let me look at how identifier references are resolved.
Assistant
Now let me look at
visit_identifier_referenceto understand when references are added.Assistant
Let me look at
declare_referenceto see whether it adds to unresolved_references immediately.Assistant
Let me check
declare_symbolto see which scope the parameter is bound on.Assistant
Let me look at how
visit_classenters scopes and how constructor is visited.Assistant
Let me search for where parameter properties create class bindings.
Assistant
Let me check how the sibling issue #20670 (type param shadowed by function arg) was handled, since it's described as the "same family."
Assistant
Let me look at the d9040ff92 commit which added tests for "decorated parameter renames" — this might give insight into how parameter decorators are handled.
Assistant
Let me search the no_unused_vars test files for existing decorator tests to understand what behavior is expected.
Assistant
Let me look at the pass cases in the no_unused_vars tests.
Assistant
Let me test several variants to understand the scope of the bug.
Assistant
Let me look at the full set of decorator-related pass tests in typescript_eslint.rs.
Assistant
Let me check how the transformer handles parameter decorators for hints on evaluation scope.
Assistant
Let me look at the exact code for
visit_formal_parameterandvisit_formal_parameter_restto edit them.Assistant
Now let me check
scope_parent_idmethod exists on scoping.Assistant
Let me check
create_ast_nodeto see if it records the scope per node.Assistant
Let me check
visit_binding_identifierto ensure bind-before-pattern order is preserved.Assistant
SemanticBuilder doesn't override visit_binding_identifier, so it uses the default walk. Let me check walk_binding_identifier.
Assistant
Let me look at the Reference struct and how its scope_id can be set/mutated.
Assistant
Let me check the signature of
visit_decorators.Assistant
Let me check the
decoratorsfield type on FormalParameter and FormalParameterRest.Assistant
Now let me implement the fix. First, let me view the top of builder.rs for imports.
Assistant
Now let me implement the fix. I'll edit
visit_formal_parameterandvisit_formal_parameter_restto visit decorators in the enclosing (class) scope.Assistant
Let me verify the resulting code.
Assistant
Now let me build and test against the reproduction.
Assistant
Let me check how
Vecis imported in builder.rs and where to put the helper method.Assistant
So
Vecin builder.rs refers tostd::vec::Vec(the default). But theVec<'a, ...>in the trait method usesoxc_allocator::Vec. How does the trait method compile? Because the traitVisitis imported, and the method signature is fixed by the trait — when I overridevisit_decorators, I must match the trait signature which usesoxc_allocator::Vec. But I'm not overriding visit_decorators; I'm calling it.Wait, actually the existing code calls
self.visit_decorators(¶m.decorators)whereparam.decoratorsisoxc_allocator::Vec<'a, Decorator<'a>>. So¶m.decoratorsis&oxc_allocator::Vec<...>. The trait method takes&Vec<'a, Decorator<'a>>where in the trait's scope Vec is the allocator Vec. So calling works because the argument type matches.For my helper, I wrote the parameter type as
&Vec<'a, Decorator<'a>>butVecin builder.rs scope isstd::vec::Vec, which takes 0 lifetime args. So error. I need to use the allocator Vec type. Let me check the actual name. The allocator Vec isoxc_allocator::Vec. Let me look at how it's referenced elsewhere in the semantic crate.Assistant
Let me check imports in builder.rs and how the allocator Vec is referenced.
Assistant
I need to use the fully-qualified
oxc_allocator::Vectype and move the helper out of theimpl Visitblock. Let me find the inherent impl block.Assistant
There are two inherent impl blocks: at 143 and 2665. The
impl Visit for SemanticBuilderstarts somewhere in between. Let me find where the Visit impl starts.Assistant
The simplest fix is to inline the logic in both visit functions rather than using a separate helper (which can't live in the
impl Visitblock). Let me revert the helper and inline.Assistant
Build succeeded. Now let me test the reproduction.
Assistant
The parameter property case (test.ts) is fixed. Let me check what test2.ts and test3.ts now report.
Assistant
The outer
const foois no longer reported — the false positive is fixed. For plain (non-property) parameters (test2/test3), the parameter itself is now correctly reported as unused (the decorator uses the outer binding, not the parameter), which matches TypeScript semantics. Let me verify the issue's exact example and run the existing tests.Assistant
The issue's exact example now produces no diagnostics. Let me run the existing no-unused-vars tests.
Assistant
All no-unused-vars tests pass. Now let me run the broader semantic tests to ensure I didn't break scope analysis.
Assistant
That's an unrelated example build error (cfg.rs requires the cfg feature). Let me run just the lib tests.
Assistant
Let me find and run the semantic snapshot tests, which might be affected by scope changes.
Assistant
Let me search for any snapshot or conformance tests involving parameter decorators across the repo.
Assistant
There are typescript-eslint decorator fixtures. Let me look at them, especially
parameter-property.tsandparameter.ts.Assistant
Let me find the test that runs these decorator fixtures and check if there's a snapshot comparison.
Assistant
Let me check if these decorator fixtures have associated
.snapfiles or a snapshot-based test runner.Assistant
There are
.snapfiles. Let me look at them to understand what they check.Assistant
This is the key snapshot. The
decoratorreferences currently havescope_id: 3(the function scope). With my change, they'd getscope_id: 2(the class scope). Let me find the snapshot test runner.Assistant
Let me check the git history of these decorator fixture snapshots to understand the expected behavior and whether they're aligned with typescript-eslint.
Assistant
The fixtures were copied from typescript-eslint's scope-manager (the test cases, not the expected output). The snapshots are oxc's own generated output. Let me determine what typescript-eslint actually does for parameter decorator scope by checking its source if available.
Assistant
Let me look at how oxc conformance compares against typescript-eslint, and check if there's a recorded expected scope for decorators.
Assistant
Let me try installing @typescript-eslint/scope-manager to verify the actual scope behavior for parameter decorators.
Assistant
Node is not installed. Let me check if there's a way to get node. The repo uses pnpm. Let me check if there's a node binary somewhere.
Assistant
Node isn't available. Let me examine the commit that copied the typescript-eslint decorator fixtures to understand the expected scope behavior.
Assistant
The
.snapfiles were generated by oxc itself (not copied from typescript-eslint's expected output). Let me examinemethod.snapandclass.snapto see how method/class decorators are currently scoped in oxc, to infer the intended pattern.Assistant
Let me examine the other decorator snapshots to fully understand the impact.
Assistant
Two snapshot files (
parameter.snap,parameter-property.snap) would change fromscope_id: 3toscope_id: 2, which is the correct behavior — consistent with how method decorators (method.snap) are already scoped in the class scope (2). Let me run the broader linter test suite to ensure no other rules break from this scope change.Assistant
All linter tests pass. Now let me run the semantic snapshot tests to confirm exactly which fail (and that they fail only due to the expected scope_id change).
Assistant
The 2 snapshot failures are exactly the expected, correct change (
scope_id: 3 → 2for parameter decorators, matching how method decorators are already scoped). Let me look at theunresolved_referencesstructure to evaluate whether a more surgical fix is feasible that wouldn't perturb the scope metadata.Assistant
Let me check whether the
tasks/coverage/snapshots/semantic_typescript.snapis exercised by tests, and confirm only the 2 expected snapshot files are affected.Assistant
The
semantic_typescript.snapis a transform-coverage snapshot (node-based task, compares resolved symbols, not scope_id) and not a standard cargo test. My change keeps resolved symbols unchanged for non-shadowing cases. Let me run the transformer's decorator tests and the e2e to be thorough.Assistant
Let me search broadly for any test fixtures across the repo that use parameter decorators, to make sure I've covered all affected snapshots.
Assistant
Let me check the transform conformance metadata fixtures with parameter decorators, since my change affects decorator reference scoping.
Assistant
Let me check how transform conformance tests are run and whether they're cargo tests.
Assistant
Let me check the
class/declaration/parameter-propertiesfixtures (parameter properties without decorators) to ensure they're unaffected.Assistant
This fixture has parameter properties but no decorators, so it's unaffected. Only
parameter.snapandparameter-property.snapchange (expected, correct). Let me test some edge cases to be confident in the fix.Assistant
All edge cases pass (0 diagnostics). The
grep -creturns exit 1 when count is 0, which is expected. Let me test the nested-function-in-decorator case and a case where the decorator references a name that exists only as the parameter.Assistant
Edge4 (nested arrow) correctly resolves to the outer
foo, and edge5 (no outer binding) produces no false positive. Let me test the non-property case where the decorator references only the param name.Assistant
The fix is solid across all edge cases. Now let me clean up temp files and review my final diff.
Assistant
Only
builder.rsis modified — no test files touched. Let me review the diff.Assistant
The diff is clean and minimal. The common case (parameters without decorators) is unchanged (just skips a no-op
visit_decorators), so no behavior/perf regression for normal code. Let me verify other dependents ofoxc_semanticstill compile.Assistant
Let me run the minifier and isolated_declarations tests to ensure no semantic-dependent snapshots break.
Assistant
Let me run the oxlint e2e tests as a final check.
Assistant
There's a test failure in oxlint. Let me investigate.
Assistant
This failure (
oxlint-suppressions.json not found) looks like a pre-existing test-infrastructure issue unrelated to my change. Let me verify by stashing my change and running the same test.Assistant
The suppression test mutates fixture files (creates/deletes
oxlint-suppressions.json). The panicked run left them deleted. Let me restore those fixtures and run the specific test in isolation with my change to determine if my change actually causes the failure.Assistant
The single test passes in isolation with my change — so my change doesn't cause it. The earlier failure was due to parallel suppression tests mutating shared fixture files. Let me confirm this is pre-existing flakiness by running the full oxlint suite on the original (stashed) code.
Assistant
Confirmed — this is a pre-existing flaky test issue (parallel suppression tests mutating shared fixture files), occurring on the original code too, unrelated to my change. Let me restore any deleted fixtures and run the non-flaky oxlint tests serially to be safe.
Assistant
Even single-threaded, some suppression tests fail. Let me see the actual failure for
test_fixing_only_ts_go_errors— is it a real assertion failure or the same fixture-file issue?Assistant
This is a real assertion failure about a suppression count mismatch (
no-unused-vars: count 2vs expected{}). Let me examine this test and its fixture to determine if my change affected it.Assistant
This is getting muddled by the shared mutable fixtures. Let me cleanly determine whether
test_fixing_only_ts_go_errorsfails on the original code with clean fixtures.Assistant
Confirmed:
test_fixing_only_ts_go_errorsfails identically on the original code (stashed) with clean fixtures — it's a pre-existing failure, NOT caused by my change. Let me clean up the stray.snap.newfiles and verify the working tree is clean except for my change.Assistant
The working tree is clean — only
builder.rsis modified. The 2 semantic snapshot failures are the correctscope_idchange (tests needing updates, which per instructions are ignored), and the oxlint suppression failures are pre-existing. Let me do a final verification: rebuild, confirm the issue reproduction is fixed, and re-run the no-unused-vars test suite.Assistant
Let me also run the
no-shadowrule tests since the issue involves shadowing, to ensure no regression.Assistant
Let me verify
walk_decoratorshas no side effects for an empty vec (so skipping it is safe).Assistant
walk_decoratorsis a pure no-op for empty vecs, so skipping is safe. Let me verify the build with thecfgfeature (sincevisit_functionnearby uses CFG) to ensure no CFG-related breakage.Assistant
Build and lib tests pass with the
cfgfeature. Let me run the integration tests withcfgas well (they include control-flow snapshots).Assistant
All tests pass with the
cfgfeature. Let me do a final check that the working tree contains only my intended change (no stray untracked files) and review the final diff once more.Assistant
The diff is minimal and correct. For better maintainability, let me refactor the duplicated logic into a helper method in the inherent impl block (the trait impl block can't host non-trait methods). First, let me revert to a cleaner version using a helper.
Assistant
Now let me rebuild and re-verify everything still works.
Assistant
The refactor works. Let me re-run the key test suites to confirm nothing broke.
Assistant
Let me confirm the 2 semantic snapshot tests still show the expected (correct)
scope_idchange with the refactored code.Assistant
Exactly the 2 expected snapshot failures, all showing the correct
scope_id: 3 → 2change for parameter decorators (now consistent with method decorators inmethod.snap). These are tests needing updates to reflect the correct behavior change, which per instructions I leave untouched. Let me review the final diff and confirm a clean working tree.Assistant
The fix is complete and verified. Let me provide the final checklist reasoning 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.