AST examples of Pattern Matching ESTree Proposal
Note: The ExpressionStatement
and Program
node are omitted in the examples.
match (x) {
when ({ colour: "#000000" }) { "yes" }
else {
"no"
}
}
match (f()) as [x, y] {
when ([_, _, 200]) { "foo"; }
else {
process(x, y)
}
}
{
"type": "MatchExpression",
"discriminant": {
"type": "CallExpression",
"callee": { "type": "Identifier", "name": "f" },
"arguments": []
},
"id": {
"type": "ArrayPattern",
"elements": [
{ "type": "Identifier", "name": "x" },
{ "type": "Identifier", "name": "y" }
]
},
"clauses": [{
"type": "MatchClause",
"test": {
"type": "ArrayMatchPattern",
"elements": [
{ "type": "NullMatchPattern" },
{ "type": "NullMatchPattern" },
{ "type": "Literal", "value": 20 }
]
},
"consequent": {
"type": "BlockStatement",
"body": [{
"type": "ExpressionStatement",
"expression": { "type": "StringLiteral", "value": "foo" }
}]
},
"guard": null,
"id": null
}, {
"type": "MatchClause",
"test": null,
"consequent": {
"type": "BlockStatement",
"body": [{
"type": "CallExpression",
"callee": { "type": "Identifier", "name": "process" },
"arguments": [
{ "type": "Identifier", "name": "x" },
{ "type": "Identifier", "name": "y" }
]
}]
},
"guard": null,
"id": null
}]
}
match (x) {
when (/(?<x>\d+)/) as { groups: { y } } if (isEmpty(y)) {}
}
{
"type": "MatchExpression",
"discriminant": { "type": "Identifier", "name": "x" },
"id": null,
"clauses": [{
"type": "MatchClause",
"test": {
"type": "Literal",
"regex": { "pattern": "(?<x>\\d+)", "flags": "" }
},
"consequent": {
"type": "BlockStatement",
"body": []
},
"guard": {
"type": "CallExpression",
"callee": { "type": "Identifier", "name": "isEmpty" },
"arguments": [
{ "type": "Identifier", "name": "y" }
]
},
"id": {
"type": "ObjectPattern",
"properties": [{
"type": "Property",
"key": { "type": "Identifier", "name": "groups" },
"value": {
"type": "ObjectPattern",
"properties": [{
"type": "Property",
"key": { "type": "Identifier", "name": "y" }
}]
},
"kind": "init",
"method": false
}]
}
}]
}
match (x) {
if(isEmpty(x)) {}
when({ status: 200 | 201 }) {}
}
{
"type": "MatchExpression",
"discriminant": { "type": "Identifier", "name": "x" },
"id": null,
"clauses": [{
"type": "MatchClause",
"test": {
"type": "Literal",
"regex": { "pattern": "(?<x>\\d+)", "flags": "" }
},
"consequent": {
"type": "BlockStatement",
"body": []
},
"guard": {
"type": "CallExpression",
"callee": { "type": "Identifier", "name": "isEmpty" },
"arguments": [{ "type": "Identifier", "name": "x" }]
},
"id": null,
}, {
"type": "MatchClause",
"test": {
"type": "ObjectMatchPattern",
"properties": [{
"type": "Property",
"key": { "type": "StringLiteral", "value": "status" },
"value": {
"type": "BinaryMatchPattern",
"operator": "or",
"left": { "type": "Literal", "value": 200 },
"right": { "type": "Literal", "value": 201 }
},
"kind": "init",
"method": false
}]
},
"consequent": {
"type": "BlockStatement",
"body": []
},
"guard": null,
"id": null
}]
}
match (x) {
when ({
input: (/(?<x>\d+(?<d>[NSEW]))/ with [_, 200, _]) as { groups: { d } }
}) {}
}
{
"type": "MatchExpression",
"discriminant": { "type": "Identifier", "name": "x" },
"id": null,
"clauses": [{
"type": "MatchClause",
"test": {
"type": "ObjectMatchPattern",
"properties": [{
"type": "Property",
"key": { "type": "StringLiteral", "value": "input" },
"value": {
"type": "AsMatchPattern",
"test": {
"type": "BinaryMatchPattern",
"operator": "with",
"left": {
"type": "Literal",
"regex": { "pattern": "(?<x>\\d+(?<d>[NSEW]))", "flags": "" }
},
"right": {
"type": "ArrayMatchPattern",
"elements": [
{ "type": "NullMatchPattern" },
{ "type": "Literal", "value": 20 },
{ "type": "NullMatchPattern" }
]
}
},
"id": {
"type": "ObjectPattern",
"properties": [{
"type": "Property",
"key": { "type": "Identifier", "name": "groups" },
"value": {
"type": "ObjectPattern",
"properties": [{
"type": "Property",
"key": { "type": "Identifier", "name": "d" }
}]
},
"kind": "init",
"method": false
}]
}
},
"kind": "init",
"method": false
}]
},
"consequent": {
"type": "BlockStatement",
"body": []
},
"guard": null,
"id": null
}]
}
match (token) {
when (^LF | ^CR) {}
}
{
"type": "MatchExpression",
"discriminant": { "type": "Identifier", "name": "x" },
"id": null,
"clauses": [{
"type": "MatchClause",
"test": {
"type": "BinaryMatchPattern",
"operator": "or",
"left": {
"type": "ExpressionMatchPattern",
"expression": { "type": "Identifier", "name": "LF" }
},
"right": {
"type": "ExpressionMatchPattern",
"expression": { "type": "Identifier", "name": "CR" }
}
},
"consequent": {
"type": "BlockStatement",
"body": []
},
"guard": null,
"id": null
}]
}
match (action) {
when (["take", ...{ [computed]: -200 }]) {}
}
{
"type": "MatchExpression",
"discriminant": { "type": "Identifier", "name": "action" },
"id": null,
"clauses": [{
"type": "MatchClause",
"test": {
"type": "ArrayMatchPattern",
"elements": [
{ "type": "Literal", "value": "take" },
{
"type": "RestMatchElement",
"argument": {
"type": "ObjectMatchPattern",
"properties": [{
"type": "Property",
"key": { "type": "Identifier", "name": "computed" },
"computed": true,
"value": {
"type": "UnaryExpression",
"operator": "-",
"prefix": true,
"argument": { "type": "Literal", "value": 200 }
},
"kind": "init",
"method": false
}]
}
}]
},
"consequent": {
"type": "BlockStatement",
"body": []
},
"guard": null,
"id": null
}]
}