Skip to content

fix: support forwardRef with out of line argument #430

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 13, 2020
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,17 @@ Object {
}
`;

exports[`defaultPropsHandler forwardRef resolves when the function is not inline 1`] = `
Object {
"foo": Object {
"defaultValue": Object {
"computed": false,
"value": "'bar'",
},
},
}
`;

exports[`defaultPropsHandler should only consider Property nodes, not e.g. spread properties 1`] = `
Object {
"bar": Object {
Expand Down
13 changes: 13 additions & 0 deletions src/handlers/__tests__/defaultPropsHandler-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -290,5 +290,18 @@ describe('defaultPropsHandler', () => {
defaultPropsHandler(documentation, parse(src).get('body', 1));
expect(documentation.descriptors).toMatchSnapshot();
});

it('resolves when the function is not inline', () => {
const src = `
import React from 'react';
const ComponentImpl = ({ foo = 'bar' }, ref) => <div ref={ref}>{foo}</div>;
React.forwardRef(ComponentImpl);
`;
defaultPropsHandler(
documentation,
parse(src).get('body', 2, 'expression'),
);
expect(documentation.descriptors).toMatchSnapshot();
});
});
});
37 changes: 36 additions & 1 deletion src/handlers/__tests__/flowTypeHandler-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

jest.mock('../../Documentation');

import { expression, statement } from '../../../tests/utils';
import { expression, statement, parse } from '../../../tests/utils';

describe('flowTypeHandler', () => {
let getFlowTypeMock;
Expand Down Expand Up @@ -334,4 +334,39 @@ describe('flowTypeHandler', () => {
`);
});
});

describe('forwardRef', () => {
it('resolves prop type from function expression', () => {
const src = `
import React from 'react';
type Props = { foo: string };
React.forwardRef((props: Props, ref) => <div ref={ref}>{props.foo}</div>);
`;
flowTypeHandler(documentation, parse(src).get('body', 2, 'expression'));
expect(documentation.descriptors).toEqual({
foo: {
flowType: {},
required: true,
description: '',
},
});
});

it('resolves when the function is not inline', () => {
const src = `
import React from 'react';
type Props = { foo: string };
const ComponentImpl = (props: Props, ref) => <div ref={ref}>{props.foo}</div>;
React.forwardRef(ComponentImpl);
`;
flowTypeHandler(documentation, parse(src).get('body', 3, 'expression'));
expect(documentation.descriptors).toEqual({
foo: {
flowType: {},
required: true,
description: '',
},
});
});
});
});
2 changes: 1 addition & 1 deletion src/handlers/defaultPropsHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ function getDefaultValue(path: NodePath) {
function getStatelessPropsPath(componentDefinition): NodePath {
const value = resolveToValue(componentDefinition);
if (isReactForwardRefCall(value)) {
const inner = value.get('arguments', 0);
const inner = resolveToValue(value.get('arguments', 0));
return inner.get('params', 0);
}
return value.get('params', 0);
Expand Down
2 changes: 1 addition & 1 deletion src/utils/getFlowTypeFromReactComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import resolveToValue from './resolveToValue';
function getStatelessPropsPath(componentDefinition): NodePath {
const value = resolveToValue(componentDefinition);
if (isReactForwardRefCall(value)) {
const inner = value.get('arguments', 0);
const inner = resolveToValue(value.get('arguments', 0));
return inner.get('params', 0);
}
return value.get('params', 0);
Expand Down