Skip to content

Commit fe18036

Browse files
committed
MatchStatement
1 parent 045731f commit fe18036

File tree

31 files changed

+4465
-3754
lines changed

31 files changed

+4465
-3754
lines changed

src/parser/expression.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -726,7 +726,8 @@ pp.parseExprAtom = function (refShorthandDefaultPos) {
726726

727727
case tt._match:
728728
if (this.hasPlugin("lightscript")) {
729-
return this.parseMatch();
729+
node = this.startNode();
730+
return this.parseMatchExpression(node);
730731
}
731732

732733
case tt.arrow:

src/parser/statement.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,9 @@ pp.parseStatement = function (declaration, topLevel) {
116116
}
117117
return starttype === tt._import ? this.parseImport(node) : this.parseExport(node);
118118

119+
case tt._match:
120+
if (this.hasPlugin("lightscript")) return this.parseMatchStatement(node);
121+
119122
case tt.name:
120123
if (this.state.value === "async") {
121124
// peek ahead and see if next token is a function

src/plugins/lightscript.js

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -550,8 +550,15 @@ pp.isBitwiseOp = function () {
550550
);
551551
};
552552

553-
pp.parseMatch = function () {
554-
const node = this.startNode();
553+
pp.parseMatchExpression = function (node) {
554+
return this.parseMatch(node, true);
555+
};
556+
557+
pp.parseMatchStatement = function (node) {
558+
return this.parseMatch(node, false);
559+
};
560+
561+
pp.parseMatch = function (node, isExpression) {
555562
this.expect(tt._match);
556563
node.discriminant = this.parseParenExpression();
557564

@@ -572,17 +579,17 @@ pp.parseMatch = function () {
572579
this.unexpected(null, "`else` must be last case.");
573580
}
574581

575-
const matchCase = this.parseMatchCase();
582+
const matchCase = this.parseMatchCase(isExpression);
576583
if (matchCase.test.type === "MatchElse") {
577584
hasUsedElse = true;
578585
}
579586
node.cases.push(matchCase);
580587
}
581588

582-
return this.finishNode(node, "MatchExpression");
589+
return this.finishNode(node, isExpression ? "MatchExpression" : "MatchStatement");
583590
};
584591

585-
pp.parseMatchCase = function () {
592+
pp.parseMatchCase = function (isExpression) {
586593
const node = this.startNode();
587594

588595
node.test = this.parseMatchCaseTest();
@@ -591,7 +598,20 @@ pp.parseMatchCase = function () {
591598
node.binding = this.parseBindingAtom();
592599
}
593600

594-
node.consequent = this.parseBlock(false);
601+
if (isExpression) {
602+
// disallow return/continue/break, etc. c/p doExpression
603+
const oldInFunction = this.state.inFunction;
604+
const oldLabels = this.state.labels;
605+
this.state.labels = [];
606+
this.state.inFunction = false;
607+
608+
node.consequent = this.parseBlock(false);
609+
610+
this.state.inFunction = oldInFunction;
611+
this.state.labels = oldLabels;
612+
} else {
613+
node.consequent = this.parseBlock(false);
614+
}
595615

596616
return this.finishNode(node, "MatchCase");
597617
};

0 commit comments

Comments
 (0)