-
Notifications
You must be signed in to change notification settings - Fork 358
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add pattern matching #251
base: master
Are you sure you want to change the base?
add pattern matching #251
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
# [Pattern Matching](proposal-pattern-matching) | ||
|
||
See also [AST examples](ast-examples) for this proposal. | ||
|
||
## MatchExpression | ||
|
||
```js | ||
interface MatchExpression <: Expression { | ||
type: "MatchExpression"; | ||
discriminant: Expression; | ||
id: Pattern | null; | ||
clauses: [ MatchClause ] | ||
} | ||
``` | ||
|
||
## MatchClause | ||
|
||
```js | ||
interface MatchClause <: Node { | ||
type: "MatchClause"; | ||
test: MatchPattern | null; | ||
guard: Expression | null; | ||
id: Pattern | null; | ||
consequent: BlockStatement | ||
} | ||
``` | ||
|
||
If `test` is `null`, `id` must be `null`. | ||
|
||
## MatchPattern | ||
|
||
```js | ||
interface MatchPattern <: Node {} | ||
``` | ||
|
||
While a Match Pattern shares similar structures with a Pattern, it is different from a Pattern: | ||
|
||
- Literals or negative numbers are match patterns, but they are not patterns | ||
- Any expression can be converted to an expression match pattern | ||
- Multiple match patterns can be joined with `and`, `or` and `with` operators, but patterns can not | ||
- Unlike array and object patterns, assignment patterns have no corresponding syntax in match patterns | ||
|
||
## ArrayMatchPattern | ||
|
||
```js | ||
interface ArrayMatchPattern <: MatchPattern { | ||
type: "ArrayMatchPattern"; | ||
elements: [ MatchPattern ]; | ||
} | ||
``` | ||
|
||
## ObjectMatchPattern | ||
|
||
```js | ||
interface AssignmentMatchProperty <: Property { | ||
type: "Property"; // inherited | ||
value: MatchPattern; | ||
kind: "init"; | ||
method: false; | ||
} | ||
|
||
interface ObjectMatchPattern <: MatchPattern { | ||
type: "ObjectMatchPattern"; | ||
properties: [ AssignmentMatchProperty ]; | ||
JLHwung marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
``` | ||
|
||
## RestMatchElement | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
|
||
```js | ||
interface RestMatchElement <: MatchPattern { | ||
type: "RestMatchElement"; | ||
argument: MatchPattern; | ||
} | ||
``` | ||
|
||
## BinaryMatchPattern | ||
|
||
```js | ||
interface BinaryMatchPattern <: MatchPattern { | ||
type: "BinaryMatchPattern"; | ||
operator: "and" | "or" | "with"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The champions have not decided the combinator token of |
||
left: MatchPattern; | ||
right: MatchPattern; | ||
} | ||
``` | ||
|
||
## AsMatchPattern | ||
|
||
```js | ||
interface AsMatchPattern <: MatchPattern { | ||
type: "AsMatchPattern"; | ||
test: MatchPattern; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
id: Pattern; | ||
} | ||
``` | ||
|
||
## ExpressionMatchPattern | ||
|
||
```js | ||
interface ExpressionMatchPattern <: MatchPattern { | ||
type: "ExpressionMatchPattern"; | ||
expression: Expression; | ||
} | ||
``` | ||
|
||
## NullMatchPattern | ||
|
||
```js | ||
interface NullMatchPattern <: MatchPattern { | ||
type: "NullMatchPattern"; | ||
} | ||
``` | ||
|
||
# Literal | ||
|
||
```js | ||
extend interface Literal <: Expression, MatchPattern {} | ||
``` | ||
|
||
# Identifier | ||
|
||
```js | ||
extend interface Identifier <: Expression, Pattern, MatchPattern {} | ||
``` | ||
|
||
### UnaryExpression | ||
|
||
```js | ||
extend interface UnaryExpression <: Expression, MatchPattern {} | ||
``` | ||
|
||
If a UnaryExpression is a MatchPattern, its `operator` must be `"-"`, its `prefix` must be `true`, and its `argument` must be either a Literal with numeric value, or an identifier named `"Infinity"`. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. as a pattern, this also allows Also, template literals haven't been thought out here - i filed tc39/proposal-pattern-matching#205 to track There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah,
The The template literals in ESTree has a specific type |
||
|
||
[proposal-pattern-matching]: https://tc39.es/proposal-pattern-matching/ | ||
[ast-examples]: https://gist.github.com/JLHwung/d2b64364dc63757282cef182367b4e84 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we add a mention here as to why we don’t just use Pattern? (You mentioned in the PR itself but seems important to include here as well.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, addressed in d67b89a.