Skip to content

Commit 2756b6e

Browse files
committed
Match discriminant as
1 parent 1ce7c74 commit 2756b6e

File tree

9 files changed

+1285
-2
lines changed

9 files changed

+1285
-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.ref = 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: 218 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,218 @@
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+
},
62+
"ref": {
63+
"type": "Identifier",
64+
"start": 13,
65+
"end": 16,
66+
"loc": {
67+
"start": {
68+
"line": 1,
69+
"column": 13
70+
},
71+
"end": {
72+
"line": 1,
73+
"column": 16
74+
},
75+
"identifierName": "foo"
76+
},
77+
"name": "foo"
78+
},
79+
"cases": [
80+
{
81+
"type": "MatchCase",
82+
"start": 20,
83+
"end": 38,
84+
"loc": {
85+
"start": {
86+
"line": 2,
87+
"column": 2
88+
},
89+
"end": {
90+
"line": 2,
91+
"column": 20
92+
}
93+
},
94+
"test": {
95+
"type": "MemberExpression",
96+
"start": 22,
97+
"end": 29,
98+
"loc": {
99+
"start": {
100+
"line": 2,
101+
"column": 4
102+
},
103+
"end": {
104+
"line": 2,
105+
"column": 11
106+
}
107+
},
108+
"object": {
109+
"type": "Identifier",
110+
"start": 22,
111+
"end": 25,
112+
"loc": {
113+
"start": {
114+
"line": 2,
115+
"column": 4
116+
},
117+
"end": {
118+
"line": 2,
119+
"column": 7
120+
},
121+
"identifierName": "foo"
122+
},
123+
"name": "foo"
124+
},
125+
"property": {
126+
"type": "Identifier",
127+
"start": 26,
128+
"end": 29,
129+
"loc": {
130+
"start": {
131+
"line": 2,
132+
"column": 8
133+
},
134+
"end": {
135+
"line": 2,
136+
"column": 11
137+
},
138+
"identifierName": "bar"
139+
},
140+
"name": "bar"
141+
},
142+
"computed": false
143+
},
144+
"consequent": {
145+
"type": "ExpressionStatement",
146+
"start": 31,
147+
"end": 38,
148+
"loc": {
149+
"start": {
150+
"line": 2,
151+
"column": 13
152+
},
153+
"end": {
154+
"line": 2,
155+
"column": 20
156+
}
157+
},
158+
"expression": {
159+
"type": "BinaryExpression",
160+
"start": 31,
161+
"end": 38,
162+
"loc": {
163+
"start": {
164+
"line": 2,
165+
"column": 13
166+
},
167+
"end": {
168+
"line": 2,
169+
"column": 20
170+
}
171+
},
172+
"left": {
173+
"type": "Identifier",
174+
"start": 31,
175+
"end": 34,
176+
"loc": {
177+
"start": {
178+
"line": 2,
179+
"column": 13
180+
},
181+
"end": {
182+
"line": 2,
183+
"column": 16
184+
},
185+
"identifierName": "foo"
186+
},
187+
"name": "foo"
188+
},
189+
"operator": "+",
190+
"right": {
191+
"type": "NumericLiteral",
192+
"start": 37,
193+
"end": 38,
194+
"loc": {
195+
"start": {
196+
"line": 2,
197+
"column": 19
198+
},
199+
"end": {
200+
"line": 2,
201+
"column": 20
202+
}
203+
},
204+
"extra": {
205+
"rawValue": 1,
206+
"raw": "1"
207+
},
208+
"value": 1
209+
}
210+
}
211+
}
212+
}
213+
]
214+
}
215+
],
216+
"directives": []
217+
}
218+
}
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)