UNPKG

2.56 kBJavaScriptView Raw
1import { parseJsDoc } from '../jsdocParser';
2import { TypeSystem } from './types';
3import { getDocgenSection, isValidDocgenSection, getDocgenDescription } from './utils';
4import { getPropDefFactory } from './createPropDef';
5
6const getTypeSystem = docgenInfo => {
7 if (docgenInfo.type != null) {
8 return TypeSystem.JAVASCRIPT;
9 }
10
11 if (docgenInfo.flowType != null) {
12 return TypeSystem.FLOW;
13 }
14
15 if (docgenInfo.tsType != null) {
16 return TypeSystem.TYPESCRIPT;
17 }
18
19 return TypeSystem.UNKNOWN;
20};
21
22export const extractComponentSectionArray = docgenSection => {
23 const typeSystem = getTypeSystem(docgenSection[0]);
24 const createPropDef = getPropDefFactory(typeSystem);
25 return docgenSection.map(item => {
26 var _item$type;
27
28 let sanitizedItem = item;
29
30 if ((_item$type = item.type) !== null && _item$type !== void 0 && _item$type.elements) {
31 sanitizedItem = Object.assign({}, item, {
32 type: Object.assign({}, item.type, {
33 value: item.type.elements
34 })
35 });
36 }
37
38 return extractProp(sanitizedItem.name, sanitizedItem, typeSystem, createPropDef);
39 });
40};
41export const extractComponentSectionObject = docgenSection => {
42 const docgenPropsKeys = Object.keys(docgenSection);
43 const typeSystem = getTypeSystem(docgenSection[docgenPropsKeys[0]]);
44 const createPropDef = getPropDefFactory(typeSystem);
45 return docgenPropsKeys.map(propName => {
46 const docgenInfo = docgenSection[propName];
47 return docgenInfo != null ? extractProp(propName, docgenInfo, typeSystem, createPropDef) : null;
48 }).filter(Boolean);
49};
50export const extractComponentProps = (component, section) => {
51 const docgenSection = getDocgenSection(component, section);
52
53 if (!isValidDocgenSection(docgenSection)) {
54 return [];
55 } // vue-docgen-api has diverged from react-docgen and returns an array
56
57
58 return Array.isArray(docgenSection) ? extractComponentSectionArray(docgenSection) : extractComponentSectionObject(docgenSection);
59};
60
61function extractProp(propName, docgenInfo, typeSystem, createPropDef) {
62 const jsDocParsingResult = parseJsDoc(docgenInfo.description);
63 const isIgnored = jsDocParsingResult.includesJsDoc && jsDocParsingResult.ignore;
64
65 if (!isIgnored) {
66 const propDef = createPropDef(propName, docgenInfo, jsDocParsingResult);
67 return {
68 propDef,
69 jsDocTags: jsDocParsingResult.extractedTags,
70 docgenInfo,
71 typeSystem
72 };
73 }
74
75 return null;
76}
77
78export function extractComponentDescription(component) {
79 return component != null && getDocgenDescription(component);
80}
\No newline at end of file