diff --git a/.changeset/mixin-class-props.md b/.changeset/mixin-class-props.md new file mode 100644 index 00000000000..2ce3ca4d25e --- /dev/null +++ b/.changeset/mixin-class-props.md @@ -0,0 +1,6 @@ +--- +'react-docgen': patch +--- + +Find TypeScript props of class components extending a mixin call such as +`class X extends withFoo(React.Component)`. diff --git a/packages/react-docgen/src/utils/__tests__/__snapshots__/getTypeFromReactComponent-test.ts.snap b/packages/react-docgen/src/utils/__tests__/__snapshots__/getTypeFromReactComponent-test.ts.snap index e2a00ad3a7d..80528f7f15f 100644 --- a/packages/react-docgen/src/utils/__tests__/__snapshots__/getTypeFromReactComponent-test.ts.snap +++ b/packages/react-docgen/src/utils/__tests__/__snapshots__/getTypeFromReactComponent-test.ts.snap @@ -83,6 +83,18 @@ exports[`getTypeFromReactComponent > Flow > stateless > finds param type annotat ] `; +exports[`getTypeFromReactComponent > TypeScript > classes > finds props type in mixin base class params 1`] = ` +[ + Node { + "type": "TSTypeReference", + "typeName": Node { + "name": "Props", + "type": "Identifier", + }, + }, +] +`; + exports[`getTypeFromReactComponent > TypeScript > classes > finds props type in params 1`] = ` [ Node { @@ -107,6 +119,18 @@ exports[`getTypeFromReactComponent > TypeScript > classes > finds props type in ] `; +exports[`getTypeFromReactComponent > TypeScript > classes > finds props type in properties when extending a mixin without params 1`] = ` +[ + Node { + "type": "TSTypeReference", + "typeName": Node { + "name": "Props", + "type": "Identifier", + }, + }, +] +`; + exports[`getTypeFromReactComponent > TypeScript > stateless > does not find generic forwardRef type annotation on typo 1`] = `[]`; exports[`getTypeFromReactComponent > TypeScript > stateless > finds generic forwardRef type annotation 1`] = ` diff --git a/packages/react-docgen/src/utils/__tests__/getTypeFromReactComponent-test.ts b/packages/react-docgen/src/utils/__tests__/getTypeFromReactComponent-test.ts index 47e6d26836d..c6dea8d6a25 100644 --- a/packages/react-docgen/src/utils/__tests__/getTypeFromReactComponent-test.ts +++ b/packages/react-docgen/src/utils/__tests__/getTypeFromReactComponent-test.ts @@ -5,6 +5,7 @@ import type { } from '@babel/types'; import { parse, parseTypescript } from '../../../tests/utils'; import getTypeFromReactComponent from '../getTypeFromReactComponent.js'; +import { parse as parseComponent } from '../../main.js'; import { describe, expect, test } from 'vitest'; import type { NodePath } from '@babel/traverse'; @@ -154,6 +155,125 @@ describe('getTypeFromReactComponent', () => { expect(getTypeFromReactComponent(path)).toMatchSnapshot(); }); + + test('finds props type in mixin base class params', () => { + const path = parseTypescript.statementLast( + `import React from 'react'; + class X extends withFoo(React.Component) { + render() {} + }`, + ); + + expect(getTypeFromReactComponent(path)).toMatchSnapshot(); + }); + + test('finds mixin props using Babel 8 type argument fields', () => { + const path = parseTypescript.statementLast( + `import React from 'react'; + class X extends withFoo(React.Component) { + render() {} + }`, + ); + const superClass = path.get('superClass'); + + if (!superClass.isCallExpression()) { + throw new Error('Expected a mixin call'); + } + const [baseClass] = superClass.get('arguments'); + + if (!baseClass?.isTSInstantiationExpression()) { + throw new Error('Expected an instantiated base class'); + } + + // Exercise Babel 8's AST shape even when running with Babel 7. + if (!('typeArguments' in baseClass.node)) { + Object.assign(baseClass.node, { + typeArguments: baseClass.node.typeParameters, + }); + Reflect.deleteProperty(baseClass.node, 'typeParameters'); + } + + expect(getTypeFromReactComponent(path)[0]?.node).toMatchObject({ + type: 'TSTypeReference', + typeName: { type: 'Identifier', name: 'Props' }, + }); + }); + + test.each(['configure', 'Other.Component'])( + 'preserves declared props when the mixin argument is %s', + (argument) => { + const result = parseComponent( + `import Other from 'other'; + interface Props { value: string } + interface Options { unrelated: number } + function configure() {} + export class Button extends makeComponent(${argument}) { + props: Props; + render() { return