Skip to content

Commit d71c3d2

Browse files
motiz88danez
authored andcommitted
fix: handle docblock on out-of-line forwardRef
1 parent f9d5490 commit d71c3d2

File tree

2 files changed

+48
-8
lines changed

2 files changed

+48
-8
lines changed

src/handlers/__tests__/componentDocblockHandler-test.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ describe('componentDocblockHandler', () => {
1919
return programPath.get('body', programPath.node.body.length - 1);
2020
}
2121

22+
function beforeLastStatement(src) {
23+
const programPath = parse(src);
24+
return programPath.get('body', programPath.node.body.length - 2);
25+
}
26+
2227
beforeEach(() => {
2328
documentation = new (require('../../Documentation'))();
2429
componentDocblockHandler = require('../componentDocblockHandler').default;
@@ -228,4 +233,27 @@ describe('componentDocblockHandler', () => {
228233
});
229234
});
230235
});
236+
237+
describe('forwardRef', () => {
238+
describe('inline implementation', () => {
239+
test(
240+
[
241+
'React.forwardRef((props, ref) => {});',
242+
'import React from "react";',
243+
].join('\n'),
244+
src => beforeLastStatement(src).get('expression'),
245+
);
246+
});
247+
248+
describe('out of line implementation', () => {
249+
test(
250+
[
251+
'let Component = (props, ref) => {};',
252+
'React.forwardRef(Component);',
253+
'import React from "react";',
254+
].join('\n'),
255+
src => beforeLastStatement(src).get('expression'),
256+
);
257+
});
258+
});
231259
});

src/handlers/componentDocblockHandler.js

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,15 @@
1010
import { namedTypes as t } from 'ast-types';
1111
import type Documentation from '../Documentation';
1212
import { getDocblock } from '../utils/docblock';
13+
import isReactForwardRefCall from '../utils/isReactForwardRefCall';
14+
import resolveToValue from '../utils/resolveToValue';
1315

1416
function isClassDefinition(nodePath) {
1517
const node = nodePath.node;
1618
return t.ClassDeclaration.check(node) || t.ClassExpression.check(node);
1719
}
1820

19-
/**
20-
* Finds the nearest block comment before the component definition.
21-
*/
22-
export default function componentDocblockHandler(
23-
documentation: Documentation,
24-
path: NodePath,
25-
) {
21+
function getDocblockFromComponent(path) {
2622
let description = null;
2723

2824
if (isClassDefinition(path)) {
@@ -52,5 +48,21 @@ export default function componentDocblockHandler(
5248
description = getDocblock(searchPath);
5349
}
5450
}
55-
documentation.set('description', description || '');
51+
if (!description && isReactForwardRefCall(path)) {
52+
const inner = resolveToValue(path.get('arguments', 0));
53+
if (inner.node !== path.node) {
54+
return getDocblockFromComponent(inner);
55+
}
56+
}
57+
return description;
58+
}
59+
60+
/**
61+
* Finds the nearest block comment before the component definition.
62+
*/
63+
export default function componentDocblockHandler(
64+
documentation: Documentation,
65+
path: NodePath,
66+
) {
67+
documentation.set('description', getDocblockFromComponent(path) || '');
5668
}

0 commit comments

Comments
 (0)