diff --git a/.changeset/ts-function-type-props.md b/.changeset/ts-function-type-props.md new file mode 100644 index 00000000000..d2137b1fd18 --- /dev/null +++ b/.changeset/ts-function-type-props.md @@ -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. diff --git a/packages/react-docgen/src/handlers/__tests__/codeTypeHandler-test.ts b/packages/react-docgen/src/handlers/__tests__/codeTypeHandler-test.ts index 7ab08002b57..19e3a19a0b8 100644 --- a/packages/react-docgen/src/handlers/__tests__/codeTypeHandler-test.ts +++ b/packages/react-docgen/src/handlers/__tests__/codeTypeHandler-test.ts @@ -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(src) + .get('declarations')[0] + .get('init') as NodePath; + + 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) => null; + type Component

= (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) => null; + interface Component

{ (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({}); + }); + }); }); diff --git a/packages/react-docgen/src/utils/getTypeFromReactComponent.ts b/packages/react-docgen/src/utils/getTypeFromReactComponent.ts index 5874488f9e9..607e0939f2e 100644 --- a/packages/react-docgen/src/utils/getTypeFromReactComponent.ts +++ b/packages/react-docgen/src/utils/getTypeFromReactComponent.ts @@ -13,7 +13,9 @@ import type { FlowType, InterfaceDeclaration, InterfaceExtends, + TSCallSignatureDeclaration, TSExpressionWithTypeArguments, + TSFunctionType, TSInterfaceDeclaration, TSType, TSTypeParameterDeclaration, @@ -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

= (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 | 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 => + 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; } /**