Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TS Enum support #884

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 18 additions & 1 deletion packages/react-docgen/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ import {
FindExportedDefinitionsResolver,
} from './resolver/index.js';

interface Features {
resolveEnums?: boolean;
}

export interface Config {
handlers?: Handler[];
importer?: Importer;
Expand All @@ -33,6 +37,7 @@ export interface Config {
*/
filename?: string;
babelOptions?: TransformOptions;
experimentalFeatures?: Features;
}
export type InternalConfig = Omit<Required<Config>, 'filename'>;

Expand Down Expand Up @@ -61,14 +66,26 @@ export const defaultHandlers: Handler[] = [
componentMethodsJsDocHandler,
];

const defaultFeatures: Required<Features> = {
resolveEnums: false,
};

export function createConfig(inputConfig: Config): InternalConfig {
const { babelOptions, filename, handlers, importer, resolver } = inputConfig;
const {
babelOptions,
filename,
experimentalFeatures,
handlers,
importer,
resolver,
} = inputConfig;

const config = {
babelOptions: { ...babelOptions },
handlers: handlers ?? defaultHandlers,
importer: importer ?? defaultImporter,
resolver: resolver ?? defaultResolver,
experimentalFeatures: { ...defaultFeatures, ...experimentalFeatures },
};

if (filename) {
Expand Down
37 changes: 26 additions & 11 deletions packages/react-docgen/src/utils/getTSType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import type {
TypeScript,
TSQualifiedName,
TSLiteralType,
TSTypeAliasDeclaration,
} from '@babel/types';
import { getDocblock } from './docblock.js';

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

const UNKNOWN_TYPE = Object.freeze({ name: 'unknown' }) as SimpleType;

const namedTypes: Record<
string,
(
Expand All @@ -71,6 +74,7 @@ const namedTypes: Record<
TSIntersectionType: handleTSIntersectionType,
TSMappedType: handleTSMappedType,
TSTupleType: handleTSTupleType,
TSTypeAliasDeclaration: handleTSTypeAliasDeclaration,
TSTypeQuery: handleTSTypeQuery,
TSTypeOperator: handleTSTypeOperator,
TSIndexedAccessType: handleTSIndexedAccessType,
Expand Down Expand Up @@ -113,6 +117,15 @@ function handleTSArrayType(
};
}

function handleTSTypeAliasDeclaration(
path: NodePath<TSTypeAliasDeclaration>,
typeParams: TypeParameters | null,
): TypeDescriptor<TSFunctionSignatureType> {
const resolvedTypeAnnotation = path.get('typeAnnotation');

return getTSTypeWithResolvedTypes(resolvedTypeAnnotation, typeParams);
}

function handleTSTypeReference(
path: NodePath<TSTypeReference>,
typeParams: TypeParameters | null,
Expand All @@ -127,8 +140,7 @@ function handleTSTypeReference(
}

const resolvedPath =
(typeParams && typeParams[type.name]) ||
resolveToValue(path.get('typeName'));
(typeParams && typeParams[type.name]) || resolveToValue(typeName);

const typeParameters = path.get('typeParameters');
const resolvedTypeParameters = resolvedPath.get('typeParameters') as NodePath<
Expand All @@ -149,15 +161,18 @@ function handleTSTypeReference(
resolvedPath as NodePath<TSType | TSTypeAnnotation>,
null,
);
}
} else if (resolvedPath !== typeName) {
const resolvedType = getTSTypeWithResolvedTypes(
resolvedPath as NodePath<TSType | TSTypeAnnotation>,
typeParams,
);

const resolvedTypeAnnotation = resolvedPath.get('typeAnnotation') as NodePath<
TSType | TSTypeAnnotation | null | undefined
>;
if (resolvedType !== UNKNOWN_TYPE) {
return resolvedType;
}
}

if (resolvedTypeAnnotation.hasNode()) {
type = getTSTypeWithResolvedTypes(resolvedTypeAnnotation, typeParams);
} else if (typeParameters.hasNode()) {
if (typeParameters.hasNode()) {
const params = typeParameters.get('params');

type = {
Expand Down Expand Up @@ -479,7 +494,7 @@ function handleTSIndexedAccessType(
});

if (!resolvedType) {
return { name: 'unknown' };
return UNKNOWN_TYPE;
}

return {
Expand Down Expand Up @@ -533,7 +548,7 @@ function getTSTypeWithResolvedTypes(
}

if (!type) {
type = { name: 'unknown' };
type = UNKNOWN_TYPE;
}

if (typeAliasName) {
Expand Down