-
-
Notifications
You must be signed in to change notification settings - Fork 31.9k
/
PropTypeDescription.js
168 lines (128 loc) · 4.59 KB
/
PropTypeDescription.js
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
157
158
159
160
161
162
163
164
165
166
167
168
import React, {Component, PropTypes} from 'react';
import {parse} from 'react-docgen';
import {parse as parseDoctrine} from 'doctrine';
import MarkdownElement from './MarkdownElement';
import recast from 'recast';
require('./prop-type-description.css');
function getDeprecatedInfo(type) {
const deprecatedPropType = 'deprecated(PropTypes.';
const indexStart = type.raw.indexOf(deprecatedPropType);
if (indexStart !== -1) {
return {
propTypes: type.raw.substring(indexStart + deprecatedPropType.length, type.raw.indexOf(',')),
explanation: recast.parse(type.raw).program.body[0].expression.arguments[1].value,
};
}
return false;
}
function generatePropType(type) {
switch (type.name) {
case 'func':
return 'function';
case 'custom':
const deprecatedInfo = getDeprecatedInfo(type);
if (deprecatedInfo !== false) {
return generatePropType({
name: deprecatedInfo.propTypes,
});
}
return type.raw;
case 'enum':
const values = type.value.map((v) => v.value).join('<br> ');
return `enum:<br> ${values}<br>`;
default:
return type.name;
}
}
function generateDescription(required, description, type) {
let deprecated = '';
if (type.name === 'custom') {
const deprecatedInfo = getDeprecatedInfo(type);
if (deprecatedInfo) {
deprecated = `*Deprecated*. ${deprecatedInfo.explanation}<br><br>`;
}
}
const parsed = parseDoctrine(description);
// two new lines result in a newline in the table. all other new lines
// must be eliminated to prevent markdown mayhem.
const jsDocText = parsed.description.replace(/\n\n/g, '<br>').replace(/\n/g, ' ');
if (parsed.tags.some((tag) => tag.title === 'ignore')) return null;
let signature = '';
if (type.name === 'func' && parsed.tags.length > 0) {
// Remove new lines from tag descriptions to avoid markdown errors.
parsed.tags.forEach((tag) => {
if (tag.description) {
tag.description = tag.description.replace(/\n/g, ' ');
}
});
// Split up the parsed tags into 'arguments' and 'returns' parsed objects. If there's no
// 'returns' parsed object (i.e., one with title being 'returns'), make one of type 'void'.
const parsedLength = parsed.tags.length;
let parsedArgs = [];
let parsedReturns;
if (parsed.tags[parsedLength - 1].title === 'returns') {
parsedArgs = parsed.tags.slice(0, parsedLength - 1);
parsedReturns = parsed.tags[parsedLength - 1];
} else {
parsedArgs = parsed.tags;
parsedReturns = {type: {name: 'void'}};
}
signature += '<br><br>**Signature:**<br>`function(';
signature += parsedArgs.map((tag) => `${tag.name}: ${tag.type.name}`).join(', ');
signature += `) => ${parsedReturns.type.name}` + '`<br>';
signature += parsedArgs.map((tag) => `*${tag.name}:* ${tag.description}`).join('<br>');
if (parsedReturns.description) {
signature += `<br> *returns* (${parsedReturns.type.name}): ${parsedReturns.description}`;
}
}
return `${deprecated} ${jsDocText}${signature}`;
}
class PropTypeDescription extends Component {
static propTypes = {
code: PropTypes.string,
header: PropTypes.string.isRequired,
};
static defaultProps = {
header: '### Properties',
};
render() {
const {
code,
header,
} = this.props;
let requiredProps = 0;
let text = `${header}
| Name | Type | Default | Description |
|:-----|:-----|:-----|:-----|\n`;
const componentInfo = parse(code);
for (let key in componentInfo.props) {
const prop = componentInfo.props[key];
const description = generateDescription(prop.required, prop.description, prop.type);
if (description === null) continue;
let defaultValue = '';
if (prop.defaultValue) {
defaultValue = prop.defaultValue.value.replace(/\n/g, '');
}
if (prop.required) {
key = `<span style="color: #31a148">${key} \*</span>`;
requiredProps += 1;
}
if (prop.type.name === 'custom') {
if (getDeprecatedInfo(prop.type)) {
key = `~~${key}~~`;
}
}
text += `| ${key} | ${generatePropType(prop.type)} | ${defaultValue} | ${description} |\n`;
}
const requiredPropFootnote = (requiredProps === 1) ? '* required property' :
(requiredProps > 1) ? '* required properties' :
'';
return (
<div className="propTypeDescription">
<MarkdownElement text={text} />
<div style={{fontSize: '90%', paddingLeft: '15px'}}>{requiredPropFootnote}</div>
</div>
);
}
}
export default PropTypeDescription;