@@ -17,16 +17,21 @@ import getMemberValuePath from '../utils/getMemberValuePath';
17
17
import printValue from '../utils/printValue' ;
18
18
import recast from 'recast' ;
19
19
import resolveToValue from '../utils/resolveToValue' ;
20
+ import isStatelessComponent from '../utils/isStatelessComponent' ;
20
21
21
22
var { types : { namedTypes : types , visit} } = recast ;
22
23
23
- function getDefaultValue ( path ) {
24
+ function getDefaultValue ( path : NodePath ) {
24
25
var node = path . node ;
25
26
var defaultValue ;
26
27
if ( types . Literal . check ( node ) ) {
27
28
defaultValue = node . raw ;
28
29
} else {
29
- path = resolveToValue ( path ) ;
30
+ if ( types . AssignmentPattern . check ( path . node ) ) {
31
+ path = resolveToValue ( path . get ( 'right' ) ) ;
32
+ } else {
33
+ path = resolveToValue ( path ) ;
34
+ }
30
35
if ( types . ImportDeclaration . check ( path . node ) ) {
31
36
defaultValue = node . name ;
32
37
} else {
@@ -44,10 +49,11 @@ function getDefaultValue(path) {
44
49
}
45
50
}
46
51
47
- export default function defaultPropsHandler (
48
- documentation : Documentation ,
49
- componentDefinition : NodePath
50
- ) {
52
+ function getStatelessPropsPath ( componentDefinition ) : NodePath {
53
+ return resolveToValue ( componentDefinition ) . get ( 'params' , 0 ) ;
54
+ }
55
+
56
+ function getDefaultPropsPath ( componentDefinition : NodePath ) : ?NodePath {
51
57
var defaultPropsPath = getMemberValuePath (
52
58
componentDefinition ,
53
59
'defaultProps'
@@ -75,18 +81,41 @@ export default function defaultPropsHandler(
75
81
} ,
76
82
} ) ;
77
83
}
84
+ return defaultPropsPath ;
85
+ }
78
86
79
- if ( types . ObjectExpression . check ( defaultPropsPath . node ) ) {
80
- defaultPropsPath . get ( 'properties' )
81
- . filter ( propertyPath => types . Property . check ( propertyPath . node ) )
82
- . forEach ( function ( propertyPath ) {
83
- var propDescriptor = documentation . getPropDescriptor (
84
- getPropertyName ( propertyPath )
85
- ) ;
86
- var defaultValue = getDefaultValue ( propertyPath . get ( 'value' ) ) ;
87
- if ( defaultValue ) {
88
- propDescriptor . defaultValue = defaultValue ;
89
- }
90
- } ) ;
87
+ function getDefaultValuesFromProps (
88
+ properties : NodePath ,
89
+ documentation : Documentation
90
+ ) {
91
+ properties
92
+ . filter ( propertyPath => types . Property . check ( propertyPath . node ) )
93
+ . forEach ( function ( propertyPath ) {
94
+ var propDescriptor = documentation . getPropDescriptor (
95
+ getPropertyName ( propertyPath )
96
+ ) ;
97
+ var defaultValue = getDefaultValue ( propertyPath . get ( 'value' ) ) ;
98
+ if ( defaultValue ) {
99
+ propDescriptor . defaultValue = defaultValue ;
100
+ }
101
+ } ) ;
102
+ }
103
+
104
+ export default function defaultPropsHandler (
105
+ documentation : Documentation ,
106
+ componentDefinition : NodePath
107
+ ) {
108
+
109
+ var statelessProps = null ;
110
+ var defaultPropsPath = getDefaultPropsPath ( componentDefinition ) ;
111
+ if ( isStatelessComponent ( componentDefinition ) ) {
112
+ statelessProps = getStatelessPropsPath ( componentDefinition ) ;
113
+ }
114
+
115
+ if ( statelessProps && types . ObjectPattern . check ( statelessProps . node ) ) {
116
+ getDefaultValuesFromProps ( statelessProps . get ( 'properties' ) , documentation ) ;
117
+ }
118
+ if ( defaultPropsPath && types . ObjectExpression . check ( defaultPropsPath . node ) ) {
119
+ getDefaultValuesFromProps ( defaultPropsPath . get ( 'properties' ) , documentation ) ;
91
120
}
92
121
}
0 commit comments