Skip to content

Commit df8b03a

Browse files
committed
Special parsing of within
1 parent 4ab4179 commit df8b03a

File tree

6 files changed

+1153
-4
lines changed

6 files changed

+1153
-4
lines changed

src/parser/expression.js

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,7 @@ pp.parseMaybeUnary = function (refShorthandDefaultPos) {
294294
const matchCaseBinaryPlusMin = this.hasPlugin("lightscript") &&
295295
this.state.inMatchCaseTest &&
296296
this.match(tt.plusMin) &&
297+
this.state.tokens[this.state.tokens.length - 1].value !== "!" &&
297298
this.isNextCharWhitespace();
298299

299300
if (this.state.type.prefix && !matchCaseBinaryPlusMin) {
@@ -312,7 +313,19 @@ pp.parseMaybeUnary = function (refShorthandDefaultPos) {
312313
this.next();
313314

314315
const argType = this.state.type;
315-
node.argument = this.parseMaybeUnary();
316+
317+
// change precedence / allow autofill of `not` within `match` test
318+
if (this.hasPlugin("lightscript") && node.operator === "!" && this.state.inMatchCaseTest && !argType.startsExpr) {
319+
if (this.match(tt.colon) || this.match(tt.logicalOR) || this.match(tt.logicalAND)) {
320+
// allow `| not:`, `| !!:`, `| not or x:`
321+
node.argument = this.parseMatchCasePlaceholder();
322+
} else {
323+
// change precedence of `| not < 3:` to `!(x < 3)` from `(!x) < 3`
324+
node.argument = this.parseExprOps();
325+
}
326+
} else {
327+
node.argument = this.parseMaybeUnary();
328+
}
316329

317330
this.addExtra(node, "parenthesizedArgument", argType === tt.parenL && (!node.argument.extra || !node.argument.extra.parenthesized));
318331

@@ -762,9 +775,7 @@ pp.parseExprAtom = function (refShorthandDefaultPos) {
762775

763776
default:
764777
if (this.hasPlugin("lightscript") && this.allowMatchCasePlaceholder()) {
765-
// use the blank space as an empty value (perhaps 0-length would be better)
766-
node = this.startNodeAt(this.state.lastTokEnd, this.state.lastTokEndLoc);
767-
return this.finishNodeAt(node, "PlaceholderExpression", this.state.start, this.state.startLoc);
778+
return this.parseMatchCasePlaceholder();
768779
}
769780
this.unexpected();
770781
}

src/plugins/lightscript.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -700,6 +700,12 @@ pp.parseMatchCaseTestPattern = function () {
700700
return node;
701701
};
702702

703+
pp.parseMatchCasePlaceholder = function () {
704+
// use the blank space as an empty value (perhaps 0-length would be better)
705+
const node = this.startNodeAt(this.state.lastTokEnd, this.state.lastTokEndLoc);
706+
return this.finishNodeAt(node, "PlaceholderExpression", this.state.start, this.state.startLoc);
707+
};
708+
703709

704710
export default function (instance) {
705711

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
match x:
2+
| f(!): false
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"throws": "Unexpected token (2:7)"
3+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
match x:
2+
| not: true
3+
| !!: true
4+
| not < 3: true
5+
| not + 1: true
6+
| ! ~f(): true
7+
| not .prop: true
8+
| not or not not and not: true
9+
| ! or !! and !: true

0 commit comments

Comments
 (0)