Skip to content

fix(js,ts): a local declared after a comma or beside a default shadows an outer variable - #3340

Merged
colbymchenry merged 1 commit into
mainfrom
claude/issue-3293-later-declarator-shadow
Nov 18, 2026
Merged

colbymchenry merged 1 commit into
mainfrom
claude/issue-3293-later-declarator-shadow

Conversation

@colbymchenry

@colbymchenry colbymchenry commented Nov 17, 2026 •

Copy link
Copy Markdown
Owner

Summary

A regression that shipped in 1.6.3. Since #2811, a call on a variable declared outside the calling function goes by that declaration's type, and a nearer binding of the name is meant to win. In two declaration shapes the nearer binding wasn't seen, so the call took the outer type and lost its edge:

  • a later declarator: let n = 0, ob = makeOther(), also on its own line after a comma;
  • a destructured name beside a default: const { ob = makeOther() } = o.

With the issue's fixture (an outer let ob: Root | null from react-dom/client), codegraph callers paint on 1.6.3 lists 2 of the 5 functions; 1.6.2 and this branch list all 5.

Root cause

jsBindingSites (src/resolution/name-matcher.ts) finds a name's bindings with the decl pattern of localBindingPatterns, tried at each const/let/var keyword. The pattern matches the name only right after the keyword, or inside a {…}/[…] with no = in it, so it misses every declarator after the first and any destructured name in a pattern with a default. With no nearer site, jsBindingAt returned the module's let ob: Root, and paint was 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.

Fix

  • jsDeclaredNames reads every declaration of the file once, declarator by declarator: a name or a destructuring pattern (names, renamed keys, nested patterns, rest names, past each default), an optional type, an optional = value, then a comma before the next declarator.
    • A value is skipped with ()/[] balanced and each { jumped to its scope's end (the scope tree the file reader already builds), type arguments before a call read past, and a line break ending it where it ends the statement (the same rule as jsBodyEnd).
    • The reader stops where it can't follow the code: a value holding a JSX element (whose text can hold commas), an unclosed bracket, or anything that isn't a declarator. What it read before stays.
  • jsBindingSites adds those names the keyword pattern missed, each at its own offset. A plain later declarator is a declaration (its type or new is read, and its call result through jsCallResultReading); a destructured name or an import (require(/import() value is other, as before.
  • The declaration and call-result readers accept a head that starts at a later declarator's own name.
  • A later declarator's binding also keeps its declaration's keyword, so the bare-call scope check from fix(js,ts): a bare call links the declaration in scope, in a callback block or a spread #3352, which keeps a var in scope through its function rather than its block, reads a later var declarator as it reads a first one.
  • The keyword pattern itself, which other gates share, is unchanged, so this reaches only the receiver scope reader that regressed.

The scanner adds no backtracking regex: it is a linear scan, and the one new regex (type arguments before a call) has one-line, bounded alternatives that each start with a different character. Nested destructuring is read at most 32 deep.

Validation

  • Tests (__tests__/js-receiver-binding-scope.test.ts), red on main and green here:

    • the issue's five functions;
    • eight more shapes that must link: typed and new later declarators, comma-first style, values holding a function or type arguments, nested and multi-line defaults, a later declarator in a for head;
    • eight comma shapes that declare nothing, where the outer type must still decide: a comma expression on the next line, JSX text <p>Hi, ob, there</p>, an object type { a: number, ob: string }, type arguments Map<string, typeof ob>, a string, a default that uses the name, a renamed key { ob: other = 1 }, an inner function's declarator;
    • bounded time: a 3,000-declarator list, 200-deep brackets, 2,000 one-statement declarations and an unclosed bracket;
    • a later var declarator in an if block, in a describe() callback, still reaching the call after the block (red without the keyword, green here).
  • CLI, the issue's own fixture: codegraph callers paint lists all five callers.

  • A/B against the 1.6.3 source arm (147fe29, the same kernel binary in both arms): graphs byte-identical (scripts/dump-graph.mjs) on fastify (825d772), express, excalidraw and vscode's src/vs/base + src/vs/editor, and on flask and gin as controls.

  • Cost, median of 3 interleaved runs per arm, whole-process CPU (all threads):

    repo 1.6.3 this branch
    fastify 12.1 s 12.1 s
    excalidraw 26.5 s 27.4 s
    vscode base + editor 57.8 s 57.7 s

    A CPU profile of excalidraw puts the new reader (jsDeclaredNames and its helpers) at about 30 ms of 30 s; the run-to-run spread is larger than that.

  • Full suite: see the READY note.

Credit and what is left out

  • Thanks @inth3shadows for the report and the diagnosis.
  • PR fix(resolution): recognize later declarators and destructuring defaults as local bindings (#3293) #3294 (@mohitdixit02) widens the shared decl regex instead. I wrote this one from the issue: a scanner reads these shapes without growing the regex that several gates run over whole files, and it keeps a JSX text comma or a comma expression from reading as a declarator. Credited in the CHANGELOG.
  • Left as is: the import-shadow gate (jsBindingEnclosesReference) and jsCodeBindsName read declarations with patterns of their own, which also miss later declarators. That isn't what regressed here, and changing them would move other edges, so it's left for a separate change.

Fixes #3293

🤖 Generated with Claude Code

@colbymchenry
colbymchenry force-pushed the claude/issue-3293-later-declarator-shadow branch 3 times, most recently from 165d4b9 to 59f03c1 Compare November 18, 2026 06:23
@colbymchenry
colbymchenry force-pushed the claude/issue-3293-later-declarator-shadow branch 4 times, most recently from faa6980 to f784b60 Compare November 18, 2026 08:50
…s an outer variable

Since 1.6.3 (PR 2811), a call on a variable declared outside the calling
function goes by that declaration's type. The scope reader finds a name's
bindings with a pattern that matches only right after `const`/`let`/`var`,
or inside a destructuring with no `=` in it. So it missed a later
declarator (`let n = 0, ob = make()`, also on its own line after a comma)
and a destructured name beside a default (`const { ob = make() } = o`).
With no nearer binding found, the call took the outer variable's type, and
a library type (react-dom's Root) dropped a call that 1.6.2 linked.

The scope reader now also reads every declaration declarator by declarator,
once per file: a name or a destructuring pattern, an optional type, an
optional value skipped to its end, then a comma before the next one. A value
is skipped with its brackets balanced, a block jumped to its scope's end,
type arguments before a call read past, and a line break ending it where it
ends the statement. The reader stops where it can't follow the code, such as
a value holding a JSX element, whose text can hold commas. A later
declarator is recorded at its own offset, so the declaration and call-result
readers also accept a head that starts at the name. It also keeps its
declaration's keyword, so the bare-call scope check (PR 3352), which keeps a
`var` in scope through its function rather than its block, reads a later
`var` declarator as it reads a first one.

Validation:
- js-receiver-binding-scope.test.ts: the issue's five shapes; eight more
  (typed and `new` later declarators, comma-first style, values holding a
  function or type arguments, nested and multi-line defaults, a for head);
  eight comma shapes that declare nothing (a comma expression, JSX text, an
  object type, type arguments, a string, a default that uses the name, a
  renamed key, an inner function's declarator); and a 3,000-declarator list,
  200-deep brackets and an unclosed bracket. Red on main, green here. Also
  a later `var` declarator in an `if` block, in a describe() callback,
  still reaching the call after the block (red without the keyword, green
  here).
- A/B against the 1.6.3 source arm (147fe29, the same kernel binary):
  byte-identical graphs on fastify, express, excalidraw and vscode's
  src/vs/base + src/vs/editor, and on flask and gin as controls.
- Cost, median of 3 interleaved runs, whole-process CPU: excalidraw 26.5 s
  vs 27.4 s, fastify 12.1 s vs 12.1 s, the vscode subtree 57.8 s vs 57.7 s.
  A CPU profile of excalidraw puts the new reader at about 30 ms of 30 s.

Fixes #3293

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@colbymchenry
colbymchenry force-pushed the claude/issue-3293-later-declarator-shadow branch from f784b60 to 175ef88 Compare November 18, 2026 10:07
@colbymchenry
colbymchenry merged commit 46450bc into main Nov 18, 2026
@colbymchenry
colbymchenry deleted the claude/issue-3293-later-declarator-shadow branch November 18, 2026 10:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

1 participant