This repository has been archived by the owner on Sep 22, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
156 lines (145 loc) · 4.61 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import * as reactDocs from 'react-docgen';
import * as doctrine from 'doctrine';
import * as camelCase from 'uppercamelcase';
import * as decamelize from 'decamelize';
import {
ComponentInfo,
CustomTypesDescription,
EnumDescription,
PropDescription,
TypeDescription
} from './types';
function getDocForFileContent(src: string, componentName: string, resolver, handlers): ComponentInfo {
const componentInfo: ComponentInfo = reactDocs.parse(src, resolver, handlers);
const description = doctrine.parse(componentInfo.description);
const preparedProps = prepareProps(componentInfo.props);
let ext;
if (description.tags && description.tags.length > 0) {
let tag = description.tags.find(tag => tag.title === 'extends');
if (tag) {
ext = {
name: tag.name,
source: decamelize(tag.name, '-')
};
}
}
return {
name: camelCase(componentName),
description: description.description,
source: componentName,
examples: description.tags.filter(tag => tag.title === 'example').map(tag => tag.description),
methods: prepareMethods(componentInfo.methods),
props: preparedProps.props,
enums: preparedProps.enums,
types: preparedProps.customTypes.reverse(),
extends: ext
};
}
/**
* Converts react-docgen output format to documentation-ready format
*/
function prepareProps(props = {}, customTypes: CustomTypesDescription[] = [], enums: EnumDescription[] = []) {
const result: PropDescription[] = Object.keys(props)
.map(name => {
const prop = props[name];
return {
name: name,
type: getType(prop.type, name, customTypes, enums),
default: prop.defaultValue ? prop.defaultValue.value : undefined,
required: prop.required,
description: prop.description || ''
};
});
return {
customTypes: customTypes,
enums: enums,
props: result
};
}
/**
* Convert methods description to documentation format
*/
function prepareMethods(methods: any[]) {
methods = methods || [];
return methods
.filter(m => !!m.docblock)
.map(m => ({
...m,
docblock: doctrine.parse(m.docblock)
}))
.filter(m => m.docblock.tags && m.docblock.tags.some(tag => tag.title === 'public'));
}
const TYPE_MAP = {
array: 'Array',
bool: 'Boolean',
func: 'Function',
number: 'Number',
object: 'Object',
string: 'String',
node: 'Node',
element: 'Element'
};
/**
* Create type description form react-docgen format.
*/
function getType(type, propName: string, customTypes: CustomTypesDescription[], enums: EnumDescription[]): TypeDescription {
if (!type) {
return 'Unknown';
}
switch (type.name) {
case 'array':
case 'bool':
case 'func':
case 'number':
case 'object':
case 'string':
case 'node':
case 'element':
return TYPE_MAP[type.name];
case 'instanceOf':
return type.value;
case 'oneOfType':
case 'union':
return {
typeName: 'union',
types: type.value.map(getType)
};
case 'arrayOf':
return {
typeName: 'array',
innerType: getType(type.value, propName, customTypes, enums)
};
case 'shape':
propName = camelCase(propName.toString()) + 'Type';
if (typeof type.value === 'string') {
customTypes.push({
name: propName,
props: []
});
} else {
Object.keys(type.value).forEach(key => {
type.value[key].type = {
name: type.value[key].name,
value: type.value[key].value
};
});
customTypes.push({
name: propName,
props: prepareProps(type.value, customTypes, enums).props
});
}
return { typeName: 'shape', name: propName };
case 'enum':
propName = camelCase(propName) + 'Enum';
enums.push({
name: propName,
values: type.value.map(v => v.value)
});
return { typeName: 'enum', name: propName };
case 'custom':
return type.raw;
default:
return type.name || type.value;
}
}
export = getDocForFileContent;