This repository has been archived by the owner on Nov 14, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
component-resolve.js
149 lines (119 loc) · 3.73 KB
/
component-resolve.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
const recast = require('recast');
const { utils } = require('react-docgen');
const {
isExportsOrModuleAssignment,
isReactComponentClass,
isReactCreateClassCall,
isStatelessComponent,
normalizeClassDefinition,
resolveExportDeclaration,
resolveToValue,
} = utils;
const n = recast.types.namedTypes;
const ERROR_MULTIPLE_DEFINITIONS = 'Multiple exported component definitions found.';
const isReactComponentExtendedClass = path => {
const node = path.node;
if (!n.ClassDeclaration.check(node) && !n.ClassExpression.check(node)) {
return false;
}
if (!node.superClass) {
return false;
}
return true;
};
const isAnnotatedComponent = path => {
const leadingComments = path.node.leadingComments;
if (!leadingComments) {
return false;
}
// Search for the @autodocs-component in identifier comment
return leadingComments.some(comment => comment.value.includes('@autodocs-component'));
};
const isComponentDefinition = path =>
[
isReactCreateClassCall,
isReactComponentClass,
isReactComponentExtendedClass,
isStatelessComponent,
isAnnotatedComponent,
].some(fn => fn(path));
const resolveHOC = path => {
const node = path.node;
if (n.CallExpression.check(node) && !isReactCreateClassCall(path)) {
if (node.arguments.length) {
return resolveHOC(path.get('arguments', node.arguments.length - 1));
}
}
return path;
};
const resolveDefinition = definition => {
if (isReactCreateClassCall(definition)) {
// return argument
const resolvedPath = resolveToValue(definition.get('arguments', 0));
if (n.ObjectExpression.check(resolvedPath.node)) {
return resolvedPath;
}
} else if (isReactComponentClass(definition) || isReactComponentExtendedClass(definition)) {
normalizeClassDefinition(definition);
return definition;
} else if (isStatelessComponent(definition)) {
return definition;
} else if (isAnnotatedComponent) {
return definition;
}
return null;
};
const componentResolver = (ast, recast) => {
let definition;
const exportDeclaration = path => {
const definitions = resolveExportDeclaration(path).reduce((acc, definition) => {
if (isComponentDefinition(definition)) {
acc.push(definition);
} else {
const resolved = resolveToValue(resolveHOC(definition));
if (isComponentDefinition(resolved)) {
acc.push(resolved);
}
}
return acc;
}, []);
if (definitions.length === 0) {
return false;
}
if (definitions.length > 1 || definition) {
// If a file exports multiple components, ... complain!
throw new Error(ERROR_MULTIPLE_DEFINITIONS);
}
definition = resolveDefinition(definitions[0]);
return false;
};
recast.visit(ast, {
visitExportDeclaration: exportDeclaration,
visitExportNamedDeclaration: exportDeclaration,
visitExportDefaultDeclaration: exportDeclaration,
visitAssignmentExpression: path => {
// Ignore anything that is not `exports.X = ...;` or
// `module.exports = ...;`
if (!isExportsOrModuleAssignment(path)) {
return false;
}
// Resolve the value of the right hand side. It should resolve to a call
// expression, something like React.createClass
path = resolveToValue(path.get('right'));
if (!isComponentDefinition(path)) {
path = resolveToValue(resolveHOC(path));
if (!isComponentDefinition(path)) {
return false;
}
}
if (definition) {
// If a file exports multiple components, ... complain!
throw new Error(ERROR_MULTIPLE_DEFINITIONS);
}
definition = resolveDefinition(path);
return false;
},
});
return definition;
};
module.exports = componentResolver;