Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .changeset/ts-function-type-props.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
'react-docgen': minor
---

Read the props of a component whose variable is typed with a TypeScript function
type, such as `const C: FnType = (props) => ...` with
`type FnType = (props: Props) => ReactNode`, or with a type that has a call
signature.
103 changes: 103 additions & 0 deletions packages/react-docgen/src/handlers/__tests__/codeTypeHandler-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -457,4 +457,107 @@ describe('codeTypeHandler', () => {

expect(documentation.descriptors).toMatchSnapshot();
});

describe('function type of the component variable', () => {
function getProps(src: string) {
const definition = parseTypescript
.statement<VariableDeclaration>(src)
.get('declarations')[0]
.get('init') as NodePath<ArrowFunctionExpression>;

codeTypeHandler(documentation, definition);

return documentation.descriptors;
}

test('reads props from a type alias of a function type', () => {
expect(
getProps(`
const MyComponent: FnType = (props) => null;
type FnType = (props: Props) => null;
type Props = {
/** The foo prop */
foo: string;
};
`),
).toEqual({
foo: {
required: true,
tsType: { name: 'string' },
description: 'The foo prop',
},
});
});

test('reads props from an interface with a call signature', () => {
expect(
Object.keys(
getProps(`
const MyComponent: Fn = (props) => null;
interface Fn {
(props: Props): null;
displayName?: string;
}
type Props = { foo: string };
`),
),
).toEqual(['foo']);
});

test('reads props from a type literal with a call signature', () => {
expect(
Object.keys(
getProps(`
const MyComponent: Fn = (props) => null;
type Fn = { (props: Props): null };
type Props = { foo: string };
`),
),
).toEqual(['foo']);
});

test('reads props from an inline function type', () => {
expect(
Object.keys(
getProps(`
const MyComponent: (props: Props) => null = (props) => null;
type Props = { foo: string };
`),
),
).toEqual(['foo']);
});

test('reads props from a generic type alias of a function type', () => {
expect(
Object.keys(
getProps(`
const MyComponent: Component<Props> = (props) => null;
type Component<P> = (props: P) => null;
type Props = { foo: string };
`),
),
).toEqual(['foo']);
});

test('reads props from a generic interface with a call signature', () => {
expect(
Object.keys(
getProps(`
const MyComponent: Component<Props> = (props) => null;
interface Component<P> { (props: P): null }
interface Props { foo: string }
`),
),
).toEqual(['foo']);
});

test('ignores a type that is not a function type', () => {
expect(
getProps(`
const MyComponent: Fn = (props) => null;
type Fn = string;
`),
).toEqual({});
});
});
});
77 changes: 76 additions & 1 deletion packages/react-docgen/src/utils/getTypeFromReactComponent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ import type {
FlowType,
InterfaceDeclaration,
InterfaceExtends,
TSCallSignatureDeclaration,
TSExpressionWithTypeArguments,
TSFunctionType,
TSInterfaceDeclaration,
TSType,
TSTypeParameterDeclaration,
Expand Down Expand Up @@ -79,10 +81,83 @@ function findAssignedVariableType(
if (typeParameters.hasNode()) {
return typeParameters.get('params')[0] ?? null;
}

return null;
}
}

return null;
return getFunctionTypePropsType(typeAnnotation);
}

/**
* Finds the type of the first parameter of a function type, such as
* `(props: Props) => ReactNode`, or of the first call signature of a
* type literal or interface, such as `{ (props: Props): ReactNode }`.
* Type references to such types are resolved, and a parameter typed with a
* type parameter of a generic type, such as `P` in
* `type Component<P> = (props: P) => ReactNode`, gets the type argument.
*/
function getFunctionTypePropsType(path: NodePath): NodePath | null {
let typePath = path;
let typeParams: TypeParameters | null = null;

if (typePath.isTSTypeReference()) {
const typeName = typePath.get('typeName');
const declaration = typeName.isIdentifier()
? resolveToValue(typeName)
: null;
const typeArguments = getTypeArguments(typePath);

if (
typeArguments.hasNode() &&
(declaration?.isTSTypeAliasDeclaration() ||
declaration?.isTSInterfaceDeclaration())
) {
const typeParameters = declaration.get('typeParameters') as NodePath<
TSTypeParameterDeclaration | null | undefined
>;

if (typeParameters.hasNode()) {
typeParams = getTypeParameters(typeParameters, typeArguments, null);
}
}

typePath = resolveGenericTypeAnnotation(typePath) ?? typePath;
}

let signature: NodePath<TSCallSignatureDeclaration | TSFunctionType> | null =
null;

if (typePath.isTSFunctionType()) {
signature = typePath;
} else if (
typePath.isTSTypeLiteral() ||
typePath.isTSInterfaceDeclaration()
) {
const members = typePath.isTSTypeLiteral()
? typePath.get('members')
: typePath.get('body').get('body');

signature =
members.find((member): member is NodePath<TSCallSignatureDeclaration> =>
member.isTSCallSignatureDeclaration(),
) ?? null;
}

if (!signature) return null;

const [propsParam] = signature.get('parameters');
const propsType = propsParam ? getTypeAnnotation(propsParam) : null;

if (typeParams && propsType?.isTSTypeReference()) {
const propsTypeName = propsType.get('typeName');

if (propsTypeName.isIdentifier()) {
return typeParams[propsTypeName.node.name] ?? propsType;
}
}

return propsType;
}

/**
Expand Down