typescript-swagger
Version:
Generate Swagger files from a decorator library like typescript-rest or a @decorators/express.
106 lines • 3.7 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.getFirstMatchingJSDocTagName = exports.getJSDocTagNames = exports.getJSDocTagComment = exports.getJSDocTags = exports.isExistJSDocTag = exports.getMatchingJSDocTags = exports.getJSDocDescription = void 0;
var typescript_1 = require("typescript");
var error_1 = require("../metadata/resolver/error");
// -----------------------------------------
// Description
// -----------------------------------------
function getJSDocDescription(node) {
var jsDocs = node.jsDoc;
if (!jsDocs || !jsDocs.length) {
return '';
}
if (typeof jsDocs[0].comment === 'string') {
return jsDocs[0].comment;
}
return undefined;
}
exports.getJSDocDescription = getJSDocDescription;
// -----------------------------------------
// Tag
// -----------------------------------------
function getMatchingJSDocTags(node, isMatching) {
var jsDocs = node.jsDoc;
if (!jsDocs || !jsDocs.length) {
return undefined;
}
var jsDoc = jsDocs[0];
if (!jsDoc.tags) {
return [];
}
return jsDoc.tags.filter(isMatching);
}
exports.getMatchingJSDocTags = getMatchingJSDocTags;
function isExistJSDocTag(node, tagName) {
var tags;
if (typeof tagName === 'string') {
tags = getJSDocTags(node, tagName);
}
else {
tags = getMatchingJSDocTags(node, tagName);
}
return !(!tags || !tags.length);
}
exports.isExistJSDocTag = isExistJSDocTag;
function getJSDocTags(node, tagName) {
return getMatchingJSDocTags(node, function (t) { return t.tagName.text === tagName; });
}
exports.getJSDocTags = getJSDocTags;
// -----------------------------------------
// Tag Comment(s)
// -----------------------------------------
function getJSDocTagComment(node, tagName) {
var tags;
if (typeof tagName === 'string') {
tags = getJSDocTags(node, tagName);
}
else {
tags = getMatchingJSDocTags(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) {
if (requireTagName === void 0) { requireTagName = false; }
var tags = [];
if (node.kind === typescript_1.SyntaxKind.Parameter) {
var parameterName_1 = node.name.text;
tags = getMatchingJSDocTags(node.parent, function (tag) {
if (typescript_1.isJSDocParameterTag(tag)) {
return false;
}
else if (tag.comment === undefined) {
throw new error_1.ResolverError("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_1) : false;
});
}
else {
tags = getMatchingJSDocTags(node, function (tag) {
return requireTagName ? tag.comment !== undefined : true;
});
}
if (typeof tags === 'undefined') {
return [];
}
return tags.map(function (tag) {
return tag.tagName.text;
});
}
exports.getJSDocTagNames = getJSDocTagNames;
function getFirstMatchingJSDocTagName(node, isMatching) {
var tags = getMatchingJSDocTags(node, isMatching);
if (!tags || !tags.length) {
return undefined;
}
return tags[0].tagName.text;
}
exports.getFirstMatchingJSDocTagName = getFirstMatchingJSDocTagName;
//# sourceMappingURL=jsDocUtils.js.map
;