UNPKG

react-tsdoc

Version:

CLI to extract information from React Typescript component files with TSDoc for documentation generation purposes

126 lines (125 loc) 4.5 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.getPropBlocks = exports.getDeclarationDescription = exports.renderCommentSummary = exports.renderPropBlock = exports.parseTSDoc = exports.findCommentRanges = void 0; var tsdoc = __importStar(require("@microsoft/tsdoc")); var reactComponentHelper_1 = require("./reactComponentHelper"); /** * Find comment ranges for the React AST node * * @param node - The current AST node */ var findCommentRanges = function (node) { var commentRanges = node.getLeadingCommentRanges(); if (commentRanges.length) { var commentStrings = commentRanges.map(function (range) { return tsdoc.TextRange.fromStringRange(node.getSourceFile().getFullText(), range.getPos(), range.getEnd()); }); return commentStrings; } return []; }; exports.findCommentRanges = findCommentRanges; /** * Parses the comment from the range * * @param comment - The object containing the comment range */ var parseTSDoc = function (comment) { var tsDocConfiguration = new tsdoc.TSDocConfiguration(); var propBlockDefinition = new tsdoc.TSDocTagDefinition({ tagName: '@prop', syntaxKind: tsdoc.TSDocTagSyntaxKind.BlockTag }); tsDocConfiguration.addTagDefinitions([ propBlockDefinition ]); var tsdocParser = new tsdoc.TSDocParser(tsDocConfiguration); var parserContext = tsdocParser.parseRange(comment); var docComment = parserContext.docComment; if (parserContext.log.messages.length > 0) return undefined; return docComment; }; exports.parseTSDoc = parseTSDoc; /** * Gets the TSDoc node's content (no API support yet) * * @param node - The current AST node */ var renderPropBlock = function (node) { if (node instanceof tsdoc.DocPlainText) { return { propName: node.text.split(' - ')[0].trim(), content: node.text.split(' - ')[1].trim() }; } for (var _i = 0, _a = node.getChildNodes(); _i < _a.length; _i++) { var childNode = _a[_i]; return (0, exports.renderPropBlock)(childNode); } return undefined; }; exports.renderPropBlock = renderPropBlock; /** * Returns the text of the summary for a given comment block * * @param comment - The comment to render */ var renderCommentSummary = function (comment) { return comment.summarySection .getChildNodes()[0] // Grabs the summary nodes // @ts-ignore .getChildNodes()[0].text; // Grabs text of the Paragraph node }; exports.renderCommentSummary = renderCommentSummary; /** * Get declaration description * * @param node - The node to grab the declaration description from */ var getDeclarationDescription = function (node) { var commentRanges = (0, exports.findCommentRanges)((0, reactComponentHelper_1.getComponentInitializer)(node)); if (commentRanges.length) { var parsed = (0, exports.parseTSDoc)(commentRanges[0]); if (!parsed) return ''; return (0, exports.renderCommentSummary)(parsed); } return ''; }; exports.getDeclarationDescription = getDeclarationDescription; /** * Gets comments for props * * @node - The node to parse comments out of */ var getPropBlocks = function (node) { var commentRanges = (0, exports.findCommentRanges)((0, reactComponentHelper_1.getComponentInitializer)(node)); if (commentRanges.length) { var comments = (0, exports.parseTSDoc)(commentRanges[0]); if (!comments) return []; return comments.customBlocks; } return []; }; exports.getPropBlocks = getPropBlocks;