Skip to content

Commit 5b3a411

Browse files
committed
Change destructuring, remove 'as'
1 parent df8b03a commit 5b3a411

File tree

38 files changed

+2311
-1465
lines changed

38 files changed

+2311
-1465
lines changed

src/parser/expression.js

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -691,9 +691,6 @@ pp.parseExprAtom = function (refShorthandDefaultPos) {
691691
return this.parseParenAndDistinguishExpression(null, null, canBeArrow);
692692

693693
case tt.bracketL:
694-
if (this.hasPlugin("lightscript") && this.state.inMatchCaseTest) {
695-
return this.parseMatchCaseTestPattern();
696-
}
697694
node = this.startNode();
698695
this.next();
699696
if (this.hasPlugin("lightscript") && this.match(tt._for)) {
@@ -704,9 +701,6 @@ pp.parseExprAtom = function (refShorthandDefaultPos) {
704701
return this.finishNode(node, "ArrayExpression");
705702

706703
case tt.braceL:
707-
if (this.hasPlugin("lightscript") && this.state.inMatchCaseTest) {
708-
return this.parseMatchCaseTestPattern();
709-
}
710704
return this.parseObj(false, refShorthandDefaultPos);
711705

712706
case tt._function:

src/plugins/lightscript.js

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -581,7 +581,7 @@ pp.parseMatch = function (node, isExpression) {
581581
}
582582

583583
const matchCase = this.parseMatchCase(isExpression);
584-
if (matchCase.test.type === "MatchElse") {
584+
if (matchCase.test && matchCase.test.type === "MatchElse") {
585585
hasUsedElse = true;
586586
}
587587
node.cases.push(matchCase);
@@ -597,16 +597,7 @@ pp.parseMatch = function (node, isExpression) {
597597
pp.parseMatchCase = function (isExpression) {
598598
const node = this.startNode();
599599

600-
this.state.allowMatchCaseTestPattern = true;
601-
node.test = this.parseMatchCaseTest();
602-
if (this.isContextual("as")) {
603-
if (!this.state.allowMatchCaseTestPattern) {
604-
this.unexpected(null, "Cannot rename after destructuring.");
605-
}
606-
this.next();
607-
node.binding = this.parseBindingAtom();
608-
}
609-
this.state.allowMatchCaseTestPattern = false;
600+
this.parseMatchCaseTest(node);
610601

611602
if (isExpression) {
612603
// disallow return/continue/break, etc. c/p doExpression
@@ -626,24 +617,37 @@ pp.parseMatchCase = function (isExpression) {
626617
return this.finishNode(node, "MatchCase");
627618
};
628619

629-
pp.parseMatchCaseTest = function () {
620+
pp.parseMatchCaseTest = function (node) {
630621
// can't be nested so no need to read/restore old value
631622
this.state.inMatchCaseTest = true;
632623

633624
this.expect(tt.bitwiseOR);
634625
if (this.isLineBreak()) this.unexpected(this.state.lastTokEnd, "Illegal newline.");
635626

636-
let test;
637627
if (this.match(tt._else)) {
638628
const elseNode = this.startNode();
639629
this.next();
640-
test = this.finishNode(elseNode, "MatchElse");
630+
node.test = this.finishNode(elseNode, "MatchElse");
631+
} else if (this.match(tt.braceL) || this.match(tt.bracketL)) {
632+
// disambiguate `| { a, b }:` from `| { a, b }~someFn():`
633+
const bindingOrTest = this.parseExprOps(false, { start: 0 });
634+
try {
635+
node.binding = this.toAssignable(bindingOrTest.__clone(), true, 'match test');
636+
node.test = null;
637+
} catch (_err) {
638+
node.test = bindingOrTest;
639+
}
641640
} else {
642-
test = this.parseExprOps();
641+
node.test = this.parseExprOps();
642+
}
643+
644+
if (this.eat(tt._with)) {
645+
if (node.binding) this.unexpected(this.state.lastTokStart, "Cannot destructure twice.")
646+
if (!(this.match(tt.braceL) || this.match(tt.bracketL))) this.unexpected();
647+
node.binding = this.parseBindingAtom();
643648
}
644649

645650
this.state.inMatchCaseTest = false;
646-
return test;
647651
};
648652

649653
pp.isBinaryTokenForMatchCase = function (tokenType) {

test/fixtures/lightscript/commaless/obj-pattern/actual.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
a
33
b: {
44
c
5-
d
5+
d = 1
6+
e
67
}
7-
e
8-
} = f
8+
f
9+
...g
10+
} = h

0 commit comments

Comments
 (0)