-
-
Notifications
You must be signed in to change notification settings - Fork 291
/
componentDocblockHandler.js
56 lines (52 loc) · 1.71 KB
/
componentDocblockHandler.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
*/
import { namedTypes as t } from 'ast-types';
import type Documentation from '../Documentation';
import { getDocblock } from '../utils/docblock';
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,
) {
let description = null;
if (isClassDefinition(path)) {
// If we have a class declaration or expression, then the comment might be
// attached to the last decorator instead as trailing comment.
if (path.node.decorators && path.node.decorators.length > 0) {
description = getDocblock(
path.get('decorators', path.node.decorators.length - 1),
true,
);
}
}
if (description == null) {
// Find parent statement (e.g. var Component = React.createClass(<path>);)
let searchPath = path;
while (searchPath && !t.Statement.check(searchPath.node)) {
searchPath = searchPath.parent;
}
if (searchPath) {
// If the parent is an export statement, we have to traverse one more up
if (
t.ExportNamedDeclaration.check(searchPath.parentPath.node) ||
t.ExportDefaultDeclaration.check(searchPath.parentPath.node)
) {
searchPath = searchPath.parentPath;
}
description = getDocblock(searchPath);
}
}
documentation.set('description', description || '');
}