-
Notifications
You must be signed in to change notification settings - Fork 67
/
index.js
243 lines (214 loc) 路 6.9 KB
/
index.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
import * as _ from 'lodash';
import * as reactDocs from 'react-docgen';
import isReactComponentClass from './isReactComponentClass';
import isStatelessComponent from './isStatelessComponent';
import * as p from 'path';
export default function ({types: t}) {
return {
visitor: {
Class(path, state) {
if(!isReactComponentClass(path)) {
return;
}
if(!path.node.id){
return;
}
const className = path.node.id.name;
if(!isExported(path, className, t)){
return;
}
injectReactDocgenInfo(className, path, state, this.file.code, t);
},
'CallExpression'(path, state) {
const callee = path.node.callee;
const objectName = _.get(callee, 'object.name') ? callee.object.name.toLowerCase() : null;
const propertyName = _.get(callee, 'property.name') ? callee.property.name.toLowerCase() : null;
const calleeName = _.get(callee, 'name') ? callee.name.toLowerCase() : null;
// Detect `React.createClass()`
const hasReactCreateClass = (objectName === 'react' && propertyName === 'createclass');
// Detect `createReactClass()`
const hasCreateReactClass = (calleeName === 'createreactclass');
// Get React class name from variable declaration
const className = _.get(path, 'parentPath.parent.declarations[0].id.name');
// Detect `React.createElement()`
const hasReactCreateElement = (objectName === 'react' && propertyName === 'createelement');
if (className && (hasReactCreateClass || hasCreateReactClass)) {
injectReactDocgenInfo(className, path, state, this.file.code, t);
}
if (hasReactCreateElement) {
const variableDeclaration = path.findParent((path) => path.isVariableDeclaration());
if (variableDeclaration) {
const elementClassName = variableDeclaration.node.declarations[0].id.name;
if (!isExported(path, elementClassName, t)) {
return;
}
injectReactDocgenInfo(elementClassName, path, state, this.file.code, t);
}
}
},
'FunctionDeclaration|FunctionExpression|ArrowFunctionExpression'(path, state) {
if(!isStatelessComponent(path)) {
return;
}
if(!path.parentPath.node.id) {
return;
}
const className = path.parentPath.node.id.name;
if(!isExported(path, className, t)) {
return;
}
injectReactDocgenInfo(className, path, state, this.file.code, t);
},
}
};
}
function isExported(path, className, t){
const types = [
'ExportDefaultDeclaration',
'ExportNamedDeclaration'
];
function findMostRightHandArgument(args = []) {
const arg = args[0]
if (t.isIdentifier(arg)) {
return arg.name
} else if(t.isCallExpression(arg)) {
return findMostRightHandArgument(arg.arguments)
}
}
if(path.parentPath.node &&
types.some(type => {return path.parentPath.node.type === type;})) {
return true;
}
const program = path.scope.getProgramParent().path;
return program.get('body').some(path => {
if(path.node.type === 'ExportNamedDeclaration' &&
path.node.specifiers &&
path.node.specifiers.length) {
return className === path.node.specifiers[0].exported.name;
} else if(path.node.type === 'ExportDefaultDeclaration') {
const decl = path.node.declaration
if (t.isCallExpression(decl)) {
return className === findMostRightHandArgument(decl.arguments);
} else {
return className === decl.name;
}
}
return false;
});
}
function alreadyVisited(program, t) {
return program.node.body.some(node => {
if(t.isExpressionStatement(node) &&
t.isAssignmentExpression(node.expression) &&
t.isMemberExpression(node.expression.left)
) {
return node.expression.left.property.name === '__docgenInfo';
}
return false;
});
}
function injectReactDocgenInfo(className, path, state, code, t) {
const program = path.scope.getProgramParent().path;
if(alreadyVisited(program, t)) {
return;
}
let docObj = {};
try {
let resolver;
if (state.opts.resolver) {
resolver = require('react-docgen').resolver[state.opts.resolver];
}
docObj = reactDocs.parse(code, resolver);
if (!state.opts.includeMethods) {
delete docObj.methods;
}
} catch(e) {
console.error(e);
return;
}
const docNode = buildObjectExpression(docObj, t);
const docgenInfo = t.expressionStatement(
t.assignmentExpression(
"=",
t.memberExpression(t.identifier(className), t.identifier('__docgenInfo')),
docNode
));
program.pushContainer('body', docgenInfo);
injectDocgenGlobal(className, path, state, t);
}
function injectDocgenGlobal(className, path, state, t) {
const program = path.scope.getProgramParent().path;
if(!state.opts.DOC_GEN_COLLECTION_NAME) {
return;
}
const globalName = state.opts.DOC_GEN_COLLECTION_NAME;
const filePath = p.relative('./', p.resolve('./', path.hub.file.opts.filename));
const globalNode = t.ifStatement(
t.binaryExpression(
'!==',
t.unaryExpression(
'typeof',
t.identifier(globalName)
),
t.stringLiteral('undefined')
),
t.blockStatement([
t.expressionStatement(
t.assignmentExpression(
'=',
t.memberExpression(
t.identifier(globalName),
t.stringLiteral(filePath),
true
),
t.objectExpression([
t.objectProperty(
t.identifier('name'),
t.stringLiteral(className)
),
t.objectProperty(
t.identifier('docgenInfo'),
t.memberExpression(
t.identifier(className),
t.identifier('__docgenInfo')
)
),
t.objectProperty(
t.identifier('path'),
t.stringLiteral(filePath)
)
])
)
)
])
);
program.pushContainer('body', globalNode);
}
function buildObjectExpression(obj, t){
if(_.isPlainObject(obj)) {
const children = [];
for (let key in obj) {
if(!obj.hasOwnProperty(key) || _.isUndefined(obj[key])) continue;
children.push(
t.objectProperty(
t.stringLiteral(key),
buildObjectExpression(obj[key], t)
));
}
return t.objectExpression(children);
} else if (_.isString(obj)) {
return t.stringLiteral(obj);
} else if (_.isBoolean(obj)) {
return t.booleanLiteral(obj);
} else if (_.isNumber(obj)){
return t.numericLiteral(obj);
} else if (_.isArray(obj)) {
const children = [];
obj.forEach(function (val) {
children.push(buildObjectExpression(val, t));
});
return t.ArrayExpression(children);
} else if(_.isNull(obj)) {
return t.nullLiteral();
}
}