@@ -9,13 +9,15 @@ import type Documentation from '../Documentation.js';
9
9
import type { DefaultValueDescriptor } from '../Documentation.js' ;
10
10
import type { NodePath } from '@babel/traverse' ;
11
11
import type {
12
+ CallExpression ,
12
13
ObjectMethod ,
13
14
ObjectProperty ,
14
15
RestElement ,
15
16
SpreadElement ,
16
17
} from '@babel/types' ;
17
18
import type { ComponentNode } from '../resolver/index.js' ;
18
19
import type { Handler } from './index.js' ;
20
+ import type { StatelessComponentNode } from '../resolver/index.js' ;
19
21
20
22
function getDefaultValue ( path : NodePath ) : DefaultValueDescriptor | null {
21
23
let defaultValue : string | undefined ;
@@ -55,12 +57,16 @@ function getDefaultValue(path: NodePath): DefaultValueDescriptor | null {
55
57
}
56
58
57
59
function getStatelessPropsPath (
58
- componentDefinition : NodePath < ComponentNode > ,
59
- ) : NodePath {
60
- let value = resolveToValue ( componentDefinition ) ;
60
+ componentDefinition : NodePath < StatelessComponentNode | CallExpression > ,
61
+ ) : NodePath | undefined {
62
+ let value : NodePath = componentDefinition ;
63
+
64
+ if ( isReactForwardRefCall ( componentDefinition ) ) {
65
+ value = resolveToValue ( componentDefinition . get ( 'arguments' ) [ 0 ] ! ) ;
66
+ }
61
67
62
- if ( isReactForwardRefCall ( value ) ) {
63
- value = resolveToValue ( value . get ( 'arguments' ) [ 0 ] ! ) ;
68
+ if ( ! value . isFunction ( ) ) {
69
+ return ;
64
70
}
65
71
66
72
return value . get ( 'params' ) [ 0 ] ;
@@ -151,14 +157,16 @@ const defaultPropsHandler: Handler = function (
151
157
documentation : Documentation ,
152
158
componentDefinition : NodePath < ComponentNode > ,
153
159
) : void {
154
- let statelessProps : NodePath | null = null ;
160
+ let statelessProps : NodePath | undefined ;
155
161
const defaultPropsPath = getDefaultPropsPath ( componentDefinition ) ;
156
162
157
163
/**
158
164
* function, lazy, memo, forwardRef etc components can resolve default props as well
159
165
*/
160
166
if ( ! isReactComponentClass ( componentDefinition ) ) {
161
- statelessProps = getStatelessPropsPath ( componentDefinition ) ;
167
+ statelessProps = getStatelessPropsPath (
168
+ componentDefinition as NodePath < StatelessComponentNode | CallExpression > ,
169
+ ) ;
162
170
}
163
171
164
172
// Do both statelessProps and defaultProps if both are available so defaultProps can override
0 commit comments