1234567891011121314151617181920212223242526272829303132333435363738394041424344 |
- import DocumentationBuilder from './Documentation.js';
- import postProcessDocumentation from './utils/postProcessDocumentation.js';
- import babelParse from './babelParser.js';
- import FileState from './FileState.js';
- import { ERROR_CODES, ReactDocgenError } from './error.js';
- import runResolver from './resolver/utils/runResolver.js';
- function executeHandlers(handlers, componentDefinitions) {
- return componentDefinitions.map((componentDefinition) => {
- const documentation = new DocumentationBuilder();
- handlers.forEach((handler) => handler(documentation, componentDefinition));
- return postProcessDocumentation(documentation.build());
- });
- }
- export default function parse(code, config) {
- const { babelOptions, handlers, importer, resolver } = config;
- const ast = babelParse(code, babelOptions);
- const fileState = new FileState(babelOptions, {
- ast,
- code,
- importer,
- });
- const componentDefinitions = runResolver(resolver, fileState);
- if (componentDefinitions.length === 0) {
- throw new ReactDocgenError(ERROR_CODES.MISSING_DEFINITION);
- }
- return executeHandlers(handlers, componentDefinitions);
- }
|