Skip to content

Commit b7c7c88

Browse files
committed
Match discriminant as
1 parent 1ce7c74 commit b7c7c88

File tree

9 files changed

+1286
-2
lines changed

9 files changed

+1286
-2
lines changed

src/plugins/lightscript.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,9 +111,10 @@ pp.expectParenFreeBlockStart = function (node) {
111111
// if true: blah
112112
// if true { blah }
113113
// if (true) blah
114+
// match (foo) as bar:
114115
if (node && node.extra && node.extra.hasParens) {
115116
this.expect(tt.parenR);
116-
} else if (!(this.match(tt.colon) || this.match(tt.braceL))) {
117+
} else if (!(this.match(tt.colon) || this.match(tt.braceL) || this.isContextual("as"))) {
117118
this.unexpected(null, "Paren-free test expressions must be followed by braces or a colon.");
118119
}
119120
};
@@ -561,7 +562,11 @@ pp.parseMatchStatement = function (node) {
561562
pp.parseMatch = function (node, isExpression) {
562563
if (this.state.inMatchCaseTest) this.unexpected();
563564
this.expect(tt._match);
565+
564566
node.discriminant = this.parseParenExpression();
567+
if (this.eatContextual("as")) {
568+
node.alias = this.parseIdentifier();
569+
}
565570

566571
const isColon = this.match(tt.colon);
567572
let isEnd;
@@ -673,7 +678,8 @@ export default function (instance) {
673678
// first, try paren-free style
674679
try {
675680
const val = this.parseExpression();
676-
if (this.match(tt.braceL) || this.match(tt.colon)) {
681+
// "as" for `match (foo) as bar:`, bit dirty to allow for all but not a problem
682+
if (this.match(tt.braceL) || this.match(tt.colon) || this.isContextual("as")) {
677683
if (val.extra && val.extra.parenthesized) {
678684
delete val.extra.parenthesized;
679685
delete val.extra.parenStart;
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
match (x) as foo:
2+
| foo.bar: foo + 1
Lines changed: 219 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,219 @@
1+
{
2+
"type": "File",
3+
"start": 0,
4+
"end": 38,
5+
"loc": {
6+
"start": {
7+
"line": 1,
8+
"column": 0
9+
},
10+
"end": {
11+
"line": 2,
12+
"column": 20
13+
}
14+
},
15+
"program": {
16+
"type": "Program",
17+
"start": 0,
18+
"end": 38,
19+
"loc": {
20+
"start": {
21+
"line": 1,
22+
"column": 0
23+
},
24+
"end": {
25+
"line": 2,
26+
"column": 20
27+
}
28+
},
29+
"sourceType": "script",
30+
"body": [
31+
{
32+
"type": "MatchStatement",
33+
"start": 0,
34+
"end": 38,
35+
"loc": {
36+
"start": {
37+
"line": 1,
38+
"column": 0
39+
},
40+
"end": {
41+
"line": 2,
42+
"column": 20
43+
}
44+
},
45+
"discriminant": {
46+
"type": "Identifier",
47+
"start": 7,
48+
"end": 8,
49+
"loc": {
50+
"start": {
51+
"line": 1,
52+
"column": 7
53+
},
54+
"end": {
55+
"line": 1,
56+
"column": 8
57+
},
58+
"identifierName": "x"
59+
},
60+
"name": "x",
61+
"extra": {}
62+
},
63+
"alias": {
64+
"type": "Identifier",
65+
"start": 13,
66+
"end": 16,
67+
"loc": {
68+
"start": {
69+
"line": 1,
70+
"column": 13
71+
},
72+
"end": {
73+
"line": 1,
74+
"column": 16
75+
},
76+
"identifierName": "foo"
77+
},
78+
"name": "foo"
79+
},
80+
"cases": [
81+
{
82+
"type": "MatchCase",
83+
"start": 20,
84+
"end": 38,
85+
"loc": {
86+
"start": {
87+
"line": 2,
88+
"column": 2
89+
},
90+
"end": {
91+
"line": 2,
92+
"column": 20
93+
}
94+
},
95+
"test": {
96+
"type": "MemberExpression",
97+
"start": 22,
98+
"end": 29,
99+
"loc": {
100+
"start": {
101+
"line": 2,
102+
"column": 4
103+
},
104+
"end": {
105+
"line": 2,
106+
"column": 11
107+
}
108+
},
109+
"object": {
110+
"type": "Identifier",
111+
"start": 22,
112+
"end": 25,
113+
"loc": {
114+
"start": {
115+
"line": 2,
116+
"column": 4
117+
},
118+
"end": {
119+
"line": 2,
120+
"column": 7
121+
},
122+
"identifierName": "foo"
123+
},
124+
"name": "foo"
125+
},
126+
"property": {
127+
"type": "Identifier",
128+
"start": 26,
129+
"end": 29,
130+
"loc": {
131+
"start": {
132+
"line": 2,
133+
"column": 8
134+
},
135+
"end": {
136+
"line": 2,
137+
"column": 11
138+
},
139+
"identifierName": "bar"
140+
},
141+
"name": "bar"
142+
},
143+
"computed": false
144+
},
145+
"consequent": {
146+
"type": "ExpressionStatement",
147+
"start": 31,
148+
"end": 38,
149+
"loc": {
150+
"start": {
151+
"line": 2,
152+
"column": 13
153+
},
154+
"end": {
155+
"line": 2,
156+
"column": 20
157+
}
158+
},
159+
"expression": {
160+
"type": "BinaryExpression",
161+
"start": 31,
162+
"end": 38,
163+
"loc": {
164+
"start": {
165+
"line": 2,
166+
"column": 13
167+
},
168+
"end": {
169+
"line": 2,
170+
"column": 20
171+
}
172+
},
173+
"left": {
174+
"type": "Identifier",
175+
"start": 31,
176+
"end": 34,
177+
"loc": {
178+
"start": {
179+
"line": 2,
180+
"column": 13
181+
},
182+
"end": {
183+
"line": 2,
184+
"column": 16
185+
},
186+
"identifierName": "foo"
187+
},
188+
"name": "foo"
189+
},
190+
"operator": "+",
191+
"right": {
192+
"type": "NumericLiteral",
193+
"start": 37,
194+
"end": 38,
195+
"loc": {
196+
"start": {
197+
"line": 2,
198+
"column": 19
199+
},
200+
"end": {
201+
"line": 2,
202+
"column": 20
203+
}
204+
},
205+
"extra": {
206+
"rawValue": 1,
207+
"raw": "1"
208+
},
209+
"value": 1
210+
}
211+
}
212+
}
213+
}
214+
]
215+
}
216+
],
217+
"directives": []
218+
}
219+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
match x as foo:
2+
| foo.bar: foo + 1

0 commit comments

Comments
 (0)