UNPKG

@trapi/metadata

Version:

Generate REST-API metadata scheme from TypeScript Decorators.

98 lines 3.58 kB
"use strict"; /* * Copyright (c) 2021-2023. * Author Peter Placzek (tada5hi) * For the full copyright and license information, * view the LICENSE file that was distributed with this source code. */ Object.defineProperty(exports, "__esModule", { value: true }); exports.getJSDocTagNames = exports.getJSDocTagComment = exports.hasJSDocTag = exports.getJSDocTags = exports.getJSDoc = exports.getJSDocDescription = void 0; const typescript_1 = require("typescript"); const object_1 = require("../object"); const utils_1 = require("./utils"); // ----------------------------------------- // Description // ----------------------------------------- function getJSDocDescription(node, index) { const jsDoc = getJSDoc(node, index); if (!jsDoc) { return undefined; } return (0, utils_1.transformJSDocComment)(jsDoc.comment); } exports.getJSDocDescription = getJSDocDescription; // ----------------------------------------- // Tag // ----------------------------------------- function getJSDoc(node, index) { if (!(0, object_1.hasOwnProperty)(node, 'jsDoc')) { return undefined; } const jsDoc = node.jsDoc; if (!jsDoc || !Array.isArray(jsDoc) || !jsDoc.length) { return undefined; } index = index ?? 0; return jsDoc.length > index && index >= 0 ? jsDoc[index] : undefined; // jsDoc[0] else case } exports.getJSDoc = getJSDoc; function getJSDocTags(node, isMatching) { const jsDoc = getJSDoc(node); if (typeof jsDoc === 'undefined') { return []; } const jsDocTags = jsDoc.tags; if (typeof jsDocTags === 'undefined') { return []; } if (typeof isMatching === 'undefined') { return jsDocTags; } if (typeof isMatching === 'function') { return jsDocTags.filter(isMatching); } const tagNames = Array.isArray(isMatching) ? isMatching : [isMatching]; return jsDocTags.filter((tag) => tagNames.indexOf(tag.tagName.text) !== -1); } exports.getJSDocTags = getJSDocTags; function hasJSDocTag(node, tagName) { const tags = getJSDocTags(node, tagName); return !(!tags || !tags.length); } exports.hasJSDocTag = hasJSDocTag; // ----------------------------------------- // Tag Comment(s) // ----------------------------------------- function getJSDocTagComment(node, tagName) { const tags = getJSDocTags(node, tagName); if (!tags || !tags.length || typeof tags[0].comment !== 'string') { return undefined; } return tags[0].comment; } exports.getJSDocTagComment = getJSDocTagComment; // ----------------------------------------- // Tag Names // ----------------------------------------- function getJSDocTagNames(node, requireTagName = false) { let tags; /* istanbul ignore next */ if (node.kind === typescript_1.SyntaxKind.Parameter) { const parameterName = node.name.text; tags = getJSDocTags(node.parent, (tag) => { if ((0, typescript_1.isJSDocParameterTag)(tag)) { return false; } if (tag.comment === undefined) { throw new Error(`Orphan tag: @${String(tag.tagName.text || tag.tagName.escapedText)} should have a parameter name follows with.`); } return typeof tag.comment === 'string' ? tag.comment.startsWith(parameterName) : false; }); } else { tags = getJSDocTags(node, (tag) => (requireTagName ? tag.comment !== undefined : true)); } return tags.map((tag) => tag.tagName.text); } exports.getJSDocTagNames = getJSDocTagNames; //# sourceMappingURL=module.js.map