/
getFlowTypeFromReactComponent.js
95 lines (84 loc) · 2.91 KB
/
getFlowTypeFromReactComponent.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
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
*/
import getTypeAnnotation from '../utils/getTypeAnnotation';
import getMemberValuePath from '../utils/getMemberValuePath';
import isReactComponentClass from '../utils/isReactComponentClass';
import isStatelessComponent from '../utils/isStatelessComponent';
import resolveGenericTypeAnnotation from '../utils/resolveGenericTypeAnnotation';
/**
* Given an React component (stateless or class) tries to find the
* flow type for the props. If not found or not one of the supported
* component types returns null.
*/
export default (path: NodePath): ?NodePath => {
let typePath: ?NodePath;
if (isReactComponentClass(path)) {
const superTypes = path.get('superTypeParameters');
if (superTypes.value) {
const params = superTypes.get('params');
if (params.value.length === 3) {
typePath = params.get(1);
} else {
typePath = params.get(0);
}
} else {
const propsMemberPath = getMemberValuePath(path, 'props');
if (!propsMemberPath) {
return null;
}
typePath = getTypeAnnotation(propsMemberPath.parentPath);
}
} else if (isStatelessComponent(path)) {
const param = path.get('params').get(0);
typePath = getTypeAnnotation(param);
}
return typePath;
};
export function applyToFlowTypeProperties(
path: NodePath,
callback: (propertyPath: NodePath) => void,
) {
if (path.node.properties) {
path.get('properties').each(propertyPath => callback(propertyPath));
} else if (path.node.members) {
path.get('members').each(propertyPath => callback(propertyPath));
} else if (path.node.type === 'InterfaceDeclaration') {
if (path.node.extends) {
applyExtends(path, callback);
}
path.get('body', 'properties').each(propertyPath => callback(propertyPath));
} else if (path.node.type === 'TSInterfaceDeclaration') {
if (path.node.extends) {
applyExtends(path, callback);
}
path.get('body', 'body').each(propertyPath => callback(propertyPath));
} else if (
path.node.type === 'IntersectionTypeAnnotation' ||
path.node.type === 'TSIntersectionType'
) {
path
.get('types')
.each(typesPath => applyToFlowTypeProperties(typesPath, callback));
} else if (path.node.type !== 'UnionTypeAnnotation') {
// The react-docgen output format does not currently allow
// for the expression of union types
const typePath = resolveGenericTypeAnnotation(path);
if (typePath) {
applyToFlowTypeProperties(typePath, callback);
}
}
}
function applyExtends(path, callback) {
path.get('extends').each((extendsPath: NodePath) => {
const resolvedPath = resolveGenericTypeAnnotation(extendsPath);
if (resolvedPath) {
applyToFlowTypeProperties(resolvedPath, callback);
}
});
}