import ts from "typescript"; import { NoRootTypeError } from "./Error/NoRootTypeError"; import { Context, NodeParser } from "./NodeParser"; import { Definition } from "./Schema/Definition"; import { Schema } from "./Schema/Schema"; import { BaseType } from "./Type/BaseType"; import { DefinitionType } from "./Type/DefinitionType"; import { TypeFormatter } from "./TypeFormatter"; import { StringMap } from "./Utils/StringMap"; import { localSymbolAtNode, symbolAtNode } from "./Utils/symbolAtNode"; import { notUndefined } from "./Utils/notUndefined"; import { removeUnreachable } from "./Utils/removeUnreachable"; import { Config } from "./Config"; import { hasJsDocTag } from "./Utils/hasJsDocTag"; export class SchemaGenerator { public constructor( private readonly program: ts.Program, private readonly nodeParser: NodeParser, private readonly typeFormatter: TypeFormatter, private readonly config?: Config ) {} public createSchema(fullName?: string): Schema { const rootNodes = this.getRootNodes(fullName); return this.createSchemaFromNodes(rootNodes); } public createSchemaFromNodes(rootNodes: ts.Node[]): Schema { const rootTypes = rootNodes .map((rootNode) => { return this.nodeParser.createType(rootNode, new Context()); }) .filter(notUndefined); const rootTypeDefinition = rootTypes.length === 1 ? this.getRootTypeDefinition(rootTypes[0]) : undefined; const definitions: StringMap = {}; rootTypes.forEach((rootType) => this.appendRootChildDefinitions(rootType, definitions)); const reachableDefinitions = removeUnreachable(rootTypeDefinition, definitions); return { ...(this.config?.schemaId ? { $id: this.config.schemaId } : {}), $schema: "http://json-schema.org/draft-07/schema#", ...(rootTypeDefinition ?? {}), definitions: reachableDefinitions, }; } private getRootNodes(fullName: string | undefined) { if (fullName && fullName !== "*") { return [this.findNamedNode(fullName)]; } else { const rootFileNames = this.program.getRootFileNames(); const rootSourceFiles = this.program .getSourceFiles() .filter((sourceFile) => rootFileNames.includes(sourceFile.fileName)); const rootNodes = new Map(); this.appendTypes(rootSourceFiles, this.program.getTypeChecker(), rootNodes); return [...rootNodes.values()]; } } private findNamedNode(fullName: string): ts.Node { const typeChecker = this.program.getTypeChecker(); const allTypes = new Map(); const { projectFiles, externalFiles } = this.partitionFiles(); this.appendTypes(projectFiles, typeChecker, allTypes); if (allTypes.has(fullName)) { return allTypes.get(fullName)!; } this.appendTypes(externalFiles, typeChecker, allTypes); if (allTypes.has(fullName)) { return allTypes.get(fullName)!; } throw new NoRootTypeError(fullName); } private getRootTypeDefinition(rootType: BaseType): Definition { return this.typeFormatter.getDefinition(rootType); } private appendRootChildDefinitions(rootType: BaseType, childDefinitions: StringMap): void { const seen = new Set(); const children = this.typeFormatter .getChildren(rootType) .filter((child): child is DefinitionType => child instanceof DefinitionType) .filter((child) => { if (!seen.has(child.getId())) { seen.add(child.getId()); return true; } return false; }); const ids = new Map(); for (const child of children) { const name = child.getName(); const previousId = ids.get(name); // remove def prefix from ids to avoid false alarms // FIXME: we probably shouldn't be doing this as there is probably something wrong with the deduplication const childId = child.getId().replace(/def-/g, ""); if (previousId && childId !== previousId) { throw new Error(`Type "${name}" has multiple definitions.`); } ids.set(name, childId); } children.reduce((definitions, child) => { const name = child.getName(); if (!(name in definitions)) { definitions[name] = this.typeFormatter.getDefinition(child.getType()); } return definitions; }, childDefinitions); } private partitionFiles() { const projectFiles = new Array(); const externalFiles = new Array(); for (const sourceFile of this.program.getSourceFiles()) { const destination = sourceFile.fileName.includes("/node_modules/") ? externalFiles : projectFiles; destination.push(sourceFile); } return { projectFiles, externalFiles }; } private appendTypes( sourceFiles: readonly ts.SourceFile[], typeChecker: ts.TypeChecker, types: Map ) { for (const sourceFile of sourceFiles) { this.inspectNode(sourceFile, typeChecker, types); } } private inspectNode(node: ts.Node, typeChecker: ts.TypeChecker, allTypes: Map): void { switch (node.kind) { case ts.SyntaxKind.InterfaceDeclaration: case ts.SyntaxKind.ClassDeclaration: case ts.SyntaxKind.EnumDeclaration: case ts.SyntaxKind.TypeAliasDeclaration: if ( this.config?.expose === "all" || (this.isExportType(node) && !this.isGenericType(node as ts.TypeAliasDeclaration)) ) { allTypes.set(this.getFullName(node, typeChecker), node); return; } return; default: ts.forEachChild(node, (subnode) => this.inspectNode(subnode, typeChecker, allTypes)); return; } } private isExportType(node: ts.Node): boolean { if (this.config?.jsDoc !== "none" && hasJsDocTag(node, "internal")) { return false; } const localSymbol = localSymbolAtNode(node); return localSymbol ? "exportSymbol" in localSymbol : false; } private isGenericType(node: ts.TypeAliasDeclaration): boolean { return !!(node.typeParameters && node.typeParameters.length > 0); } private getFullName(node: ts.Node, typeChecker: ts.TypeChecker): string { const symbol = symbolAtNode(node)!; return typeChecker.getFullyQualifiedName(symbol).replace(/".*"\./, ""); } }