UNPKG

react-docgen

Version:

A library to extract information from React components for documentation generation.

51 lines (50 loc) 2.08 kB
import { getDocblock } from '../utils/docblock.js'; import isReactForwardRefCall from '../utils/isReactForwardRefCall.js'; import resolveToValue from '../utils/resolveToValue.js'; function getDocblockFromComponent(path) { let description = null; if (path.isClassDeclaration() || path.isClassExpression()) { const decorators = path.get('decorators'); // If we have a class declaration or expression, then the comment might be // attached to the last decorator instead as trailing comment. if (decorators && decorators.length > 0) { const lastDecorator = decorators[decorators.length - 1]; if (lastDecorator) { description = getDocblock(lastDecorator, true); } } } if (description == null) { // Find parent statement (e.g. var Component = React.createClass(<path>);) let searchPath = path; while (searchPath && !searchPath.isStatement()) { searchPath = searchPath.parentPath; } if (searchPath) { // If the parent is an export statement, we have to traverse one more up if (searchPath.parentPath.isExportNamedDeclaration() || searchPath.parentPath.isExportDefaultDeclaration()) { searchPath = searchPath.parentPath; } description = getDocblock(searchPath); } } if (!description) { const [forwardRefArgument] = isReactForwardRefCall(path) ? path.get('arguments') : []; const searchPath = forwardRefArgument ?? path; const inner = resolveToValue(searchPath); if (inner.node !== path.node) { return getDocblockFromComponent(inner); } } return description; } /** * Finds the nearest block comment before the component definition. */ const componentDocblockHandler = function (documentation, componentDefinition) { documentation.set('description', getDocblockFromComponent(componentDefinition) || ''); }; export default componentDocblockHandler;