UNPKG

prettier-plugin-jsdoc

Version:

Prettier plugin for format comment blocks and convert to standard Match with Visual studio and other IDE which support jsdoc and comments as markdown.

99 lines (98 loc) 4.37 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.stringify = void 0; const descriptionFormatter_1 = require("./descriptionFormatter"); const tags_1 = require("./tags"); const roles_1 = require("./roles"); const utils_1 = require("./utils"); const stringify = ({ name, description, type, tag }, tagIndex, finalTagsArray, options, maxTagTitleLength, maxTagTypeNameLength, maxTagNameLength) => { let tagString = "\n"; if (tag === tags_1.SPACE_TAG_DATA.tag) { return tagString; } const { printWidth, jsdocSpaces, jsdocVerticalAlignment, jsdocDescriptionTag, tsdoc, useTabs, tabWidth, } = options; const gap = " ".repeat(jsdocSpaces); let tagTitleGapAdj = 0; let tagTypeGapAdj = 0; let tagNameGapAdj = 0; let descGapAdj = 0; if (jsdocVerticalAlignment && roles_1.TAGS_VERTICALLY_ALIGN_ABLE.includes(tag)) { if (tag) tagTitleGapAdj += maxTagTitleLength - tag.length; else if (maxTagTitleLength) descGapAdj += maxTagTitleLength + gap.length; if (type) tagTypeGapAdj += maxTagTypeNameLength - type.length; else if (maxTagTypeNameLength) descGapAdj += maxTagTypeNameLength + gap.length; if (name) tagNameGapAdj += maxTagNameLength - name.length; else if (maxTagNameLength) descGapAdj = maxTagNameLength + gap.length; } const useTagTitle = tag !== tags_1.DESCRIPTION || jsdocDescriptionTag; if (useTagTitle) { tagString += `@${tag}${" ".repeat(tagTitleGapAdj || 0)}`; } if (type) { tagString += gap + `{${type}}` + " ".repeat(tagTypeGapAdj); } if (name) tagString += `${gap}${name}${" ".repeat(tagNameGapAdj)}`; // Try to use prettier on @example tag description if (tag === tags_1.EXAMPLE && !tsdoc) { const exampleCaption = description.match(/<caption>([\s\S]*?)<\/caption>/i); if (exampleCaption) { description = description.replace(exampleCaption[0], ""); tagString = `${tagString} ${exampleCaption[0]}`; } const beginningSpace = useTabs ? "\t" : " ".repeat(tabWidth); const formattedExample = utils_1.formatCode(description, beginningSpace, options); tagString += formattedExample .replace(new RegExp(`^\\n${beginningSpace .replace(/[\t]/g, "[\\t]") .replace(/[^S\r\n]/g, "[^S\\r\\n]")}\\n`), "") .trimEnd(); } // Add description (complicated because of text wrap) else if (description) { if (useTagTitle) tagString += gap + " ".repeat(descGapAdj); if (roles_1.TAGS_PEV_FORMATE_DESCRIPTION.includes(tag) || !roles_1.TAGS_ORDER.includes(tag)) { // Avoid wrapping tagString += description; } else { const [, firstWord] = /^\s*(\S+)/.exec(description) || ["", ""]; // Wrap tag description const beginningSpace = tag === tags_1.DESCRIPTION || ([tags_1.EXAMPLE, tags_1.REMARKS, tags_1.PRIVATE_REMARKS].includes(tag) && tsdoc) ? "" : " "; // google style guide space if ((tag !== tags_1.DESCRIPTION && tagString.length + firstWord.length > printWidth) || // tsdoc tags [tags_1.REMARKS, tags_1.PRIVATE_REMARKS].includes(tag)) { // the tag is already longer than we are allowed to, so let's start at a new line tagString += `\n${beginningSpace}` + descriptionFormatter_1.formatDescription(tag, description, options, { beginningSpace }); } else { // append the description to the tag tagString += descriptionFormatter_1.formatDescription(tag, description, options, { // 1 is `\n` which added to tagString tagStringLength: tagString.length - 1, beginningSpace, }); } } } // Add empty line after some tags if there is something below tagString += descriptionFormatter_1.descriptionEndLine({ tag, isEndTag: tagIndex === finalTagsArray.length - 1, }); return tagString; }; exports.stringify = stringify;