Skip to content

Commit 40b1783

Browse files
NumminorihSFtmcw
authored andcommitted
fix: membership infering for methods (#1122)
* 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
1 parent 255d8ee commit 40b1783

File tree

2 files changed

+60
-1
lines changed

2 files changed

+60
-1
lines changed

__tests__/lib/infer/membership.js

+32
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,38 @@ test('inferMembership - explicit', function() {
276276
scope: 'instance'
277277
});
278278

279+
expect(
280+
pick(
281+
evaluate(function() {
282+
/** @memberof bar */
283+
class Foo {
284+
/** */
285+
baz() {}
286+
}
287+
})[1], // [0] is an description for class Foo
288+
['memberof', 'scope']
289+
)
290+
).toEqual({
291+
memberof: 'bar.Foo',
292+
scope: 'instance'
293+
});
294+
295+
expect(
296+
pick(
297+
evaluate(function() {
298+
/** @memberof bar */
299+
class Foo {
300+
/** */
301+
static baz() {}
302+
}
303+
})[1], // [0] is an description for class Foo
304+
['memberof', 'scope']
305+
)
306+
).toEqual({
307+
memberof: 'bar.Foo',
308+
scope: 'static'
309+
});
310+
279311
expect(
280312
pick(
281313
evaluate(function() {

src/infer/membership.js

+28-1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,33 @@ function findLendsIdentifiers(path) {
3939
}
4040
}
4141

42+
/**
43+
* Given an AST node, try to find a comment before the class declaration that
44+
* has a `memberof` tag, and if it has that, return the tag, split by
45+
* .s with the name of the class.
46+
*
47+
* @private
48+
* @param {Object} path AST node
49+
* @returns {Array<string>} class membership
50+
*/
51+
function inferClassMembership(path) {
52+
if (path.get('leadingComments')) {
53+
const leadingComments = path.get('leadingComments');
54+
55+
for (let i = leadingComments.length - 1; i >= 0; i--) {
56+
const comment = leadingComments[i];
57+
if (isJSDocComment(comment.node)) {
58+
const memberof = parse(comment.node.value).memberof;
59+
if (memberof) {
60+
return [...memberof.split('.'), path.node.id.name];
61+
}
62+
}
63+
}
64+
}
65+
66+
return [path.node.id.name];
67+
}
68+
4269
/**
4370
* Extract and return the identifiers for expressions of
4471
* type this.foo
@@ -347,7 +374,7 @@ module.exports = function() {
347374

348375
return inferMembershipFromIdentifiers(
349376
comment,
350-
[declarationNode.id.name],
377+
inferClassMembership(path.parentPath.parentPath),
351378
scope
352379
);
353380
}

0 commit comments

Comments
 (0)