-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathindex.js
59 lines (49 loc) · 1.56 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
const { utils: docgenUtils } = require("react-docgen");
const { visit } = require("ast-types/main");
const { default: resolveHOC } = require("react-docgen/dist/utils/resolveHOC");
module.exports = function(ast) {
const components = [];
const exportTagged = path => {
/**
* If an export doesn't have leading comments, we can simply continue
*/
const leadingComments = path.value.leadingComments;
if (!leadingComments) {
return false;
}
// Search for the @component tag in any export.
const isTagged = leadingComments.reduce(
(acc, comment) =>
acc === true || comment.value.indexOf("@component") !== -1,
[],
);
if (!isTagged) {
return false;
}
const definitions = docgenUtils.resolveExportDeclaration(path);
definitions.forEach(definition => {
if (definition && components.indexOf(definition) === -1) {
components.push(docgenUtils.resolveToValue(resolveHOC(definition)));
}
});
return false;
};
visit(ast, {
visitFunctionDeclaration: false,
visitFunctionExpression: false,
visitClassDeclaration: false,
visitClassExpression: false,
visitIfStatement: false,
visitWithStatement: false,
visitSwitchStatement: false,
visitCatchCause: false,
visitWhileStatement: false,
visitDoWhileStatement: false,
visitForStatement: false,
visitForInStatement: false,
visitExportDeclaration: exportTagged,
visitExportNamedDeclaration: exportTagged,
visitExportDefaultDeclaration: exportTagged,
});
return components;
};