Skip to content

Commit

Permalink
fix: membership infering for methods (#1122)
Browse files Browse the repository at this point in the history
* Fix membership infering for methods

Improve inferMembership to allow to use next code:
```js
/**
 * @memberof BigFeature
 */
class MyClass {
  method() {}
}
```
Before this improvement, we have `method` inside
 `global.MyClass` instead of `global.BigFeature.MyClass`

* Add tests fir membership infering for methods
  • Loading branch information
NumminorihSF authored and tmcw committed Aug 23, 2018
1 parent 255d8ee commit 40b1783
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 1 deletion.
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

0 comments on commit 40b1783

Please sign in to comment.