Skip to content

Commit b27c286

Browse files
committed
Refactor code to prepare for future support of enums for example
1 parent e60db94 commit b27c286

File tree

1 file changed

+26
-11
lines changed

1 file changed

+26
-11
lines changed

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

+26-11
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import type {
3636
TypeScript,
3737
TSQualifiedName,
3838
TSLiteralType,
39+
TSTypeAliasDeclaration,
3940
} from '@babel/types';
4041
import { getDocblock } from './docblock.js';
4142

@@ -54,6 +55,8 @@ const tsTypes: Record<string, string> = {
5455
TSVoidKeyword: 'void',
5556
};
5657

58+
const UNKNOWN_TYPE = Object.freeze({ name: 'unknown' }) as SimpleType;
59+
5760
const namedTypes: Record<
5861
string,
5962
(
@@ -71,6 +74,7 @@ const namedTypes: Record<
7174
TSIntersectionType: handleTSIntersectionType,
7275
TSMappedType: handleTSMappedType,
7376
TSTupleType: handleTSTupleType,
77+
TSTypeAliasDeclaration: handleTSTypeAliasDeclaration,
7478
TSTypeQuery: handleTSTypeQuery,
7579
TSTypeOperator: handleTSTypeOperator,
7680
TSIndexedAccessType: handleTSIndexedAccessType,
@@ -113,6 +117,15 @@ function handleTSArrayType(
113117
};
114118
}
115119

120+
function handleTSTypeAliasDeclaration(
121+
path: NodePath<TSTypeAliasDeclaration>,
122+
typeParams: TypeParameters | null,
123+
): TypeDescriptor<TSFunctionSignatureType> {
124+
const resolvedTypeAnnotation = path.get('typeAnnotation');
125+
126+
return getTSTypeWithResolvedTypes(resolvedTypeAnnotation, typeParams);
127+
}
128+
116129
function handleTSTypeReference(
117130
path: NodePath<TSTypeReference>,
118131
typeParams: TypeParameters | null,
@@ -127,8 +140,7 @@ function handleTSTypeReference(
127140
}
128141

129142
const resolvedPath =
130-
(typeParams && typeParams[type.name]) ||
131-
resolveToValue(path.get('typeName'));
143+
(typeParams && typeParams[type.name]) || resolveToValue(typeName);
132144

133145
const typeParameters = path.get('typeParameters');
134146
const resolvedTypeParameters = resolvedPath.get('typeParameters') as NodePath<
@@ -149,15 +161,18 @@ function handleTSTypeReference(
149161
resolvedPath as NodePath<TSType | TSTypeAnnotation>,
150162
null,
151163
);
152-
}
164+
} else if (resolvedPath !== typeName) {
165+
const resolvedType = getTSTypeWithResolvedTypes(
166+
resolvedPath as NodePath<TSType | TSTypeAnnotation>,
167+
typeParams,
168+
);
153169

154-
const resolvedTypeAnnotation = resolvedPath.get('typeAnnotation') as NodePath<
155-
TSType | TSTypeAnnotation | null | undefined
156-
>;
170+
if (resolvedType !== UNKNOWN_TYPE) {
171+
return resolvedType;
172+
}
173+
}
157174

158-
if (resolvedTypeAnnotation.hasNode()) {
159-
type = getTSTypeWithResolvedTypes(resolvedTypeAnnotation, typeParams);
160-
} else if (typeParameters.hasNode()) {
175+
if (typeParameters.hasNode()) {
161176
const params = typeParameters.get('params');
162177

163178
type = {
@@ -479,7 +494,7 @@ function handleTSIndexedAccessType(
479494
});
480495

481496
if (!resolvedType) {
482-
return { name: 'unknown' };
497+
return UNKNOWN_TYPE;
483498
}
484499

485500
return {
@@ -533,7 +548,7 @@ function getTSTypeWithResolvedTypes(
533548
}
534549

535550
if (!type) {
536-
type = { name: 'unknown' };
551+
type = UNKNOWN_TYPE;
537552
}
538553

539554
if (typeAliasName) {

0 commit comments

Comments
 (0)