A library to extract information from React components for documentation generation.
—
Quality
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Handlers that extract specific types of information from React components during the parsing process.
The base interface for all information extraction handlers.
type Handler = (
documentation: Documentation,
componentDefinition: NodePath<ComponentNode>
) => void;Handlers receive a Documentation object (to populate) and a ComponentNodePath (to analyze), and modify the documentation object in place.
Extracts JSDoc comments and descriptions from the component definition.
const componentDocblockHandler: Handler;Extracted Information:
Usage Example:
// Input component:
/**
* A reusable button component for user interactions
* @deprecated Use Button2 instead
*/
export default function Button(props) {
return <button {...props} />;
}
// Extracted documentation:
{
description: "A reusable button component for user interactions",
// Additional JSDoc metadata
}Extracts the display name of the component from various sources.
const displayNameHandler: Handler;Sources for Display Name:
displayName propertyUsage Example:
// Various display name sources:
function MyButton() {} // displayName: "MyButton"
const Button = () => {}; // displayName: "Button"
export { Button as PrimaryButton }; // displayName: "PrimaryButton"
Button.displayName = "CustomButton"; // displayName: "CustomButton"Extracts PropTypes definitions and converts them to documentation format.
const propTypeHandler: Handler;Supported PropTypes:
.isRequiredUsage Example:
import PropTypes from 'prop-types';
function Button({ label, onClick, size }) {
return <button onClick={onClick}>{label}</button>;
}
Button.propTypes = {
label: PropTypes.string.isRequired,
onClick: PropTypes.func,
size: PropTypes.oneOf(['small', 'medium', 'large'])
};
// Extracted documentation includes detailed prop informationExtracts default prop values from various definition patterns.
const defaultPropsHandler: Handler;Supported Patterns:
Component.defaultProps assignmentsUsage Example:
// defaultProps assignment
Button.defaultProps = {
size: 'medium',
disabled: false
};
// ES6 default parameters
function Button({ label, size = 'medium', disabled = false }) {}
// Both patterns are extracted to defaultValue in PropDescriptorExtracts TypeScript and Flow type information from component definitions.
const codeTypeHandler: Handler;Supported Type Systems:
Usage Example:
interface ButtonProps {
/** Button label text */
label: string;
/** Click handler function */
onClick?: () => void;
/** Button size variant */
size?: 'small' | 'medium' | 'large';
}
function Button({ label, onClick, size }: ButtonProps) {
return <button onClick={onClick}>{label}</button>;
}
// Extracts complete TypeScript type informationExtracts class component methods and their signatures.
const componentMethodsHandler: Handler;Extracted Information:
Usage Example:
class MyComponent extends React.Component {
/**
* Handles user click events
*/
handleClick = (event) => {
// Method implementation
};
/**
* Validates component props
*/
static validateProps(props) {
return props.isValid;
}
render() {
return <div onClick={this.handleClick} />;
}
}
// Extracts method information including JSDocExtracts JSDoc documentation specifically from component methods.
const componentMethodsJsDocHandler: Handler;Extracted Information:
Extracts JSDoc comments associated with individual props.
const propDocblockHandler: Handler;Usage Example:
Button.propTypes = {
/** The text to display on the button */
label: PropTypes.string.isRequired,
/** Function called when button is clicked */
onClick: PropTypes.func,
/** Visual size of the button */
size: PropTypes.oneOf(['small', 'medium', 'large'])
};
// Extracts individual prop descriptionsHandles composed PropTypes and HOC prop inheritance patterns.
const propTypeCompositionHandler: Handler;Handles:
Object.assignExtract React context information from components.
const contextTypeHandler: Handler;
const childContextTypeHandler: Handler;Extracted Information:
Usage Example:
class MyComponent extends React.Component {
static contextTypes = {
theme: PropTypes.object.isRequired
};
static childContextTypes = {
locale: PropTypes.string
};
getChildContext() {
return { locale: 'en-US' };
}
}
// Extracts context type informationThe default set of handlers used by the parser.
const defaultHandlers: Handler[];The default handlers include (in order):
propTypeHandler - Extract PropTypescontextTypeHandler - Extract context typeschildContextTypeHandler - Extract child context typespropTypeCompositionHandler - Handle prop compositionpropDocblockHandler - Extract prop JSDoccodeTypeHandler - Extract TypeScript/Flow typesdefaultPropsHandler - Extract default valuescomponentDocblockHandler - Extract component JSDocdisplayNameHandler - Extract display namecomponentMethodsHandler - Extract methodscomponentMethodsJsDocHandler - Extract method JSDocHandlers are functions that modify the Documentation object based on AST analysis.
Handler Implementation Example:
import type { Handler } from "react-docgen";
const customHandler: Handler = (documentation, componentDefinition) => {
// Analyze the componentDefinition AST node
// Extract specific information
// Modify the documentation object
documentation.set('customField', extractedValue);
};
// Usage with custom handler
const docs = parse(sourceCode, {
handlers: [...defaultHandlers, customHandler]
});Install with Tessl CLI
npx tessl i tessl/npm-react-docgen