Skip to content
This repository was archived by the owner on Jan 19, 2019. It is now read-only.
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 28d1344

Browse files
committedFeb 7, 2017
Fix: Calculate range correctly for exported generic class (fixes #152)
When a class with a generic definition is exported we must take the type parameters into account when finding the opening brace. This continues the fix of pull request #153 and handles the case of exporting a class with generics. We now find the last type parameter and use the succeeding token as the opening brace.
1 parent 11d5a7d commit 28d1344

9 files changed

+1017
-0
lines changed
 

‎lib/ast-converter.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1465,6 +1465,13 @@ module.exports = function(ast, extra) {
14651465
var lastClassToken = heritageClauses.length ? heritageClauses[heritageClauses.length - 1] : node.name;
14661466
var classNodeType = SyntaxKind[node.kind];
14671467

1468+
if (node.typeParameters && node.typeParameters.length) {
1469+
var lastTypeParameter = node.typeParameters[node.typeParameters.length - 1];
1470+
if (!lastClassToken || lastTypeParameter.pos > lastClassToken.pos) {
1471+
lastClassToken = ts.findNextToken(lastTypeParameter, ast);
1472+
}
1473+
}
1474+
14681475
if (node.modifiers && node.modifiers.length) {
14691476

14701477
/**
Lines changed: 222 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,222 @@
1+
module.exports = {
2+
"type": "Program",
3+
"range": [
4+
0,
5+
28
6+
],
7+
"loc": {
8+
"start": {
9+
"line": 1,
10+
"column": 0
11+
},
12+
"end": {
13+
"line": 3,
14+
"column": 1
15+
}
16+
},
17+
"body": [
18+
{
19+
"type": "ExportDefaultDeclaration",
20+
"declaration": {
21+
"type": "ClassDeclaration",
22+
"range": [
23+
15,
24+
28
25+
],
26+
"loc": {
27+
"start": {
28+
"line": 1,
29+
"column": 15
30+
},
31+
"end": {
32+
"line": 3,
33+
"column": 1
34+
}
35+
},
36+
"id": null,
37+
"body": {
38+
"type": "ClassBody",
39+
"body": [],
40+
"range": [
41+
24,
42+
28
43+
],
44+
"loc": {
45+
"start": {
46+
"line": 1,
47+
"column": 24
48+
},
49+
"end": {
50+
"line": 3,
51+
"column": 1
52+
}
53+
}
54+
},
55+
"superClass": null,
56+
"implements": [],
57+
"decorators": []
58+
},
59+
"range": [
60+
0,
61+
28
62+
],
63+
"loc": {
64+
"start": {
65+
"line": 1,
66+
"column": 0
67+
},
68+
"end": {
69+
"line": 3,
70+
"column": 1
71+
}
72+
}
73+
}
74+
],
75+
"sourceType": "module",
76+
"tokens": [
77+
{
78+
"type": "Keyword",
79+
"value": "export",
80+
"range": [
81+
0,
82+
6
83+
],
84+
"loc": {
85+
"start": {
86+
"line": 1,
87+
"column": 0
88+
},
89+
"end": {
90+
"line": 1,
91+
"column": 6
92+
}
93+
}
94+
},
95+
{
96+
"type": "Keyword",
97+
"value": "default",
98+
"range": [
99+
7,
100+
14
101+
],
102+
"loc": {
103+
"start": {
104+
"line": 1,
105+
"column": 7
106+
},
107+
"end": {
108+
"line": 1,
109+
"column": 14
110+
}
111+
}
112+
},
113+
{
114+
"type": "Keyword",
115+
"value": "class",
116+
"range": [
117+
15,
118+
20
119+
],
120+
"loc": {
121+
"start": {
122+
"line": 1,
123+
"column": 15
124+
},
125+
"end": {
126+
"line": 1,
127+
"column": 20
128+
}
129+
}
130+
},
131+
{
132+
"type": "Punctuator",
133+
"value": "<",
134+
"range": [
135+
20,
136+
21
137+
],
138+
"loc": {
139+
"start": {
140+
"line": 1,
141+
"column": 20
142+
},
143+
"end": {
144+
"line": 1,
145+
"column": 21
146+
}
147+
}
148+
},
149+
{
150+
"type": "Identifier",
151+
"value": "T",
152+
"range": [
153+
21,
154+
22
155+
],
156+
"loc": {
157+
"start": {
158+
"line": 1,
159+
"column": 21
160+
},
161+
"end": {
162+
"line": 1,
163+
"column": 22
164+
}
165+
}
166+
},
167+
{
168+
"type": "Punctuator",
169+
"value": ">",
170+
"range": [
171+
22,
172+
23
173+
],
174+
"loc": {
175+
"start": {
176+
"line": 1,
177+
"column": 22
178+
},
179+
"end": {
180+
"line": 1,
181+
"column": 23
182+
}
183+
}
184+
},
185+
{
186+
"type": "Punctuator",
187+
"value": "{",
188+
"range": [
189+
24,
190+
25
191+
],
192+
"loc": {
193+
"start": {
194+
"line": 1,
195+
"column": 24
196+
},
197+
"end": {
198+
"line": 1,
199+
"column": 25
200+
}
201+
}
202+
},
203+
{
204+
"type": "Punctuator",
205+
"value": "}",
206+
"range": [
207+
27,
208+
28
209+
],
210+
"loc": {
211+
"start": {
212+
"line": 3,
213+
"column": 0
214+
},
215+
"end": {
216+
"line": 3,
217+
"column": 1
218+
}
219+
}
220+
}
221+
]
222+
};
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export default class<T> {
2+
3+
}
Lines changed: 258 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,258 @@
1+
module.exports = {
2+
"type": "Program",
3+
"range": [
4+
0,
5+
31
6+
],
7+
"loc": {
8+
"start": {
9+
"line": 1,
10+
"column": 0
11+
},
12+
"end": {
13+
"line": 3,
14+
"column": 1
15+
}
16+
},
17+
"body": [
18+
{
19+
"type": "ExportDefaultDeclaration",
20+
"declaration": {
21+
"type": "ClassDeclaration",
22+
"range": [
23+
15,
24+
31
25+
],
26+
"loc": {
27+
"start": {
28+
"line": 1,
29+
"column": 15
30+
},
31+
"end": {
32+
"line": 3,
33+
"column": 1
34+
}
35+
},
36+
"id": null,
37+
"body": {
38+
"type": "ClassBody",
39+
"body": [],
40+
"range": [
41+
27,
42+
31
43+
],
44+
"loc": {
45+
"start": {
46+
"line": 1,
47+
"column": 27
48+
},
49+
"end": {
50+
"line": 3,
51+
"column": 1
52+
}
53+
}
54+
},
55+
"superClass": null,
56+
"implements": [],
57+
"decorators": []
58+
},
59+
"range": [
60+
0,
61+
31
62+
],
63+
"loc": {
64+
"start": {
65+
"line": 1,
66+
"column": 0
67+
},
68+
"end": {
69+
"line": 3,
70+
"column": 1
71+
}
72+
}
73+
}
74+
],
75+
"sourceType": "module",
76+
"tokens": [
77+
{
78+
"type": "Keyword",
79+
"value": "export",
80+
"range": [
81+
0,
82+
6
83+
],
84+
"loc": {
85+
"start": {
86+
"line": 1,
87+
"column": 0
88+
},
89+
"end": {
90+
"line": 1,
91+
"column": 6
92+
}
93+
}
94+
},
95+
{
96+
"type": "Keyword",
97+
"value": "default",
98+
"range": [
99+
7,
100+
14
101+
],
102+
"loc": {
103+
"start": {
104+
"line": 1,
105+
"column": 7
106+
},
107+
"end": {
108+
"line": 1,
109+
"column": 14
110+
}
111+
}
112+
},
113+
{
114+
"type": "Keyword",
115+
"value": "class",
116+
"range": [
117+
15,
118+
20
119+
],
120+
"loc": {
121+
"start": {
122+
"line": 1,
123+
"column": 15
124+
},
125+
"end": {
126+
"line": 1,
127+
"column": 20
128+
}
129+
}
130+
},
131+
{
132+
"type": "Punctuator",
133+
"value": "<",
134+
"range": [
135+
20,
136+
21
137+
],
138+
"loc": {
139+
"start": {
140+
"line": 1,
141+
"column": 20
142+
},
143+
"end": {
144+
"line": 1,
145+
"column": 21
146+
}
147+
}
148+
},
149+
{
150+
"type": "Identifier",
151+
"value": "T",
152+
"range": [
153+
21,
154+
22
155+
],
156+
"loc": {
157+
"start": {
158+
"line": 1,
159+
"column": 21
160+
},
161+
"end": {
162+
"line": 1,
163+
"column": 22
164+
}
165+
}
166+
},
167+
{
168+
"type": "Punctuator",
169+
"value": ",",
170+
"range": [
171+
22,
172+
23
173+
],
174+
"loc": {
175+
"start": {
176+
"line": 1,
177+
"column": 22
178+
},
179+
"end": {
180+
"line": 1,
181+
"column": 23
182+
}
183+
}
184+
},
185+
{
186+
"type": "Identifier",
187+
"value": "U",
188+
"range": [
189+
24,
190+
25
191+
],
192+
"loc": {
193+
"start": {
194+
"line": 1,
195+
"column": 24
196+
},
197+
"end": {
198+
"line": 1,
199+
"column": 25
200+
}
201+
}
202+
},
203+
{
204+
"type": "Punctuator",
205+
"value": ">",
206+
"range": [
207+
25,
208+
26
209+
],
210+
"loc": {
211+
"start": {
212+
"line": 1,
213+
"column": 25
214+
},
215+
"end": {
216+
"line": 1,
217+
"column": 26
218+
}
219+
}
220+
},
221+
{
222+
"type": "Punctuator",
223+
"value": "{",
224+
"range": [
225+
27,
226+
28
227+
],
228+
"loc": {
229+
"start": {
230+
"line": 1,
231+
"column": 27
232+
},
233+
"end": {
234+
"line": 1,
235+
"column": 28
236+
}
237+
}
238+
},
239+
{
240+
"type": "Punctuator",
241+
"value": "}",
242+
"range": [
243+
30,
244+
31
245+
],
246+
"loc": {
247+
"start": {
248+
"line": 3,
249+
"column": 0
250+
},
251+
"end": {
252+
"line": 3,
253+
"column": 1
254+
}
255+
}
256+
}
257+
]
258+
};
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export default class<T, U> {
2+
3+
}
Lines changed: 241 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,241 @@
1+
module.exports = {
2+
"type": "Program",
3+
"range": [
4+
0,
5+
24
6+
],
7+
"loc": {
8+
"start": {
9+
"line": 1,
10+
"column": 0
11+
},
12+
"end": {
13+
"line": 3,
14+
"column": 1
15+
}
16+
},
17+
"body": [
18+
{
19+
"type": "ExportNamedDeclaration",
20+
"declaration": {
21+
"type": "ClassDeclaration",
22+
"range": [
23+
7,
24+
24
25+
],
26+
"loc": {
27+
"start": {
28+
"line": 1,
29+
"column": 7
30+
},
31+
"end": {
32+
"line": 3,
33+
"column": 1
34+
}
35+
},
36+
"id": {
37+
"type": "Identifier",
38+
"range": [
39+
13,
40+
16
41+
],
42+
"loc": {
43+
"start": {
44+
"line": 1,
45+
"column": 13
46+
},
47+
"end": {
48+
"line": 1,
49+
"column": 16
50+
}
51+
},
52+
"name": "Foo"
53+
},
54+
"body": {
55+
"type": "ClassBody",
56+
"body": [],
57+
"range": [
58+
20,
59+
24
60+
],
61+
"loc": {
62+
"start": {
63+
"line": 1,
64+
"column": 20
65+
},
66+
"end": {
67+
"line": 3,
68+
"column": 1
69+
}
70+
}
71+
},
72+
"superClass": null,
73+
"implements": [],
74+
"decorators": []
75+
},
76+
"range": [
77+
0,
78+
24
79+
],
80+
"loc": {
81+
"start": {
82+
"line": 1,
83+
"column": 0
84+
},
85+
"end": {
86+
"line": 3,
87+
"column": 1
88+
}
89+
},
90+
"specifiers": [],
91+
"source": null
92+
}
93+
],
94+
"sourceType": "module",
95+
"tokens": [
96+
{
97+
"type": "Keyword",
98+
"value": "export",
99+
"range": [
100+
0,
101+
6
102+
],
103+
"loc": {
104+
"start": {
105+
"line": 1,
106+
"column": 0
107+
},
108+
"end": {
109+
"line": 1,
110+
"column": 6
111+
}
112+
}
113+
},
114+
{
115+
"type": "Keyword",
116+
"value": "class",
117+
"range": [
118+
7,
119+
12
120+
],
121+
"loc": {
122+
"start": {
123+
"line": 1,
124+
"column": 7
125+
},
126+
"end": {
127+
"line": 1,
128+
"column": 12
129+
}
130+
}
131+
},
132+
{
133+
"type": "Identifier",
134+
"value": "Foo",
135+
"range": [
136+
13,
137+
16
138+
],
139+
"loc": {
140+
"start": {
141+
"line": 1,
142+
"column": 13
143+
},
144+
"end": {
145+
"line": 1,
146+
"column": 16
147+
}
148+
}
149+
},
150+
{
151+
"type": "Punctuator",
152+
"value": "<",
153+
"range": [
154+
16,
155+
17
156+
],
157+
"loc": {
158+
"start": {
159+
"line": 1,
160+
"column": 16
161+
},
162+
"end": {
163+
"line": 1,
164+
"column": 17
165+
}
166+
}
167+
},
168+
{
169+
"type": "Identifier",
170+
"value": "T",
171+
"range": [
172+
17,
173+
18
174+
],
175+
"loc": {
176+
"start": {
177+
"line": 1,
178+
"column": 17
179+
},
180+
"end": {
181+
"line": 1,
182+
"column": 18
183+
}
184+
}
185+
},
186+
{
187+
"type": "Punctuator",
188+
"value": ">",
189+
"range": [
190+
18,
191+
19
192+
],
193+
"loc": {
194+
"start": {
195+
"line": 1,
196+
"column": 18
197+
},
198+
"end": {
199+
"line": 1,
200+
"column": 19
201+
}
202+
}
203+
},
204+
{
205+
"type": "Punctuator",
206+
"value": "{",
207+
"range": [
208+
20,
209+
21
210+
],
211+
"loc": {
212+
"start": {
213+
"line": 1,
214+
"column": 20
215+
},
216+
"end": {
217+
"line": 1,
218+
"column": 21
219+
}
220+
}
221+
},
222+
{
223+
"type": "Punctuator",
224+
"value": "}",
225+
"range": [
226+
23,
227+
24
228+
],
229+
"loc": {
230+
"start": {
231+
"line": 3,
232+
"column": 0
233+
},
234+
"end": {
235+
"line": 3,
236+
"column": 1
237+
}
238+
}
239+
}
240+
]
241+
};
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export class Foo<T> {
2+
3+
}
Lines changed: 277 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,277 @@
1+
module.exports = {
2+
"type": "Program",
3+
"range": [
4+
0,
5+
27
6+
],
7+
"loc": {
8+
"start": {
9+
"line": 1,
10+
"column": 0
11+
},
12+
"end": {
13+
"line": 3,
14+
"column": 1
15+
}
16+
},
17+
"body": [
18+
{
19+
"type": "ExportNamedDeclaration",
20+
"declaration": {
21+
"type": "ClassDeclaration",
22+
"range": [
23+
7,
24+
27
25+
],
26+
"loc": {
27+
"start": {
28+
"line": 1,
29+
"column": 7
30+
},
31+
"end": {
32+
"line": 3,
33+
"column": 1
34+
}
35+
},
36+
"id": {
37+
"type": "Identifier",
38+
"range": [
39+
13,
40+
16
41+
],
42+
"loc": {
43+
"start": {
44+
"line": 1,
45+
"column": 13
46+
},
47+
"end": {
48+
"line": 1,
49+
"column": 16
50+
}
51+
},
52+
"name": "Foo"
53+
},
54+
"body": {
55+
"type": "ClassBody",
56+
"body": [],
57+
"range": [
58+
23,
59+
27
60+
],
61+
"loc": {
62+
"start": {
63+
"line": 1,
64+
"column": 23
65+
},
66+
"end": {
67+
"line": 3,
68+
"column": 1
69+
}
70+
}
71+
},
72+
"superClass": null,
73+
"implements": [],
74+
"decorators": []
75+
},
76+
"range": [
77+
0,
78+
27
79+
],
80+
"loc": {
81+
"start": {
82+
"line": 1,
83+
"column": 0
84+
},
85+
"end": {
86+
"line": 3,
87+
"column": 1
88+
}
89+
},
90+
"specifiers": [],
91+
"source": null
92+
}
93+
],
94+
"sourceType": "module",
95+
"tokens": [
96+
{
97+
"type": "Keyword",
98+
"value": "export",
99+
"range": [
100+
0,
101+
6
102+
],
103+
"loc": {
104+
"start": {
105+
"line": 1,
106+
"column": 0
107+
},
108+
"end": {
109+
"line": 1,
110+
"column": 6
111+
}
112+
}
113+
},
114+
{
115+
"type": "Keyword",
116+
"value": "class",
117+
"range": [
118+
7,
119+
12
120+
],
121+
"loc": {
122+
"start": {
123+
"line": 1,
124+
"column": 7
125+
},
126+
"end": {
127+
"line": 1,
128+
"column": 12
129+
}
130+
}
131+
},
132+
{
133+
"type": "Identifier",
134+
"value": "Foo",
135+
"range": [
136+
13,
137+
16
138+
],
139+
"loc": {
140+
"start": {
141+
"line": 1,
142+
"column": 13
143+
},
144+
"end": {
145+
"line": 1,
146+
"column": 16
147+
}
148+
}
149+
},
150+
{
151+
"type": "Punctuator",
152+
"value": "<",
153+
"range": [
154+
16,
155+
17
156+
],
157+
"loc": {
158+
"start": {
159+
"line": 1,
160+
"column": 16
161+
},
162+
"end": {
163+
"line": 1,
164+
"column": 17
165+
}
166+
}
167+
},
168+
{
169+
"type": "Identifier",
170+
"value": "T",
171+
"range": [
172+
17,
173+
18
174+
],
175+
"loc": {
176+
"start": {
177+
"line": 1,
178+
"column": 17
179+
},
180+
"end": {
181+
"line": 1,
182+
"column": 18
183+
}
184+
}
185+
},
186+
{
187+
"type": "Punctuator",
188+
"value": ",",
189+
"range": [
190+
18,
191+
19
192+
],
193+
"loc": {
194+
"start": {
195+
"line": 1,
196+
"column": 18
197+
},
198+
"end": {
199+
"line": 1,
200+
"column": 19
201+
}
202+
}
203+
},
204+
{
205+
"type": "Identifier",
206+
"value": "U",
207+
"range": [
208+
20,
209+
21
210+
],
211+
"loc": {
212+
"start": {
213+
"line": 1,
214+
"column": 20
215+
},
216+
"end": {
217+
"line": 1,
218+
"column": 21
219+
}
220+
}
221+
},
222+
{
223+
"type": "Punctuator",
224+
"value": ">",
225+
"range": [
226+
21,
227+
22
228+
],
229+
"loc": {
230+
"start": {
231+
"line": 1,
232+
"column": 21
233+
},
234+
"end": {
235+
"line": 1,
236+
"column": 22
237+
}
238+
}
239+
},
240+
{
241+
"type": "Punctuator",
242+
"value": "{",
243+
"range": [
244+
23,
245+
24
246+
],
247+
"loc": {
248+
"start": {
249+
"line": 1,
250+
"column": 23
251+
},
252+
"end": {
253+
"line": 1,
254+
"column": 24
255+
}
256+
}
257+
},
258+
{
259+
"type": "Punctuator",
260+
"value": "}",
261+
"range": [
262+
26,
263+
27
264+
],
265+
"loc": {
266+
"start": {
267+
"line": 3,
268+
"column": 0
269+
},
270+
"end": {
271+
"line": 3,
272+
"column": 1
273+
}
274+
}
275+
}
276+
]
277+
};
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export class Foo<T, U> {
2+
3+
}

0 commit comments

Comments
 (0)
This repository has been archived.