Skip to content

Commit 1eee5a8

Browse files
committed
Infer class kind from es6 class
1 parent d53a9de commit 1eee5a8

13 files changed

+1388
-6
lines changed

lib/hierarchy.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ module.exports = function (comments) {
4444
scope = segment[0],
4545
name = segment[1];
4646

47-
if (!node.members[scope][name]) {
47+
if (!node.members[scope].hasOwnProperty(name)) {
4848
node.members[scope][name] = {
4949
comments: [],
5050
members: {

lib/infer/kind.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ module.exports = function () {
2828
}
2929

3030
types.visit(comment.context.ast, {
31+
visitClassDeclaration: function () {
32+
comment.kind = 'class';
33+
this.abort();
34+
},
3135
visitFunction: function (path) {
3236
if (path.value && path.value.id && path.value.id.name && !!/^[A-Z]/.exec(path.value.id.name)) {
3337
comment.kind = 'class';

lib/infer/membership.js

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,12 @@ module.exports = function () {
9999
*
100100
* @param {Object} comment comment for which to infer memberships
101101
* @param {Array<string>} identifiers array of identifier names
102+
* @param {string} explicitScope if derived from an es6 class, whether or
103+
* not this method had the static keyword
102104
* @returns {undefined} mutates `comment`
103105
* @private
104106
*/
105-
function inferMembershipFromIdentifiers(comment, identifiers) {
107+
function inferMembershipFromIdentifiers(comment, identifiers, explicitScope) {
106108
if (identifiers.length === 1 && identifiers[0] === 'module' && comment.name === 'exports') {
107109
comment.name = inferModuleName(currentModule || comment);
108110
return;
@@ -123,7 +125,11 @@ module.exports = function () {
123125
comment.scope = 'instance';
124126
} else {
125127
comment.memberof = identifiers.join('.');
126-
comment.scope = 'static';
128+
if (explicitScope !== undefined) {
129+
comment.scope = explicitScope;
130+
} else {
131+
comment.scope = 'static';
132+
}
127133
}
128134
}
129135

@@ -206,6 +212,18 @@ module.exports = function () {
206212
inferMembershipFromIdentifiers(comment, identifiers);
207213
}
208214

215+
// class Foo { bar() { } }
216+
if (n.MethodDefinition.check(path.node) &&
217+
n.ClassBody.check(path.parent.node) &&
218+
n.ClassDeclaration.check(path.parent.parent.node)) {
219+
identifiers = [path.parent.parent.node.id.name];
220+
var scope = 'instance';
221+
if (path.node.static == true) {
222+
scope = 'static';
223+
}
224+
inferMembershipFromIdentifiers(comment, identifiers, scope);
225+
}
226+
209227
return comment;
210228
};
211229
}

test/fixture/bad/syntax.output.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@
66
},
77
"_babel": true,
88
"codeFrame": "> 1 | *\n | ^\n 2 | "
9-
}
9+
}

test/fixture/es6-import.output.custom.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,37 @@
1+
# Sink
2+
3+
This is a sink
4+
5+
6+
## constructor
7+
8+
9+
10+
11+
**Parameters**
12+
13+
- `height` **number** the height of the thing
14+
15+
- `width` **number** the width of the thing
16+
17+
18+
19+
20+
## empty
21+
22+
Is it empty
23+
24+
25+
26+
27+
## hello
28+
29+
This method says hello
30+
31+
32+
33+
34+
135
# multiply
236

337
This function returns the number one.

test/fixture/es6-import.output.json

Lines changed: 203 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,206 @@
11
[
2+
{
3+
"description": "This is a sink",
4+
"tags": [],
5+
"loc": {
6+
"start": {
7+
"line": 7,
8+
"column": 0
9+
},
10+
"end": {
11+
"line": 9,
12+
"column": 3
13+
}
14+
},
15+
"context": {
16+
"loc": {
17+
"start": {
18+
"line": 10,
19+
"column": 0
20+
},
21+
"end": {
22+
"line": 33,
23+
"column": 1
24+
}
25+
},
26+
"code": "/**\n * This function returns the number one.\n * @returns {Number} numberone\n */\nvar multiply = (a, b) => a * b;\n\n/**\n * This is a sink\n */\nclass Sink {\n /**\n * Is it empty\n */\n empty() {\n return 1;\n }\n\n /**\n * This method says hello\n */\n static hello() {\n return 'hello';\n }\n\n /**\n * @param {number} height the height of the thing\n * @param {number} width the width of the thing\n */\n constructor(height, width) {\n this.height = height;\n this.width = width;\n }\n}\n\nexport default multiply;\n"
27+
},
28+
"errors": [],
29+
"name": "Sink",
30+
"kind": "class",
31+
"members": {
32+
"instance": [
33+
{
34+
"description": "",
35+
"tags": [
36+
{
37+
"title": "param",
38+
"description": "the height of the thing",
39+
"lineNumber": 1,
40+
"type": {
41+
"type": "NameExpression",
42+
"name": "number"
43+
},
44+
"name": "height"
45+
},
46+
{
47+
"title": "param",
48+
"description": "the width of the thing",
49+
"lineNumber": 2,
50+
"type": {
51+
"type": "NameExpression",
52+
"name": "number"
53+
},
54+
"name": "width"
55+
}
56+
],
57+
"loc": {
58+
"start": {
59+
"line": 25,
60+
"column": 2
61+
},
62+
"end": {
63+
"line": 28,
64+
"column": 5
65+
}
66+
},
67+
"context": {
68+
"loc": {
69+
"start": {
70+
"line": 29,
71+
"column": 2
72+
},
73+
"end": {
74+
"line": 32,
75+
"column": 3
76+
}
77+
},
78+
"code": "/**\n * This function returns the number one.\n * @returns {Number} numberone\n */\nvar multiply = (a, b) => a * b;\n\n/**\n * This is a sink\n */\nclass Sink {\n /**\n * Is it empty\n */\n empty() {\n return 1;\n }\n\n /**\n * This method says hello\n */\n static hello() {\n return 'hello';\n }\n\n /**\n * @param {number} height the height of the thing\n * @param {number} width the width of the thing\n */\n constructor(height, width) {\n this.height = height;\n this.width = width;\n }\n}\n\nexport default multiply;\n"
79+
},
80+
"errors": [],
81+
"params": [
82+
{
83+
"title": "param",
84+
"description": "the height of the thing",
85+
"lineNumber": 1,
86+
"type": {
87+
"type": "NameExpression",
88+
"name": "number"
89+
},
90+
"name": "height"
91+
},
92+
{
93+
"title": "param",
94+
"description": "the width of the thing",
95+
"lineNumber": 2,
96+
"type": {
97+
"type": "NameExpression",
98+
"name": "number"
99+
},
100+
"name": "width"
101+
}
102+
],
103+
"name": "constructor",
104+
"kind": "function",
105+
"memberof": "Sink",
106+
"scope": "instance",
107+
"members": {
108+
"instance": [],
109+
"static": []
110+
},
111+
"path": [
112+
"Sink",
113+
"constructor"
114+
]
115+
},
116+
{
117+
"description": "Is it empty",
118+
"tags": [],
119+
"loc": {
120+
"start": {
121+
"line": 11,
122+
"column": 2
123+
},
124+
"end": {
125+
"line": 13,
126+
"column": 5
127+
}
128+
},
129+
"context": {
130+
"loc": {
131+
"start": {
132+
"line": 14,
133+
"column": 2
134+
},
135+
"end": {
136+
"line": 16,
137+
"column": 3
138+
}
139+
},
140+
"code": "/**\n * This function returns the number one.\n * @returns {Number} numberone\n */\nvar multiply = (a, b) => a * b;\n\n/**\n * This is a sink\n */\nclass Sink {\n /**\n * Is it empty\n */\n empty() {\n return 1;\n }\n\n /**\n * This method says hello\n */\n static hello() {\n return 'hello';\n }\n\n /**\n * @param {number} height the height of the thing\n * @param {number} width the width of the thing\n */\n constructor(height, width) {\n this.height = height;\n this.width = width;\n }\n}\n\nexport default multiply;\n"
141+
},
142+
"errors": [],
143+
"name": "empty",
144+
"kind": "function",
145+
"memberof": "Sink",
146+
"scope": "instance",
147+
"members": {
148+
"instance": [],
149+
"static": []
150+
},
151+
"path": [
152+
"Sink",
153+
"empty"
154+
]
155+
}
156+
],
157+
"static": [
158+
{
159+
"description": "This method says hello",
160+
"tags": [],
161+
"loc": {
162+
"start": {
163+
"line": 18,
164+
"column": 2
165+
},
166+
"end": {
167+
"line": 20,
168+
"column": 5
169+
}
170+
},
171+
"context": {
172+
"loc": {
173+
"start": {
174+
"line": 21,
175+
"column": 2
176+
},
177+
"end": {
178+
"line": 23,
179+
"column": 3
180+
}
181+
},
182+
"code": "/**\n * This function returns the number one.\n * @returns {Number} numberone\n */\nvar multiply = (a, b) => a * b;\n\n/**\n * This is a sink\n */\nclass Sink {\n /**\n * Is it empty\n */\n empty() {\n return 1;\n }\n\n /**\n * This method says hello\n */\n static hello() {\n return 'hello';\n }\n\n /**\n * @param {number} height the height of the thing\n * @param {number} width the width of the thing\n */\n constructor(height, width) {\n this.height = height;\n this.width = width;\n }\n}\n\nexport default multiply;\n"
183+
},
184+
"errors": [],
185+
"name": "hello",
186+
"kind": "function",
187+
"memberof": "Sink",
188+
"scope": "static",
189+
"members": {
190+
"instance": [],
191+
"static": []
192+
},
193+
"path": [
194+
"Sink",
195+
"hello"
196+
]
197+
}
198+
]
199+
},
200+
"path": [
201+
"Sink"
202+
]
203+
},
2204
{
3205
"description": "This function returns the number one.",
4206
"tags": [
@@ -33,7 +235,7 @@
33235
"column": 31
34236
}
35237
},
36-
"code": "/**\n * This function returns the number one.\n * @returns {Number} numberone\n */\nvar multiply = (a, b) => a * b;\n\nexport default multiply;\n"
238+
"code": "/**\n * This function returns the number one.\n * @returns {Number} numberone\n */\nvar multiply = (a, b) => a * b;\n\n/**\n * This is a sink\n */\nclass Sink {\n /**\n * Is it empty\n */\n empty() {\n return 1;\n }\n\n /**\n * This method says hello\n */\n static hello() {\n return 'hello';\n }\n\n /**\n * @param {number} height the height of the thing\n * @param {number} width the width of the thing\n */\n constructor(height, width) {\n this.height = height;\n this.width = width;\n }\n}\n\nexport default multiply;\n"
37239
},
38240
"errors": [],
39241
"returns": [

test/fixture/es6-import.output.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,37 @@
1+
# Sink
2+
3+
This is a sink
4+
5+
6+
## constructor
7+
8+
9+
10+
11+
**Parameters**
12+
13+
- `height` **number** the height of the thing
14+
15+
- `width` **number** the width of the thing
16+
17+
18+
19+
20+
## empty
21+
22+
Is it empty
23+
24+
25+
26+
27+
## hello
28+
29+
This method says hello
30+
31+
32+
33+
34+
135
# multiply
236

337
This function returns the number one.

0 commit comments

Comments
 (0)