-
-
Notifications
You must be signed in to change notification settings - Fork 290
/
defaultPropsHandler.js
144 lines (133 loc) · 4.16 KB
/
defaultPropsHandler.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
/**
* 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 { namedTypes as t } from 'ast-types';
import getPropertyName from '../utils/getPropertyName';
import getMemberValuePath from '../utils/getMemberValuePath';
import printValue from '../utils/printValue';
import resolveToValue from '../utils/resolveToValue';
import resolveFunctionDefinitionToReturnValue from '../utils/resolveFunctionDefinitionToReturnValue';
import isReactComponentClass from '../utils/isReactComponentClass';
import isReactForwardRefCall from '../utils/isReactForwardRefCall';
import type Documentation from '../Documentation';
function getDefaultValue(path: NodePath) {
let node = path.node;
let defaultValue;
if (t.Literal.check(node)) {
defaultValue = node.raw;
} else {
if (t.AssignmentPattern.check(path.node)) {
path = resolveToValue(path.get('right'));
} else {
path = resolveToValue(path);
}
if (t.ImportDeclaration.check(path.node)) {
defaultValue = node.name;
} else {
node = path.node;
defaultValue = printValue(path);
}
}
if (typeof defaultValue !== 'undefined') {
return {
value: defaultValue,
computed:
t.CallExpression.check(node) ||
t.MemberExpression.check(node) ||
t.Identifier.check(node),
};
}
return null;
}
function getStatelessPropsPath(componentDefinition): NodePath {
const value = resolveToValue(componentDefinition);
if (isReactForwardRefCall(value)) {
const inner = value.get('arguments', 0);
return inner.get('params', 0);
}
return value.get('params', 0);
}
function getDefaultPropsPath(componentDefinition: NodePath): ?NodePath {
let defaultPropsPath = getMemberValuePath(
componentDefinition,
'defaultProps',
);
if (!defaultPropsPath) {
return null;
}
defaultPropsPath = resolveToValue(defaultPropsPath);
if (!defaultPropsPath) {
return null;
}
if (t.FunctionExpression.check(defaultPropsPath.node)) {
// Find the value that is returned from the function and process it if it is
// an object literal.
const returnValue = resolveFunctionDefinitionToReturnValue(
defaultPropsPath,
);
if (returnValue && t.ObjectExpression.check(returnValue.node)) {
defaultPropsPath = returnValue;
}
}
return defaultPropsPath;
}
function getDefaultValuesFromProps(
properties: NodePath,
documentation: Documentation,
isStateless: boolean,
) {
properties
.filter(propertyPath => t.Property.check(propertyPath.node))
// Don't evaluate property if component is functional and the node is not an AssignmentPattern
.filter(
propertyPath =>
!isStateless ||
t.AssignmentPattern.check(propertyPath.get('value').node),
)
.forEach(propertyPath => {
const propName = getPropertyName(propertyPath);
if (!propName) return;
const propDescriptor = documentation.getPropDescriptor(propName);
const defaultValue = getDefaultValue(
isStateless
? propertyPath.get('value', 'right')
: propertyPath.get('value'),
);
if (defaultValue) {
propDescriptor.defaultValue = defaultValue;
}
});
}
export default function defaultPropsHandler(
documentation: Documentation,
componentDefinition: NodePath,
) {
let statelessProps = null;
const defaultPropsPath = getDefaultPropsPath(componentDefinition);
/**
* function, lazy, memo, forwardRef etc components can resolve default props as well
*/
if (!isReactComponentClass(componentDefinition)) {
statelessProps = getStatelessPropsPath(componentDefinition);
}
// Do both statelessProps and defaultProps if both are available so defaultProps can override
if (statelessProps && t.ObjectPattern.check(statelessProps.node)) {
getDefaultValuesFromProps(
statelessProps.get('properties'),
documentation,
true,
);
}
if (defaultPropsPath && t.ObjectExpression.check(defaultPropsPath.node)) {
getDefaultValuesFromProps(
defaultPropsPath.get('properties'),
documentation,
false,
);
}
}