Repository navigation
Description
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
firstDeclaratoranddestructureddo.
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
mohitdixit02 commentedon Nov 14, 2026
Thanks for the clear reproduction and diagnosis, @inth3shadows!
I verified this against
main. Insrc/resolution/name-matcher.ts,jsBindingSitesuseslocalBindingPatterns(name, 'y').declat eachvarDeclskeyword offset.Because the sticky regex expects
\b(?:const|let|var)\s+to immediately precedename(and uses[^;=]*?inside destructuring brackets), it:1. Fails on subsequent comma-separated declarators (
let n = 0, ob = ...and multilineconst a = 1,\n ob = ...).2. Fails on destructuring with defaults (
const { ob = makeOther() } = oor{ 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.
fix(resolution): recognize later declarators and destructuring defaul…
fix(resolution): bound declaration list scanning and fix destructurin…
fix(js,ts): a local declared after a comma or beside a default shadow…
fix(js,ts): a local declared after a comma or beside a default shadow…
fix(js,ts): a local declared after a comma or beside a default shadow…
fix(js,ts): a local declared after a comma or beside a default shadow…
fix(js,ts): a local declared after a comma or beside a default shadow…
fix(js,ts): a local declared after a comma or beside a default shadow…
fix(js,ts): a local declared after a comma or beside a default shadow…
fix(js,ts): a local declared after a comma or beside a default shadow…
colbymchenry commentedon Nov 18, 2026
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.fix(js,ts): a local declared after a comma or beside a default shadow…