Add throw expressions (#740)

This commit is contained in:
Justin Ridgewell 2017-09-27 14:12:30 -04:00 committed by Henry Zhu
parent ef8e30419c
commit 8af007d560
19 changed files with 666 additions and 3 deletions

View File

@ -148,6 +148,7 @@ require("babylon").parse("code", {
| `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();}` |
| `throwExpressions` ([proposal](https://github.com/babel/proposals/issues/23)) | `() => throw new Error("")` |
### FAQ

View File

@ -736,7 +736,7 @@ A unary operator expression.
```js
enum UnaryOperator {
"-" | "+" | "!" | "~" | "typeof" | "void" | "delete"
"-" | "+" | "!" | "~" | "typeof" | "void" | "delete" | "throw"
}
```

View File

@ -324,6 +324,10 @@ export default class ExpressionParser extends LValParser {
const update = this.match(tt.incDec);
node.operator = this.state.value;
node.prefix = true;
if (node.operator === "throw") {
this.expectPlugin("throwExpressions");
}
this.next();
const argType = this.state.type;

View File

@ -165,7 +165,7 @@ export const keywords = {
if: new KeywordTokenType("if"),
return: new KeywordTokenType("return", { beforeExpr }),
switch: new KeywordTokenType("switch"),
throw: new KeywordTokenType("throw", { beforeExpr }),
throw: new KeywordTokenType("throw", { beforeExpr, prefix, startsExpr }),
try: new KeywordTokenType("try"),
var: new KeywordTokenType("var"),
let: new KeywordTokenType("let"),

View File

@ -423,7 +423,8 @@ export type UnaryOperator =
| "~"
| "typeof"
| "void"
| "delete";
| "delete"
| "throw";
export type UpdateExpression = NodeBase & {
type: "UpdateExpression",

View File

@ -0,0 +1,3 @@
function test() {
(throw 1, 2);
}

View File

@ -0,0 +1,184 @@
{
"type": "File",
"start": 0,
"end": 35,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 3,
"column": 1
}
},
"program": {
"type": "Program",
"start": 0,
"end": 35,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 3,
"column": 1
}
},
"sourceType": "script",
"body": [
{
"type": "FunctionDeclaration",
"start": 0,
"end": 35,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 3,
"column": 1
}
},
"id": {
"type": "Identifier",
"start": 9,
"end": 13,
"loc": {
"start": {
"line": 1,
"column": 9
},
"end": {
"line": 1,
"column": 13
},
"identifierName": "test"
},
"name": "test"
},
"generator": false,
"expression": false,
"async": false,
"params": [],
"body": {
"type": "BlockStatement",
"start": 16,
"end": 35,
"loc": {
"start": {
"line": 1,
"column": 16
},
"end": {
"line": 3,
"column": 1
}
},
"body": [
{
"type": "ExpressionStatement",
"start": 20,
"end": 33,
"loc": {
"start": {
"line": 2,
"column": 2
},
"end": {
"line": 2,
"column": 15
}
},
"expression": {
"type": "SequenceExpression",
"start": 21,
"end": 31,
"loc": {
"start": {
"line": 2,
"column": 3
},
"end": {
"line": 2,
"column": 13
}
},
"expressions": [
{
"type": "UnaryExpression",
"start": 21,
"end": 28,
"loc": {
"start": {
"line": 2,
"column": 3
},
"end": {
"line": 2,
"column": 10
}
},
"operator": "throw",
"prefix": true,
"argument": {
"type": "NumericLiteral",
"start": 27,
"end": 28,
"loc": {
"start": {
"line": 2,
"column": 9
},
"end": {
"line": 2,
"column": 10
}
},
"extra": {
"rawValue": 1,
"raw": "1"
},
"value": 1
},
"extra": {
"parenthesizedArgument": false
}
},
{
"type": "NumericLiteral",
"start": 30,
"end": 31,
"loc": {
"start": {
"line": 2,
"column": 12
},
"end": {
"line": 2,
"column": 13
}
},
"extra": {
"rawValue": 2,
"raw": "2"
},
"value": 2
}
],
"extra": {
"parenthesized": true,
"parenStart": 20
}
}
}
],
"directives": []
}
}
],
"directives": []
}
}

View File

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

View File

@ -0,0 +1,3 @@
function test() {
(throw 1);
}

View File

@ -0,0 +1,145 @@
{
"type": "File",
"start": 0,
"end": 32,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 3,
"column": 1
}
},
"program": {
"type": "Program",
"start": 0,
"end": 32,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 3,
"column": 1
}
},
"sourceType": "script",
"body": [
{
"type": "FunctionDeclaration",
"start": 0,
"end": 32,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 3,
"column": 1
}
},
"id": {
"type": "Identifier",
"start": 9,
"end": 13,
"loc": {
"start": {
"line": 1,
"column": 9
},
"end": {
"line": 1,
"column": 13
},
"identifierName": "test"
},
"name": "test"
},
"generator": false,
"expression": false,
"async": false,
"params": [],
"body": {
"type": "BlockStatement",
"start": 16,
"end": 32,
"loc": {
"start": {
"line": 1,
"column": 16
},
"end": {
"line": 3,
"column": 1
}
},
"body": [
{
"type": "ExpressionStatement",
"start": 20,
"end": 30,
"loc": {
"start": {
"line": 2,
"column": 2
},
"end": {
"line": 2,
"column": 12
}
},
"expression": {
"type": "UnaryExpression",
"start": 21,
"end": 28,
"loc": {
"start": {
"line": 2,
"column": 3
},
"end": {
"line": 2,
"column": 10
}
},
"operator": "throw",
"prefix": true,
"argument": {
"type": "NumericLiteral",
"start": 27,
"end": 28,
"loc": {
"start": {
"line": 2,
"column": 9
},
"end": {
"line": 2,
"column": 10
}
},
"extra": {
"rawValue": 1,
"raw": "1"
},
"value": 1
},
"extra": {
"parenthesizedArgument": false,
"parenthesized": true,
"parenStart": 20
}
}
}
],
"directives": []
}
}
],
"directives": []
}
}

View File

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

View File

@ -0,0 +1,3 @@
function test() {
true && throw 1;
}

View File

@ -0,0 +1,175 @@
{
"type": "File",
"start": 0,
"end": 38,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 3,
"column": 1
}
},
"program": {
"type": "Program",
"start": 0,
"end": 38,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 3,
"column": 1
}
},
"sourceType": "script",
"body": [
{
"type": "FunctionDeclaration",
"start": 0,
"end": 38,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 3,
"column": 1
}
},
"id": {
"type": "Identifier",
"start": 9,
"end": 13,
"loc": {
"start": {
"line": 1,
"column": 9
},
"end": {
"line": 1,
"column": 13
},
"identifierName": "test"
},
"name": "test"
},
"generator": false,
"expression": false,
"async": false,
"params": [],
"body": {
"type": "BlockStatement",
"start": 16,
"end": 38,
"loc": {
"start": {
"line": 1,
"column": 16
},
"end": {
"line": 3,
"column": 1
}
},
"body": [
{
"type": "ExpressionStatement",
"start": 20,
"end": 36,
"loc": {
"start": {
"line": 2,
"column": 2
},
"end": {
"line": 2,
"column": 18
}
},
"expression": {
"type": "LogicalExpression",
"start": 20,
"end": 35,
"loc": {
"start": {
"line": 2,
"column": 2
},
"end": {
"line": 2,
"column": 17
}
},
"left": {
"type": "BooleanLiteral",
"start": 20,
"end": 24,
"loc": {
"start": {
"line": 2,
"column": 2
},
"end": {
"line": 2,
"column": 6
}
},
"value": true
},
"operator": "&&",
"right": {
"type": "UnaryExpression",
"start": 28,
"end": 35,
"loc": {
"start": {
"line": 2,
"column": 10
},
"end": {
"line": 2,
"column": 17
}
},
"operator": "throw",
"prefix": true,
"argument": {
"type": "NumericLiteral",
"start": 34,
"end": 35,
"loc": {
"start": {
"line": 2,
"column": 16
},
"end": {
"line": 2,
"column": 17
}
},
"extra": {
"rawValue": 1,
"raw": "1"
},
"value": 1
},
"extra": {
"parenthesizedArgument": false
}
}
}
}
],
"directives": []
}
}
],
"directives": []
}
}

View File

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

View File

@ -0,0 +1,3 @@
function test() {
(throw 1, 2);
}

View File

@ -0,0 +1,3 @@
{
"throws": "This experimental syntax requires enabling the parser plugin: 'throwExpressions' (2:3)"
}

View File

@ -0,0 +1,3 @@
function test() {
throw 1;
}

View File

@ -0,0 +1,123 @@
{
"type": "File",
"start": 0,
"end": 30,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 3,
"column": 1
}
},
"program": {
"type": "Program",
"start": 0,
"end": 30,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 3,
"column": 1
}
},
"sourceType": "script",
"body": [
{
"type": "FunctionDeclaration",
"start": 0,
"end": 30,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 3,
"column": 1
}
},
"id": {
"type": "Identifier",
"start": 9,
"end": 13,
"loc": {
"start": {
"line": 1,
"column": 9
},
"end": {
"line": 1,
"column": 13
},
"identifierName": "test"
},
"name": "test"
},
"generator": false,
"expression": false,
"async": false,
"params": [],
"body": {
"type": "BlockStatement",
"start": 16,
"end": 30,
"loc": {
"start": {
"line": 1,
"column": 16
},
"end": {
"line": 3,
"column": 1
}
},
"body": [
{
"type": "ThrowStatement",
"start": 20,
"end": 28,
"loc": {
"start": {
"line": 2,
"column": 2
},
"end": {
"line": 2,
"column": 10
}
},
"argument": {
"type": "NumericLiteral",
"start": 26,
"end": 27,
"loc": {
"start": {
"line": 2,
"column": 8
},
"end": {
"line": 2,
"column": 9
}
},
"extra": {
"rawValue": 1,
"raw": "1"
},
"value": 1
}
}
],
"directives": []
}
}
],
"directives": []
}
}

View File

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