From ebbf75c609e5ec27b633ccc1e79a530574510112 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mikl=C3=B3s=20Fazekas?= Date: Wed, 21 Oct 2026 12:15:56 +0200 Subject: [PATCH] fix: find TS props of classes extending a mixin call --- .changeset/mixin-class-props.md | 6 ++++ .../getTypeFromReactComponent-test.ts.snap | 24 ++++++++++++++ .../getTypeFromReactComponent-test.ts | 23 +++++++++++++ .../src/utils/getTypeFromReactComponent.ts | 32 +++++++++++++++++++ .../__fixtures__/ts-extends-mixin.tsx | 20 ++++++++++++ .../__snapshots__/integration-test.ts.snap | 19 +++++++++++ 6 files changed, 124 insertions(+) create mode 100644 .changeset/mixin-class-props.md create mode 100644 packages/react-docgen/tests/integration/__fixtures__/ts-extends-mixin.tsx 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..f69daec71ee 100644 --- a/packages/react-docgen/src/utils/__tests__/getTypeFromReactComponent-test.ts +++ b/packages/react-docgen/src/utils/__tests__/getTypeFromReactComponent-test.ts @@ -154,6 +154,29 @@ 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 props type in properties when extending a mixin without params', () => { + const path = parseTypescript.statementLast( + `import React from 'react'; + class X extends withFoo(React.Component) { + props: Props; + render() {} + }`, + ); + + expect(getTypeFromReactComponent(path)).toMatchSnapshot(); + }); }); }); diff --git a/packages/react-docgen/src/utils/getTypeFromReactComponent.ts b/packages/react-docgen/src/utils/getTypeFromReactComponent.ts index 028be7e28f7..28f780ad9c9 100644 --- a/packages/react-docgen/src/utils/getTypeFromReactComponent.ts +++ b/packages/react-docgen/src/utils/getTypeFromReactComponent.ts @@ -18,6 +18,8 @@ import type { TSTypeParameterDeclaration, TSTypeParameterInstantiation, TypeParameterDeclaration, + ClassDeclaration, + ClassExpression, TypeParameterInstantiation, VariableDeclarator, } from '@babel/types'; @@ -82,6 +84,28 @@ function findAssignedVariableType( return null; } +/** + * Finds the props type of a class extending a mixin call, e.g. + * `class X extends withFoo(React.Component) {}`. + */ +function getMixinSuperClassPropsPath( + classDefinition: NodePath, +): NodePath | null { + const superClass = classDefinition.get('superClass'); + + if (!superClass.isCallExpression()) return null; + + const [baseClass] = superClass.get('arguments'); + + if (!baseClass?.isTSInstantiationExpression()) return null; + + const typeParameters = baseClass.get('typeParameters'); + + if (!typeParameters.hasNode()) return null; + + return typeParameters.get('params')[0] ?? null; +} + /** * Given an React component (stateless or class) tries to find * flow or TS types for the props. It may find multiple types. @@ -105,6 +129,14 @@ export default (componentDefinition: NodePath): NodePath[] => { } } } else { + const mixinPropsPath = getMixinSuperClassPropsPath(componentDefinition); + + if (mixinPropsPath) { + typePaths.push(mixinPropsPath); + + return typePaths; + } + const propsMemberPath = getMemberValuePath(componentDefinition, 'props'); if (!propsMemberPath) { diff --git a/packages/react-docgen/tests/integration/__fixtures__/ts-extends-mixin.tsx b/packages/react-docgen/tests/integration/__fixtures__/ts-extends-mixin.tsx new file mode 100644 index 00000000000..b526beb4e46 --- /dev/null +++ b/packages/react-docgen/tests/integration/__fixtures__/ts-extends-mixin.tsx @@ -0,0 +1,20 @@ +import React from 'react'; + +interface Props { + /** + * description of value + */ + value: string; +} + +function withFoo React.Component>( + Base: T, +) { + return class extends Base {}; +} + +export default class MyComponent extends withFoo(React.Component) { + render() { + return
; + } +} diff --git a/packages/react-docgen/tests/integration/__snapshots__/integration-test.ts.snap b/packages/react-docgen/tests/integration/__snapshots__/integration-test.ts.snap index 9e4ece0c25a..dec57998272 100644 --- a/packages/react-docgen/tests/integration/__snapshots__/integration-test.ts.snap +++ b/packages/react-docgen/tests/integration/__snapshots__/integration-test.ts.snap @@ -2163,3 +2163,22 @@ exports[`integration > fixtures > processes component "test-all-imports.tsx" wit }, ] `; + +exports[`integration > fixtures > processes component "ts-extends-mixin.tsx" without errors 1`] = ` +[ + { + "description": "", + "displayName": "MyComponent", + "methods": [], + "props": { + "value": { + "description": "description of value", + "required": true, + "tsType": { + "name": "string", + }, + }, + }, + }, +] +`;