Skip to content

Commit cbb6ae5

Browse files
committed
Enforce matching whiteblock style in ifs
1 parent 019b133 commit cbb6ae5

File tree

23 files changed

+122
-1433
lines changed

23 files changed

+122
-1433
lines changed

src/plugins/lightscript.js

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -155,8 +155,7 @@ pp.parseMultilineWhiteBlock = function(node, indentLevel) {
155155

156156
pp.parseWhiteBlock = function (isExpression?) {
157157
const node = this.startNode(), indentLevel = this.state.indentLevel;
158-
159-
if (!this.eat(tt.colon)) this.unexpected(null, "Whitespace Block must start with a colon or arrow");
158+
if (!this.eat(tt.colon)) this.unexpected(this.state.lastTokEnd, tt.colon);
160159

161160
// Oneline whiteblock
162161
if (!this.isLineBreak()) {
@@ -383,12 +382,18 @@ pp.popBlockState = function() {
383382

384383
// c/p parseIfStatement
385384

386-
pp.parseIf = function (node, isExpression) {
385+
pp.parseIf = function (node, isExpression, requireColon = null) {
387386
const indentLevel = this.state.indentLevel;
388387
this.next();
389388
node.test = this.parseParenExpression();
390389

391-
const isColon = this.match(tt.colon);
390+
const isColon = requireColon
391+
? this.check(tt.colon)
392+
: this.match(tt.colon);
393+
394+
// colon not allowed, parent `if` didn't use one.
395+
if (isColon && requireColon === false) this.expect(tt.braceL);
396+
392397
if (isColon) this.pushBlockState("if", indentLevel);
393398

394399
if (isExpression) {
@@ -425,28 +430,28 @@ pp.parseIfAlternate = function (node, isExpression, ifIsWhiteBlock, ifIndentLeve
425430
}
426431

427432
if (this.match(tt._elif)) {
428-
return this.parseIf(this.startNode(), isExpression);
433+
return this.parseIf(this.startNode(), isExpression, ifIsWhiteBlock);
429434
}
430435

431436
if (this.eat(tt._else)) {
432437
if (this.match(tt._if)) {
433438
if (this.isLineBreak()) {
434439
this.unexpected(this.state.lastTokEnd, "Illegal newline.");
435440
}
436-
return this.parseIf(this.startNode(), isExpression);
441+
return this.parseIf(this.startNode(), isExpression, ifIsWhiteBlock);
437442
}
438443

439-
if (this.isLineBreak()) {
440-
this.unexpected(this.state.lastTokEnd, tt.colon);
444+
if (ifIsWhiteBlock) {
445+
return this.parseWhiteBlock(isExpression);
446+
} else if (this.match(tt.colon)) {
447+
this.expect(tt.braceL);
441448
}
442449

443450
if (isExpression) {
444451
if (this.match(tt.braceL)) {
445452
return this.parseBlock(false);
446-
} else if (!this.match(tt.colon)) {
447-
return this.parseMaybeAssign();
448453
} else {
449-
return this.parseWhiteBlock(true);
454+
return this.parseMaybeAssign();
450455
}
451456
}
452457

test/fixtures/lightscript/if-expression/colon-oneline/expected.json renamed to test/fixtures/lightscript/if-expression/braces-no-colon/expected.json

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,45 @@
11
{
22
"type": "File",
33
"start": 0,
4-
"end": 28,
4+
"end": 33,
55
"loc": {
66
"start": {
77
"line": 1,
88
"column": 0
99
},
1010
"end": {
11-
"line": 3,
12-
"column": 8
11+
"line": 4,
12+
"column": 5
1313
}
1414
},
1515
"program": {
1616
"type": "Program",
1717
"start": 0,
18-
"end": 28,
18+
"end": 33,
1919
"loc": {
2020
"start": {
2121
"line": 1,
2222
"column": 0
2323
},
2424
"end": {
25-
"line": 3,
26-
"column": 8
25+
"line": 4,
26+
"column": 5
2727
}
2828
},
2929
"sourceType": "script",
3030
"body": [
3131
{
3232
"type": "VariableDeclaration",
3333
"start": 0,
34-
"end": 28,
34+
"end": 33,
3535
"loc": {
3636
"start": {
3737
"line": 1,
3838
"column": 0
3939
},
4040
"end": {
41-
"line": 3,
42-
"column": 8
41+
"line": 4,
42+
"column": 5
4343
}
4444
},
4545
"kind": "const",
@@ -50,15 +50,15 @@
5050
{
5151
"type": "VariableDeclarator",
5252
"start": 0,
53-
"end": 28,
53+
"end": 33,
5454
"loc": {
5555
"start": {
5656
"line": 1,
5757
"column": 0
5858
},
5959
"end": {
60-
"line": 3,
61-
"column": 8
60+
"line": 4,
61+
"column": 5
6262
}
6363
},
6464
"id": {
@@ -81,15 +81,15 @@
8181
"init": {
8282
"type": "IfExpression",
8383
"start": 4,
84-
"end": 28,
84+
"end": 33,
8585
"loc": {
8686
"start": {
8787
"line": 1,
8888
"column": 4
8989
},
9090
"end": {
91-
"line": 3,
92-
"column": 8
91+
"line": 4,
92+
"column": 5
9393
}
9494
},
9595
"test": {
@@ -147,23 +147,23 @@
147147
},
148148
"consequent": {
149149
"type": "BlockStatement",
150-
"start": 12,
151-
"end": 19,
150+
"start": 13,
151+
"end": 22,
152152
"loc": {
153153
"start": {
154154
"line": 1,
155-
"column": 12
155+
"column": 13
156156
},
157157
"end": {
158-
"line": 2,
159-
"column": 5
158+
"line": 3,
159+
"column": 1
160160
}
161161
},
162162
"body": [
163163
{
164164
"type": "ExpressionStatement",
165-
"start": 16,
166-
"end": 19,
165+
"start": 17,
166+
"end": 20,
167167
"loc": {
168168
"start": {
169169
"line": 2,
@@ -176,8 +176,8 @@
176176
},
177177
"expression": {
178178
"type": "StringLiteral",
179-
"start": 16,
180-
"end": 19,
179+
"start": 17,
180+
"end": 20,
181181
"loc": {
182182
"start": {
183183
"line": 2,
@@ -198,21 +198,21 @@
198198
],
199199
"directives": [],
200200
"extra": {
201-
"curly": false
201+
"curly": true
202202
}
203203
},
204204
"alternate": {
205205
"type": "StringLiteral",
206-
"start": 25,
207-
"end": 28,
206+
"start": 30,
207+
"end": 33,
208208
"loc": {
209209
"start": {
210-
"line": 3,
211-
"column": 5
210+
"line": 4,
211+
"column": 2
212212
},
213213
"end": {
214-
"line": 3,
215-
"column": 8
214+
"line": 4,
215+
"column": 5
216216
}
217217
},
218218
"extra": {

test/fixtures/lightscript/if-expression/braces-no-colon/options.json

Lines changed: 0 additions & 3 deletions
This file was deleted.

0 commit comments

Comments
 (0)