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

    That example is a toy. Let me run the real oxlint binary.

  3. Assistant

    Reproduced. Now let's determine whether the reference foo inside the decorator resolves to the parameter binding (semantic bug) or the outer const (linter bug).

  4. Assistant

    Now add the helper method in the private impl SemanticBuilder block at the end:

  5. Assistant

    The false positive is gone. Now run the semantic test suite and check for regressions:

  6. Assistant

    Pre-existing issue — the cfg example needs the cfg feature. Let me check how just test invokes it:

  7. Assistant

    Snapshot confirms correct resolution. Now run the full semantic test suite and the linter's no-unused-vars tests:

  8. Assistant

    Now add a regression test to the no-unused-vars rule. Let me find where its TS pass cases live:

  9. Assistant

    All pass. Now run broader regression checks — full linter suite, semantic conformance coverage, and transformer conformance (the decorator transforms rely on this scoping):

  10. Assistant

    No regression (2671/4773 both before and after; the diff removes one mismatch in an already-failing case). Now check the transformer, since decorator transforms consume this scoping:

  11. Assistant

    I'll compare baseline vs. changed runs with the same fixture set:

  12. Assistant

    Now format and lint the changes:

  13. Assistant

    Pre-existing oxc_parser warnings only — nothing from my changes. Let me review the final diff:

  14. Assistant

    One more sanity pass over neighboring crates that consume semantic scoping:

  15. Assistant

    All checks pass. The fix is complete.

    Summary

    Root cause — a semantic-analysis bug, not a linter bug: SemanticBuilder::visit_formal_parameter visited parameter decorators inside the function scope, after the parameter was bound. So in constructor(@Inject(foo.KEY) private readonly foo: number), the foo reference in the decorator resolved to the parameter symbol instead of the outer binding, leaving the outer const foo with zero references — which no-unused-vars then correctly (per its data) but wrongly (per language semantics) reported.

    Fix (crates/oxc_semantic/src/builder.rs): parameter decorators are evaluated at class-decoration time in the scope enclosing the method — before any parameter binding exists. The new visit_parameter_decorators helper visits them with current_scope_id temporarily set to the function scope's parent (the class scope), so references inside them skip the function's own bindings during resolution. Both visit_formal_parameter and visit_formal_parameter_rest now use it. This mirrors the existing treatment of class decorators, which are already visited before entering the class scope.

    Tests added:

    • Semantic fixture crates/oxc_semantic/tests/fixtures/oxc/ts/decorators/parameter-decorator-shadowed-binding.ts + snapshot — verifies the decorator reference resolves to the outer const foo from the class scope.
    • no-unused-vars pass case in tests/oxc.rs (test_classes) with the exact issue repro.
    • Two existing typescript-eslint decorator snapshots updated: decorator references now carry the class scope id (2) instead of the method scope id (3) — the intended behavior change.

    Verification:

    • oxlint on the repro (plus the NestJS import variant and an arrow-inside-decorator variant): 0 warnings; parameter-shadow usage inside the method body still resolves to the parameter.
    • cargo test -p oxc_semantic --all-features — all pass.
    • cargo test -p oxc_linter — all 1144+ pass, including the 47 no_unused_vars tests.
    • cargo coverage -- semantic — identical pass rates to baseline (typescript 2671/4773 before and after); the semantic_typescript.snap diff only changes details within already-failing cases and removes one mismatch.
    • Transformer conformance output byte-identical to baseline; oxc_transformer, oxc_traverse, oxc_isolated_declarations, oxc_minifier tests all green.
    • cargo fmt + clippy clean for the touched crates.

    Note: one pre-existing environment quirk — cargo test -p oxc_semantic without --all-features fails to compile the cfg example (needs the cfg feature), unrelated to this change.

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