1 | import DocumentationBuilder from './Documentation.js';
|
2 | import postProcessDocumentation from './utils/postProcessDocumentation.js';
|
3 | import babelParse from './babelParser.js';
|
4 | import FileState from './FileState.js';
|
5 | import { ERROR_CODES, ReactDocgenError } from './error.js';
|
6 | import runResolver from './resolver/utils/runResolver.js';
|
7 | function executeHandlers(handlers, componentDefinitions) {
|
8 | return componentDefinitions.map((componentDefinition) => {
|
9 | const documentation = new DocumentationBuilder();
|
10 | handlers.forEach((handler) => handler(documentation, componentDefinition));
|
11 | return postProcessDocumentation(documentation.build());
|
12 | });
|
13 | }
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 | export default function parse(code, config) {
|
32 | const { babelOptions, handlers, importer, resolver } = config;
|
33 | const ast = babelParse(code, babelOptions);
|
34 | const fileState = new FileState(babelOptions, {
|
35 | ast,
|
36 | code,
|
37 | importer,
|
38 | });
|
39 | const componentDefinitions = runResolver(resolver, fileState);
|
40 | if (componentDefinitions.length === 0) {
|
41 | throw new ReactDocgenError(ERROR_CODES.MISSING_DEFINITION);
|
42 | }
|
43 | return executeHandlers(handlers, componentDefinitions);
|
44 | }
|