Skip to content

Commit

Permalink
Enforce with before destructuring in match case
Browse files Browse the repository at this point in the history
  • Loading branch information
rattrayalex committed Jun 9, 2017
1 parent 2756b6e commit 2815b3d
Show file tree
Hide file tree
Showing 14 changed files with 3,377 additions and 1,013 deletions.
21 changes: 9 additions & 12 deletions src/plugins/lightscript.js
Original file line number Diff line number Diff line change
Expand Up @@ -638,28 +638,25 @@ pp.parseMatchCaseTest = function (node) {
const elseNode = this.startNode();
this.next();
node.test = this.finishNode(elseNode, "MatchElse");
} else if (this.match(tt.braceL) || this.match(tt.bracketL)) {
// disambiguate `| { a, b }:` from `| { a, b }~someFn():`
const bindingOrTest = this.parseExprOps(false, { start: 0 });
try {
node.binding = this.toAssignable(bindingOrTest.__clone(), true, "match test");
node.test = null;
} catch (_err) {
node.test = bindingOrTest;
}
} else if (this.eat(tt._with)) {
this.parseMatchCaseBinding(node);
} else {
node.test = this.parseExprOps();
}

if (this.eat(tt._with)) {
if (node.binding) this.unexpected(this.state.lastTokStart, "Cannot destructure twice.");
if (!(this.match(tt.braceL) || this.match(tt.bracketL))) this.unexpected();
node.binding = this.parseBindingAtom();
this.parseMatchCaseBinding(node);
}

this.state.inMatchCaseTest = false;
};

pp.parseMatchCaseBinding = function (node) {
if (node.binding) this.unexpected(this.state.lastTokStart, "Cannot destructure twice.");
if (!(this.match(tt.braceL) || this.match(tt.bracketL))) this.unexpected();
node.binding = this.parseBindingAtom();
};


export default function (instance) {

Expand Down
14 changes: 9 additions & 5 deletions test/fixtures/lightscript/match/destructuring-arr/actual.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
match x:
// | [ a, b ] and a > 1: a + b
| [ a, b ]: a - b
| [ a, b = 2 ]: a + b - 2
| [ a, ...b ]: b.concat(a)
| [
| with [ a, b ]: a - b
| with [ a, b = 2 ]: a + b - 2
| with [ a, ...b ]: b.concat(a)
| with [
[
b
d = 'e'
Expand All @@ -12,3 +11,8 @@ match x:
...j
]:
[b, d, g, ...j].join('')
| foo with [ a, b ]: a - b
| foo with [ a, b = 2 ]: a + b - 2
| foo with [ a, ...b ]: b.concat(a)
| foo with [[b, d = 'e'], [g,,h], ...j ]:
[b, d, g, ...j].join('')
Loading

0 comments on commit 2815b3d

Please sign in to comment.