Skip to content

fix: handle docblock on out-of-line forwardRef #435

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
28 changes: 28 additions & 0 deletions src/handlers/__tests__/componentDocblockHandler-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ describe('componentDocblockHandler', () => {
return programPath.get('body', programPath.node.body.length - 1);
}

function beforeLastStatement(src) {
const programPath = parse(src);
return programPath.get('body', programPath.node.body.length - 2);
}

beforeEach(() => {
documentation = new (require('../../Documentation'))();
componentDocblockHandler = require('../componentDocblockHandler').default;
Expand Down Expand Up @@ -228,4 +233,27 @@ describe('componentDocblockHandler', () => {
});
});
});

describe('forwardRef', () => {
describe('inline implementation', () => {
test(
[
'React.forwardRef((props, ref) => {});',
'import React from "react";',
].join('\n'),
src => beforeLastStatement(src).get('expression'),
);
});

describe('out of line implementation', () => {
test(
[
'let Component = (props, ref) => {};',
'React.forwardRef(Component);',
'import React from "react";',
].join('\n'),
src => beforeLastStatement(src).get('expression'),
);
});
});
});
28 changes: 20 additions & 8 deletions src/handlers/componentDocblockHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,15 @@
import { namedTypes as t } from 'ast-types';
import type Documentation from '../Documentation';
import { getDocblock } from '../utils/docblock';
import isReactForwardRefCall from '../utils/isReactForwardRefCall';
import resolveToValue from '../utils/resolveToValue';

function isClassDefinition(nodePath) {
const node = nodePath.node;
return t.ClassDeclaration.check(node) || t.ClassExpression.check(node);
}

/**
* Finds the nearest block comment before the component definition.
*/
export default function componentDocblockHandler(
documentation: Documentation,
path: NodePath,
) {
function getDocblockFromComponent(path) {
let description = null;

if (isClassDefinition(path)) {
Expand Down Expand Up @@ -52,5 +48,21 @@ export default function componentDocblockHandler(
description = getDocblock(searchPath);
}
}
documentation.set('description', description || '');
if (!description && isReactForwardRefCall(path)) {
const inner = resolveToValue(path.get('arguments', 0));
if (inner.node !== path.node) {
return getDocblockFromComponent(inner);
}
}
return description;
}

/**
* Finds the nearest block comment before the component definition.
*/
export default function componentDocblockHandler(
documentation: Documentation,
path: NodePath,
) {
documentation.set('description', getDocblockFromComponent(path) || '');
}