Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix membership infering for methods #1122

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions __tests__/lib/infer/membership.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
29 changes: 28 additions & 1 deletion src/infer/membership.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>} 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
Expand Down Expand Up @@ -347,7 +374,7 @@ module.exports = function() {

return inferMembershipFromIdentifiers(
comment,
[declarationNode.id.name],
inferClassMembership(path.parentPath.parentPath),
scope
);
}
Expand Down