UNPKG

storybook-addon-jsdoc-to-mdx

Version:

Storybook addon that automatically generates MDX documentation from JSDoc comments in your TypeScript and JavaScript files. Supports HTML tags in comments, complex TypeScript types, and integrates seamlessly with Storybook 7.x and 8.x.

52 lines (51 loc) 2.43 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getFunctionName = getFunctionName; const ts_morph_1 = require("ts-morph"); function getFunctionName(node) { // Try to get a name for each type of node const name = ts_morph_1.Node.isMethodDeclaration(node) && node.getName(); if (name) { return name; } // Checking for the default function export case if (ts_morph_1.Node.isExportAssignment(node)) { const expression = node.getExpression(); if (ts_morph_1.Node.isFunctionExpression(expression) || ts_morph_1.Node.isArrowFunction(expression)) { // For anonymous functions exported by default, return 'default' return (ts_morph_1.Node.isFunctionExpression(expression) && expression.getName()) || "default"; } } else if (ts_morph_1.Node.isFunctionDeclaration(node) || ts_morph_1.Node.isArrowFunction(node)) { // Check for functions and arrow functions return (ts_morph_1.Node.isFunctionDeclaration(node) && node.getName()) || "default"; } switch (node.getKind()) { case ts_morph_1.SyntaxKind.ClassDeclaration: case ts_morph_1.SyntaxKind.InterfaceDeclaration: case ts_morph_1.SyntaxKind.EnumDeclaration: case ts_morph_1.SyntaxKind.ModuleDeclaration: case ts_morph_1.SyntaxKind.TypeAliasDeclaration: // Return the name for classes, interfaces, enums, modules, and type aliases const namedNode = node; return namedNode.getName() || "Unnamed"; case ts_morph_1.SyntaxKind.VariableStatement: // For variable declarations, try to find the name of the first declaration const declarations = ts_morph_1.Node.isVariableStatement(node) && node.getDeclarations(); if (declarations && declarations.length > 0 && declarations[0].getName()) { return declarations[0].getName(); } break; case ts_morph_1.SyntaxKind.FunctionType: // For function types, return the return type or "FunctionType" const signature = node.getType().getCallSignatures()[0]; if (signature) { return signature.getReturnType().getText() || "FunctionType"; } break; default: break; } // Return "Unnamed" if no name has been determined return "Unnamed"; }