Skip to content

Commit 837a358

Browse files
committed
Destructure as patterns, allow destructured 'as'
1 parent e4b33f5 commit 837a358

File tree

12 files changed

+1403
-1582
lines changed

12 files changed

+1403
-1582
lines changed

src/parser/expression.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -678,8 +678,7 @@ pp.parseExprAtom = function (refShorthandDefaultPos) {
678678
return this.parseParenAndDistinguishExpression(null, null, canBeArrow);
679679

680680
case tt.bracketL:
681-
if (this.hasPlugin("lightscript") && this.state.inMatchCaseTest && !refShorthandDefaultPos) {
682-
// re-enter this fn, but with refShorthandDefaultPos and state tracking
681+
if (this.hasPlugin("lightscript") && this.state.inMatchCaseTest) {
683682
return this.parseMatchCaseTestPattern();
684683
}
685684
node = this.startNode();
@@ -692,8 +691,7 @@ pp.parseExprAtom = function (refShorthandDefaultPos) {
692691
return this.finishNode(node, "ArrayExpression");
693692

694693
case tt.braceL:
695-
if (this.hasPlugin("lightscript") && this.state.inMatchCaseTest && !refShorthandDefaultPos) {
696-
// re-enter this fn, but with refShorthandDefaultPos and state tracking
694+
if (this.hasPlugin("lightscript") && this.state.inMatchCaseTest) {
697695
return this.parseMatchCaseTestPattern();
698696
}
699697
return this.parseObj(false, refShorthandDefaultPos);

src/plugins/lightscript.js

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -593,15 +593,16 @@ pp.parseMatch = function (node, isExpression) {
593593
pp.parseMatchCase = function (isExpression) {
594594
const node = this.startNode();
595595

596+
this.state.allowMatchCaseTestPattern = true;
596597
node.test = this.parseMatchCaseTest();
597-
598598
if (this.isContextual("as")) {
599599
if (!this.state.allowMatchCaseTestPattern) {
600600
this.unexpected(null, "Cannot rename after destructuring.");
601601
}
602602
this.next();
603-
node.binding = this.parseIdentifier();
603+
node.binding = this.parseBindingAtom();
604604
}
605+
this.state.allowMatchCaseTestPattern = false;
605606

606607
if (isExpression) {
607608
// disallow return/continue/break, etc. c/p doExpression
@@ -624,7 +625,6 @@ pp.parseMatchCase = function (isExpression) {
624625
pp.parseMatchCaseTest = function () {
625626
// can't be nested so no need to read/restore old value
626627
this.state.inMatchCaseTest = true;
627-
this.state.allowMatchCaseTestPattern = true;
628628

629629
this.expect(tt.bitwiseOR);
630630
if (this.isLineBreak()) this.unexpected(this.state.lastTokEnd, "Illegal newline.");
@@ -639,7 +639,6 @@ pp.parseMatchCaseTest = function () {
639639
}
640640

641641
this.state.inMatchCaseTest = false;
642-
this.state.allowMatchCaseTestPattern = false;
643642
return test;
644643
};
645644

@@ -686,9 +685,7 @@ pp.parseMatchCaseTestPattern = function () {
686685
const oldInMatchCaseTestPattern = this.state.inMatchCaseTestPattern;
687686
this.state.inMatchCaseTestPattern = true;
688687

689-
// re-enter parent function, allowing patterns.
690-
const refShorthandDefaultPos = { start: 0 };
691-
const node = this.parseExprAtom(refShorthandDefaultPos);
688+
const node = this.parseBindingAtom();
692689

693690
// once we have finished recursing through a pattern, disallow future patterns
694691
this.state.inMatchCaseTestPattern = oldInMatchCaseTestPattern;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
match x:
2+
| .a as { a, b = 1 }: a + b
3+
| .0 and .1 as [ a, b ]: a + b

0 commit comments

Comments
 (0)