Skip to content
This repository was archived by the owner on Jan 19, 2019. It is now read-only.

Commit 6312383

Browse files
soda0289JamesHenry
authored andcommitted
Fix: Calculate constructor range using node.parameters.pos (fixes #139) (#140)
1 parent 3d71c8e commit 6312383

File tree

3 files changed

+358
-4
lines changed

3 files changed

+358
-4
lines changed

lib/ast-converter.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -1166,9 +1166,7 @@ module.exports = function(ast, extra) {
11661166

11671167
var constructorIsStatic = Boolean(node.flags & ts.NodeFlags.Static),
11681168
firstConstructorToken = constructorIsStatic ? ts.findNextToken(node.getFirstToken(), ast) : node.getFirstToken(),
1169-
constructorOffset = 11,
1170-
constructorStartOffset = constructorOffset + firstConstructorToken.getStart() - node.getFirstToken().getStart(),
1171-
constructorLoc = ast.getLineAndCharacterOfPosition(result.range[0] + constructorStartOffset),
1169+
constructorLoc = ast.getLineAndCharacterOfPosition(node.parameters.pos - 1),
11721170
constructor = {
11731171
type: "FunctionExpression",
11741172
id: null,
@@ -1183,7 +1181,7 @@ module.exports = function(ast, extra) {
11831181
expression: false,
11841182
async: false,
11851183
body: convertChild(node.body),
1186-
range: [ result.range[0] + constructorStartOffset, result.range[1]],
1184+
range: [ node.parameters.pos - 1, result.range[1]],
11871185
loc: {
11881186
start: {
11891187
line: constructorLoc.line + 1,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,355 @@
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": 1,
14+
"column": 27
15+
}
16+
},
17+
"body": [
18+
{
19+
"type": "ClassDeclaration",
20+
"range": [
21+
0,
22+
26
23+
],
24+
"loc": {
25+
"start": {
26+
"line": 1,
27+
"column": 0
28+
},
29+
"end": {
30+
"line": 1,
31+
"column": 26
32+
}
33+
},
34+
"id": {
35+
"type": "Identifier",
36+
"range": [
37+
6,
38+
7
39+
],
40+
"loc": {
41+
"start": {
42+
"line": 1,
43+
"column": 6
44+
},
45+
"end": {
46+
"line": 1,
47+
"column": 7
48+
}
49+
},
50+
"name": "A"
51+
},
52+
"body": {
53+
"type": "ClassBody",
54+
"body": [
55+
{
56+
"type": "MethodDefinition",
57+
"range": [
58+
9,
59+
25
60+
],
61+
"loc": {
62+
"start": {
63+
"line": 1,
64+
"column": 9
65+
},
66+
"end": {
67+
"line": 1,
68+
"column": 25
69+
}
70+
},
71+
"key": {
72+
"type": "Identifier",
73+
"name": "constructor",
74+
"range": [
75+
9,
76+
20
77+
],
78+
"loc": {
79+
"start": {
80+
"line": 1,
81+
"column": 9
82+
},
83+
"end": {
84+
"line": 1,
85+
"column": 21
86+
}
87+
}
88+
},
89+
"value": {
90+
"type": "FunctionExpression",
91+
"id": null,
92+
"params": [],
93+
"generator": false,
94+
"expression": false,
95+
"async": false,
96+
"body": {
97+
"type": "BlockStatement",
98+
"range": [
99+
23,
100+
25
101+
],
102+
"loc": {
103+
"start": {
104+
"line": 1,
105+
"column": 23
106+
},
107+
"end": {
108+
"line": 1,
109+
"column": 25
110+
}
111+
},
112+
"body": []
113+
},
114+
"range": [
115+
21,
116+
25
117+
],
118+
"loc": {
119+
"start": {
120+
"line": 1,
121+
"column": 21
122+
},
123+
"end": {
124+
"line": 1,
125+
"column": 25
126+
}
127+
}
128+
},
129+
"computed": false,
130+
"accessibility": null,
131+
"static": false,
132+
"kind": "constructor"
133+
}
134+
],
135+
"range": [
136+
8,
137+
26
138+
],
139+
"loc": {
140+
"start": {
141+
"line": 1,
142+
"column": 8
143+
},
144+
"end": {
145+
"line": 1,
146+
"column": 26
147+
}
148+
}
149+
},
150+
"superClass": null,
151+
"implements": [],
152+
"decorators": []
153+
},
154+
{
155+
"type": "EmptyStatement",
156+
"range": [
157+
26,
158+
27
159+
],
160+
"loc": {
161+
"start": {
162+
"line": 1,
163+
"column": 26
164+
},
165+
"end": {
166+
"line": 1,
167+
"column": 27
168+
}
169+
}
170+
}
171+
],
172+
"sourceType": "script",
173+
"tokens": [
174+
{
175+
"type": "Keyword",
176+
"value": "class",
177+
"range": [
178+
0,
179+
5
180+
],
181+
"loc": {
182+
"start": {
183+
"line": 1,
184+
"column": 0
185+
},
186+
"end": {
187+
"line": 1,
188+
"column": 5
189+
}
190+
}
191+
},
192+
{
193+
"type": "Identifier",
194+
"value": "A",
195+
"range": [
196+
6,
197+
7
198+
],
199+
"loc": {
200+
"start": {
201+
"line": 1,
202+
"column": 6
203+
},
204+
"end": {
205+
"line": 1,
206+
"column": 7
207+
}
208+
}
209+
},
210+
{
211+
"type": "Punctuator",
212+
"value": "{",
213+
"range": [
214+
8,
215+
9
216+
],
217+
"loc": {
218+
"start": {
219+
"line": 1,
220+
"column": 8
221+
},
222+
"end": {
223+
"line": 1,
224+
"column": 9
225+
}
226+
}
227+
},
228+
{
229+
"type": "Identifier",
230+
"value": "constructor",
231+
"range": [
232+
9,
233+
20
234+
],
235+
"loc": {
236+
"start": {
237+
"line": 1,
238+
"column": 9
239+
},
240+
"end": {
241+
"line": 1,
242+
"column": 20
243+
}
244+
}
245+
},
246+
{
247+
"type": "Punctuator",
248+
"value": "(",
249+
"range": [
250+
21,
251+
22
252+
],
253+
"loc": {
254+
"start": {
255+
"line": 1,
256+
"column": 21
257+
},
258+
"end": {
259+
"line": 1,
260+
"column": 22
261+
}
262+
}
263+
},
264+
{
265+
"type": "Punctuator",
266+
"value": ")",
267+
"range": [
268+
22,
269+
23
270+
],
271+
"loc": {
272+
"start": {
273+
"line": 1,
274+
"column": 22
275+
},
276+
"end": {
277+
"line": 1,
278+
"column": 23
279+
}
280+
}
281+
},
282+
{
283+
"type": "Punctuator",
284+
"value": "{",
285+
"range": [
286+
23,
287+
24
288+
],
289+
"loc": {
290+
"start": {
291+
"line": 1,
292+
"column": 23
293+
},
294+
"end": {
295+
"line": 1,
296+
"column": 24
297+
}
298+
}
299+
},
300+
{
301+
"type": "Punctuator",
302+
"value": "}",
303+
"range": [
304+
24,
305+
25
306+
],
307+
"loc": {
308+
"start": {
309+
"line": 1,
310+
"column": 24
311+
},
312+
"end": {
313+
"line": 1,
314+
"column": 25
315+
}
316+
}
317+
},
318+
{
319+
"type": "Punctuator",
320+
"value": "}",
321+
"range": [
322+
25,
323+
26
324+
],
325+
"loc": {
326+
"start": {
327+
"line": 1,
328+
"column": 25
329+
},
330+
"end": {
331+
"line": 1,
332+
"column": 26
333+
}
334+
}
335+
},
336+
{
337+
"type": "Punctuator",
338+
"value": ";",
339+
"range": [
340+
26,
341+
27
342+
],
343+
"loc": {
344+
"start": {
345+
"line": 1,
346+
"column": 26
347+
},
348+
"end": {
349+
"line": 1,
350+
"column": 27
351+
}
352+
}
353+
}
354+
]
355+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
class A {constructor (){}};

0 commit comments

Comments
 (0)