Skip to content

Commit 34b24a9

Browse files
committed
Find correct docblock for definitions in export statements
1 parent 76496f5 commit 34b24a9

File tree

2 files changed

+56
-1
lines changed

2 files changed

+56
-1
lines changed

src/handlers/__tests__/componentDocblockHandler-test.js

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,53 @@ describe('componentDocblockHandler', () => {
9595

9696
describe('ClassExpression', () => {
9797
test(
98-
'var Compoent = class {};',
98+
'var Component = class {};',
9999
src => lastStatement(src).get('declarations', 0, 'init')
100100
);
101101
});
102+
103+
describe('ES6 default exports', () => {
104+
105+
describe('Default React.createClass export', () => {
106+
test(
107+
'export default React.createClass({});',
108+
src => lastStatement(src).get('declaration', 'arguments', 0)
109+
);
110+
});
111+
112+
describe('Default class declaration export', () => {
113+
test(
114+
'export default class Component {}',
115+
src => lastStatement(src).get('declaration')
116+
);
117+
});
118+
119+
describe('Default class expression export', () => {
120+
test(
121+
'export default class {}',
122+
src => lastStatement(src).get('declaration')
123+
);
124+
});
125+
126+
});
127+
128+
describe('ES6 named exports', () => {
129+
130+
describe('Named React.createClass export', () => {
131+
test(
132+
'export var Component = React.createClass({});',
133+
src => lastStatement(src).get(
134+
'declaration', 'declarations', '0', 'init', 'arguments', 0
135+
)
136+
);
137+
});
138+
139+
describe('Named class declaration export', () => {
140+
test(
141+
'export class Component {}',
142+
src => lastStatement(src).get('declaration')
143+
);
144+
});
145+
146+
});
102147
});

src/handlers/componentDocblockHandler.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,16 @@ export default function componentDocblockHandler(
3030
path = path.parent;
3131
}
3232
if (path) {
33+
// Class declarations are statements but can be part of default
34+
// export declarations
35+
if (types.ClassDeclaration.check(path.node) &&
36+
types.ExportDefaultDeclaration.check(path.parentPath.node)) {
37+
path = path.parentPath;
38+
}
39+
// If the parent is an export statement, we have to traverse one more up
40+
if (types.ExportNamedDeclaration.check(path.parentPath.node)) {
41+
path = path.parentPath;
42+
}
3343
description = getDocblock(path);
3444
}
3545
if (description == null) {

0 commit comments

Comments
 (0)