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

Method dfsRecursive

src/graph/traversal.ts:212–260  ·  view source on GitHub ↗

* Recursive DFS helper

(
    node: Node,
    depth: number,
    opts: Required<TraversalOptions>,
    nodes: Map<string, Node>,
    edges: Edge[],
    visited: Set<string>
  )

Source from the content-addressed store, hash-verified

210 * Recursive DFS helper
211 */
212 private dfsRecursive(
213 node: Node,
214 depth: number,
215 opts: Required<TraversalOptions>,
216 nodes: Map<string, Node>,
217 edges: Edge[],
218 visited: Set<string>
219 ): void {
220 if (visited.has(node.id) || nodes.size >= opts.limit || depth >= opts.maxDepth) {
221 return;
222 }
223
224 visited.add(node.id);
225
226 // Get adjacent edges
227 const adjacentEdges = this.getAdjacentEdges(node.id, opts.direction, opts.edgeKinds);
228
229 // Batch-fetch unvisited neighbors (was N+1 per DFS step).
230 const wantIds = adjacentEdges
231 .map((e) => (e.source === node.id ? e.target : e.source))
232 .filter((id) => !visited.has(id));
233 const neighborNodes = wantIds.length > 0 ? this.queries.getNodesByIds(wantIds) : new Map();
234
235 for (const edge of adjacentEdges) {
236 // Cap per-add, not just at the top of each frame: the top-of-function
237 // guard only stops the next recursion, so without this every sibling of
238 // the first over-budget child still got inserted, overshooting
239 // `opts.limit` by a node's full fan-out (#1088).
240 if (nodes.size >= opts.limit) break;
241
242 const nextNodeId = edge.source === node.id ? edge.target : edge.source;
243 if (visited.has(nextNodeId)) continue;
244
245 const nextNode = neighborNodes.get(nextNodeId);
246 if (!nextNode) continue;
247
248 // Apply node kind filter
249 if (opts.nodeKinds && opts.nodeKinds.length > 0 && !opts.nodeKinds.includes(nextNode.kind)) {
250 continue;
251 }
252
253 // Add node and edge to result
254 nodes.set(nextNode.id, nextNode);
255 edges.push(edge);
256
257 // Recurse
258 this.dfsRecursive(nextNode, depth + 1, opts, nodes, edges, visited);
259 }
260 }
261
262 /**
263 * Get adjacent edges based on direction

Callers 1

traverseDFSMethod · 0.95

Calls 8

getAdjacentEdgesMethod · 0.95
hasMethod · 0.80
mapMethod · 0.80
pushMethod · 0.80
getMethod · 0.65
addMethod · 0.45
getNodesByIdsMethod · 0.45
setMethod · 0.45

Tested by

no test coverage detected