Add optionality to catch bindings (#634)

* Add optionality to catch bindings (plus tests)

* Update ast/spec, README, set param to null if no param with plugin optionalCatchBinding

* Fix: wrap param = null in else case

* Fix tests for optional catch binding; add tests which include finally clause
This commit is contained in:
MarckK 2017-07-21 15:18:57 +01:00 committed by Henry Zhu
parent 77bdb9ae3e
commit c88af90c0a
23 changed files with 788 additions and 7 deletions

View File

@ -146,6 +146,7 @@ require("babylon").parse("code", {
| `optionalChaining` ([proposal](https://github.com/tc39/proposal-optional-chaining)) | `a?.b` |
| `importMeta` ([proposal](https://github.com/tc39/proposal-import-meta)) | `import.meta.url` |
| `bigInt` ([proposal](https://github.com/tc39/proposal-bigint)) | `100n` |
| `optionalCatchBinding` ([proposal](https://github.com/babel/proposals/issues/7)) | `try {throw 0;} catch{do();}` |
### FAQ

View File

@ -442,7 +442,7 @@ A `try` statement. If `handler` is `null` then `finalizer` must be a `BlockState
```js
interface CatchClause <: Node {
type: "CatchClause";
param: Pattern;
param: Pattern | null;
body: BlockStatement;
}
```

View File

@ -491,12 +491,14 @@ export default class StatementParser extends ExpressionParser {
if (this.match(tt._catch)) {
const clause = this.startNode();
this.next();
if (this.match(tt.parenL) || !this.hasPlugin("optionalCatchBinding")) {
this.expect(tt.parenL);
clause.param = this.parseBindingAtom();
this.checkLVal(clause.param, true, Object.create(null), "catch clause");
this.expect(tt.parenR);
} else {
clause.param = null;
}
clause.body = this.parseBlock();
node.handler = this.finishNode(clause, "CatchClause");
}

View File

@ -0,0 +1,9 @@
try {
}
catch {
}
finally {
}

View File

@ -0,0 +1,3 @@
{
"throws": "Unexpected token, expected ( (4:6)"
}

View File

@ -0,0 +1,6 @@
try {
}
catch {
}

View File

@ -0,0 +1,3 @@
{
"throws": "Unexpected token, expected ( (4:6)"
}

View File

@ -0,0 +1,4 @@
try {
} catch (err) {
} finally {
}

View File

@ -0,0 +1,133 @@
{
"type": "File",
"start": 0,
"end": 35,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 4,
"column": 1
}
},
"program": {
"type": "Program",
"start": 0,
"end": 35,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 4,
"column": 1
}
},
"sourceType": "script",
"body": [
{
"type": "TryStatement",
"start": 0,
"end": 35,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 4,
"column": 1
}
},
"block": {
"type": "BlockStatement",
"start": 4,
"end": 7,
"loc": {
"start": {
"line": 1,
"column": 4
},
"end": {
"line": 2,
"column": 1
}
},
"body": [],
"directives": []
},
"handler": {
"type": "CatchClause",
"start": 8,
"end": 23,
"loc": {
"start": {
"line": 2,
"column": 2
},
"end": {
"line": 3,
"column": 1
}
},
"param": {
"type": "Identifier",
"start": 15,
"end": 18,
"loc": {
"start": {
"line": 2,
"column": 9
},
"end": {
"line": 2,
"column": 12
},
"identifierName": "err"
},
"name": "err"
},
"body": {
"type": "BlockStatement",
"start": 20,
"end": 23,
"loc": {
"start": {
"line": 2,
"column": 14
},
"end": {
"line": 3,
"column": 1
}
},
"body": [],
"directives": []
}
},
"guardedHandlers": [],
"finalizer": {
"type": "BlockStatement",
"start": 32,
"end": 35,
"loc": {
"start": {
"line": 3,
"column": 10
},
"end": {
"line": 4,
"column": 1
}
},
"body": [],
"directives": []
}
}
],
"directives": []
}
}

View File

@ -0,0 +1,2 @@
try {
} catch (err) {}

View File

@ -0,0 +1,117 @@
{
"type": "File",
"start": 0,
"end": 22,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 2,
"column": 16
}
},
"program": {
"type": "Program",
"start": 0,
"end": 22,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 2,
"column": 16
}
},
"sourceType": "script",
"body": [
{
"type": "TryStatement",
"start": 0,
"end": 22,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 2,
"column": 16
}
},
"block": {
"type": "BlockStatement",
"start": 4,
"end": 7,
"loc": {
"start": {
"line": 1,
"column": 4
},
"end": {
"line": 2,
"column": 1
}
},
"body": [],
"directives": []
},
"handler": {
"type": "CatchClause",
"start": 8,
"end": 22,
"loc": {
"start": {
"line": 2,
"column": 2
},
"end": {
"line": 2,
"column": 16
}
},
"param": {
"type": "Identifier",
"start": 15,
"end": 18,
"loc": {
"start": {
"line": 2,
"column": 9
},
"end": {
"line": 2,
"column": 12
},
"identifierName": "err"
},
"name": "err"
},
"body": {
"type": "BlockStatement",
"start": 20,
"end": 22,
"loc": {
"start": {
"line": 2,
"column": 14
},
"end": {
"line": 2,
"column": 16
}
},
"body": [],
"directives": []
}
},
"guardedHandlers": [],
"finalizer": null
}
],
"directives": []
}
}

View File

@ -0,0 +1,9 @@
try {
}
catch {
}
finally {
}

View File

@ -0,0 +1,117 @@
{
"type": "File",
"start": 0,
"end": 34,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 9,
"column": 1
}
},
"program": {
"type": "Program",
"start": 0,
"end": 34,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 9,
"column": 1
}
},
"sourceType": "script",
"body": [
{
"type": "TryStatement",
"start": 0,
"end": 34,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 9,
"column": 1
}
},
"block": {
"type": "BlockStatement",
"start": 4,
"end": 8,
"loc": {
"start": {
"line": 1,
"column": 4
},
"end": {
"line": 3,
"column": 1
}
},
"body": [],
"directives": []
},
"handler": {
"type": "CatchClause",
"start": 9,
"end": 19,
"loc": {
"start": {
"line": 4,
"column": 0
},
"end": {
"line": 6,
"column": 1
}
},
"param": null,
"body": {
"type": "BlockStatement",
"start": 15,
"end": 19,
"loc": {
"start": {
"line": 4,
"column": 6
},
"end": {
"line": 6,
"column": 1
}
},
"body": [],
"directives": []
}
},
"guardedHandlers": [],
"finalizer": {
"type": "BlockStatement",
"start": 28,
"end": 34,
"loc": {
"start": {
"line": 7,
"column": 8
},
"end": {
"line": 9,
"column": 1
}
},
"body": [],
"directives": []
}
}
],
"directives": []
}
}

View File

@ -0,0 +1,3 @@
{
"plugins": ["optionalCatchBinding"]
}

View File

@ -0,0 +1,6 @@
try {
}
catch {
}

View File

@ -0,0 +1,101 @@
{
"type": "File",
"start": 0,
"end": 21,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 6,
"column": 1
}
},
"program": {
"type": "Program",
"start": 0,
"end": 21,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 6,
"column": 1
}
},
"sourceType": "script",
"body": [
{
"type": "TryStatement",
"start": 0,
"end": 21,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 6,
"column": 1
}
},
"block": {
"type": "BlockStatement",
"start": 4,
"end": 8,
"loc": {
"start": {
"line": 1,
"column": 4
},
"end": {
"line": 3,
"column": 1
}
},
"body": [],
"directives": []
},
"handler": {
"type": "CatchClause",
"start": 9,
"end": 21,
"loc": {
"start": {
"line": 4,
"column": 0
},
"end": {
"line": 6,
"column": 1
}
},
"param": null,
"body": {
"type": "BlockStatement",
"start": 15,
"end": 21,
"loc": {
"start": {
"line": 4,
"column": 6
},
"end": {
"line": 6,
"column": 1
}
},
"body": [],
"directives": []
}
},
"guardedHandlers": [],
"finalizer": null
}
],
"directives": []
}
}

View File

@ -0,0 +1,3 @@
{
"plugins": ["optionalCatchBinding"]
}

View File

@ -0,0 +1,4 @@
try {
} catch (err) {
} finally {
}

View File

@ -0,0 +1,133 @@
{
"type": "File",
"start": 0,
"end": 35,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 4,
"column": 1
}
},
"program": {
"type": "Program",
"start": 0,
"end": 35,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 4,
"column": 1
}
},
"sourceType": "script",
"body": [
{
"type": "TryStatement",
"start": 0,
"end": 35,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 4,
"column": 1
}
},
"block": {
"type": "BlockStatement",
"start": 4,
"end": 7,
"loc": {
"start": {
"line": 1,
"column": 4
},
"end": {
"line": 2,
"column": 1
}
},
"body": [],
"directives": []
},
"handler": {
"type": "CatchClause",
"start": 8,
"end": 23,
"loc": {
"start": {
"line": 2,
"column": 2
},
"end": {
"line": 3,
"column": 1
}
},
"param": {
"type": "Identifier",
"start": 15,
"end": 18,
"loc": {
"start": {
"line": 2,
"column": 9
},
"end": {
"line": 2,
"column": 12
},
"identifierName": "err"
},
"name": "err"
},
"body": {
"type": "BlockStatement",
"start": 20,
"end": 23,
"loc": {
"start": {
"line": 2,
"column": 14
},
"end": {
"line": 3,
"column": 1
}
},
"body": [],
"directives": []
}
},
"guardedHandlers": [],
"finalizer": {
"type": "BlockStatement",
"start": 32,
"end": 35,
"loc": {
"start": {
"line": 3,
"column": 10
},
"end": {
"line": 4,
"column": 1
}
},
"body": [],
"directives": []
}
}
],
"directives": []
}
}

View File

@ -0,0 +1,3 @@
{
"plugins": ["optionalCatchBinding"]
}

View File

@ -0,0 +1,2 @@
try {
} catch (err) {}

View File

@ -0,0 +1,117 @@
{
"type": "File",
"start": 0,
"end": 22,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 2,
"column": 16
}
},
"program": {
"type": "Program",
"start": 0,
"end": 22,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 2,
"column": 16
}
},
"sourceType": "script",
"body": [
{
"type": "TryStatement",
"start": 0,
"end": 22,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 2,
"column": 16
}
},
"block": {
"type": "BlockStatement",
"start": 4,
"end": 7,
"loc": {
"start": {
"line": 1,
"column": 4
},
"end": {
"line": 2,
"column": 1
}
},
"body": [],
"directives": []
},
"handler": {
"type": "CatchClause",
"start": 8,
"end": 22,
"loc": {
"start": {
"line": 2,
"column": 2
},
"end": {
"line": 2,
"column": 16
}
},
"param": {
"type": "Identifier",
"start": 15,
"end": 18,
"loc": {
"start": {
"line": 2,
"column": 9
},
"end": {
"line": 2,
"column": 12
},
"identifierName": "err"
},
"name": "err"
},
"body": {
"type": "BlockStatement",
"start": 20,
"end": 22,
"loc": {
"start": {
"line": 2,
"column": 14
},
"end": {
"line": 2,
"column": 16
}
},
"body": [],
"directives": []
}
},
"guardedHandlers": [],
"finalizer": null
}
],
"directives": []
}
}

View File

@ -0,0 +1,3 @@
{
"plugins": ["optionalCatchBinding"]
}