Skip to content

Commit f2572dd

Browse files
committed
Parse consequent bindings
1 parent 1a73a60 commit f2572dd

File tree

4 files changed

+445
-18
lines changed

4 files changed

+445
-18
lines changed

src/plugins/lightscript.js

Lines changed: 40 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -659,31 +659,54 @@ pp.parseMatch = function (node, isExpression) {
659659
return this.finishNode(node, isExpression ? "MatchExpression" : "MatchStatement");
660660
};
661661

662+
pp.parseMatchCaseConsequent = function(matchNode) {
663+
// c/p parseIf
664+
if (this.match(tt.braceL)) {
665+
matchNode.consequent = this.parseBlock(false, true);
666+
} else {
667+
matchNode.consequent = this.parseWhiteBlock(true);
668+
}
669+
};
670+
671+
pp.parseMatchCaseWithConsequent = function(matchNode) {
672+
// with (x) -> ..., with async, with ->
673+
if (this.match(tt.parenL) || this.match(tt.arrow) || this.isContextual("async")) {
674+
const consequent = this.parseMaybeAssign();
675+
if (consequent.type !== "ArrowFunctionExpression") {
676+
this.unexpected(consequent.start, tt.arrow);
677+
}
678+
matchNode.consequent = consequent;
679+
matchNode.functional = true;
680+
return;
681+
}
682+
683+
// with <Identifier> -> ...
684+
// with <BindingAtom>: ...
685+
const node = this.startNode();
686+
const bindingAtom = this.parseBindingAtom();
687+
if (this.match(tt.arrow)) {
688+
matchNode.consequent = this.parseArrowExpression(node, bindingAtom ? [bindingAtom] : [], false);
689+
matchNode.functional = true;
690+
return;
691+
}
692+
693+
matchNode.binding = bindingAtom;
694+
this.parseMatchCaseConsequent(matchNode);
695+
};
696+
662697
pp.parseMatchCase = function () {
663698
const node = this.startNode();
664699

665700
node.test = this.parseMatchCaseTest();
666701

702+
const oldInMatchCaseConsequent = this.state.inMatchCaseConsequent;
703+
this.state.inMatchCaseConsequent = true;
667704
if (this.eat(tt._with)) {
668-
const oldInMatchCaseConsequent = this.state.inMatchCaseConsequent;
669-
this.state.inMatchCaseConsequent = true;
670-
node.consequent = this.parseMaybeAssign();
671-
this.state.inMatchCaseConsequent = oldInMatchCaseConsequent;
672-
if (node.consequent.type !== "ArrowFunctionExpression") {
673-
this.unexpected(node.consequent.start, tt.arrow);
674-
}
675-
node.functional = true;
705+
this.parseMatchCaseWithConsequent(node);
676706
} else {
677-
const oldInMatchCaseConsequent = this.state.inMatchCaseConsequent;
678-
this.state.inMatchCaseConsequent = true;
679-
// c/p parseIf
680-
if (this.match(tt.braceL)) {
681-
node.consequent = this.parseBlock(false, true);
682-
} else {
683-
node.consequent = this.parseWhiteBlock(true);
684-
}
685-
this.state.inMatchCaseConsequent = oldInMatchCaseConsequent;
707+
this.parseMatchCaseConsequent(node);
686708
}
709+
this.state.inMatchCaseConsequent = oldInMatchCaseConsequent;
687710

688711
return this.finishNode(node, "MatchCase");
689712
};
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
match x:
2+
| 1 with y: y
3+
| ~isArray() with [ first ]: first
4+
| ~isObject() with { second: third }: third

0 commit comments

Comments
 (0)