MCPcopy Create free account
hub / github.com/colbymchenry/codegraph / closureCollectionEdges

Function closureCollectionEdges

src/resolution/callback-synthesizer.ts:278–352  ·  view source on GitHub ↗

* Closure-collection dispatch: dispatcher iterates a closure-collection property * invoking each element; registrar appends a closure to the same-named property. * Emits dispatcher → registrar so a flow reaches the registration site (where the * appended closure's body — and its callers — live).

(queries: QueryBuilder, ctx: ResolutionContext, onYield: MaybeYield)

Source from the content-addressed store, hash-verified

276 * name shared across unrelated classes can't fan out into noise.
277 */
278async function closureCollectionEdges(queries: QueryBuilder, ctx: ResolutionContext, onYield: MaybeYield): Promise<Edge[]> {
279 const dispatchers = new Map<string, Array<{ node: Node; line: number }>>(); // field → dispatcher methods + forEach line
280 const registrars = new Map<string, Array<{ node: Node; line: number }>>(); // field → registrar methods + append line
281
282 const addReg = (field: string | undefined, node: Node, absLine: number) => {
283 if (!field || /^\d+$/.test(field)) return; // `$0.append` mis-captures the `0`; the write-RE owns that field
284 const arr = registrars.get(field) ?? [];
285 if (!arr.some((r) => r.node.id === node.id)) arr.push({ node, line: absLine });
286 registrars.set(field, arr);
287 };
288
289 // Slices EVERY Swift/Kotlin method/function's source (no cheap name-gate), so
290 // on a repo with a huge file this is the heaviest synthesis pass — yield
291 // mid-scan (and mid-match-loop below: a single generated function dense with
292 // matches must not starve the watchdog either) so it can't wedge the #850
293 // watchdog on its own (#1091, #1235).
294 let scanned = 0;
295 let matchTick = 0;
296 for (const m of methodAndFunctionNodes(queries)) {
297 if ((++scanned & 127) === 0) await onYield();
298 if (!CC_LANGUAGES.has(m.language)) continue;
299 const content = ctx.readFile(m.filePath);
300 const src = content && sliceLines(content, m.startLine, m.endLine);
301 if (!src) continue;
302 const hasForEach = src.includes('.forEach');
303 const hasAppend = src.includes('.append(') || src.includes('.add(') || src.includes('.push(') || src.includes('.insert(');
304 if (!hasForEach && !hasAppend) continue;
305 const lineAt = makeLineAt(src, m.startLine ?? 1);
306
307 if (hasForEach) {
308 CC_DISPATCH_RE.lastIndex = 0;
309 let d: RegExpExecArray | null;
310 while ((d = CC_DISPATCH_RE.exec(src))) {
311 if ((++matchTick & 255) === 0) await onYield();
312 const arr = dispatchers.get(d[1]!) ?? [];
313 if (!arr.some((n) => n.node.id === m.id)) arr.push({ node: m, line: lineAt(d.index) });
314 dispatchers.set(d[1]!, arr);
315 }
316 }
317 if (hasAppend) {
318 CC_APPEND_WRITE_RE.lastIndex = 0;
319 let w: RegExpExecArray | null;
320 while ((w = CC_APPEND_WRITE_RE.exec(src))) {
321 if ((++matchTick & 255) === 0) await onYield();
322 addReg(w[2] || w[1], m, lineAt(w.index)); // nested `$0.streams` else the `.write` receiver
323 }
324 CC_APPEND_DIRECT_RE.lastIndex = 0;
325 let a: RegExpExecArray | null;
326 while ((a = CC_APPEND_DIRECT_RE.exec(src))) {
327 if ((++matchTick & 255) === 0) await onYield();
328 addReg(a[1], m, lineAt(a.index));
329 }
330 }
331 }
332
333 const edges: Edge[] = [];
334 const seen = new Set<string>();
335 for (const [field, disps] of dispatchers) {

Callers 1

Calls 12

makeLineAtFunction · 0.90
methodAndFunctionNodesFunction · 0.85
sliceLinesFunction · 0.85
hasMethod · 0.80
pushMethod · 0.80
addRegFunction · 0.70
readFileMethod · 0.65
execMethod · 0.65
getMethod · 0.65
lineAtFunction · 0.50
setMethod · 0.45
addMethod · 0.45

Tested by

no test coverage detected