Skip to content

Commit 85d3bb1

Browse files
committed
Enforce with before destructuring in match case
1 parent b7c7c88 commit 85d3bb1

File tree

14 files changed

+3377
-1013
lines changed

14 files changed

+3377
-1013
lines changed

src/plugins/lightscript.js

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -638,28 +638,25 @@ pp.parseMatchCaseTest = function (node) {
638638
const elseNode = this.startNode();
639639
this.next();
640640
node.test = this.finishNode(elseNode, "MatchElse");
641-
} else if (this.match(tt.braceL) || this.match(tt.bracketL)) {
642-
// disambiguate `| { a, b }:` from `| { a, b }~someFn():`
643-
const bindingOrTest = this.parseExprOps(false, { start: 0 });
644-
try {
645-
node.binding = this.toAssignable(bindingOrTest.__clone(), true, "match test");
646-
node.test = null;
647-
} catch (_err) {
648-
node.test = bindingOrTest;
649-
}
641+
} else if (this.eat(tt._with)) {
642+
this.parseMatchCaseBinding(node);
650643
} else {
651644
node.test = this.parseExprOps();
652645
}
653646

654647
if (this.eat(tt._with)) {
655-
if (node.binding) this.unexpected(this.state.lastTokStart, "Cannot destructure twice.");
656-
if (!(this.match(tt.braceL) || this.match(tt.bracketL))) this.unexpected();
657-
node.binding = this.parseBindingAtom();
648+
this.parseMatchCaseBinding(node);
658649
}
659650

660651
this.state.inMatchCaseTest = false;
661652
};
662653

654+
pp.parseMatchCaseBinding = function (node) {
655+
if (node.binding) this.unexpected(this.state.lastTokStart, "Cannot destructure twice.");
656+
if (!(this.match(tt.braceL) || this.match(tt.bracketL))) this.unexpected();
657+
node.binding = this.parseBindingAtom();
658+
};
659+
663660

664661
export default function (instance) {
665662

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
match x:
2-
// | [ a, b ] and a > 1: a + b
3-
| [ a, b ]: a - b
4-
| [ a, b = 2 ]: a + b - 2
5-
| [ a, ...b ]: b.concat(a)
6-
| [
2+
| with [ a, b ]: a - b
3+
| with [ a, b = 2 ]: a + b - 2
4+
| with [ a, ...b ]: b.concat(a)
5+
| with [
76
[
87
b
98
d = 'e'
@@ -12,3 +11,8 @@ match x:
1211
...j
1312
]:
1413
[b, d, g, ...j].join('')
14+
| foo with [ a, b ]: a - b
15+
| foo with [ a, b = 2 ]: a + b - 2
16+
| foo with [ a, ...b ]: b.concat(a)
17+
| foo with [[b, d = 'e'], [g,,h], ...j ]:
18+
[b, d, g, ...j].join('')

0 commit comments

Comments
 (0)