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
13 changes: 13 additions & 0 deletions .changeset/fc-interface-props.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
'react-docgen': patch
---

Resolve component props that are only defined through a React function
component type annotation, e.g. `interface Props {...}` used in
`const MyComponent: React.FC<Props> = () => ...` (issue #387). This now works
both for TypeScript-parsed and Flow-parsed sources, for the deprecated
`React.SFC`/`React.StatelessComponent` aliases, when a named function is
assigned to an annotated variable (`const C: React.FC<Props> = Inner`), and
for props expressed only via the generic argument of `React.memo<Props>(...)`.
Flow `React.PropsWithChildren`/`PropsWithRef`/`PropsWithoutRef` wrappers are
now unwrapped as well.
132 changes: 132 additions & 0 deletions packages/react-docgen/src/handlers/__tests__/codeTypeHandler-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,138 @@ describe('codeTypeHandler', () => {
);
});

describe('stateless component with props only in React.FC<Props> (issue #387)', () => {
test('flow: resolves props from an interface referenced by React.FC', () => {
const definition = parse
.statement(
`const MyComponent: React.FC<Props> = (props) => null;
var React = require('react');
interface Props {
foo: string,
bar?: number,
}`,
)
.get('declarations')[0]
.get('init') as NodePath<ArrowFunctionExpression>;

codeTypeHandler(documentation, definition);

expect(documentation.descriptors).toEqual({
foo: {
description: '',
flowType: { name: 'string' },
required: true,
},
bar: {
description: '',
flowType: { name: 'number' },
required: false,
},
});
});

test('ts: resolves props from an interface referenced by React.FC', () => {
const definition = parseTypescript
.statement(
`const MyComponent: React.FC<Props> = (props) => null;
import React from 'react';
interface Props {
foo: string;
bar?: number;
}`,
)
.get('declarations')[0]
.get('init') as NodePath<ArrowFunctionExpression>;

codeTypeHandler(documentation, definition);

expect(documentation.descriptors).toEqual({
foo: {
description: '',
tsType: { name: 'string' },
required: true,
},
bar: {
description: '',
tsType: { name: 'number' },
required: false,
},
});
});

test('ts: resolves props from an interface extending another interface', () => {
const definition = parseTypescript
.statement(
`const MyComponent: React.FC<Props> = () => null;
import React from 'react';
interface Base {
baseProp: string;
}
interface Props extends Base {
foo: string;
}`,
)
.get('declarations')[0]
.get('init') as NodePath<ArrowFunctionExpression>;

codeTypeHandler(documentation, definition);

expect(Object.keys(documentation.descriptors).sort()).toEqual([
'baseProp',
'foo',
]);
});

test('ts: resolves props from React.memo generics', () => {
const definition = parseTypescript
.statement(
`const MyComponent = React.memo<Props>((props) => null);
import React from 'react';
interface Props {
foo: string;
}`,
)
.get('declarations')[0]
.get('init') as NodePath<CallExpression>;

const argument = definition.get(
'arguments',
)[0] as NodePath<ArrowFunctionExpression>;

codeTypeHandler(documentation, argument);

expect(documentation.descriptors).toEqual({
foo: {
description: '',
tsType: { name: 'string' },
required: true,
},
});
});

test('ts: resolves props when a named function is assigned to an annotated variable', () => {
const definition = parseTypescript.statement(
`function MyComponent() { return null; }
const Alias: React.FC<Props> = MyComponent;
import React from 'react';
interface Props {
foo: string;
}`,
0,
);

codeTypeHandler(documentation, definition);

expect(documentation.descriptors).toEqual({
foo: {
description: '',
tsType: { name: 'string' },
required: true,
},
});
});
});

describe.each([
['flow', parse],
['ts', parseTypescript],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,71 @@ exports[`getTypeFromReactComponent > Flow > stateless > finds param type annotat
]
`;

exports[`getTypeFromReactComponent > Flow > stateless > finds variable type annotation (FC) 1`] = `
[
Node {
"id": Node {
"name": "Props",
"type": "Identifier",
},
"type": "GenericTypeAnnotation",
"typeParameters": null,
},
]
`;

exports[`getTypeFromReactComponent > Flow > stateless > finds variable type annotation (FunctionComponent) 1`] = `
[
Node {
"id": Node {
"name": "Props",
"type": "Identifier",
},
"type": "GenericTypeAnnotation",
"typeParameters": null,
},
]
`;

exports[`getTypeFromReactComponent > Flow > stateless > finds variable type annotation (SFC) 1`] = `
[
Node {
"id": Node {
"name": "Props",
"type": "Identifier",
},
"type": "GenericTypeAnnotation",
"typeParameters": null,
},
]
`;

exports[`getTypeFromReactComponent > Flow > stateless > finds variable type annotation (StatelessComponent) 1`] = `
[
Node {
"id": Node {
"name": "Props",
"type": "Identifier",
},
"type": "GenericTypeAnnotation",
"typeParameters": null,
},
]
`;

exports[`getTypeFromReactComponent > Flow > stateless > unwraps React.PropsWithChildren in variable type annotation 1`] = `
[
Node {
"id": Node {
"name": "Props",
"type": "Identifier",
},
"type": "GenericTypeAnnotation",
"typeParameters": null,
},
]
`;

exports[`getTypeFromReactComponent > TypeScript > classes > finds props type in mixin base class params 1`] = `
[
Node {
Expand Down Expand Up @@ -200,6 +265,42 @@ exports[`getTypeFromReactComponent > TypeScript > stateless > finds param type a
]
`;

exports[`getTypeFromReactComponent > TypeScript > stateless > finds props type in React.memo generics 1`] = `
[
Node {
"type": "TSTypeReference",
"typeName": Node {
"name": "Props",
"type": "Identifier",
},
},
]
`;

exports[`getTypeFromReactComponent > TypeScript > stateless > finds props type in React.memo generics without param annotation 1`] = `
[
Node {
"type": "TSTypeReference",
"typeName": Node {
"name": "Props",
"type": "Identifier",
},
},
]
`;

exports[`getTypeFromReactComponent > TypeScript > stateless > finds props type when the component is assigned to an annotated variable 1`] = `
[
Node {
"type": "TSTypeReference",
"typeName": Node {
"name": "Props",
"type": "Identifier",
},
},
]
`;

exports[`getTypeFromReactComponent > TypeScript > stateless > finds variable type annotation (FC) > with MemberExpression 1`] = `
[
Node {
Expand Down Expand Up @@ -248,6 +349,30 @@ exports[`getTypeFromReactComponent > TypeScript > stateless > finds variable typ
]
`;

exports[`getTypeFromReactComponent > TypeScript > stateless > finds variable type annotation (SFC) 1`] = `
[
Node {
"type": "TSTypeReference",
"typeName": Node {
"name": "Props",
"type": "Identifier",
},
},
]
`;

exports[`getTypeFromReactComponent > TypeScript > stateless > finds variable type annotation (StatelessComponent) 1`] = `
[
Node {
"type": "TSTypeReference",
"typeName": Node {
"name": "Props",
"type": "Identifier",
},
},
]
`;

exports[`getTypeFromReactComponent > TypeScript > stateless > finds variable type annotation (VFC) > with MemberExpression 1`] = `
[
Node {
Expand Down
Loading