Skip to content

Commit b469bb7

Browse files
arvtmcw
authored andcommitted
Fix the name of infer membership for non named export default (#508)
When inferring the membership of a class that is an unnamed default export we hit a null pointer exception. ```js export default class { /** */ member() {} } ``` Inferring the name for the class above fails Fixes #506
1 parent dc846b4 commit b469bb7

File tree

2 files changed

+50
-8
lines changed

2 files changed

+50
-8
lines changed

lib/infer/membership.js

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -157,19 +157,16 @@ module.exports = function () {
157157
var path = comment.context.ast;
158158
var identifiers;
159159

160-
/*
161-
* Deal with an oddity of espree: the jsdoc comment is attached to a different
162-
* node in the two expressions `a.b = c` vs `a.b = function () {}`.
163-
*/
160+
161+
// Deal with an oddity of espree: the jsdoc comment is attached to a different
162+
// node in the two expressions `a.b = c` vs `a.b = function () {}`.
164163
if (n.isExpressionStatement(path.node) &&
165164
n.isAssignmentExpression(path.node.expression) &&
166165
n.isMemberExpression(path.node.expression.left)) {
167166
path = path.get('expression').get('left');
168167
}
169168

170-
/*
171-
* Same as above but for `b: c` vs `b: function () {}`.
172-
*/
169+
// Same as above but for `b: c` vs `b: function () {}`.
173170
if (n.isObjectProperty(path.node) &&
174171
n.isIdentifier(path.node.key)) {
175172
path = path.get('key');
@@ -230,7 +227,15 @@ module.exports = function () {
230227
if (n.isExpression(path.parentPath.parentPath)) {
231228
identifiers = extractIdentifiers(path.parentPath.parentPath.parentPath.get('left'));
232229
} else {
233-
identifiers = [path.parentPath.parentPath.node.id.name];
230+
var declarationNode = path.parentPath.parentPath.node;
231+
if (!declarationNode.id) {
232+
// export default function () {}
233+
// export default class {}
234+
// Use module name instead.
235+
identifiers = [pathParse(comment.context.file).name];
236+
} else {
237+
identifiers = [declarationNode.id.name];
238+
}
234239
}
235240
var scope = 'instance';
236241
if (path.node.static == true) {

test/lib/infer/membership.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,3 +380,40 @@ test('inferMembership - https://github.com/documentationjs/documentation/issues/
380380

381381
t.end();
382382
});
383+
384+
test('inferMembership - export', function (t) {
385+
t.deepEqual(pick(evaluate(
386+
'export default class {' +
387+
' /** */' +
388+
' method() {}' +
389+
'}',
390+
'test-file'
391+
)[0], ['memberof', 'scope']), {
392+
memberof: 'test-file',
393+
scope: 'instance'
394+
});
395+
396+
t.deepEqual(pick(evaluate(
397+
'export default class C {' +
398+
' /** */' +
399+
' method() {}' +
400+
'}',
401+
'test-file'
402+
)[0], ['memberof', 'scope']), {
403+
memberof: 'C',
404+
scope: 'instance'
405+
});
406+
407+
t.deepEqual(pick(evaluate(
408+
'export class C {' +
409+
' /** */' +
410+
' method() {}' +
411+
'}',
412+
'test-file'
413+
)[0], ['memberof', 'scope']), {
414+
memberof: 'C',
415+
scope: 'instance'
416+
});
417+
418+
t.end();
419+
});

0 commit comments

Comments
 (0)