Skip to content
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 numberDiff line numberDiff 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 DownExpand 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 numberDiff line numberDiff 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 DownExpand 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) || '');
}