UNPKG

typescript-json-schema

Version:

typescript-json-schema generates JSON Schema files from your Typescript sources

1,197 lines 58.5 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.JsonSchemaGenerator = exports.ts = void 0; exports.getDefaultArgs = getDefaultArgs; exports.regexRequire = regexRequire; exports.getProgramFromFiles = getProgramFromFiles; exports.buildGenerator = buildGenerator; exports.generateSchema = generateSchema; exports.programFromConfig = programFromConfig; exports.exec = exec; const glob = require("glob"); const safe_stable_stringify_1 = require("safe-stable-stringify"); const path = require("path"); const crypto_1 = require("crypto"); const ts = require("typescript"); exports.ts = ts; const path_equal_1 = require("path-equal"); const { VM } = require("vm2"); const REGEX_FILE_NAME_OR_SPACE = /(\bimport\(".*?"\)|".*?")\.| /g; const REGEX_TSCONFIG_NAME = /^.*\.json$/; const REGEX_TJS_JSDOC = /^-([\w]+)\s+(\S|\S[\s\S]*\S)\s*$/g; const REGEX_GROUP_JSDOC = /^[.]?([\w]+)\s+(\S|\S[\s\S]*\S)\s*$/g; const REGEX_REQUIRE = /^(\s+)?require\((\'@?[a-zA-Z0-9.\/_-]+\'|\"@?[a-zA-Z0-9.\/_-]+\")\)(\.([a-zA-Z0-9_$]+))?(\s+|$)/; const NUMERIC_INDEX_PATTERN = "^[0-9]+$"; function getDefaultArgs() { return { ref: true, aliasRef: false, topRef: false, titles: false, defaultProps: false, noExtraProps: false, propOrder: false, typeOfKeyword: false, required: false, strictNullChecks: false, esModuleInterop: false, skipLibCheck: false, experimentalDecorators: true, ignoreErrors: false, out: "", validationKeywords: [], include: [], excludePrivate: false, uniqueNames: false, rejectDateType: false, id: "", defaultNumberType: "number", tsNodeRegister: false, constAsEnum: false, }; } function extend(target, ..._) { if (target == null) { throw new TypeError("Cannot convert undefined or null to object"); } const to = Object(target); for (var index = 1; index < arguments.length; index++) { const nextSource = arguments[index]; if (nextSource != null) { for (const nextKey in nextSource) { if (Object.prototype.hasOwnProperty.call(nextSource, nextKey)) { to[nextKey] = nextSource[nextKey]; } } } } return to; } function unique(arr) { const temp = {}; for (const e of arr) { temp[e] = true; } const r = []; for (const k in temp) { if (Object.prototype.hasOwnProperty.call(temp, k)) { r.push(k); } } return r; } function resolveRequiredFile(symbol, key, fileName, objectName) { const sourceFile = getSourceFile(symbol); const requiredFilePath = /^[.\/]+/.test(fileName) ? fileName === "." ? path.resolve(sourceFile.fileName) : path.resolve(path.dirname(sourceFile.fileName), fileName) : fileName; const requiredFile = require(requiredFilePath); if (!requiredFile) { throw Error("Required: File couldn't be loaded"); } const requiredObject = objectName ? requiredFile[objectName] : requiredFile.default; if (requiredObject === undefined) { throw Error("Required: Variable is undefined"); } if (typeof requiredObject === "function") { throw Error("Required: Can't use function as a variable"); } if (key === "examples" && !Array.isArray(requiredObject)) { throw Error("Required: Variable isn't an array"); } return requiredObject; } function regexRequire(value) { return REGEX_REQUIRE.exec(value); } function parseValue(symbol, key, value) { const match = regexRequire(value); if (match) { const fileName = match[2].substr(1, match[2].length - 2).trim(); const objectName = match[4]; return resolveRequiredFile(symbol, key, fileName, objectName); } try { return JSON.parse(value); } catch (error) { return value; } } function extractLiteralValue(typ) { let str = typ.value; if (str === undefined) { str = typ.text; } if (typ.flags & ts.TypeFlags.StringLiteral) { return str; } else if (typ.flags & ts.TypeFlags.BooleanLiteral) { return typ.intrinsicName === "true"; } else if (typ.flags & ts.TypeFlags.EnumLiteral) { const num = parseFloat(str); return isNaN(num) ? str : num; } else if (typ.flags & ts.TypeFlags.NumberLiteral) { return parseFloat(str); } return undefined; } function resolveTupleType(propertyType) { if (!propertyType.getSymbol() && propertyType.getFlags() & ts.TypeFlags.Object && propertyType.objectFlags & ts.ObjectFlags.Reference) { return propertyType.target; } if (!(propertyType.getFlags() & ts.TypeFlags.Object && propertyType.objectFlags & ts.ObjectFlags.Tuple)) { return null; } return propertyType; } const simpleTypesAllowedProperties = { type: true, description: true, }; function addSimpleType(def, type) { for (const k in def) { if (!simpleTypesAllowedProperties[k]) { return false; } } if (!def.type) { def.type = type; } else if (typeof def.type !== "string") { if (!def.type.every((val) => { return typeof val === "string"; })) { return false; } if (def.type.indexOf("null") === -1) { def.type.push("null"); } } else { if (typeof def.type !== "string") { return false; } if (def.type !== "null") { def.type = [def.type, "null"]; } } return true; } function makeNullable(def) { if (!addSimpleType(def, "null")) { const union = def.oneOf || def.anyOf; if (union) { union.push({ type: "null" }); } else { const subdef = {}; for (var k in def) { if (def.hasOwnProperty(k)) { subdef[k] = def[k]; delete def[k]; } } def.anyOf = [subdef, { type: "null" }]; } } return def; } function getCanonicalDeclaration(sym) { var _a, _b, _c; if (sym.valueDeclaration !== undefined) { return sym.valueDeclaration; } else if (((_a = sym.declarations) === null || _a === void 0 ? void 0 : _a.length) === 1) { return sym.declarations[0]; } const declarationCount = (_c = (_b = sym.declarations) === null || _b === void 0 ? void 0 : _b.length) !== null && _c !== void 0 ? _c : 0; throw new Error(`Symbol "${sym.name}" has no valueDeclaration and ${declarationCount} declarations.`); } function getSourceFile(sym) { let currentDecl = getCanonicalDeclaration(sym); while (currentDecl.kind !== ts.SyntaxKind.SourceFile) { if (currentDecl.parent === undefined) { throw new Error(`Unable to locate source file for declaration "${sym.name}".`); } currentDecl = currentDecl.parent; } return currentDecl; } const validationKeywords = { multipleOf: true, maximum: true, exclusiveMaximum: true, minimum: true, exclusiveMinimum: true, maxLength: true, minLength: true, pattern: true, items: true, maxItems: true, minItems: true, uniqueItems: true, contains: true, maxProperties: true, minProperties: true, additionalProperties: true, enum: true, type: true, examples: true, ignore: true, description: true, format: true, default: true, $ref: true, id: true, $id: true, $comment: true, title: true }; const annotationKeywords = { description: true, default: true, examples: true, title: true, $ref: true, }; const subDefinitions = { items: true, additionalProperties: true, contains: true, }; class JsonSchemaGenerator { constructor(symbols, allSymbols, userSymbols, inheritingTypes, tc, args = getDefaultArgs()) { this.args = args; this.reffedDefinitions = {}; this.schemaOverrides = new Map(); this.typeNamesById = {}; this.typeIdsByName = {}; this.recursiveTypeRef = new Map(); this.symbols = symbols; this.allSymbols = allSymbols; this.userSymbols = userSymbols; this.inheritingTypes = inheritingTypes; this.tc = tc; this.userValidationKeywords = args.validationKeywords.reduce((acc, word) => (Object.assign(Object.assign({}, acc), { [word]: true })), {}); this.constAsEnum = args.constAsEnum; } get ReffedDefinitions() { return this.reffedDefinitions; } isFromDefaultLib(symbol) { const declarations = symbol.getDeclarations(); if (declarations && declarations.length > 0 && declarations[0].parent) { return declarations[0].parent.getSourceFile().hasNoDefaultLib; } return false; } resetSchemaSpecificProperties(includeAllOverrides = false) { this.reffedDefinitions = {}; this.typeIdsByName = {}; this.typeNamesById = {}; if (includeAllOverrides) { this.schemaOverrides.forEach((value, key) => { this.reffedDefinitions[key] = value; }); } } parseCommentsIntoDefinition(symbol, definition, otherAnnotations) { if (!symbol) { return; } if (!this.isFromDefaultLib(symbol)) { const comments = symbol.getDocumentationComment(this.tc); if (comments.length) { definition.description = comments .map((comment) => { const newlineNormalizedComment = comment.text.replace(/\r\n/g, "\n"); if (comment.kind === "linkText") { return newlineNormalizedComment.trim(); } return newlineNormalizedComment; }) .join("").trim(); } } const jsdocs = symbol.getJsDocTags(); jsdocs.forEach((doc) => { let name = doc.name; const originalText = doc.text ? doc.text.map((t) => t.text).join("") : ""; let text = originalText; if (name.startsWith("TJS-")) { name = name.slice(4); if (!text) { text = "true"; } } else if (name === "TJS" && text.startsWith("-")) { let match = new RegExp(REGEX_TJS_JSDOC).exec(originalText); if (match) { name = match[1]; text = match[2]; } else { name = text.replace(/^[\s\-]+/, ""); text = "true"; } } if (subDefinitions[name]) { const match = new RegExp(REGEX_GROUP_JSDOC).exec(text); if (match) { const k = match[1]; const v = match[2]; definition[name] = Object.assign(Object.assign({}, definition[name]), { [k]: v ? parseValue(symbol, k, v) : true }); return; } } if (name.includes(".")) { const parts = name.split("."); const key = parts[0]; if (parts.length === 2 && subDefinitions[key]) { definition[key] = Object.assign(Object.assign({}, definition[key]), { [parts[1]]: text ? parseValue(symbol, name, text) : true }); } } if (validationKeywords[name] || this.userValidationKeywords[name]) { definition[name] = text === undefined ? "" : parseValue(symbol, name, text); } else { otherAnnotations[doc.name] = true; } }); } getDefinitionForRootType(propertyType, reffedType, definition, defaultNumberType = this.args.defaultNumberType, ignoreUndefined = false) { var _a; const tupleType = resolveTupleType(propertyType); if (tupleType) { const elemTypes = propertyType.typeArguments; const targetTupleType = propertyType.target; const fixedTypes = elemTypes.map((elType, index) => { var _a, _b, _c; const def = this.getTypeDefinition(elType); const label = (_c = (_b = (_a = targetTupleType.labeledElementDeclarations) === null || _a === void 0 ? void 0 : _a[index]) === null || _b === void 0 ? void 0 : _b.name) === null || _c === void 0 ? void 0 : _c.getFullText().trim(); if (label) { def.title = label; } return def; }); definition.type = "array"; if (fixedTypes.length > 0) { definition.items = fixedTypes; } definition.minItems = targetTupleType.minLength; if (targetTupleType.hasRestElement) { definition.additionalItems = fixedTypes[fixedTypes.length - 1]; fixedTypes.splice(fixedTypes.length - 1, 1); } else { definition.maxItems = targetTupleType.fixedLength; } } else { const propertyTypeString = this.tc.typeToString(propertyType, undefined, ts.TypeFormatFlags.UseFullyQualifiedType); const flags = propertyType.flags; const arrayType = this.tc.getIndexTypeOfType(propertyType, ts.IndexKind.Number); if (flags & ts.TypeFlags.String) { definition.type = "string"; } else if (flags & ts.TypeFlags.Number) { const isInteger = definition.type === "integer" || (reffedType === null || reffedType === void 0 ? void 0 : reffedType.getName()) === "integer" || defaultNumberType === "integer"; definition.type = isInteger ? "integer" : "number"; } else if (flags & ts.TypeFlags.Boolean) { definition.type = "boolean"; } else if (flags & ts.TypeFlags.ESSymbol) { definition.type = "object"; } else if (flags & ts.TypeFlags.Null) { definition.type = "null"; } else if (flags & ts.TypeFlags.Undefined || propertyTypeString === "void") { if (!ignoreUndefined) { throw new Error("Not supported: root type undefined"); } definition.type = "undefined"; } else if (flags & ts.TypeFlags.Any || flags & ts.TypeFlags.Unknown) { } else if (propertyTypeString === "Date" && !this.args.rejectDateType) { definition.type = "string"; definition.format = definition.format || "date-time"; } else if (propertyTypeString === "object") { definition.type = "object"; definition.properties = {}; definition.additionalProperties = true; } else if (propertyTypeString === "bigint") { definition.type = "number"; definition.properties = {}; definition.additionalProperties = false; } else { const value = extractLiteralValue(propertyType); if (value !== undefined) { const typeofValue = typeof value; switch (typeofValue) { case "string": case "boolean": definition.type = typeofValue; break; case "number": definition.type = this.args.defaultNumberType; break; case "object": definition.type = "null"; break; default: throw new Error(`Not supported: ${value} as a enum value`); } if (this.constAsEnum) { definition.enum = [value]; } else { definition.const = value; } } else if (arrayType !== undefined) { if (propertyType.flags & ts.TypeFlags.Object && propertyType.objectFlags & (ts.ObjectFlags.Anonymous | ts.ObjectFlags.Interface | ts.ObjectFlags.Mapped)) { definition.type = "object"; definition.additionalProperties = false; definition.patternProperties = { [NUMERIC_INDEX_PATTERN]: this.getTypeDefinition(arrayType), }; if (!!((_a = Array.from(propertyType.members)) === null || _a === void 0 ? void 0 : _a.find((member) => member[0] !== "__index"))) { this.getClassDefinition(propertyType, definition); } } else if (propertyType.flags & ts.TypeFlags.TemplateLiteral) { definition.type = "string"; const { texts, types } = propertyType; const pattern = []; for (let i = 0; i < texts.length; i++) { const text = texts[i].replace(/[\\^$.*+?()[\]{}|]/g, "\\$&"); const type = types[i]; if (i === 0) { pattern.push(`^`); } if (type) { if (type.flags & ts.TypeFlags.String) { pattern.push(`${text}.*`); } if (type.flags & ts.TypeFlags.Number || type.flags & ts.TypeFlags.BigInt) { pattern.push(`${text}[0-9]*`); } if (type.flags & ts.TypeFlags.Undefined) { pattern.push(`${text}undefined`); } if (type.flags & ts.TypeFlags.Null) { pattern.push(`${text}null`); } } if (i === texts.length - 1) { pattern.push(`${text}$`); } } definition.pattern = pattern.join(""); } else { definition.type = "array"; if (!definition.items) { definition.items = this.getTypeDefinition(arrayType); } } } else { const error = new TypeError("Unsupported type: " + propertyTypeString); error.type = propertyType; throw error; } } } return definition; } getReferencedTypeSymbol(prop) { const decl = prop.getDeclarations(); if (decl === null || decl === void 0 ? void 0 : decl.length) { const type = decl[0].type; if (type && type.kind & ts.SyntaxKind.TypeReference && type.typeName) { const symbol = this.tc.getSymbolAtLocation(type.typeName); if (symbol && symbol.flags & ts.SymbolFlags.Alias) { return this.tc.getAliasedSymbol(symbol); } return symbol; } } return undefined; } getDefinitionForProperty(prop, node) { if (prop.flags & ts.SymbolFlags.Method) { return null; } const propertyName = prop.getName(); const propertyType = this.tc.getTypeOfSymbolAtLocation(prop, node); const reffedType = this.getReferencedTypeSymbol(prop); const definition = this.getTypeDefinition(propertyType, undefined, undefined, prop, reffedType); if (this.args.titles) { definition.title = propertyName; } if (definition.hasOwnProperty("ignore")) { return null; } const valDecl = prop.valueDeclaration; if (valDecl === null || valDecl === void 0 ? void 0 : valDecl.initializer) { let initial = valDecl.initializer; while (ts.isTypeAssertionExpression(initial)) { initial = initial.expression; } if (initial.expression) { console.warn("initializer is expression for property " + propertyName); } else if (initial.kind && initial.kind === ts.SyntaxKind.NoSubstitutionTemplateLiteral) { definition.default = initial.getText(); } else { try { const vm = new VM(); const val = vm.run("sandboxvar=" + initial.getText()); if (val === null || typeof val === "string" || typeof val === "number" || typeof val === "boolean" || Object.prototype.toString.call(val) === "[object Array]") { definition.default = val; } else if (val) { console.warn("unknown initializer for property " + propertyName + ": " + val); } } catch (e) { console.warn("exception evaluating initializer for property " + propertyName); } } } return definition; } getEnumDefinition(clazzType, definition) { const node = clazzType.getSymbol().getDeclarations()[0]; const fullName = this.tc.typeToString(clazzType, undefined, ts.TypeFormatFlags.UseFullyQualifiedType); const members = node.kind === ts.SyntaxKind.EnumDeclaration ? node.members : ts.factory.createNodeArray([node]); var enumValues = []; const enumTypes = []; const addType = (type) => { if (enumTypes.indexOf(type) === -1) { enumTypes.push(type); } }; members.forEach((member) => { const caseLabel = member.name.text; const constantValue = this.tc.getConstantValue(member); if (constantValue !== undefined) { enumValues.push(constantValue); addType(typeof constantValue); } else { const initial = member.initializer; if (initial) { if (initial.expression) { const exp = initial.expression; const text = exp.text; if (text) { enumValues.push(text); addType("string"); } else if (exp.kind === ts.SyntaxKind.TrueKeyword || exp.kind === ts.SyntaxKind.FalseKeyword) { enumValues.push(exp.kind === ts.SyntaxKind.TrueKeyword); addType("boolean"); } else { console.warn("initializer is expression for enum: " + fullName + "." + caseLabel); } } else if (initial.kind === ts.SyntaxKind.NoSubstitutionTemplateLiteral) { enumValues.push(initial.getText()); addType("string"); } else if (initial.kind === ts.SyntaxKind.NullKeyword) { enumValues.push(null); addType("null"); } } } }); if (enumTypes.length) { definition.type = enumTypes.length === 1 ? enumTypes[0] : enumTypes; } if (enumValues.length > 0) { if (enumValues.length > 1) { definition.enum = enumValues; } else { definition.const = enumValues[0]; } } return definition; } getUnionDefinition(unionType, unionModifier, definition) { const enumValues = []; const simpleTypes = []; const schemas = []; const pushSimpleType = (type) => { if (simpleTypes.indexOf(type) === -1) { simpleTypes.push(type); } }; const pushEnumValue = (val) => { if (enumValues.indexOf(val) === -1) { enumValues.push(val); } }; for (const valueType of unionType.types) { const value = extractLiteralValue(valueType); if (value !== undefined) { pushEnumValue(value); } else { const symbol = valueType.aliasSymbol; const def = this.getTypeDefinition(valueType, undefined, undefined, symbol, symbol, undefined, undefined, true); if (def.type === "undefined") { continue; } const keys = Object.keys(def); if (keys.length === 1 && keys[0] === "type") { if (typeof def.type !== "string") { console.error("Expected only a simple type."); } else { pushSimpleType(def.type); } } else { schemas.push(def); } } } if (enumValues.length > 0) { const isOnlyBooleans = enumValues.length === 2 && typeof enumValues[0] === "boolean" && typeof enumValues[1] === "boolean" && enumValues[0] !== enumValues[1]; if (isOnlyBooleans) { pushSimpleType("boolean"); } else { const enumSchema = enumValues.length > 1 ? { enum: enumValues.sort() } : { const: enumValues[0] }; if (enumValues.every((x) => { return typeof x === "string"; })) { enumSchema.type = "string"; } else if (enumValues.every((x) => { return typeof x === "number"; })) { enumSchema.type = "number"; } else if (enumValues.every((x) => { return typeof x === "boolean"; })) { enumSchema.type = "boolean"; } schemas.push(enumSchema); } } if (simpleTypes.length > 0) { schemas.push({ type: simpleTypes.length === 1 ? simpleTypes[0] : simpleTypes }); } if (schemas.length === 1) { for (const k in schemas[0]) { if (schemas[0].hasOwnProperty(k)) { if (k === "description" && definition.hasOwnProperty(k)) { continue; } definition[k] = schemas[0][k]; } } } else { definition[unionModifier] = schemas; } return definition; } getIntersectionDefinition(intersectionType, definition) { const simpleTypes = []; const schemas = []; const pushSimpleType = (type) => { if (simpleTypes.indexOf(type) === -1) { simpleTypes.push(type); } }; for (const intersectionMember of intersectionType.types) { const def = this.getTypeDefinition(intersectionMember); const keys = Object.keys(def); if (keys.length === 1 && keys[0] === "type") { if (typeof def.type !== "string") { console.error("Expected only a simple type."); } else { pushSimpleType(def.type); } } else { schemas.push(def); } } if (simpleTypes.length > 0) { schemas.push({ type: simpleTypes.length === 1 ? simpleTypes[0] : simpleTypes }); } if (schemas.length === 1) { for (const k in schemas[0]) { if (schemas[0].hasOwnProperty(k)) { definition[k] = schemas[0][k]; } } } else { definition.allOf = schemas; } return definition; } getClassDefinition(clazzType, definition) { var _a, _b; const node = clazzType.getSymbol().getDeclarations()[0]; if (!node) { definition.type = "object"; return definition; } if (this.args.typeOfKeyword && node.kind === ts.SyntaxKind.FunctionType) { definition.typeof = "function"; return definition; } const clazz = node; const props = this.tc.getPropertiesOfType(clazzType).filter((prop) => { const propertyFlagType = this.tc.getTypeOfSymbolAtLocation(prop, node).getFlags(); if (ts.TypeFlags.Never === propertyFlagType || ts.TypeFlags.Undefined === propertyFlagType) { return false; } if (!this.args.excludePrivate) { return true; } const decls = prop.declarations; return !(decls && decls.filter((decl) => { const mods = decl.modifiers; return mods && mods.filter((mod) => mod.kind === ts.SyntaxKind.PrivateKeyword).length > 0; }).length > 0); }); const fullName = this.tc.typeToString(clazzType, undefined, ts.TypeFormatFlags.UseFullyQualifiedType); const modifierFlags = ts.getCombinedModifierFlags(node); if (modifierFlags & ts.ModifierFlags.Abstract && this.inheritingTypes[fullName]) { const oneOf = this.inheritingTypes[fullName].map((typename) => { return this.getTypeDefinition(this.allSymbols[typename]); }); definition.oneOf = oneOf; } else { if (clazz.members) { const indexSignatures = clazz.members == null ? [] : clazz.members.filter((x) => x.kind === ts.SyntaxKind.IndexSignature); if (indexSignatures.length === 1) { const indexSignature = indexSignatures[0]; if (indexSignature.parameters.length !== 1) { throw new Error("Not supported: IndexSignatureDeclaration parameters.length != 1"); } const indexSymbol = indexSignature.parameters[0].symbol; const indexType = this.tc.getTypeOfSymbolAtLocation(indexSymbol, node); const isIndexedObject = indexType.flags === ts.TypeFlags.String || indexType.flags === ts.TypeFlags.Number; if (indexType.flags !== ts.TypeFlags.Number && !isIndexedObject) { throw new Error("Not supported: IndexSignatureDeclaration with index symbol other than a number or a string"); } const typ = this.tc.getTypeAtLocation(indexSignature.type); let def; if (typ.flags & ts.TypeFlags.IndexedAccess) { const targetName = ts.escapeLeadingUnderscores((_b = (_a = clazzType.mapper) === null || _a === void 0 ? void 0 : _a.target) === null || _b === void 0 ? void 0 : _b.value); const indexedAccessType = typ; const symbols = indexedAccessType.objectType.members; const targetSymbol = symbols === null || symbols === void 0 ? void 0 : symbols.get(targetName); if (targetSymbol) { const targetNode = targetSymbol.getDeclarations()[0]; const targetDef = this.getDefinitionForProperty(targetSymbol, targetNode); if (targetDef) { def = targetDef; } } } if (!def) { def = this.getTypeDefinition(typ, undefined, "anyOf"); } if (isIndexedObject) { definition.type = "object"; if (!Object.keys(definition.patternProperties || {}).length) { definition.additionalProperties = def; } } else { definition.type = "array"; if (!definition.items) { definition.items = def; } } } } const propertyDefinitions = props.reduce((all, prop) => { const propertyName = prop.getName(); const propDef = this.getDefinitionForProperty(prop, node); if (propDef != null) { all[propertyName] = propDef; } return all; }, {}); if (definition.type === undefined) { definition.type = "object"; } if (definition.type === "object" && Object.keys(propertyDefinitions).length > 0) { definition.properties = propertyDefinitions; } if (this.args.defaultProps) { definition.defaultProperties = []; } if (this.args.noExtraProps && definition.additionalProperties === undefined) { definition.additionalProperties = false; } if (this.args.propOrder) { const propertyOrder = props.reduce((order, prop) => { order.push(prop.getName()); return order; }, []); definition.propertyOrder = propertyOrder; } if (this.args.required) { const requiredProps = props.reduce((required, prop) => { var _a, _b, _c, _d; const def = {}; this.parseCommentsIntoDefinition(prop, def, {}); const allUnionTypesFlags = ((_d = (_c = (_b = (_a = prop.links) === null || _a === void 0 ? void 0 : _a.type) === null || _b === void 0 ? void 0 : _b.types) === null || _c === void 0 ? void 0 : _c.map) === null || _d === void 0 ? void 0 : _d.call(_c, (t) => t.flags)) || []; if (!(prop.flags & ts.SymbolFlags.Optional) && !(prop.flags & ts.SymbolFlags.Method) && !allUnionTypesFlags.includes(ts.TypeFlags.Undefined) && !allUnionTypesFlags.includes(ts.TypeFlags.Void) && !def.hasOwnProperty("ignore")) { required.push(prop.getName()); } return required; }, []); if (requiredProps.length > 0) { definition.required = unique(requiredProps).sort(); } } } return definition; } getTypeName(typ) { const id = typ.id; if (this.typeNamesById[id]) { return this.typeNamesById[id]; } return this.makeTypeNameUnique(typ, this.tc .typeToString(typ, undefined, ts.TypeFormatFlags.NoTruncation | ts.TypeFormatFlags.UseFullyQualifiedType) .replace(REGEX_FILE_NAME_OR_SPACE, "")); } makeTypeNameUnique(typ, baseName) { const id = typ.id; let name = baseName; for (let i = 1; this.typeIdsByName[name] !== undefined && this.typeIdsByName[name] !== id; ++i) { name = baseName + "_" + i; } this.typeNamesById[id] = name; this.typeIdsByName[name] = id; return name; } getTypeDefinition(typ, asRef = this.args.ref, unionModifier = "anyOf", prop, reffedType, pairedSymbol, forceNotRef = false, ignoreUndefined = false) { var _a; const definition = {}; while (typ.aliasSymbol && (typ.aliasSymbol.escapedName === "Readonly" || typ.aliasSymbol.escapedName === "Mutable") && typ.aliasTypeArguments && typ.aliasTypeArguments[0]) { typ = typ.aliasTypeArguments[0]; reffedType = undefined; } if (this.args.typeOfKeyword && typ.flags & ts.TypeFlags.Object && typ.objectFlags & ts.ObjectFlags.Anonymous) { definition.typeof = "function"; return definition; } let returnedDefinition = definition; if (prop) { const defs = {}; const others = {}; this.parseCommentsIntoDefinition(prop, defs, others); if (defs.hasOwnProperty("ignore") || defs.hasOwnProperty("type")) { return defs; } } const symbol = typ.getSymbol(); let isRawType = !symbol || (this.tc.getFullyQualifiedName(symbol) !== "Window" && (this.tc.getFullyQualifiedName(symbol) === "Date" || symbol.name === "integer" || this.tc.getIndexInfoOfType(typ, ts.IndexKind.Number) !== undefined)); if (isRawType && ((_a = typ.aliasSymbol) === null || _a === void 0 ? void 0 : _a.escapedName) && typ.types) { isRawType = false; } let isStringEnum = false; if (typ.flags & ts.TypeFlags.Union) { const unionType = typ; isStringEnum = unionType.types.every((propType) => { return (propType.getFlags() & ts.TypeFlags.StringLiteral) !== 0; }); } const asTypeAliasRef = asRef && reffedType && (this.args.aliasRef || isStringEnum); if (!asTypeAliasRef) { if (isRawType || (typ.getFlags() & ts.TypeFlags.Object && typ.objectFlags & ts.ObjectFlags.Anonymous)) { asRef = false; } } let fullTypeName = ""; if (asTypeAliasRef) { const typeName = this.tc .getFullyQualifiedName(reffedType.getFlags() & ts.SymbolFlags.Alias ? this.tc.getAliasedSymbol(reffedType) : reffedType) .replace(REGEX_FILE_NAME_OR_SPACE, ""); if (this.args.uniqueNames && reffedType) { const sourceFile = getSourceFile(reffedType); const relativePath = path.relative(process.cwd(), sourceFile.fileName); fullTypeName = `${typeName}.${generateHashOfNode(getCanonicalDeclaration(reffedType), relativePath)}`; } else { fullTypeName = this.makeTypeNameUnique(typ, typeName); } } else { if (this.args.uniqueNames && typ.symbol) { const sym = typ.symbol; const sourceFile = getSourceFile(sym); const relativePath = path.relative(process.cwd(), sourceFile.fileName); fullTypeName = `${this.getTypeName(typ)}.${generateHashOfNode(getCanonicalDeclaration(sym), relativePath)}`; } else if (reffedType && this.schemaOverrides.has(reffedType.escapedName)) { fullTypeName = reffedType.escapedName; } else { fullTypeName = this.getTypeName(typ); } } if (!isRawType || !!typ.aliasSymbol) { if (this.recursiveTypeRef.has(fullTypeName) && !forceNotRef) { asRef = true; } else { this.recursiveTypeRef.set(fullTypeName, definition); } } if (asRef) { returnedDefinition = { $ref: createRefURI(this.args.id, fullTypeName), }; } const otherAnnotations = {}; this.parseCommentsIntoDefinition(reffedType, definition, otherAnnotations); this.parseCommentsIntoDefinition(symbol, definition, otherAnnotations); this.parseCommentsIntoDefinition(typ.aliasSymbol, definition, otherAnnotations); if (prop) { this.parseCommentsIntoDefinition(prop, returnedDefinition, otherAnnotations); } if (pairedSymbol && symbol && this.isFromDefaultLib(symbol)) { this.parseCommentsIntoDefinition(pairedSymbol, definition, otherAnnotations); } const overrideDefinition = this.schemaOverrides.get(fullTypeName); if (overrideDefinition) { this.reffedDefinitions[fullTypeName] = overrideDefinition; } else if (!asRef || !this.reffedDefinitions[fullTypeName]) { if (asRef) { let reffedDefinition; if (asTypeAliasRef && reffedType && typ.symbol !== reffedType && symbol) { reffedDefinition = this.getTypeDefinition(typ, true, undefined, symbol, symbol); } else { reffedDefinition = definition; } this.reffedDefinitions[fullTypeName] = reffedDefinition; if (this.args.titles && fullTypeName) { definition.title = fullTypeName; } } const node = (symbol === null || symbol === void 0 ? void 0 : symbol.getDeclarations()) !== undefined ? symbol.getDeclarations()[0] : null; if (definition.type === undefined) { if (typ.flags & ts.TypeFlags.Union && (node === null || node.kind !== ts.SyntaxKind.EnumDeclaration)) { this.getUnionDefinition(typ, unionModifier, definition); } else if (typ.flags & ts.TypeFlags.Intersection) { if (this.args.noExtraProps) { if (this.args.noExtraProps) { definition.additionalProperties = false; } const types = typ.types; for (const member of types) { const other = this.getTypeDefinition(member, false, undefined, undefined, undefined, undefined, true); definition.type = other.type; definition.properties = Object.assign(Object.assign({}, definition.properties), other.properties); if (Object.keys(other.default || {}).length > 0) { definition.default = extend(definition.default || {}, other.default); } if (other.required) { definition.required = unique((definition.required || []).concat(other.required)).sort(); } } } else { this.getIntersectionDefinition(typ, definition); } } else if (isRawType) { if (pairedSymbol) { this.parseCommentsIntoDefinition(pairedSymbol, definition, {}); } this.getDefinitionForRootType(typ, reffedType, definition, undefined, ignoreUndefined); } else if (node && (node.kind === ts.SyntaxKind.EnumDeclaration || node.kind === ts.SyntaxKind.EnumMember)) { this.getEnumDefinition(typ, definition); } else if (symbol && symbol.flags & ts.SymbolFlags.TypeLiteral && symbol.members.size === 0 && !(node && node.kind === ts.SyntaxKind.MappedType)) { definition.type = "object"; definition.properties = {}; } else { this.getClassDefinition(typ, definition); } } } if (this.recursiveTypeRef.get(fullTypeName) === definition) { this.recursiveTypeRef.delete(fullTypeName); if (this.reffedDefinitions[fullTypeName]) { const annotations = Object.entries(returnedDefinition).reduce((acc, [key, value]) => { if (annotationKeywords[key] && typeof value !== undefined) { acc[key] = value; } return acc; }, {}); returnedDefinition = Object.assign({ $ref: createRefURI(this.args.id, fullTypeName) }, annotations); } } if (otherAnnotations["nullable"]) { makeNullable(returnedDefinition); } return returnedDefinition; } setSchemaOverride(symbolName, schema) { this.schemaOverrides.set(symbolName, schema); } getSchemaForSymbol(symbolName, includeReffedDefinitions = true, includeAllOverrides = false) { const overrideDefinition = this.schemaOverrides.get(symbolName); if (!this.allSymbols[symbolName] && !overrideDefinition) { throw new Error(`type ${symbolName} not found`); } this.resetSchemaSpecificProperties(includeAllOverrides); let def; if (overrideDefinition) { def = Object.assign({}, overrideDefinition); } else { def = overrideDefinition ? overrideDefinition : this.getTypeDefinition(this.allSymbols[symbolName], this.args.topRef, undefined, undefined, undefined, this.userSymbols[symbolName] || undefined); } if (this.args.ref && includeReffedDefinitions && Object.keys(this.reffedDefinitions).length > 0) { def.definitions = this.reffedDefinitions; } def["$schema"] = "http://json-schema.org/draft-07/schema#"; const id = this.args.id; if (id) { def["$id"] = this.args.id; } return def; } getSchemaForSymbols(symbolNames, includeReffedDefinitions = true, includeAllOverrides = false) { const root = { $schema: "http://json-schema.org/draft-07/schema#", definitions: {}, }; this.resetSchemaSpecificProperties(includeAllOverrides); const id = this.args.id; if (id) { root["$id"] = id; } for (const symbolName of symbolNames) { root.definitions[symbolName] = this.getTypeDefinition(this.allSymbols[symbolName], this.args.topRef, undefined, undefined, undefined, this.userSymbols[symbolName]); } if (this.args.ref && includeReffedDefinitions && Object.keys(this.reffedDefinitions).length > 0) { root.definitions = Object.assign(Object.assign({}, root.definitions), this.reffedDefinitions); } return root; } getSymbols(name) { if (name === void 0) { return this.symbols; } return this.symbols.filter((symbol) => symbol.typeName === name); } getUserSymbols() { return Object.keys(this.userSymbols); } getMainFileSymbols(program, onlyIncludeFiles) { function includeFile(file) { if (onlyIncludeFiles === undefined) { return !file.isDeclarationFile; } return onlyIncludeFiles.filter((f) => (0, path_equal_1.pathEqual)(f, file.fileName)).length > 0; } const files = program.getSourceFiles().filter(includeFile); if (files.length) { return Object.keys(this.userSymbols).filter((key) => { const symbol = this.userSymbols[key]; if (!symbol || !symbol.declarations || !symbol.declarations.length) { return false; } let node = symbol.declarations[0]; while (node === null || node === void 0 ? void 0 : node.parent) { node = node.parent; } return files.indexOf(node.getSourceFile()) > -1; }); } return []; } } exports.JsonSchemaGenerator = JsonSchemaGenerator; function createRefURI(id, name) { const encoded = encodeURIComponent(name); return `${id}#/definitions/${encoded}`; } function getProgramFromFiles(file