UNPKG

ts-json-schema-generator

Version:

Generate JSON schema from your Typescript sources

95 lines 3.5 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ExtendedAnnotationsReader = void 0; const tslib_1 = require("tslib"); const json5_1 = tslib_1.__importDefault(require("json5")); const symbolAtNode_js_1 = require("../Utils/symbolAtNode.js"); const BasicAnnotationsReader_js_1 = require("./BasicAnnotationsReader.js"); class ExtendedAnnotationsReader extends BasicAnnotationsReader_js_1.BasicAnnotationsReader { typeChecker; markdownDescription; constructor(typeChecker, extraTags, markdownDescription) { super(extraTags); this.typeChecker = typeChecker; this.markdownDescription = markdownDescription; } getAnnotations(node) { const annotations = { ...this.getDescriptionAnnotation(node), ...this.getTypeAnnotation(node), ...this.getExampleAnnotation(node), ...super.getAnnotations(node), }; return Object.keys(annotations).length ? annotations : undefined; } isNullable(node) { const symbol = (0, symbolAtNode_js_1.symbolAtNode)(node); if (!symbol) { return false; } const jsDocTags = symbol.getJsDocTags(); if (!jsDocTags || !jsDocTags.length) { return false; } const jsDocTag = jsDocTags.find((tag) => tag.name === "nullable"); return !!jsDocTag; } getDescriptionAnnotation(node) { const symbol = (0, symbolAtNode_js_1.symbolAtNode)(node); if (!symbol) { return undefined; } const comments = symbol.getDocumentationComment(this.typeChecker); if (!comments || !comments.length) { return undefined; } const markdownDescription = comments .map((comment) => comment.text) .join(" ") .replace(/\r/g, "") .trim(); const description = markdownDescription.replace(/(?<=[^\n])\n(?=[^\n*-])/g, " ").trim(); return this.markdownDescription ? { description, markdownDescription } : { description }; } getTypeAnnotation(node) { const symbol = (0, symbolAtNode_js_1.symbolAtNode)(node); if (!symbol) { return undefined; } const jsDocTags = symbol.getJsDocTags(); if (!jsDocTags || !jsDocTags.length) { return undefined; } const jsDocTag = jsDocTags.find((tag) => tag.name === "asType"); if (!jsDocTag) { return undefined; } const text = (jsDocTag.text ?? []).map((part) => part.text).join(""); return { type: text }; } getExampleAnnotation(node) { const symbol = (0, symbolAtNode_js_1.symbolAtNode)(node); if (!symbol) { return undefined; } const jsDocTags = symbol.getJsDocTags(); if (!jsDocTags || !jsDocTags.length) { return undefined; } const examples = []; for (const example of jsDocTags.filter((tag) => tag.name === "example")) { const text = (example.text ?? []).map((part) => part.text).join(""); try { examples.push(json5_1.default.parse(text)); } catch { } } if (examples.length === 0) { return undefined; } return { examples }; } } exports.ExtendedAnnotationsReader = ExtendedAnnotationsReader; //# sourceMappingURL=ExtendedAnnotationsReader.js.map