Skip to content

Commit 1c48a4f

Browse files
authored
Fix membership assignment for old-fashioned prototype members (#599)
Fixes #598 This _changes behavior_: previously in the case of ```js /** base */ var Foo = function Bar() { { /** */ this.baz = 0; } }; ``` We assigned baz to Bar. Now we will assign Baz to Foo. I believe this is right, since in JavaScript Bar is not actually bound in this code.
1 parent fc58beb commit 1c48a4f

File tree

6 files changed

+654
-4
lines changed

6 files changed

+654
-4
lines changed

lib/infer/membership.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,18 @@ function extractThis(path) {
5959
identifiers.push(scope.path.parentPath.parentPath.node.id.name, 'prototype');
6060
}
6161

62-
if (n.isFunctionDeclaration(scope.block) ||
63-
n.isFunctionExpression(scope.block)) {
64-
identifiers.push(scope.block.id.name , 'prototype');
62+
if (n.isFunctionDeclaration(scope.block)) {
63+
identifiers.push(scope.block.id.name, 'prototype');
64+
} else if (n.isFunctionExpression(scope.block)) {
65+
if (n.isVariableDeclarator(scope.path.parentPath)) {
66+
/** var Bar = function(foo) { this.foo = foo; }; */
67+
identifiers = identifiers
68+
.concat(scope.path.parentPath.get('id').node.name).concat('prototype');
69+
} else if (n.isAssignmentExpression(scope.path.parentPath)) {
70+
/** this.Bar = function(foo) { this.foo = foo; }; */
71+
identifiers = identifiers
72+
.concat(extractIdentifiers(scope.path.parentPath.get('left'))).concat('prototype');
73+
}
6574
}
6675
}
6776
});

test/fixture/this-class.input.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/** @module bookshelf */
2+
3+
/** @class */
4+
var Book = function(title) {
5+
/** The title of the book. */
6+
this.title = title;
7+
}
8+
9+
/** @class */
10+
this.BookShelf = function(title) {
11+
/** The title of the bookshelf. */
12+
this.title = title;
13+
}

test/fixture/this-class.output.json

Lines changed: 361 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,361 @@
1+
[
2+
{
3+
"description": "",
4+
"tags": [
5+
{
6+
"title": "module",
7+
"description": null,
8+
"lineNumber": 0,
9+
"type": null,
10+
"name": "bookshelf"
11+
}
12+
],
13+
"loc": {
14+
"start": {
15+
"line": 1,
16+
"column": 0
17+
},
18+
"end": {
19+
"line": 1,
20+
"column": 24
21+
}
22+
},
23+
"context": {
24+
"loc": {
25+
"start": {
26+
"line": 4,
27+
"column": 0
28+
},
29+
"end": {
30+
"line": 7,
31+
"column": 1
32+
}
33+
}
34+
},
35+
"errors": [],
36+
"kind": "module",
37+
"name": "bookshelf",
38+
"params": [
39+
{
40+
"title": "param",
41+
"name": "title",
42+
"lineNumber": 4
43+
}
44+
],
45+
"members": {
46+
"instance": [],
47+
"static": []
48+
},
49+
"path": [
50+
{
51+
"name": "bookshelf",
52+
"kind": "module"
53+
}
54+
],
55+
"namespace": "bookshelf"
56+
},
57+
{
58+
"description": "",
59+
"tags": [
60+
{
61+
"title": "class",
62+
"description": null,
63+
"lineNumber": 0,
64+
"name": null
65+
}
66+
],
67+
"loc": {
68+
"start": {
69+
"line": 3,
70+
"column": 0
71+
},
72+
"end": {
73+
"line": 3,
74+
"column": 13
75+
}
76+
},
77+
"context": {
78+
"loc": {
79+
"start": {
80+
"line": 4,
81+
"column": 0
82+
},
83+
"end": {
84+
"line": 7,
85+
"column": 1
86+
}
87+
}
88+
},
89+
"errors": [],
90+
"kind": "class",
91+
"name": "Book",
92+
"params": [
93+
{
94+
"title": "param",
95+
"name": "title",
96+
"lineNumber": 4
97+
}
98+
],
99+
"members": {
100+
"instance": [
101+
{
102+
"description": {
103+
"type": "root",
104+
"children": [
105+
{
106+
"type": "paragraph",
107+
"children": [
108+
{
109+
"type": "text",
110+
"value": "The title of the book.",
111+
"position": {
112+
"start": {
113+
"line": 1,
114+
"column": 1,
115+
"offset": 0
116+
},
117+
"end": {
118+
"line": 1,
119+
"column": 23,
120+
"offset": 22
121+
},
122+
"indent": []
123+
}
124+
}
125+
],
126+
"position": {
127+
"start": {
128+
"line": 1,
129+
"column": 1,
130+
"offset": 0
131+
},
132+
"end": {
133+
"line": 1,
134+
"column": 23,
135+
"offset": 22
136+
},
137+
"indent": []
138+
}
139+
}
140+
],
141+
"position": {
142+
"start": {
143+
"line": 1,
144+
"column": 1,
145+
"offset": 0
146+
},
147+
"end": {
148+
"line": 1,
149+
"column": 23,
150+
"offset": 22
151+
}
152+
}
153+
},
154+
"tags": [],
155+
"loc": {
156+
"start": {
157+
"line": 5,
158+
"column": 4
159+
},
160+
"end": {
161+
"line": 5,
162+
"column": 33
163+
}
164+
},
165+
"context": {
166+
"loc": {
167+
"start": {
168+
"line": 6,
169+
"column": 3
170+
},
171+
"end": {
172+
"line": 6,
173+
"column": 22
174+
}
175+
}
176+
},
177+
"errors": [],
178+
"name": "title",
179+
"memberof": "Book",
180+
"scope": "instance",
181+
"members": {
182+
"instance": [],
183+
"static": []
184+
},
185+
"path": [
186+
{
187+
"name": "Book",
188+
"kind": "class"
189+
},
190+
{
191+
"name": "title",
192+
"scope": "instance"
193+
}
194+
],
195+
"namespace": "Book#title"
196+
}
197+
],
198+
"static": [],
199+
"events": []
200+
},
201+
"path": [
202+
{
203+
"name": "Book",
204+
"kind": "class"
205+
}
206+
],
207+
"namespace": "Book"
208+
},
209+
{
210+
"description": "",
211+
"tags": [
212+
{
213+
"title": "class",
214+
"description": null,
215+
"lineNumber": 0,
216+
"name": null
217+
}
218+
],
219+
"loc": {
220+
"start": {
221+
"line": 9,
222+
"column": 0
223+
},
224+
"end": {
225+
"line": 9,
226+
"column": 13
227+
}
228+
},
229+
"context": {
230+
"loc": {
231+
"start": {
232+
"line": 10,
233+
"column": 0
234+
},
235+
"end": {
236+
"line": 13,
237+
"column": 1
238+
}
239+
}
240+
},
241+
"errors": [],
242+
"kind": "class",
243+
"name": "BookShelf",
244+
"params": [
245+
{
246+
"title": "param",
247+
"name": "title",
248+
"lineNumber": 10
249+
}
250+
],
251+
"members": {
252+
"instance": [
253+
{
254+
"description": {
255+
"type": "root",
256+
"children": [
257+
{
258+
"type": "paragraph",
259+
"children": [
260+
{
261+
"type": "text",
262+
"value": "The title of the bookshelf.",
263+
"position": {
264+
"start": {
265+
"line": 1,
266+
"column": 1,
267+
"offset": 0
268+
},
269+
"end": {
270+
"line": 1,
271+
"column": 28,
272+
"offset": 27
273+
},
274+
"indent": []
275+
}
276+
}
277+
],
278+
"position": {
279+
"start": {
280+
"line": 1,
281+
"column": 1,
282+
"offset": 0
283+
},
284+
"end": {
285+
"line": 1,
286+
"column": 28,
287+
"offset": 27
288+
},
289+
"indent": []
290+
}
291+
}
292+
],
293+
"position": {
294+
"start": {
295+
"line": 1,
296+
"column": 1,
297+
"offset": 0
298+
},
299+
"end": {
300+
"line": 1,
301+
"column": 28,
302+
"offset": 27
303+
}
304+
}
305+
},
306+
"tags": [],
307+
"loc": {
308+
"start": {
309+
"line": 11,
310+
"column": 4
311+
},
312+
"end": {
313+
"line": 11,
314+
"column": 38
315+
}
316+
},
317+
"context": {
318+
"loc": {
319+
"start": {
320+
"line": 12,
321+
"column": 3
322+
},
323+
"end": {
324+
"line": 12,
325+
"column": 22
326+
}
327+
}
328+
},
329+
"errors": [],
330+
"name": "title",
331+
"memberof": "BookShelf",
332+
"scope": "instance",
333+
"members": {
334+
"instance": [],
335+
"static": []
336+
},
337+
"path": [
338+
{
339+
"name": "BookShelf",
340+
"kind": "class"
341+
},
342+
{
343+
"name": "title",
344+
"scope": "instance"
345+
}
346+
],
347+
"namespace": "BookShelf#title"
348+
}
349+
],
350+
"static": [],
351+
"events": []
352+
},
353+
"path": [
354+
{
355+
"name": "BookShelf",
356+
"kind": "class"
357+
}
358+
],
359+
"namespace": "BookShelf"
360+
}
361+
]

0 commit comments

Comments
 (0)