Skip to content

Commit 3cf7bde

Browse files
committed
Simplify code, better types
1 parent 644b49e commit 3cf7bde

1 file changed

Lines changed: 19 additions & 20 deletions

File tree

packages/react-docgen/src/utils/getTypeFromReactComponent.ts

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import type {
1212
FlowType,
1313
InterfaceDeclaration,
1414
InterfaceExtends,
15-
Node,
1615
TSExpressionWithTypeArguments,
1716
TSInterfaceDeclaration,
1817
TSType,
@@ -26,29 +25,23 @@ import getTypeIdentifier from './getTypeIdentifier.js';
2625
import isReactBuiltinReference from './isReactBuiltinReference.js';
2726
import unwrapBuiltinTSPropTypes from './unwrapBuiltinTSPropTypes.js';
2827

29-
// TODO TESTME
30-
3128
function getStatelessPropsPath(
3229
componentDefinition: NodePath,
3330
): NodePath | undefined {
34-
let value = componentDefinition;
35-
36-
if (isReactForwardRefCall(value)) {
37-
value = resolveToValue(value.get('arguments')[0]!);
38-
}
39-
40-
if (!value.isFunction()) return;
31+
if (!componentDefinition.isFunction()) return;
4132

42-
return value.get('params')[0];
33+
return componentDefinition.get('params')[0];
4334
}
4435

45-
function getForwardRefGenericsType(componentDefinition: NodePath) {
46-
const typeParameters = componentDefinition.get(
47-
'typeParameters',
48-
) as NodePath<Node>;
36+
function getForwardRefGenericsType(
37+
componentDefinition: NodePath,
38+
): NodePath<TSType> | null {
39+
const typeParameters = componentDefinition.get('typeParameters') as NodePath<
40+
TSTypeParameterInstantiation | null | undefined
41+
>;
4942

5043
if (typeParameters && typeParameters.hasNode()) {
51-
const params = typeParameters.get('params') as Array<NodePath<Node>>;
44+
const params = typeParameters.get('params');
5245

5346
return params[1] ?? null;
5447
}
@@ -121,11 +114,17 @@ export default (componentDefinition: NodePath): NodePath[] => {
121114
}
122115
}
123116
} else {
124-
const genericTypeAnnotation =
125-
getForwardRefGenericsType(componentDefinition);
117+
if (isReactForwardRefCall(componentDefinition)) {
118+
const genericTypeAnnotation =
119+
getForwardRefGenericsType(componentDefinition);
120+
121+
if (genericTypeAnnotation) {
122+
typePaths.push(genericTypeAnnotation);
123+
}
126124

127-
if (genericTypeAnnotation) {
128-
typePaths.push(genericTypeAnnotation);
125+
componentDefinition = resolveToValue(
126+
componentDefinition.get('arguments')[0]!,
127+
);
129128
}
130129

131130
const propsParam = getStatelessPropsPath(componentDefinition);

0 commit comments

Comments
 (0)