Skip to content

Commit 1db47b1

Browse files
committed
fix: support forwardRef with out of line argument
1 parent c73fbb9 commit 1db47b1

File tree

5 files changed

+62
-3
lines changed

5 files changed

+62
-3
lines changed

src/handlers/__tests__/__snapshots__/defaultPropsHandler-test.js.snap

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,17 @@ Object {
299299
}
300300
`;
301301

302+
exports[`defaultPropsHandler forwardRef resolves when the function is not inline 1`] = `
303+
Object {
304+
"foo": Object {
305+
"defaultValue": Object {
306+
"computed": false,
307+
"value": "'bar'",
308+
},
309+
},
310+
}
311+
`;
312+
302313
exports[`defaultPropsHandler should only consider Property nodes, not e.g. spread properties 1`] = `
303314
Object {
304315
"bar": Object {

src/handlers/__tests__/defaultPropsHandler-test.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,5 +290,18 @@ describe('defaultPropsHandler', () => {
290290
defaultPropsHandler(documentation, parse(src).get('body', 1));
291291
expect(documentation.descriptors).toMatchSnapshot();
292292
});
293+
294+
it('resolves when the function is not inline', () => {
295+
const src = `
296+
import React from 'react';
297+
const ComponentImpl = ({ foo = 'bar' }, ref) => <div ref={ref}>{foo}</div>;
298+
React.forwardRef(ComponentImpl);
299+
`;
300+
defaultPropsHandler(
301+
documentation,
302+
parse(src).get('body', 2, 'expression'),
303+
);
304+
expect(documentation.descriptors).toMatchSnapshot();
305+
});
293306
});
294307
});

src/handlers/__tests__/flowTypeHandler-test.js

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

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

11-
import { expression, statement } from '../../../tests/utils';
11+
import { expression, statement, parse } from '../../../tests/utils';
1212

1313
describe('flowTypeHandler', () => {
1414
let getFlowTypeMock;
@@ -334,4 +334,39 @@ describe('flowTypeHandler', () => {
334334
`);
335335
});
336336
});
337+
338+
describe('forwardRef', () => {
339+
it('resolves prop type from function expression', () => {
340+
const src = `
341+
import React from 'react';
342+
type Props = { foo: string };
343+
React.forwardRef((props: Props, ref) => <div ref={ref}>{props.foo}</div>);
344+
`;
345+
flowTypeHandler(documentation, parse(src).get('body', 2, 'expression'));
346+
expect(documentation.descriptors).toEqual({
347+
foo: {
348+
flowType: {},
349+
required: true,
350+
description: '',
351+
},
352+
});
353+
});
354+
355+
it('resolves when the function is not inline', () => {
356+
const src = `
357+
import React from 'react';
358+
type Props = { foo: string };
359+
const ComponentImpl = (props: Props, ref) => <div ref={ref}>{props.foo}</div>;
360+
React.forwardRef(ComponentImpl);
361+
`;
362+
flowTypeHandler(documentation, parse(src).get('body', 3, 'expression'));
363+
expect(documentation.descriptors).toEqual({
364+
foo: {
365+
flowType: {},
366+
required: true,
367+
description: '',
368+
},
369+
});
370+
});
371+
});
337372
});

src/handlers/defaultPropsHandler.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ function getDefaultValue(path: NodePath) {
5151
function getStatelessPropsPath(componentDefinition): NodePath {
5252
const value = resolveToValue(componentDefinition);
5353
if (isReactForwardRefCall(value)) {
54-
const inner = value.get('arguments', 0);
54+
const inner = resolveToValue(value.get('arguments', 0));
5555
return inner.get('params', 0);
5656
}
5757
return value.get('params', 0);

src/utils/getFlowTypeFromReactComponent.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import resolveToValue from './resolveToValue';
1919
function getStatelessPropsPath(componentDefinition): NodePath {
2020
const value = resolveToValue(componentDefinition);
2121
if (isReactForwardRefCall(value)) {
22-
const inner = value.get('arguments', 0);
22+
const inner = resolveToValue(value.get('arguments', 0));
2323
return inner.get('params', 0);
2424
}
2525
return value.get('params', 0);

0 commit comments

Comments
 (0)