Skip to content

Commit 130e345

Browse files
committed
Fix: Calculate range correctly when class is exported (fixes eslint#152) (eslint#153)
The range of a class body should be from the opening brace to the closing brace. To find the opening brace the ast converter parses over modifiers of the class declaration and assumes it is the last token after the last node modifier. This assumption is not always true when the class is exported. This commit ensures we only skip over modifiers which are after the class name or heritage clause.
1 parent c45de02 commit 130e345

8 files changed

+574
-4
lines changed

lib/ast-converter.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -1476,7 +1476,9 @@ module.exports = function(ast, extra) {
14761476
* could be multiple before the open brace
14771477
*/
14781478
var lastModifier = node.modifiers[node.modifiers.length - 1];
1479-
lastClassToken = ts.findNextToken(lastModifier, ast);
1479+
if (!lastClassToken || lastModifier.pos > lastClassToken.pos) {
1480+
lastClassToken = ts.findNextToken(lastModifier, ast);
1481+
}
14801482

14811483
} else if (!lastClassToken) { // no name
14821484
lastClassToken = node.getFirstToken();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
module.exports = {
2+
"type": "Program",
3+
"range": [
4+
0,
5+
25
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+
25
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+
21,
42+
25
43+
],
44+
"loc": {
45+
"start": {
46+
"line": 1,
47+
"column": 21
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+
25
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+
21,
136+
22
137+
],
138+
"loc": {
139+
"start": {
140+
"line": 1,
141+
"column": 21
142+
},
143+
"end": {
144+
"line": 1,
145+
"column": 22
146+
}
147+
}
148+
},
149+
{
150+
"type": "Punctuator",
151+
"value": "}",
152+
"range": [
153+
24,
154+
25
155+
],
156+
"loc": {
157+
"start": {
158+
"line": 3,
159+
"column": 0
160+
},
161+
"end": {
162+
"line": 3,
163+
"column": 1
164+
}
165+
}
166+
}
167+
]
168+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export default class {
2+
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,203 @@
1+
module.exports = {
2+
"type": "Program",
3+
"range": [
4+
0,
5+
30
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+
30
25+
],
26+
"loc": {
27+
"start": {
28+
"line": 1,
29+
"column": 15
30+
},
31+
"end": {
32+
"line": 3,
33+
"column": 1
34+
}
35+
},
36+
"id": {
37+
"type": "Identifier",
38+
"range": [
39+
21,
40+
25
41+
],
42+
"loc": {
43+
"start": {
44+
"line": 1,
45+
"column": 21
46+
},
47+
"end": {
48+
"line": 1,
49+
"column": 25
50+
}
51+
},
52+
"name": "Test"
53+
},
54+
"body": {
55+
"type": "ClassBody",
56+
"body": [],
57+
"range": [
58+
26,
59+
30
60+
],
61+
"loc": {
62+
"start": {
63+
"line": 1,
64+
"column": 26
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+
30
79+
],
80+
"loc": {
81+
"start": {
82+
"line": 1,
83+
"column": 0
84+
},
85+
"end": {
86+
"line": 3,
87+
"column": 1
88+
}
89+
}
90+
}
91+
],
92+
"sourceType": "module",
93+
"tokens": [
94+
{
95+
"type": "Keyword",
96+
"value": "export",
97+
"range": [
98+
0,
99+
6
100+
],
101+
"loc": {
102+
"start": {
103+
"line": 1,
104+
"column": 0
105+
},
106+
"end": {
107+
"line": 1,
108+
"column": 6
109+
}
110+
}
111+
},
112+
{
113+
"type": "Keyword",
114+
"value": "default",
115+
"range": [
116+
7,
117+
14
118+
],
119+
"loc": {
120+
"start": {
121+
"line": 1,
122+
"column": 7
123+
},
124+
"end": {
125+
"line": 1,
126+
"column": 14
127+
}
128+
}
129+
},
130+
{
131+
"type": "Keyword",
132+
"value": "class",
133+
"range": [
134+
15,
135+
20
136+
],
137+
"loc": {
138+
"start": {
139+
"line": 1,
140+
"column": 15
141+
},
142+
"end": {
143+
"line": 1,
144+
"column": 20
145+
}
146+
}
147+
},
148+
{
149+
"type": "Identifier",
150+
"value": "Test",
151+
"range": [
152+
21,
153+
25
154+
],
155+
"loc": {
156+
"start": {
157+
"line": 1,
158+
"column": 21
159+
},
160+
"end": {
161+
"line": 1,
162+
"column": 25
163+
}
164+
}
165+
},
166+
{
167+
"type": "Punctuator",
168+
"value": "{",
169+
"range": [
170+
26,
171+
27
172+
],
173+
"loc": {
174+
"start": {
175+
"line": 1,
176+
"column": 26
177+
},
178+
"end": {
179+
"line": 1,
180+
"column": 27
181+
}
182+
}
183+
},
184+
{
185+
"type": "Punctuator",
186+
"value": "}",
187+
"range": [
188+
29,
189+
30
190+
],
191+
"loc": {
192+
"start": {
193+
"line": 3,
194+
"column": 0
195+
},
196+
"end": {
197+
"line": 3,
198+
"column": 1
199+
}
200+
}
201+
}
202+
]
203+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export default class Test {
2+
3+
}

0 commit comments

Comments
 (0)