Repository navigation
fix(js): an emitted event links only to handlers on the same emitter - #3362
Merged
Merged
Conversation
colbymchenry
force-pushed
the
claude/issue-2973-eventemitter-pairing
branch
3 times, most recently
from
November 18, 2026 00:17
9e7dda8 to
8e49b5c
Compare
colbymchenry
force-pushed
the
claude/issue-2973-eventemitter-pairing
branch
2 times, most recently
from
November 18, 2026 07:06
2b3703b to
1130321
Compare
The EventEmitter pass paired `x.emit('e')` with every `y.on('e', h)` in the
project by the event name alone, and looked the handler up by name
anywhere in the project (`getNodesByName(h).find`). Two modules that each
keep their own emitter and use one event name cross-linked: with
same-named handlers, both emits reached the first module's handler, and
with different names, each emit reached both. The edges carried no line,
and the handler's registration site was the last one seen.
The pass moves to its own module, event-emitter-synthesizer.ts, and a pair
now needs:
- the handler the registration names in its own scope: an inline
`function name`, a local of the registering function (`const h =
this.m.bind(this)` is method m), the function_ref reference the resolver
made at that argument, the file's own function of that name, or the one
it imports. Never a namesake in another file.
- receivers that can be the same object. Each receiver gets the
identities the code gives it: the class whose instance `this` is (an
arrow has no `this` of its own, an anonymous `function` callback rebinds
it), a binding made by `new C()` or typed `C` (TypeScript, or a JSDoc
`@param {C}`), a field of such an instance (typed by its declaration, a
parameter property or `this.f = new C()`), a module binding and the names
its module exports it under, an import of one, a local of one function,
or a global such as `process`. Classes related by `extends` count as one.
A receiver the code ties to nothing (an untyped parameter, a call's
result) pairs only with another such receiver in its own file. Only
JavaScript-family files read receivers; elsewhere a pair stays in a file.
- the event fan-out cap, unchanged.
Each edge now carries the line of its `emit` and its own registration site.
Validation, main (1.6.3 source) vs this branch, both on the 1.6.3 kernel:
- New test file, 7 cases, all fail on main and pass now (the issue's two
fixtures, a bus shared through CommonJS and ESM imports, class
instances and subclasses, typed fields and JSDoc parameters, untyped
receivers kept to their file, a handler from an unimported file).
- jest-manual-mocks.test.ts (issue 2455): its event-bus fixture paired
two untyped `bus` parameters in different files, with a handler its
file never imports, which the pass no longer takes. The fixture now
shares one bus module and imports its handlers; its three assertions
(the production handler, never the mock's, never a test's
registration) hold here and with main's pass.
- Event-emitter edges, main -> branch (every other edge byte-identical):
fastify 1 -> 1 and express 1 -> 1 (same pair, now with a line); ws
23 -> 9; socket.io 113 -> 18; node-postgres 26 -> 0; MQTT.js 14 -> 1.
Triaged by reading every pair: main's 178 held 30 right ones (and 5 hard
to call); the branch's 30 are all right but one, a socket.io example's
client socket in one file, which main has too. 22 pairs are kept, 9
right pairs lost, each on a receiver nothing types (a factory's result in
pg's tests and MQTT.js's benchmark, an indexed `this[kWebSocket]` in ws),
and 8 right pairs gained (ws's stream and test listeners on a WebSocket,
engine.io's transport packets and data onto its socket and socket.io's
client).
- Controls gin and flask: byte-identical.
- Index time, median of 3 interleaved: vscode's base+editor (1,871 files)
32.5 s -> 31.5 s, the pass 1.47 s -> 1.30 s; socket.io 8.1 s -> 7.7 s,
the pass 53 ms -> 201 ms (it runs beside the other passes).
- Re-run against adad315, both arms on its kernel (the branch now sits
on d2f3f07, whose later commits touch PHP, Java, Kotlin, Laravel, Rust
and Python only): the same event-emitter edges change in all six repos
and every
other edge is identical; gin and flask byte-identical. adad315 carries
the change to the test-request pass (PR 3370), registered just before
this one in tier-synthesizer.ts: the two passes share no code or
constants, and one links a test to a route node while this one links
function to function.
Fixes #2973
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
colbymchenry
force-pushed
the
claude/issue-2973-eventemitter-pairing
branch
from
November 18, 2026 08:24
1130321 to
217358e
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
The EventEmitter synthesis pass (
x.on('e', handler)↔y.emit('e')) paired an emit with every handler of the same event name anywhere in the project, and picked the handler by name project-wide. Two modules that each keep their own emitter and use the same event name cross-linked. It now pairs an emit only with handlers registered on what can be the same object, takes the handler from the registering file's own scope, and puts theemit's line on every edge.Fixes #2973
Root cause
eventEmitterEdgesinsrc/resolution/callback-synthesizer.ts:cartvsprofile) were never compared;ctx.getNodesByName(handlerName).find(...): the first function of that name anywhere, not the one in the registering file;lineon the edge.Fix
The pass moves to its own module,
src/resolution/event-emitter-synthesizer.ts(the registry entry incallback-synthesizer.tsnow hands it theQueryBuilder). A pair needs:function name, a local of the registering function (const onPacket = this.onPacket.bind(this)is the method), thefunction_refreference the resolver already made at that argument, the file's own function of that name (the one in the registration's scope when a test file defines several), or the function it imports. Never a namesake in a file it doesn't import.thisis (an arrow function has nothisof its own; an anonymousfunctioncallback rebinds it), andself/thataliases ofthis;new C()or typedC(a TypeScript annotation, or a JSDoc@param {C}), the class found through the resolver's own import-resolved edge, the file's class, or the import (followingexport { X } fromand import-then-export forwarding, and, behind an entry point no source declares, the only class of that name in its package);this.f = new C()), includingconst con = this.connectionaliases;process.Two receivers with identities pair when one is shared; classes related by
extendscount as one, since a base method runs on subclass instances. A receiver the code ties to nothing (an untyped parameter, a call's result,this[k]) pairs only with another such receiver in its own file, which keeps express'sfn.emit('mount')↔this.on('mount', onmount). Only JavaScript-family files (.js/.jsx/.ts/.tsx/.vue/.svelte) read receivers; elsewhere a pair stays within one file.Each edge carries
line(theemit) and its ownregisteredAt;provenance: 'heuristic'andsynthesizedBy: 'event-emitter'are unchanged, socodegraph_explore's dynamic-dispatch links and thecodegraph_nodetrail read the same, with the right handler and wiring site (checked on the issue's fixture:saveProfile → handleSave (src/b_profile.js:3) [dynamic: event \save` @src/b_profile.js:4]`).The design doc's EventEmitter section (
docs/design/callback-edge-synthesis.md) is updated to match.Validation
Tests. New
__tests__/event-emitter-synthesizer.test.ts, 7 cases; all 7 fail with main'scallback-synthesizer.tsand pass with the fix: the issue's two fixtures (same-named and distinct handlers, with line and registration site), a bus shared through CommonJS and ESM imports beside a module's private emitter, class instances and subclasses (TS and JS), a typed field + JSDoc parameter + a class re-exported by an index, untyped receivers kept to their file (express's shape), and a handler name from an unimported file. 60 JS / synthesis test files (462 tests,jest-manual-mocks.test.tsamong them) pass on the commit. The full suite is in the READY note.__tests__/jest-manual-mocks.test.ts(#2455) changes too: its event-bus fixture paired two untypedbusparameters in different files, with a handler its file never imports, which this pass no longer takes. The fixture now shares one bus module and imports its handlers. Its three assertions stand, and they hold on this branch and with main's pass alike:A/B (main = the 1.6.3 source arm, branch = this commit, both on the 1.6.3 kernel; full graph dumped by natural keys; every changed line is an event-emitter edge):
Triage, every pair read in the source: main's 178 edges held 30 right ones (and 5 hard to call); the branch's 30 are all right except one, a socket.io example's client socket (
socket.emit('drawing')↔socket.on('drawing', …)in one file), which main has too.client.connectionof a helper-made client (6), MQTT.js's benchmark listens onmqtt.connect()'s result (2), and ws'sthis[kWebSocket].emit('message')(1). Typing what a factory returns is left out.new WebSocket()client (5:open,close, now under the cap since name-only lookups no longer count unrelated handlers), engine.io's transportpacketonto its socket's boundonPacketand the probe'sonPacket, and engine.io'sdataonto socket.io'sClient::ondata(3).Sender::close/push/Wait::donefound by name from another file, and every cross-module pair on two emitters of one event name.Re-run on adad315 (both arms on its kernel; the branch now sits on d2f3f07, whose later commits touch PHP, Java, Kotlin, Laravel, Rust and Python only): the same event-emitter edges change in all six repos and every other edge is identical; gin and flask stay byte-identical.
Cost, median of 3 interleaved runs: vscode's
base+editor(1,871 files) 32.5 s → 31.5 s (the pass 1.47 s → 1.30 s); socket.io 8.1 s → 7.7 s (the pass 53 ms → 201 ms; it runs in the resolver pool beside the other passes).Areas touched
src/resolution/event-emitter-synthesizer.ts(new), the emitter entry and header comment insrc/resolution/callback-synthesizer.ts, the design doc, the test file and CHANGELOG. Nothing inname-matcher.ts,import-resolver.ts,resolution/index.tsor the parse path. Rebased onto adad315, which carries #3370's change to the test-request pass intier-synthesizer.ts, registered just before this one: the two passes share no code or constants, and one links a test to a route node while this one links a function to a function, so neither changes the other's edges.Left out
const client = mqtt.connect()) stays untyped.No contributor PR covers this issue.
🤖 Generated with Claude Code