Skip to content

Commit 019b133

Browse files
committed
Enforce try/catch/finally white block alignment
1 parent 6d4b5d5 commit 019b133

File tree

25 files changed

+1013
-2
lines changed

25 files changed

+1013
-2
lines changed

src/parser/statement.js

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -439,10 +439,23 @@ const empty = [];
439439
pp.parseTryStatement = function (node) {
440440
this.next();
441441

442+
let indentLevel, isColon;
443+
if (this.hasPlugin("lightscript")) {
444+
indentLevel = this.state.indentLevel;
445+
isColon = this.match(tt.colon);
446+
if (isColon) this.pushBlockState("try", indentLevel);
447+
}
448+
442449
node.block = this.parseBlock();
443450
node.handler = null;
444451

445-
if (this.match(tt._catch)) {
452+
const shouldParseCatch = this.match(tt._catch) && (
453+
!this.hasPlugin("lightscript") ||
454+
indentLevel === this.state.indentLevel ||
455+
(!isColon && !this.matchBlockState("try", this.state.indentLevel))
456+
);
457+
458+
if (shouldParseCatch) {
446459
const clause = this.startNode();
447460
this.next();
448461

@@ -457,6 +470,11 @@ pp.parseTryStatement = function (node) {
457470
this.checkLVal(clause.param, true, Object.create(null), "catch clause");
458471
if (this.hasPlugin("lightscript")) {
459472
this.expectParenFreeBlockStart(clause);
473+
if (isColon) {
474+
this.check(tt.colon);
475+
} else {
476+
this.check(tt.braceL);
477+
}
460478
} else {
461479
this.expect(tt.parenR);
462480
}
@@ -465,12 +483,34 @@ pp.parseTryStatement = function (node) {
465483
}
466484

467485
node.guardedHandlers = empty;
468-
node.finalizer = this.eat(tt._finally) ? this.parseBlock() : null;
486+
487+
488+
if (this.hasPlugin("lightscript")) {
489+
const shouldParseFinally = this.match(tt._finally) && (
490+
indentLevel === this.state.indentLevel ||
491+
(!isColon && !this.matchBlockState("try", this.state.indentLevel))
492+
);
493+
if (shouldParseFinally) {
494+
this.next();
495+
if (isColon) {
496+
this.check(tt.colon);
497+
} else {
498+
this.check(tt.braceL);
499+
}
500+
node.finalizer = this.parseBlock();
501+
} else {
502+
node.finalizer = null;
503+
}
504+
} else {
505+
node.finalizer = this.eat(tt._finally) ? this.parseBlock() : null;
506+
}
469507

470508
if (!node.handler && !node.finalizer) {
471509
this.raise(node.start, "Missing catch or finally clause");
472510
}
473511

512+
if (this.hasPlugin("lightscript") && isColon) this.popBlockState();
513+
474514
return this.finishNode(node, "TryStatement");
475515
};
476516

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
try {
2+
1
3+
} catch err:
4+
2
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"throws": "Unexpected token, expected { (3:11)"
3+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
try:
2+
1
3+
catch (err) {
4+
2
5+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"throws": "Unexpected token, expected : (3:12)"
3+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
try {
2+
1
3+
}
4+
catch err {
5+
2
6+
}
7+
finally {
8+
3
9+
}
Lines changed: 250 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,250 @@
1+
{
2+
"type": "File",
3+
"start": 0,
4+
"end": 63,
5+
"loc": {
6+
"start": {
7+
"line": 1,
8+
"column": 0
9+
},
10+
"end": {
11+
"line": 9,
12+
"column": 5
13+
}
14+
},
15+
"program": {
16+
"type": "Program",
17+
"start": 0,
18+
"end": 63,
19+
"loc": {
20+
"start": {
21+
"line": 1,
22+
"column": 0
23+
},
24+
"end": {
25+
"line": 9,
26+
"column": 5
27+
}
28+
},
29+
"sourceType": "script",
30+
"body": [
31+
{
32+
"type": "TryStatement",
33+
"start": 2,
34+
"end": 63,
35+
"loc": {
36+
"start": {
37+
"line": 1,
38+
"column": 2
39+
},
40+
"end": {
41+
"line": 9,
42+
"column": 5
43+
}
44+
},
45+
"block": {
46+
"type": "BlockStatement",
47+
"start": 6,
48+
"end": 17,
49+
"loc": {
50+
"start": {
51+
"line": 1,
52+
"column": 6
53+
},
54+
"end": {
55+
"line": 3,
56+
"column": 3
57+
}
58+
},
59+
"body": [
60+
{
61+
"type": "ExpressionStatement",
62+
"start": 12,
63+
"end": 13,
64+
"loc": {
65+
"start": {
66+
"line": 2,
67+
"column": 4
68+
},
69+
"end": {
70+
"line": 2,
71+
"column": 5
72+
}
73+
},
74+
"expression": {
75+
"type": "NumericLiteral",
76+
"start": 12,
77+
"end": 13,
78+
"loc": {
79+
"start": {
80+
"line": 2,
81+
"column": 4
82+
},
83+
"end": {
84+
"line": 2,
85+
"column": 5
86+
}
87+
},
88+
"extra": {
89+
"rawValue": 1,
90+
"raw": "1"
91+
},
92+
"value": 1
93+
}
94+
}
95+
],
96+
"directives": [],
97+
"extra": {
98+
"curly": true
99+
}
100+
},
101+
"handler": {
102+
"type": "CatchClause",
103+
"start": 18,
104+
"end": 35,
105+
"loc": {
106+
"start": {
107+
"line": 4,
108+
"column": 0
109+
},
110+
"end": {
111+
"line": 6,
112+
"column": 1
113+
}
114+
},
115+
"param": {
116+
"type": "Identifier",
117+
"start": 24,
118+
"end": 27,
119+
"loc": {
120+
"start": {
121+
"line": 4,
122+
"column": 6
123+
},
124+
"end": {
125+
"line": 4,
126+
"column": 9
127+
},
128+
"identifierName": "err"
129+
},
130+
"name": "err"
131+
},
132+
"body": {
133+
"type": "BlockStatement",
134+
"start": 28,
135+
"end": 35,
136+
"loc": {
137+
"start": {
138+
"line": 4,
139+
"column": 10
140+
},
141+
"end": {
142+
"line": 6,
143+
"column": 1
144+
}
145+
},
146+
"body": [
147+
{
148+
"type": "ExpressionStatement",
149+
"start": 32,
150+
"end": 33,
151+
"loc": {
152+
"start": {
153+
"line": 5,
154+
"column": 2
155+
},
156+
"end": {
157+
"line": 5,
158+
"column": 3
159+
}
160+
},
161+
"expression": {
162+
"type": "NumericLiteral",
163+
"start": 32,
164+
"end": 33,
165+
"loc": {
166+
"start": {
167+
"line": 5,
168+
"column": 2
169+
},
170+
"end": {
171+
"line": 5,
172+
"column": 3
173+
}
174+
},
175+
"extra": {
176+
"rawValue": 2,
177+
"raw": "2"
178+
},
179+
"value": 2
180+
}
181+
}
182+
],
183+
"directives": [],
184+
"extra": {
185+
"curly": true
186+
}
187+
}
188+
},
189+
"guardedHandlers": [],
190+
"finalizer": {
191+
"type": "BlockStatement",
192+
"start": 48,
193+
"end": 63,
194+
"loc": {
195+
"start": {
196+
"line": 7,
197+
"column": 12
198+
},
199+
"end": {
200+
"line": 9,
201+
"column": 5
202+
}
203+
},
204+
"body": [
205+
{
206+
"type": "ExpressionStatement",
207+
"start": 56,
208+
"end": 57,
209+
"loc": {
210+
"start": {
211+
"line": 8,
212+
"column": 6
213+
},
214+
"end": {
215+
"line": 8,
216+
"column": 7
217+
}
218+
},
219+
"expression": {
220+
"type": "NumericLiteral",
221+
"start": 56,
222+
"end": 57,
223+
"loc": {
224+
"start": {
225+
"line": 8,
226+
"column": 6
227+
},
228+
"end": {
229+
"line": 8,
230+
"column": 7
231+
}
232+
},
233+
"extra": {
234+
"rawValue": 3,
235+
"raw": "3"
236+
},
237+
"value": 3
238+
}
239+
}
240+
],
241+
"directives": [],
242+
"extra": {
243+
"curly": true
244+
}
245+
}
246+
}
247+
],
248+
"directives": []
249+
}
250+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
try:
2+
1
3+
catch err:
4+
2
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"throws": "Missing catch or finally clause (1:2)"
3+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
try:
2+
1
3+
catch err:
4+
2
5+
finally:
6+
3

0 commit comments

Comments
 (0)