Skip to content
TheChatArchiveThe Chat Archive
fable-5· unknownHugging Face ibragim-bad/swe_rebench_07_2026_trajectories

linter: no-unused-vars false positive when constructor parameter property shadows an…

  1. User

    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.ts
    

    What does your .oxlintrc.json (or oxlint.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's no-unused-vars reports 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 foo inside @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 inject registerAs(...)-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 here
    

    Expected

    No diagnostics — foo is used in @Inject(foo.KEY).

    ESLint with @typescript-eslint/parser + @typescript-eslint/eslint-plugin does 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 (namespace shadowed by generic).
    • #2180 — typescript-eslint umbrella tracker.

    Interface

    No new interfaces are introduced.

  2. Assistant

    The linter example didn't flag it — probably rule config. Let me run the actual oxlint binary:

  3. Assistant

    Reproduced. Now let's find where semantic resolves decorator references:

  4. Assistant

    Now add the helper method in the SemanticBuilder impl block (before the Visit impl):

  5. Assistant

    Fixed. Now run the semantic test suite to check for regressions:

  6. Assistant

    Pre-existing example failure (needs a feature flag). Run tests excluding examples:

  7. Assistant

    Now add pass cases to the no-unused-vars rule tests:

  8. Assistant

    The Tester handles TS sources. Add a regression test fn in oxc.rs:

  9. Assistant

    That's just the new snapshot needing acceptance (the fail case correctly emits a diagnostic). Accept it:

  10. Assistant

    Conformance submodules aren't available in this environment (just isn't installed and submodules are empty), so I can't run those. The transformer's legacy-decorator transform depends on semantic scoping, so run its tests:

  11. Assistant

    Transformer unit tests pass; Babel conformance also requires missing submodules. Run remaining checks — clippy and fmt:

  12. Assistant

    All pre-existing. Run clippy on lib + tests (excluding the broken example):

  13. Assistant

    Clean. Let me sanity-check a few more edge cases through oxlint:

  14. Assistant

    All edge cases behave correctly. Quick check that the decorator transform still works (it consumes semantic scoping):

  15. Assistant

    All pass. Final review of the change:

  16. Assistant

    The fix is complete. Summary:

    Root cause

    SemanticBuilder visited TS parameter decorators inside the function scope, after the parameter had been bound. Since reference resolution walks up from the reference's recorded scope, foo in @Inject(foo.KEY) resolved to the same-named parameter property instead of the outer binding — leaving the outer const/import with zero references, which no-unused-vars then flagged.

    Fix — crates/oxc_semantic/src/builder.rs

    Added visit_parameter_decorators, which visits parameter decorators with current_scope_id temporarily set to the function scope's parent (the class scope). This matches TS semantics — parameter decorators are evaluated at class-decoration time in the enclosing class scope, before any parameter binding exists — and matches typescript-eslint's scope manager. Both visit_formal_parameter and visit_formal_parameter_rest now use it.

    Tests

    • New semantic fixture crates/oxc_semantic/tests/fixtures/oxc/ts/decorators/parameter-shadowed-outer-binding.ts + snapshot — confirms the decorator reference attaches to the outer foo (class scope) and the parameter symbol has no references.
    • New linter test test_ts_parameter_decorators in no_unused_vars/tests/oxc.rs — passes for the issue repro and the NestJS @Inject(fooConfig.KEY) import pattern; still fails (correctly) when the decorator doesn't reference the outer binding.
    • Updated 2 existing snapshots (typescript-eslint/decorators/parameter{,-property}.snap) — only change is decorator references now reporting the class scope id instead of the function scope id.

    Verification

    • Repro now clean: oxlint test.ts → 0 warnings; the non-use case still warns.
    • Edge cases checked manually: method (non-constructor) param decorators, arrow closures inside decorators, no-outer-binding case.
    • Legacy decorator transform output verified: hoisted Inject(foo.KEY) correctly references the outer foo.
    • cargo test green for oxc_semantic (72), oxc_linter (1149), oxc_transformer, oxc_traverse, oxc_minifier, oxc_isolated_declarations, oxc_codegen; clippy clean; cargo fmt applied.

    Two pre-existing failures unrelated to this change (verified against the clean tree via git stash): the oxc_semantic cfg example doesn't compile, and oxc_codegen's sourcemap::stacktrace_is_correct fails. Conformance suites (cargo coverage, transform conformance) couldn't run here — git submodules are empty and just isn't installed — so those should be exercised in CI.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.