(
source: Source,
components: JsonSchema.Definitions,
typeOnly: boolean,
options?: GenerateOptions
)
| 65 | } |
| 66 | |
| 67 | function generate( |
| 68 | source: Source, |
| 69 | components: JsonSchema.Definitions, |
| 70 | typeOnly: boolean, |
| 71 | options?: GenerateOptions |
| 72 | ) { |
| 73 | const generated = makeCodeDocument(source, components, options) |
| 74 | if (generated === undefined) { |
| 75 | return "" |
| 76 | } |
| 77 | |
| 78 | const nonRecursiveReferences = generated.codeDocument.references.nonRecursives |
| 79 | const recursiveReferences = Object.entries(generated.codeDocument.references.recursives) |
| 80 | |
| 81 | const nonRecursives = nonRecursiveReferences.map(({ $ref, code }) => |
| 82 | renderSchemaTypeAndRuntime($ref, code, typeOnly) |
| 83 | ) |
| 84 | |
| 85 | const recursiveDeclarations: Array<string> = [] |
| 86 | const recursives: Array<string> = [] |
| 87 | |
| 88 | if (typeOnly) { |
| 89 | for (const [$ref, code] of recursiveReferences) { |
| 90 | recursives.push(renderSchemaTypeAndRuntime($ref, code, true)) |
| 91 | } |
| 92 | } else { |
| 93 | const recursivelyForwardReferenced = collectForwardReferencedRecursives( |
| 94 | nonRecursiveReferences, |
| 95 | recursiveReferences |
| 96 | ) |
| 97 | const recursiveInternalNames = makeRecursiveInternalNameMap( |
| 98 | recursivelyForwardReferenced, |
| 99 | [ |
| 100 | ...nonRecursiveReferences.map(({ $ref }) => $ref), |
| 101 | ...recursiveReferences.map(([$ref]) => $ref), |
| 102 | ...generated.nameMap |
| 103 | ] |
| 104 | ) |
| 105 | |
| 106 | for (const [$ref, code] of recursiveReferences) { |
| 107 | if (recursivelyForwardReferenced.has($ref)) { |
| 108 | const internalName = recursiveInternalNames.get($ref)! |
| 109 | recursiveDeclarations.push(renderRecursiveReferenceDeclaration($ref, code, internalName)) |
| 110 | recursives.push(`const ${internalName} = ${code.runtime}`) |
| 111 | continue |
| 112 | } |
| 113 | |
| 114 | recursives.push(renderSchemaTypeAndRuntime($ref, code, false)) |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | const codes = generated.codeDocument.codes.map((code, i) => |
| 119 | renderSchemaTypeAndRuntime(generated.nameMap[i], code, typeOnly) |
| 120 | ) |
| 121 | |
| 122 | return renderImportArtifacts(generated.codeDocument, !typeOnly) + |
| 123 | render("recursive declarations", recursiveDeclarations) + |
| 124 | render("non-recursive definitions", nonRecursives) + |
nothing calls this directly
no test coverage detected