Skip to content

Commit 347d118

Browse files
committed
Enforce matching | indent levels
1 parent 5b3a411 commit 347d118

File tree

7 files changed

+405
-3
lines changed

7 files changed

+405
-3
lines changed

src/plugins/lightscript.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -563,8 +563,9 @@ pp.parseMatch = function (node, isExpression) {
563563
this.expect(tt._match);
564564
node.discriminant = this.parseParenExpression();
565565

566+
const isColon = this.match(tt.colon);
566567
let isEnd;
567-
if (this.match(tt.colon)) {
568+
if (isColon) {
568569
const indentLevel = this.state.indentLevel;
569570
this.next();
570571
isEnd = () => this.state.indentLevel <= indentLevel || this.match(tt.eof);
@@ -574,11 +575,15 @@ pp.parseMatch = function (node, isExpression) {
574575
}
575576

576577
node.cases = [];
578+
const caseIndentLevel = this.state.indentLevel;
577579
let hasUsedElse = false;
578580
while (!isEnd()) {
579581
if (hasUsedElse) {
580582
this.unexpected(null, "`else` must be last case.");
581583
}
584+
if (isColon && this.state.indentLevel !== caseIndentLevel) {
585+
this.unexpected(null, "Mismatched indent.");
586+
}
582587

583588
const matchCase = this.parseMatchCase(isExpression);
584589
if (matchCase.test && matchCase.test.type === "MatchElse") {
@@ -632,7 +637,7 @@ pp.parseMatchCaseTest = function (node) {
632637
// disambiguate `| { a, b }:` from `| { a, b }~someFn():`
633638
const bindingOrTest = this.parseExprOps(false, { start: 0 });
634639
try {
635-
node.binding = this.toAssignable(bindingOrTest.__clone(), true, 'match test');
640+
node.binding = this.toAssignable(bindingOrTest.__clone(), true, "match test");
636641
node.test = null;
637642
} catch (_err) {
638643
node.test = bindingOrTest;
@@ -642,7 +647,7 @@ pp.parseMatchCaseTest = function (node) {
642647
}
643648

644649
if (this.eat(tt._with)) {
645-
if (node.binding) this.unexpected(this.state.lastTokStart, "Cannot destructure twice.")
650+
if (node.binding) this.unexpected(this.state.lastTokStart, "Cannot destructure twice.");
646651
if (!(this.match(tt.braceL) || this.match(tt.bracketL))) this.unexpected();
647652
node.binding = this.parseBindingAtom();
648653
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
match x {
2+
| true: true
3+
| false: false
4+
}
Lines changed: 192 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,192 @@
1+
{
2+
"type": "File",
3+
"start": 0,
4+
"end": 45,
5+
"loc": {
6+
"start": {
7+
"line": 1,
8+
"column": 0
9+
},
10+
"end": {
11+
"line": 4,
12+
"column": 1
13+
}
14+
},
15+
"program": {
16+
"type": "Program",
17+
"start": 0,
18+
"end": 45,
19+
"loc": {
20+
"start": {
21+
"line": 1,
22+
"column": 0
23+
},
24+
"end": {
25+
"line": 4,
26+
"column": 1
27+
}
28+
},
29+
"sourceType": "script",
30+
"body": [
31+
{
32+
"type": "MatchStatement",
33+
"start": 0,
34+
"end": 45,
35+
"loc": {
36+
"start": {
37+
"line": 1,
38+
"column": 0
39+
},
40+
"end": {
41+
"line": 4,
42+
"column": 1
43+
}
44+
},
45+
"discriminant": {
46+
"type": "Identifier",
47+
"start": 6,
48+
"end": 7,
49+
"loc": {
50+
"start": {
51+
"line": 1,
52+
"column": 6
53+
},
54+
"end": {
55+
"line": 1,
56+
"column": 7
57+
},
58+
"identifierName": "x"
59+
},
60+
"name": "x"
61+
},
62+
"cases": [
63+
{
64+
"type": "MatchCase",
65+
"start": 12,
66+
"end": 24,
67+
"loc": {
68+
"start": {
69+
"line": 2,
70+
"column": 2
71+
},
72+
"end": {
73+
"line": 2,
74+
"column": 14
75+
}
76+
},
77+
"test": {
78+
"type": "BooleanLiteral",
79+
"start": 14,
80+
"end": 18,
81+
"loc": {
82+
"start": {
83+
"line": 2,
84+
"column": 4
85+
},
86+
"end": {
87+
"line": 2,
88+
"column": 8
89+
}
90+
},
91+
"value": true
92+
},
93+
"consequent": {
94+
"type": "ExpressionStatement",
95+
"start": 20,
96+
"end": 24,
97+
"loc": {
98+
"start": {
99+
"line": 2,
100+
"column": 10
101+
},
102+
"end": {
103+
"line": 2,
104+
"column": 14
105+
}
106+
},
107+
"expression": {
108+
"type": "BooleanLiteral",
109+
"start": 20,
110+
"end": 24,
111+
"loc": {
112+
"start": {
113+
"line": 2,
114+
"column": 10
115+
},
116+
"end": {
117+
"line": 2,
118+
"column": 14
119+
}
120+
},
121+
"value": true
122+
}
123+
}
124+
},
125+
{
126+
"type": "MatchCase",
127+
"start": 29,
128+
"end": 43,
129+
"loc": {
130+
"start": {
131+
"line": 3,
132+
"column": 4
133+
},
134+
"end": {
135+
"line": 3,
136+
"column": 18
137+
}
138+
},
139+
"test": {
140+
"type": "BooleanLiteral",
141+
"start": 31,
142+
"end": 36,
143+
"loc": {
144+
"start": {
145+
"line": 3,
146+
"column": 6
147+
},
148+
"end": {
149+
"line": 3,
150+
"column": 11
151+
}
152+
},
153+
"value": false
154+
},
155+
"consequent": {
156+
"type": "ExpressionStatement",
157+
"start": 38,
158+
"end": 43,
159+
"loc": {
160+
"start": {
161+
"line": 3,
162+
"column": 13
163+
},
164+
"end": {
165+
"line": 3,
166+
"column": 18
167+
}
168+
},
169+
"expression": {
170+
"type": "BooleanLiteral",
171+
"start": 38,
172+
"end": 43,
173+
"loc": {
174+
"start": {
175+
"line": 3,
176+
"column": 13
177+
},
178+
"end": {
179+
"line": 3,
180+
"column": 18
181+
}
182+
},
183+
"value": false
184+
}
185+
}
186+
}
187+
]
188+
}
189+
],
190+
"directives": []
191+
}
192+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
match x:
2+
| true: true
3+
| false: false
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"throws": "Mismatched indent. (3:4)"
3+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
match x:
2+
| true: true
3+
| false: false

0 commit comments

Comments
 (0)