( node: unknown, defs: ReadonlyMap<string, unknown>, found: Set<string> = new Set(), )
| 89 | * e.g. "#/$defs/Address" → "Address", and if Address references City, both. |
| 90 | */ |
| 91 | export const collectRefs = ( |
| 92 | node: unknown, |
| 93 | defs: ReadonlyMap<string, unknown>, |
| 94 | found: Set<string> = new Set(), |
| 95 | ): Set<string> => { |
| 96 | if (node == null || typeof node !== "object") return found; |
| 97 | const obj = node as Obj; |
| 98 | |
| 99 | if (typeof obj.$ref === "string") { |
| 100 | const name = parseRefName(obj.$ref); |
| 101 | if (name && !found.has(name)) { |
| 102 | found.add(name); |
| 103 | const def = defs.get(name); |
| 104 | if (def) collectRefs(def, defs, found); |
| 105 | } |
| 106 | return found; |
| 107 | } |
| 108 | |
| 109 | for (const v of Object.values(obj)) { |
| 110 | if (v && typeof v === "object") { |
| 111 | if (Array.isArray(v)) { |
| 112 | for (const item of v) collectRefs(item, defs, found); |
| 113 | } else { |
| 114 | collectRefs(v, defs, found); |
| 115 | } |
| 116 | } |
| 117 | } |
| 118 | return found; |
| 119 | }; |
| 120 | |
| 121 | export const collectReferencedDefinitions = ( |
| 122 | roots: readonly unknown[], |
no test coverage detected