Skip to content

Commit

Permalink
Parse consequent bindings
Browse files Browse the repository at this point in the history
  • Loading branch information
wcjohnson committed May 17, 2017
1 parent 1a73a60 commit f2572dd
Show file tree
Hide file tree
Showing 4 changed files with 445 additions and 18 deletions.
57 changes: 40 additions & 17 deletions src/plugins/lightscript.js
Original file line number Diff line number Diff line change
Expand Up @@ -659,31 +659,54 @@ pp.parseMatch = function (node, isExpression) {
return this.finishNode(node, isExpression ? "MatchExpression" : "MatchStatement");
};

pp.parseMatchCaseConsequent = function(matchNode) {
// c/p parseIf
if (this.match(tt.braceL)) {
matchNode.consequent = this.parseBlock(false, true);
} else {
matchNode.consequent = this.parseWhiteBlock(true);
}
};

pp.parseMatchCaseWithConsequent = function(matchNode) {
// with (x) -> ..., with async, with ->
if (this.match(tt.parenL) || this.match(tt.arrow) || this.isContextual("async")) {
const consequent = this.parseMaybeAssign();
if (consequent.type !== "ArrowFunctionExpression") {
this.unexpected(consequent.start, tt.arrow);
}
matchNode.consequent = consequent;
matchNode.functional = true;
return;
}

// with <Identifier> -> ...
// with <BindingAtom>: ...
const node = this.startNode();
const bindingAtom = this.parseBindingAtom();
if (this.match(tt.arrow)) {
matchNode.consequent = this.parseArrowExpression(node, bindingAtom ? [bindingAtom] : [], false);
matchNode.functional = true;
return;
}

matchNode.binding = bindingAtom;
this.parseMatchCaseConsequent(matchNode);
};

pp.parseMatchCase = function () {
const node = this.startNode();

node.test = this.parseMatchCaseTest();

const oldInMatchCaseConsequent = this.state.inMatchCaseConsequent;
this.state.inMatchCaseConsequent = true;
if (this.eat(tt._with)) {
const oldInMatchCaseConsequent = this.state.inMatchCaseConsequent;
this.state.inMatchCaseConsequent = true;
node.consequent = this.parseMaybeAssign();
this.state.inMatchCaseConsequent = oldInMatchCaseConsequent;
if (node.consequent.type !== "ArrowFunctionExpression") {
this.unexpected(node.consequent.start, tt.arrow);
}
node.functional = true;
this.parseMatchCaseWithConsequent(node);
} else {
const oldInMatchCaseConsequent = this.state.inMatchCaseConsequent;
this.state.inMatchCaseConsequent = true;
// c/p parseIf
if (this.match(tt.braceL)) {
node.consequent = this.parseBlock(false, true);
} else {
node.consequent = this.parseWhiteBlock(true);
}
this.state.inMatchCaseConsequent = oldInMatchCaseConsequent;
this.parseMatchCaseConsequent(node);
}
this.state.inMatchCaseConsequent = oldInMatchCaseConsequent;

return this.finishNode(node, "MatchCase");
};
Expand Down
4 changes: 4 additions & 0 deletions test/fixtures/lightscript/match/binding/actual.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
match x:
| 1 with y: y
| ~isArray() with [ first ]: first
| ~isObject() with { second: third }: third
Loading

0 comments on commit f2572dd

Please sign in to comment.