correctly handle classes that are the default export (#1113)

This commit is contained in:
Jeff Williams 2015-12-03 06:03:20 -08:00
parent 224e796428
commit 1bdcc52156
6 changed files with 45 additions and 1 deletions

View File

@ -330,7 +330,13 @@ var getInfo = exports.getInfo = function(node) {
// or "class" in: "export default class {}"
case Syntax.ClassDeclaration:
info.node = node;
info.name = node.id ? nodeToValue(node.id) : '';
// if this class is the default export, we need to use a special name
if (node.parent && node.parent.type === Syntax.ExportDefaultDeclaration) {
info.name = 'module.exports';
}
else {
info.name = node.id ? nodeToValue(node.id) : '';
}
info.type = info.node.type;
info.paramnames = [];

View File

@ -176,6 +176,11 @@ walkers[Syntax.ExportAllDeclaration] = function(node, parent, state, cb) {
};
walkers[Syntax.ExportDefaultDeclaration] = function(node, parent, state, cb) {
// if the declaration target is a class, move leading comments to the declaration target
if (node.declaration && node.declaration.type === Syntax.ClassDeclaration) {
moveComments(node, node.declaration);
}
if (node.declaration) {
cb(node.declaration, node, state);
}

4
test/fixtures/exportdefault.js vendored Normal file
View File

@ -0,0 +1,4 @@
/** @module test */
/** Test value */
export default 'foo';

7
test/fixtures/exportdefaultclass.js vendored Normal file
View File

@ -0,0 +1,7 @@
/** @module test */
/** Test class */
export default class Foo {
/** Test constructor */
constructor() {}
}

View File

@ -0,0 +1,11 @@
'use strict';
describe('export default', function() {
var docSet = jasmine.getDocSetFromFile('test/fixtures/exportdefault.js');
var member = docSet.getByLongname('module:test')[1];
it('should use the correct kind and description for the default export', function() {
expect(member.kind).toBe('member');
expect(member.description).toBe('Test value');
});
});

View File

@ -0,0 +1,11 @@
'use strict';
describe('export default class', function() {
var docSet = jasmine.getDocSetFromFile('test/fixtures/exportdefaultclass.js');
var klass = docSet.getByLongname('module:test')[2];
it('should combine the classdesc and constructor description into a single doclet', function() {
expect(klass.classdesc).toBe('Test class');
expect(klass.description).toBe('Test constructor');
});
});