diff --git a/__tests__/lib/infer/membership.js b/__tests__/lib/infer/membership.js index 22849a0a4..cccc71108 100644 --- a/__tests__/lib/infer/membership.js +++ b/__tests__/lib/infer/membership.js @@ -276,6 +276,38 @@ test('inferMembership - explicit', function() { scope: 'instance' }); + expect( + pick( + evaluate(function() { + /** @memberof bar */ + class Foo { + /** */ + baz() {} + } + })[1], // [0] is an description for class Foo + ['memberof', 'scope'] + ) + ).toEqual({ + memberof: 'bar.Foo', + scope: 'instance' + }); + + expect( + pick( + evaluate(function() { + /** @memberof bar */ + class Foo { + /** */ + static baz() {} + } + })[1], // [0] is an description for class Foo + ['memberof', 'scope'] + ) + ).toEqual({ + memberof: 'bar.Foo', + scope: 'static' + }); + expect( pick( evaluate(function() { diff --git a/src/infer/membership.js b/src/infer/membership.js index 438a3aec4..9494ce03b 100644 --- a/src/infer/membership.js +++ b/src/infer/membership.js @@ -39,6 +39,33 @@ function findLendsIdentifiers(path) { } } +/** + * Given an AST node, try to find a comment before the class declaration that + * has a `memberof` tag, and if it has that, return the tag, split by + * .s with the name of the class. + * + * @private + * @param {Object} path AST node + * @returns {Array} class membership + */ +function inferClassMembership(path) { + if (path.get('leadingComments')) { + const leadingComments = path.get('leadingComments'); + + for (let i = leadingComments.length - 1; i >= 0; i--) { + const comment = leadingComments[i]; + if (isJSDocComment(comment.node)) { + const memberof = parse(comment.node.value).memberof; + if (memberof) { + return [...memberof.split('.'), path.node.id.name]; + } + } + } + } + + return [path.node.id.name]; +} + /** * Extract and return the identifiers for expressions of * type this.foo @@ -347,7 +374,7 @@ module.exports = function() { return inferMembershipFromIdentifiers( comment, - [declarationNode.id.name], + inferClassMembership(path.parentPath.parentPath), scope ); }