Skip to content

fix(js): an emitted event links only to handlers on the same emitter - #3362

Merged
colbymchenry merged 1 commit into
mainfrom
claude/issue-2973-eventemitter-pairing
Nov 18, 2026
Merged

colbymchenry merged 1 commit into
mainfrom
claude/issue-2973-eventemitter-pairing

Conversation

@colbymchenry

@colbymchenry colbymchenry commented Nov 17, 2026 •

Copy link
Copy Markdown
Owner

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 the emit's line on every edge.

Fixes #2973

Root cause

eventEmitterEdges in src/resolution/callback-synthesizer.ts:

  • kept handlers in a map keyed by the event string only, so the receivers (cart vs profile) were never compared;
  • found a handler with ctx.getNodesByName(handlerName).find(...): the first function of that name anywhere, not the one in the registering file;
  • recorded one registration site per handler node, so the last registration won, and set no line on the edge.

Fix

The pass moves to its own module, src/resolution/event-emitter-synthesizer.ts (the registry entry in callback-synthesizer.ts now hands it the QueryBuilder). A pair needs:

  1. The handler the registration names in its own scope: an inline function name, a local of the registering function (const onPacket = this.onPacket.bind(this) is the method), the function_ref reference 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.
  2. Receivers that can be the same object. Each receiver gets the identities the code gives it:
    • the class whose instance this is (an arrow function has no this of its own; an anonymous function callback rebinds it), and self/that aliases of this;
    • a binding made by new C() or typed C (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 (following export { X } from and import-then-export forwarding, and, behind an entry point no source declares, the only class of that name in its package);
    • a field of such an instance (typed by its declaration, a parameter property or this.f = new C()), including const con = this.connection aliases;
    • a module binding and the names its module exports it under, an import of one, a package import, a local of one function, or a global such as process.
      Two receivers with identities pair when one is shared; classes related by extends count 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's fn.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.
  3. The event fan-out cap, unchanged (more than 6 handlers or dispatchers for one event name project-wide skips it).

Each edge carries line (the emit) and its own registeredAt; provenance: 'heuristic' and synthesizedBy: 'event-emitter' are unchanged, so codegraph_explore's dynamic-dispatch links and the codegraph_node trail 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's callback-synthesizer.ts and 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.ts among 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 untyped bus parameters 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):

repo (commit) files event-emitter edges main → branch kept / lost / gained pairs
fastify (825d772) 397 1 → 1 (now with a line) 1 / 0 / 0
express (1c6f1b0) 216 1 → 1 (now with a line) 1 / 0 / 0
ws (0cc9185) 64 23 → 9 4 / 19 / 5
socket.io (cbbcb19) 866 113 → 18 15 / 98 / 3
node-postgres (2a5850f) 367 26 → 0 0 / 26 / 0
MQTT.js (0ceae41) 145 14 → 1 1 / 13 / 0
gin (control, Go) 130 byte-identical
flask (control, Python) 236 byte-identical

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.

  • Lost right pairs (9): each needs a receiver nothing in the code types: pg's unit tests emit fake events on client.connection of a helper-made client (6), MQTT.js's benchmark listens on mqtt.connect()'s result (2), and ws's this[kWebSocket].emit('message') (1). Typing what a factory returns is left out.
  • Gained right pairs (8): ws's stream and tests listening on a JSDoc-typed / new WebSocket() client (5: open, close, now under the cap since name-only lookups no longer count unrelated handlers), engine.io's transport packet onto its socket's bound onPacket and the probe's onPacket, and engine.io's data onto socket.io's Client::ondata (3).
  • Removed wrong pairs include socket.io's minified bundles and examples linked across packages, engine.io server emits linked to the client package, Sender::close / push / Wait::done found 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 in src/resolution/callback-synthesizer.ts, the design doc, the test file and CHANGELOG. Nothing in name-matcher.ts, import-resolver.ts, resolution/index.ts or the parse path. Rebased onto adad315, which carries #3370's change to the test-request pass in tier-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

  • The fan-out cap still counts per event name project-wide; with receivers matched it could count per channel, which would add edges, so it is left for a separate change.
  • A receiver typed only by what a factory or helper returns (const client = mqtt.connect()) stays untyped.

No contributor PR covers this issue.

🤖 Generated with Claude Code

@colbymchenry
colbymchenry force-pushed the claude/issue-2973-eventemitter-pairing branch 3 times, most recently from 9e7dda8 to 8e49b5c Compare November 18, 2026 00:17
@colbymchenry
colbymchenry force-pushed the claude/issue-2973-eventemitter-pairing branch 2 times, most recently from 2b3703b to 1130321 Compare November 18, 2026 07:06
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>
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: the EventEmitter pass pairs emit/on by event name only and picks the handler by name project-wide

1 participant