Skip to content

Commit 1ebbab0

Browse files
committed
fix types and remove unnecessary resolveToValue
1 parent 637492e commit 1ebbab0

File tree

1 file changed

+15
-7
lines changed

1 file changed

+15
-7
lines changed

packages/react-docgen/src/handlers/defaultPropsHandler.ts

+15-7
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,15 @@ import type Documentation from '../Documentation.js';
99
import type { DefaultValueDescriptor } from '../Documentation.js';
1010
import type { NodePath } from '@babel/traverse';
1111
import type {
12+
CallExpression,
1213
ObjectMethod,
1314
ObjectProperty,
1415
RestElement,
1516
SpreadElement,
1617
} from '@babel/types';
1718
import type { ComponentNode } from '../resolver/index.js';
1819
import type { Handler } from './index.js';
20+
import type { StatelessComponentNode } from '../resolver/index.js';
1921

2022
function getDefaultValue(path: NodePath): DefaultValueDescriptor | null {
2123
let defaultValue: string | undefined;
@@ -55,12 +57,16 @@ function getDefaultValue(path: NodePath): DefaultValueDescriptor | null {
5557
}
5658

5759
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+
}
6167

62-
if (isReactForwardRefCall(value)) {
63-
value = resolveToValue(value.get('arguments')[0]!);
68+
if (!value.isFunction()) {
69+
return;
6470
}
6571

6672
return value.get('params')[0];
@@ -151,14 +157,16 @@ const defaultPropsHandler: Handler = function (
151157
documentation: Documentation,
152158
componentDefinition: NodePath<ComponentNode>,
153159
): void {
154-
let statelessProps: NodePath | null = null;
160+
let statelessProps: NodePath | undefined;
155161
const defaultPropsPath = getDefaultPropsPath(componentDefinition);
156162

157163
/**
158164
* function, lazy, memo, forwardRef etc components can resolve default props as well
159165
*/
160166
if (!isReactComponentClass(componentDefinition)) {
161-
statelessProps = getStatelessPropsPath(componentDefinition);
167+
statelessProps = getStatelessPropsPath(
168+
componentDefinition as NodePath<StatelessComponentNode | CallExpression>,
169+
);
162170
}
163171

164172
// Do both statelessProps and defaultProps if both are available so defaultProps can override

0 commit comments

Comments
 (0)