UNPKG

react-tsdoc

Version:

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

55 lines (54 loc) 1.71 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getComponentFunction = exports.getComponentInitializer = exports.isReactComponent = void 0; var ts_morph_1 = require("ts-morph"); /** * Checks if a given declaration is a React component * * @param node - The node to check */ var isReactComponent = function (node) { if (node.getKind() === ts_morph_1.SyntaxKind.VariableDeclaration || node.getKind() === ts_morph_1.SyntaxKind.FunctionDeclaration) { // @ts-ignore var name_1 = node.getName(); if (!name_1) throw Error; return name_1[0] === name_1[0].toUpperCase(); } return false; }; exports.isReactComponent = isReactComponent; /** * Evaluates a node and returns the function declaration or variable statement * * @param node - The node to search */ var getComponentInitializer = function (node) { if (node.getKind() === ts_morph_1.SyntaxKind.FunctionDeclaration) { return node; } try { return node === null || node === void 0 ? void 0 : node.getFirstAncestorByKindOrThrow(ts_morph_1.SyntaxKind.VariableStatement); } catch (_a) { return undefined; } }; exports.getComponentInitializer = getComponentInitializer; /** * Gets the function for a given component * * @param node - The node to evaluate */ var getComponentFunction = function (node) { if (node.getKind() === ts_morph_1.SyntaxKind.FunctionDeclaration) { return node; } try { return node.getFirstDescendantByKindOrThrow(ts_morph_1.SyntaxKind.ArrowFunction); } catch (_a) { return undefined; } }; exports.getComponentFunction = getComponentFunction;