Skip to content

Commit

Permalink
Throw unexpected for different binary match pattern operators at the …
Browse files Browse the repository at this point in the history
…same level
  • Loading branch information
fedeci committed Sep 3, 2021
1 parent 5078ace commit 04cb052
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 4 deletions.
2 changes: 2 additions & 0 deletions packages/babel-parser/src/parser/error-message.js
Expand Up @@ -77,6 +77,8 @@ export const ErrorMessages = makeErrorTemplates(
ImportCallNotNewExpression: "Cannot use new with import(...).",
ImportCallSpreadArgument: "`...` is not allowed in `import()`.",
InvalidBigIntLiteral: "Invalid BigIntLiteral.",
InvalidBinaryMatchPattern:
"Different binary match pattern operators cannot be at the same level.",
InvalidCodePoint: "Code point out of bounds.",
InvalidDecimal: "Invalid decimal.",
InvalidDigit: "Expected number in radix %0.",
Expand Down
14 changes: 10 additions & 4 deletions packages/babel-parser/src/parser/expression.js
Expand Up @@ -3082,14 +3082,20 @@ export default class ExpressionParser extends LValParser {
return this.finishNode(node, "UnaryExpression");
}

parseMaybeBinaryMatchPattern(): N.BinaryMatchPattern | N.MatchPattern {
parseMaybeBinaryMatchPattern(
previousOp?: "and" | "or",
): N.BinaryMatchPattern | N.MatchPattern {
const node = this.startNode<N.BinaryMatchPattern>();
const lhs = this.parseMatchPattern();
if (this.match(tt.bitwiseOR) || this.match(tt.bitwiseAND)) {
const operator = this.match(tt.bitwiseOR) ? "or" : "and";
if (previousOp && previousOp !== operator) {
this.unexpected(this.state.start, Errors.InvalidBinaryMatchPattern);
}
this.next(); // skip "or" or "and"
node.left = lhs;
node.operator = this.match(tt.bitwiseOR) ? "or" : "and";
this.next();
node.right = this.parseMaybeBinaryMatchPattern();
node.operator = operator;
node.right = this.parseMaybeBinaryMatchPattern(operator);
return this.finishNode(node, "BinaryMatchPattern");
} else {
return lhs;
Expand Down
@@ -0,0 +1,3 @@
match (x) {
when ({ v: 200 | 300 & 400 }) {}
}
@@ -0,0 +1,8 @@
{
"plugins": [
[
"patternMatching"
]
],
"throws": "Different binary match pattern operators cannot be at the same level. (2:23)"
}

0 comments on commit 04cb052

Please sign in to comment.