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

Method getCallersRecursive

src/graph/traversal.ts:317–357  ·  view source on GitHub ↗
(
    nodeId: string,
    maxDepth: number,
    currentDepth: number,
    result: Array<{ node: Node; edge: Edge }>,
    visited: Map<string, number>,
    reported: Set<string>
  )

Source from the content-addressed store, hash-verified

315 }
316
317 private getCallersRecursive(
318 nodeId: string,
319 maxDepth: number,
320 currentDepth: number,
321 result: Array<{ node: Node; edge: Edge }>,
322 visited: Map<string, number>,
323 reported: Set<string>
324 ): void {
325 // Mark visited BEFORE the depth check, not after. Folding both into one
326 // guard meant that when `currentDepth >= maxDepth` fired we returned without
327 // marking the node — so a caller reachable from the same parent via two
328 // edges (two call sites, or calls + references) was pushed once per edge,
329 // duplicating it in `result` at the default `maxDepth=1` (#1086).
330 if (!this.enterAtDepth(visited, nodeId, currentDepth)) return;
331 if (currentDepth >= maxDepth) {
332 return;
333 }
334
335 // `instantiates` counts as a caller: constructing a class (`Foo(...)` /
336 // `new Foo()`) is calling its constructor, so the instantiation site is a
337 // caller of the class. Without it, `callers <Class>` surfaced only the
338 // importing file (via `imports`) and missed every construction site —
339 // the opposite of "what breaks if I change this class?" (#774).
340 const incomingEdges = this.queries.getIncomingEdges(nodeId, CALL_EDGE_KINDS);
341 if (incomingEdges.length === 0) return;
342
343 // Batch-fetch all caller nodes in one round-trip instead of one
344 // getNodeById per edge (was N+1 — meaningful on functions with many callers).
345 const sourceIds = incomingEdges.map((e) => e.source);
346 const callerNodes = this.queries.getNodesByIds(sourceIds);
347
348 for (const edge of incomingEdges) {
349 const callerNode = callerNodes.get(edge.source);
350 if (!callerNode || !this.nearerThanBefore(visited, callerNode.id, currentDepth + 1)) continue;
351 if (!reported.has(callerNode.id)) {
352 reported.add(callerNode.id);
353 result.push({ node: callerNode, edge });
354 }
355 this.getCallersRecursive(callerNode.id, maxDepth, currentDepth + 1, result, visited, reported);
356 }
357 }
358
359 /**
360 * Find all functions/methods called by a function

Callers 1

getCallersMethod · 0.95

Calls 9

enterAtDepthMethod · 0.95
nearerThanBeforeMethod · 0.95
mapMethod · 0.80
hasMethod · 0.80
pushMethod · 0.80
getIncomingEdgesMethod · 0.65
getMethod · 0.65
getNodesByIdsMethod · 0.45
addMethod · 0.45

Tested by

no test coverage detected