Skip to content
Closed
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
6 changes: 6 additions & 0 deletions .changeset/mixin-class-props.md
Original file line number Diff line number Diff line change
@@ -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<Props>)`.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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`] = `
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,29 @@ describe('getTypeFromReactComponent', () => {

expect(getTypeFromReactComponent(path)).toMatchSnapshot();
});

test('finds props type in mixin base class params', () => {
const path = parseTypescript.statementLast<ClassDeclaration>(
`import React from 'react';
class X extends withFoo(React.Component<Props, State>) {
render() {}
}`,
);

expect(getTypeFromReactComponent(path)).toMatchSnapshot();
});

test('finds props type in properties when extending a mixin without params', () => {
const path = parseTypescript.statementLast<ClassDeclaration>(
`import React from 'react';
class X extends withFoo(React.Component) {
props: Props;
render() {}
}`,
);

expect(getTypeFromReactComponent(path)).toMatchSnapshot();
});
});
});

Expand Down
32 changes: 32 additions & 0 deletions packages/react-docgen/src/utils/getTypeFromReactComponent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import type {
TSTypeParameterDeclaration,
TSTypeParameterInstantiation,
TypeParameterDeclaration,
ClassDeclaration,
ClassExpression,
TypeParameterInstantiation,
VariableDeclarator,
} from '@babel/types';
Expand Down Expand Up @@ -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<Props>) {}`.
*/
function getMixinSuperClassPropsPath(
classDefinition: NodePath<ClassDeclaration | ClassExpression>,
): 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.
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import React from 'react';

interface Props {
/**
* description of value
*/
value: string;
}

function withFoo<T extends new (...args: any[]) => React.Component<any>>(
Base: T,
) {
return class extends Base {};
}

export default class MyComponent extends withFoo(React.Component<Props>) {
render() {
return <div />;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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",
},
},
},
},
]
`;