Skip to content

Commit 8159e93

Browse files
committed
Disallow control flow in if-expressions
1 parent cbb6ae5 commit 8159e93

File tree

9 files changed

+331
-298
lines changed

9 files changed

+331
-298
lines changed

src/plugins/lightscript.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,13 +397,22 @@ pp.parseIf = function (node, isExpression, requireColon = null) {
397397
if (isColon) this.pushBlockState("if", indentLevel);
398398

399399
if (isExpression) {
400+
// disallow return/continue/break, etc. c/p doExpression
401+
const oldInFunction = this.state.inFunction;
402+
const oldLabels = this.state.labels;
403+
this.state.labels = [];
404+
this.state.inFunction = false;
405+
400406
if (this.match(tt.braceL)) {
401407
node.consequent = this.parseBlock(false);
402408
} else if (!isColon) {
403409
node.consequent = this.parseMaybeAssign();
404410
} else {
405411
node.consequent = this.parseWhiteBlock(true);
406412
}
413+
414+
this.state.inFunction = oldInFunction;
415+
this.state.labels = oldLabels;
407416
} else {
408417
node.consequent = this.parseStatement(false);
409418
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
f() -/>
2+
x = if true:
3+
if true:
4+
await 1
Lines changed: 301 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,301 @@
1+
{
2+
"type": "File",
3+
"start": 0,
4+
"end": 49,
5+
"loc": {
6+
"start": {
7+
"line": 1,
8+
"column": 0
9+
},
10+
"end": {
11+
"line": 4,
12+
"column": 13
13+
}
14+
},
15+
"program": {
16+
"type": "Program",
17+
"start": 0,
18+
"end": 49,
19+
"loc": {
20+
"start": {
21+
"line": 1,
22+
"column": 0
23+
},
24+
"end": {
25+
"line": 4,
26+
"column": 13
27+
}
28+
},
29+
"sourceType": "script",
30+
"body": [
31+
{
32+
"type": "NamedArrowDeclaration",
33+
"start": 0,
34+
"end": 49,
35+
"loc": {
36+
"start": {
37+
"line": 1,
38+
"column": 0
39+
},
40+
"end": {
41+
"line": 4,
42+
"column": 13
43+
}
44+
},
45+
"id": {
46+
"type": "Identifier",
47+
"start": 0,
48+
"end": 1,
49+
"loc": {
50+
"start": {
51+
"line": 1,
52+
"column": 0
53+
},
54+
"end": {
55+
"line": 1,
56+
"column": 1
57+
},
58+
"identifierName": "f"
59+
},
60+
"name": "f"
61+
},
62+
"generator": false,
63+
"expression": false,
64+
"async": true,
65+
"params": [],
66+
"skinny": true,
67+
"body": {
68+
"type": "BlockStatement",
69+
"start": 4,
70+
"end": 49,
71+
"loc": {
72+
"start": {
73+
"line": 1,
74+
"column": 4
75+
},
76+
"end": {
77+
"line": 4,
78+
"column": 13
79+
}
80+
},
81+
"body": [
82+
{
83+
"type": "VariableDeclaration",
84+
"start": 10,
85+
"end": 49,
86+
"loc": {
87+
"start": {
88+
"line": 2,
89+
"column": 2
90+
},
91+
"end": {
92+
"line": 4,
93+
"column": 13
94+
}
95+
},
96+
"kind": "const",
97+
"extra": {
98+
"implicit": true
99+
},
100+
"declarations": [
101+
{
102+
"type": "VariableDeclarator",
103+
"start": 10,
104+
"end": 49,
105+
"loc": {
106+
"start": {
107+
"line": 2,
108+
"column": 2
109+
},
110+
"end": {
111+
"line": 4,
112+
"column": 13
113+
}
114+
},
115+
"id": {
116+
"type": "Identifier",
117+
"start": 10,
118+
"end": 11,
119+
"loc": {
120+
"start": {
121+
"line": 2,
122+
"column": 2
123+
},
124+
"end": {
125+
"line": 2,
126+
"column": 3
127+
},
128+
"identifierName": "x"
129+
},
130+
"name": "x"
131+
},
132+
"init": {
133+
"type": "IfExpression",
134+
"start": 14,
135+
"end": 49,
136+
"loc": {
137+
"start": {
138+
"line": 2,
139+
"column": 6
140+
},
141+
"end": {
142+
"line": 4,
143+
"column": 13
144+
}
145+
},
146+
"test": {
147+
"type": "BooleanLiteral",
148+
"start": 17,
149+
"end": 21,
150+
"loc": {
151+
"start": {
152+
"line": 2,
153+
"column": 9
154+
},
155+
"end": {
156+
"line": 2,
157+
"column": 13
158+
}
159+
},
160+
"value": true
161+
},
162+
"consequent": {
163+
"type": "BlockStatement",
164+
"start": 21,
165+
"end": 49,
166+
"loc": {
167+
"start": {
168+
"line": 2,
169+
"column": 13
170+
},
171+
"end": {
172+
"line": 4,
173+
"column": 13
174+
}
175+
},
176+
"body": [
177+
{
178+
"type": "IfStatement",
179+
"start": 27,
180+
"end": 49,
181+
"loc": {
182+
"start": {
183+
"line": 3,
184+
"column": 4
185+
},
186+
"end": {
187+
"line": 4,
188+
"column": 13
189+
}
190+
},
191+
"test": {
192+
"type": "BooleanLiteral",
193+
"start": 30,
194+
"end": 34,
195+
"loc": {
196+
"start": {
197+
"line": 3,
198+
"column": 7
199+
},
200+
"end": {
201+
"line": 3,
202+
"column": 11
203+
}
204+
},
205+
"value": true
206+
},
207+
"consequent": {
208+
"type": "BlockStatement",
209+
"start": 34,
210+
"end": 49,
211+
"loc": {
212+
"start": {
213+
"line": 3,
214+
"column": 11
215+
},
216+
"end": {
217+
"line": 4,
218+
"column": 13
219+
}
220+
},
221+
"body": [
222+
{
223+
"type": "ExpressionStatement",
224+
"start": 42,
225+
"end": 49,
226+
"loc": {
227+
"start": {
228+
"line": 4,
229+
"column": 6
230+
},
231+
"end": {
232+
"line": 4,
233+
"column": 13
234+
}
235+
},
236+
"expression": {
237+
"type": "AwaitExpression",
238+
"start": 42,
239+
"end": 49,
240+
"loc": {
241+
"start": {
242+
"line": 4,
243+
"column": 6
244+
},
245+
"end": {
246+
"line": 4,
247+
"column": 13
248+
}
249+
},
250+
"argument": {
251+
"type": "NumericLiteral",
252+
"start": 48,
253+
"end": 49,
254+
"loc": {
255+
"start": {
256+
"line": 4,
257+
"column": 12
258+
},
259+
"end": {
260+
"line": 4,
261+
"column": 13
262+
}
263+
},
264+
"extra": {
265+
"rawValue": 1,
266+
"raw": "1"
267+
},
268+
"value": 1
269+
}
270+
}
271+
}
272+
],
273+
"directives": [],
274+
"extra": {
275+
"curly": false
276+
}
277+
},
278+
"alternate": null
279+
}
280+
],
281+
"directives": [],
282+
"extra": {
283+
"curly": false
284+
}
285+
},
286+
"alternate": null
287+
}
288+
}
289+
]
290+
}
291+
],
292+
"directives": [],
293+
"extra": {
294+
"curly": false
295+
}
296+
}
297+
}
298+
],
299+
"directives": []
300+
}
301+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
while false:
2+
x = if true:
3+
if true:
4+
break
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"throws": "Unsyntactic break (4:6)"
3+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
f() ->
2+
x = if true:
3+
if true:
4+
return
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"throws": "'return' outside of function (4:6)"
3+
}

0 commit comments

Comments
 (0)