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

    Let me first reproduce the issue.

  3. Assistant

    Reproduced. Now let me look at how the semantic builder handles parameter decorators.

  4. Assistant

    Let me look at how visit_function and method visiting work in the builder.

  5. Assistant

    Now update the two visit sites:

  6. Assistant

    Now let me test more variants:

  7. Assistant

    These are oxc's own snapshots of its scoping data. Let me see the full diffs before accepting:

  8. Assistant

    The diffs are exactly the intended change. Accept them:

  9. Assistant

    The snapshot confirms the outer const foo now owns the decorator reference. Promote it and run the remaining test suites:

  10. Assistant

    Semantic tests pass. Now add a regression test to no-unused-vars:

  11. Assistant

    I can replicate the clones with plain git:

  12. Assistant

    These are pre-existing known-failure entries whose details shifted. Let me check the context to verify the changes are improvements:

  13. Assistant

    Only the expected snapshot changed. Now run the transformer conformance (decorator transforms rely on semantic scoping) and remaining unit tests:

  14. Assistant

    The total test count changed (1165 → 1000) — I skipped just update-transformer-fixtures. Let me check that recipe:

  15. Assistant

    Now check the decorator legacy transform end-to-end with the shadow case, since emitted code must reference the outer binding:

  16. Assistant

    Transform output is correct. Now run formatting and clippy:

  17. Assistant

    Let me run the remaining hygiene checks (typos, and a broader compile check):

  18. Assistant

    The fix is complete. Summary:

    Root cause

    no-unused-vars sits on top of oxc_semantic's reference resolution, and the semantic builder was visiting parameter decorators inside the function scope (visit_formal_parameter runs after visit_function enters the function scope). So foo in @Inject(foo.KEY) resolved to the same-named parameter property instead of the outer const/import — leaving the outer binding with zero references.

    Fix — crates/oxc_semantic/src/builder.rs

    Added visit_parameter_decorators, which temporarily rebases current_scope_id to the function scope's parent (the class scope) while visiting a parameter's decorators, then restores it. Both visit_formal_parameter and visit_formal_parameter_rest now use it. This matches:

    • Runtime semantics — parameter decorators are evaluated at class-decoration time in the enclosing scope, before any parameter binding exists
    • typescript-eslint's scope manager, which visits parameter decorators before nesting the function scope
    • tsc's own expectations — the decoratorReferences.ts compiler test literally asserts "decorator y should be resolved at the class declaration, not the parameter", and the conformance diff moved in exactly that direction

    Tests

    • New semantic fixture crates/oxc_semantic/tests/fixtures/oxc/ts/decorators/parameter-decorator-shadow.ts + snapshot — confirms the outer const foo owns the decorator reference and the parameter has none
    • Two new no-unused-vars pass cases (const-shadow and import-shadow, referencing issue #22687) in tests/oxc.rs
    • Updated 2 existing semantic snapshots (decorator refs now recorded in class scope) and semantic_typescript.snap (one pre-existing mismatch eliminated)

    Verification

    • cargo test -p oxc_semantic --all-features — all pass
    • cargo test -p oxc_linter — 1144 pass, cargo test -p oxc_transformer — all pass
    • Full cargo coverage (test262/babel/typescript/estree across parser, semantic, codegen, formatter, transformer, minifier) — no regressions; only the expected semantic_typescript.snap improvement
    • Legacy decorator transform emits correct output for the shadow case: Inject(foo.KEY) outside the constructor referencing the outer foo
    • Decorator referencing its own parameter (@Inject(p) p) now correctly reports no-undef, matching typescript-eslint

    One note: I could not regenerate tasks/transform_conformance Babel fixtures (requires node + @babel/core, unavailable here), so I reverted the invalid snapshot that run produced; transformer correctness is covered by the unit tests and cargo coverage transformer suites instead.

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