Repository navigation
fix(js,ts): a local declared after a comma or beside a default shadows an outer variable - #3340
Merged
Merged
Conversation
colbymchenry
force-pushed
the
claude/issue-3293-later-declarator-shadow
branch
3 times, most recently
from
November 18, 2026 06:23
165d4b9 to
59f03c1
Compare
colbymchenry
force-pushed
the
claude/issue-3293-later-declarator-shadow
branch
4 times, most recently
from
November 18, 2026 08:50
faa6980 to
f784b60
Compare
…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
force-pushed
the
claude/issue-3293-later-declarator-shadow
branch
from
November 18, 2026 10:07
f784b60 to
175ef88
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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:
let n = 0, ob = makeOther(), also on its own line after a comma;const { ob = makeOther() } = o.With the issue's fixture (an outer
let ob: Root | nullfromreact-dom/client),codegraph callers painton 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 thedeclpattern oflocalBindingPatterns, tried at eachconst/let/varkeyword. 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,jsBindingAtreturned the module'slet ob: Root, andpaintwas looked up on react-dom'sRoot. Before #2811 a receiver's type was read only inside the calling function, so these shapes never met an outer type.Fix
jsDeclaredNamesreads 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.()/[]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 asjsBodyEnd).jsBindingSitesadds those names the keyword pattern missed, each at its own offset. A plain later declarator is adeclaration(its type ornewis read, and its call result throughjsCallResultReading); a destructured name or an import (require(/import() value isother, as before.varin scope through its function rather than its block, reads a latervardeclarator as it reads a first one.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:newlater declarators, comma-first style, values holding a function or type arguments, nested and multi-line defaults, a later declarator in aforhead;<p>Hi, ob, there</p>, an object type{ a: number, ob: string }, type argumentsMap<string, typeof ob>, a string, a default that uses the name, a renamed key{ ob: other = 1 }, an inner function's declarator;vardeclarator in anifblock, in adescribe()callback, still reaching the call after the block (red without the keyword, green here).CLI, the issue's own fixture:
codegraph callers paintlists 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'ssrc/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):
base+editorA CPU profile of excalidraw puts the new reader (
jsDeclaredNamesand 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
declregex 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.jsBindingEnclosesReference) andjsCodeBindsNameread 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