Skip to content

Commit 0b8546a

Browse files
committed
Fix docblock handler for (default) exported named functions (fixes #51)
1 parent 3b3390d commit 0b8546a

File tree

2 files changed

+67
-7
lines changed

2 files changed

+67
-7
lines changed

src/handlers/__tests__/componentDocblockHandler-test.js

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,21 @@ describe('componentDocblockHandler', () => {
151151
);
152152
});
153153

154+
describe('Stateless functions', () => {
155+
test(
156+
'function Component() {}',
157+
src => lastStatement(src)
158+
);
159+
test(
160+
'var Component = function () {};',
161+
src => lastStatement(src).get('declarations', 0, 'init')
162+
);
163+
test(
164+
'var Component = () => {}',
165+
src => lastStatement(src).get('declarations', 0, 'init')
166+
);
167+
});
168+
154169
describe('ES6 default exports', () => {
155170

156171
describe('Default React.createClass export', () => {
@@ -182,6 +197,31 @@ describe('componentDocblockHandler', () => {
182197
);
183198
});
184199

200+
describe('Default stateless function export', () => {
201+
202+
describe('named function', () => {
203+
test(
204+
'export default function Component() {}',
205+
src => lastStatement(src).get('declaration')
206+
);
207+
});
208+
209+
describe('anonymous function', () => {
210+
test(
211+
'export default function() {}',
212+
src => lastStatement(src).get('declaration')
213+
);
214+
});
215+
216+
describe('arrow function', () => {
217+
test(
218+
'export default () => {}',
219+
src => lastStatement(src).get('declaration')
220+
);
221+
});
222+
223+
});
224+
185225
});
186226

187227
describe('ES6 named exports', () => {
@@ -206,5 +246,30 @@ describe('componentDocblockHandler', () => {
206246
);
207247
});
208248

249+
describe('Named stateless function', () => {
250+
251+
describe('named function', () => {
252+
test(
253+
'export function Component() {}',
254+
src => lastStatement(src).get('declaration')
255+
);
256+
});
257+
258+
describe('anonymous function', () => {
259+
test(
260+
'export var Component = function() {}',
261+
src => lastStatement(src).get('declaration')
262+
);
263+
});
264+
265+
describe('arrow function', () => {
266+
test(
267+
'export var Component = () => {}',
268+
src => lastStatement(src).get('declaration')
269+
);
270+
});
271+
272+
});
273+
209274
});
210275
});

src/handlers/componentDocblockHandler.js

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,9 @@ export default function componentDocblockHandler(
3737
searchPath = searchPath.parent;
3838
}
3939
if (searchPath) {
40-
// Class declarations are statements but can be part of default
41-
// export declarations
42-
if (types.ClassDeclaration.check(searchPath.node) &&
43-
types.ExportDefaultDeclaration.check(searchPath.parentPath.node)) {
44-
searchPath = searchPath.parentPath;
45-
}
4640
// If the parent is an export statement, we have to traverse one more up
47-
if (types.ExportNamedDeclaration.check(searchPath.parentPath.node)) {
41+
if (types.ExportNamedDeclaration.check(searchPath.parentPath.node) ||
42+
types.ExportDefaultDeclaration.check(searchPath.parentPath.node)) {
4843
searchPath = searchPath.parentPath;
4944
}
5045
description = getDocblock(searchPath);

0 commit comments

Comments
 (0)