Skip to content

JS/TS: a call on a local is dropped when an outer same-named variable has a package type #3293

Closed

Description

@inth3shadows

What happens

Since #2811, a call on a variable declared outside the calling function goes by the outside declaration's type. A nearer binding of the same name is meant to win ("A nearer binding of the name … still decides", in the #2811 test). In three declaration shapes, the nearer binding isn't seen, so the call takes the outer type and loses its edge:

  • a later declarator: let n = 0, ob = makeOther();
  • a later declarator on its own line: const a = 1,\n ob = makeOther();
  • a destructured name with a default: const { ob = makeOther() } = o; (one line or several)
// src/Other.ts
export class Other { paint() { return 2; } }
export function makeOther() { return new Other(); }
// src/shadow.ts
import type { Root } from "react-dom/client";
import { makeOther } from "./Other";

let ob: Root | null = null;

// The nearer `ob` holds an Other in every function below.
export function firstDeclarator() { const ob = makeOther(); ob.paint(); }
export function laterDeclarator() { let n = 0, ob = makeOther(); ob.paint(); }
export function declaratorBelow() {
  const a = 1,
    ob = makeOther();
  ob.paint();
}
export function destructured(o: any) { const { ob } = { ob: makeOther() }; ob.paint(); }
export function destructDefault(o: any) { const { ob = makeOther() } = o; ob.paint(); }
  • Expected: all five functions call Other::paint, as they do in 1.6.2.
  • Actual: only firstDeclarator and destructured do.

Repro (main at f72c9d2; 1.6.2 not affected; Node 22.23.2, Linux, CODEGRAPH_KERNEL=0)

With package.json { "name": "app", "dependencies": { "react-dom": "^19.0.0" } } and the two files above:

$ codegraph init -i .        # output omitted
$ codegraph callers paint
Callers of "paint" (2):

method Other::paint (typescript) — src/Other.ts:1
function    destructured
  src/shadow.ts:14

function    firstDeclarator
  src/shadow.ts:7
build callers of paint
main at f72c9d2 firstDeclarator, destructured
main, with the let ob: Root line removed all five
1.6.2 (npm) all five
08478ee (#2811) same as main
f3fdba2 (its parent) all five

First bad commit: 08478ee (#2811). It reproduces every run.

Cause (inferred)

The scope reader that #2811 added finds a name's bindings with the decl pattern of localBindingPatterns, through jsBindingSites. That pattern matches a name only right after const/let/var or inside a {…}/[…] with no = in it. So it misses a second or later declarator, and a destructured name that has a default. With no nearer binding found, the module's let ob: Root is the binding in scope, and paint is looked up on react-dom's Root. Before #2811 a receiver's type was read only inside the calling function, so these shapes never met an outer type.

A fix has to keep what #2811 fixed: root.render() on the outer Root still links to nothing in the project.

Related: #2589 (the bug #2811 fixed), #2947 (another follow-up to #2589: an untyped local initialized from a library call. That one is about guessing, not shadowing).

Activity

  1. mohitdixit02 commented on Nov 14, 2026

    @mohitdixit02

    Thanks for the clear reproduction and diagnosis, @inth3shadows!

    I verified this against main. In src/resolution/name-matcher.ts, jsBindingSites uses localBindingPatterns(name, 'y').decl at each varDecls keyword offset.

    Because the sticky regex expects \b(?:const|let|var)\s+ to immediately precede name (and uses [^;=]*? inside destructuring brackets), it:
    1. Fails on subsequent comma-separated declarators (let n = 0, ob = ... and multiline const a = 1,\n ob = ...).
    2. Fails on destructuring with defaults (const { ob = makeOther() } = o or { x = 1, ob }) because of the forbidden = in [^;=].

    I'm putting together a PR with the updated pattern and regression tests matching your reproduction cases to ensure nearer bindings shadow outer package types properly.

  2. added 8 commits that reference this issue on Nov 17, 2026
    a98e0a1
    165d4b9
    59f03c1
    296699a
    074e60a
    faa6980
    f784b60
    175ef88
  3. colbymchenry commented on Nov 18, 2026

    @colbymchenry
    Owner

    Thanks for the report, @inth3shadows. This was a regression in 1.6.3: a local declared after a comma (let n = 0, ob = make()) or beside a default in a destructuring (const { ob = make() } = o) wasn't seen as shadowing an outer variable of the same name, so when the outer one held a library's type, the call on the local was dropped. #3340 reads those declarators again, so the call links as it did before 1.6.3. Thanks also to @mohitdixit02. It is merged to main today (2026-11-18) and ships in the next release.

  4. added a commit that references this issue on Nov 18, 2026
    46450bc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions