Skip to content

Commit

Permalink
use the correct scope when an ES 2015 class is a memberof something e…
Browse files Browse the repository at this point in the history
…lse, and the class has instance members (#994)
  • Loading branch information
hegemonic committed May 5, 2015
1 parent cfb04e3 commit faba1c8
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 3 deletions.
19 changes: 16 additions & 3 deletions lib/jsdoc/name.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,11 @@ function prototypeToPunc(name) {
exports.resolve = function(doclet) {
var about = {};
var memberof = doclet.memberof || '';
var metaName;
var name = doclet.name ? String(doclet.name) : '';

var parentDoc;
var puncAndName;
var puncAndNameIndex;

// change MyClass.prototype.instanceMethod to MyClass#instanceMethod
// (but not in function params, which lack doclet.kind)
Expand Down Expand Up @@ -167,10 +169,21 @@ exports.resolve = function(doclet) {
doclet.scope = puncToScope[RegExp.$1];
doclet.name = doclet.name.substr(1);
}
else {
doclet.scope = DEFAULT_SCOPE;
else if (doclet.meta.code && doclet.meta.code.name) {
// HACK: Handle cases where an ES 2015 class is a static memberof something else, and
// the class has instance members. In these cases, we have to detect the instance
// members' scope by looking at the meta info. There's almost certainly a better way to
// do this...
metaName = String(doclet.meta.code.name);
puncAndName = SCOPE.PUNC.INSTANCE + doclet.name;
puncAndNameIndex = metaName.indexOf(puncAndName);
if ( puncAndNameIndex !== -1 &&
(puncAndNameIndex === metaName.length - puncAndName.length) ) {
doclet.scope = SCOPE.NAMES.INSTANCE;
}
}

doclet.scope = doclet.scope || DEFAULT_SCOPE;
doclet.setLongname(doclet.memberof + scopeToPunc[doclet.scope] + doclet.name);
}

Expand Down
6 changes: 6 additions & 0 deletions test/fixtures/classtag2.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,9 @@ subclasses.ExpiringSubscription = class ExpiringSubscription {
*/
constructor(name) {}
}

/** @memberof subclasses */
class InvalidSubscription {
/** Instance method. */
foo() {}
}
7 changes: 7 additions & 0 deletions test/specs/tags/classtag.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ describe('@class tag', function() {
var subscriber = docSet2.getByLongname('Subscriber')[0];
var hasCallback = docSet2.getByLongname('Subscriber#hasCallback')[0];
var expiringSubscription = docSet2.getByLongname('subclasses.ExpiringSubscription')[0];
var invalidSubscriptionFoo = docSet2.getByLongname('subclasses.InvalidSubscription#foo')[0];

it('When a symbol is a class declaration, the doclet does not require the @class tag', function() {
expect(subscription.kind).toBe('class');
Expand Down Expand Up @@ -64,6 +65,12 @@ describe('@class tag', function() {
expect(expiringSubscription.name).toBe('ExpiringSubscription');
expect(expiringSubscription.params[0].name).toBe('name');
});

it('When a class is a static memberof something else, the class\' instance methods have the correct scope', function() {
expect(invalidSubscriptionFoo.kind).toBe('function');
expect(invalidSubscriptionFoo.name).toBe('foo');
expect(invalidSubscriptionFoo.scope).toBe('instance');
});
});
}
});

0 comments on commit faba1c8

Please sign in to comment.