MCPcopy Create free account
hub / github.com/anus-dev/ANUS / traverse

Function traverse

packages/core/src/tools/tools.ts:347–402  ·  view source on GitHub ↗
(
    node: unknown,
    visitedRefs: Set<string>,
    pathRefs: Set<string>,
  )

Source from the content-addressed store, hash-verified

345 }
346
347 function traverse(
348 node: unknown,
349 visitedRefs: Set<string>,
350 pathRefs: Set<string>,
351 ): boolean {
352 if (typeof node !== 'object' || node === null) {
353 return false;
354 }
355
356 if (Array.isArray(node)) {
357 for (const item of node) {
358 if (traverse(item, visitedRefs, pathRefs)) {
359 return true;
360 }
361 }
362 return false;
363 }
364
365 if ('$ref' in node && typeof node.$ref === 'string') {
366 const ref = node.$ref;
367 if (ref === '#/' || pathRefs.has(ref)) {
368 // A ref to just '#/' is always a cycle.
369 return true; // Cycle detected!
370 }
371 if (visitedRefs.has(ref)) {
372 return false; // Bail early, we have checked this ref before.
373 }
374
375 const resolvedNode = resolveRef(ref);
376 if (resolvedNode) {
377 // Add it to both visited and the current path
378 visitedRefs.add(ref);
379 pathRefs.add(ref);
380 const hasCycle = traverse(resolvedNode, visitedRefs, pathRefs);
381 pathRefs.delete(ref); // Backtrack, leaving it in visited
382 return hasCycle;
383 }
384 }
385
386 // Crawl all the properties of node
387 for (const key in node) {
388 if (Object.prototype.hasOwnProperty.call(node, key)) {
389 if (
390 traverse(
391 (node as Record<string, unknown>)[key],
392 visitedRefs,
393 pathRefs,
394 )
395 ) {
396 return true;
397 }
398 }
399 }
400
401 return false;
402 }
403
404 return traverse(schema, new Set<string>(), new Set<string>());

Callers 1

hasCycleInSchemaFunction · 0.85

Calls 2

resolveRefFunction · 0.85
addMethod · 0.80

Tested by

no test coverage detected