Parse async do expressions (#13043)

* parse async do expressions

* add test cases

* update test fixtures

* chore: add syntax-async-do-expressions

* generater support

* fix: do not transform async do expressions

* chore: add asyncDoExpressions to missing plugin helpers

* update ast types

* add more test cases

* throw when asyncDoExpressions is enabled but not doExpressions

* avoid add parentheses for async do expressions

* address review comments

* chore: update parser typings
This commit is contained in:
Huáng Jùnliàng 2021-04-07 12:24:33 -04:00 committed by Nicolò Ribaudo
parent f30c99aa24
commit 28d7442aae
76 changed files with 897 additions and 23 deletions

View File

@ -1,4 +1,10 @@
const pluginNameMap = {
asyncDoExpressions: {
syntax: {
name: "@babel/plugin-syntax-async-do-expressions",
url: "https://git.io/JYer8",
},
},
classProperties: {
syntax: {
name: "@babel/plugin-syntax-class-properties",

View File

@ -20,6 +20,10 @@ export function UnaryExpression(this: Printer, node: t.UnaryExpression) {
}
export function DoExpression(this: Printer, node: t.DoExpression) {
if (node.async) {
this.word("async");
this.space();
}
this.word("do");
this.space();
this.print(node.body, node);

View File

@ -82,7 +82,8 @@ export function DoExpression(
parent: any,
printStack: Array<any>,
): boolean {
return isFirstInStatement(printStack);
// `async do` can start an expression statement
return !node.async && isFirstInStatement(printStack);
}
export function Binary(node: any, parent: any): boolean {

View File

@ -0,0 +1,5 @@
async do {
1;
};
(async do {});

View File

@ -0,0 +1,4 @@
async do {
1;
};
async do {};

View File

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

View File

@ -0,0 +1,3 @@
/* leading comments
*/async do
{ 1 } + 0;

View File

@ -0,0 +1,3 @@
{
"retainLines": true
}

View File

@ -0,0 +1,3 @@
/* leading comments
*/async do
{1;} + 0;

View File

@ -1084,6 +1084,7 @@ An expression wrapped by parentheses. By default `@babel/parser` does not create
interface DoExpression <: Expression {
type: "DoExpression";
body: BlockStatement;
async: boolean;
}
```

View File

@ -1033,6 +1033,8 @@ export default class ExpressionParser extends LValParser {
);
} else if (this.match(tt.name)) {
return this.parseAsyncArrowUnaryFunction(id);
} else if (this.match(tt._do)) {
return this.parseDo(true);
}
}
@ -1049,7 +1051,7 @@ export default class ExpressionParser extends LValParser {
}
case tt._do: {
return this.parseDo();
return this.parseDo(false);
}
case tt.regexp: {
@ -1231,13 +1233,27 @@ export default class ExpressionParser extends LValParser {
}
// https://github.com/tc39/proposal-do-expressions
parseDo(): N.DoExpression {
// https://github.com/tc39/proposal-async-do-expressions
parseDo(isAsync: boolean): N.DoExpression {
this.expectPlugin("doExpressions");
if (isAsync) {
this.expectPlugin("asyncDoExpressions");
}
const node = this.startNode();
node.async = isAsync;
this.next(); // eat `do`
const oldLabels = this.state.labels;
this.state.labels = [];
if (isAsync) {
// AsyncDoExpression :
// async [no LineTerminator here] do Block[~Yield, +Await, ~Return]
this.prodParam.enter(PARAM_AWAIT);
node.body = this.parseBlock();
this.prodParam.exit();
} else {
node.body = this.parseBlock();
}
this.state.labels = oldLabels;
return this.finishNode(node, "DoExpression");
}

View File

@ -117,6 +117,18 @@ export function validatePlugins(plugins: PluginList) {
RECORD_AND_TUPLE_SYNTAX_TYPES.map(p => `'${p}'`).join(", "),
);
}
if (
hasPlugin(plugins, "asyncDoExpressions") &&
!hasPlugin(plugins, "doExpressions")
) {
const error = new Error(
"'asyncDoExpressions' requires 'doExpressions', please add 'doExpressions' to parser plugins.",
);
// $FlowIgnore
error.missingPlugins = "doExpressions"; // so @babel/core can provide better error message
throw error;
}
}
// These plugins are defined using a mixin which extends the parser class.

View File

@ -402,6 +402,7 @@ export type ArrayExpression = NodeBase & {
export type DoExpression = NodeBase & {
type: "DoExpression",
body: ?BlockStatement,
async: boolean,
};
export type TupleExpression = NodeBase & {

View File

@ -0,0 +1,6 @@
{
"throws": "This experimental syntax requires enabling the parser plugin: 'asyncDoExpressions' (1:7)",
"plugins": [
"doExpressions"
]
}

View File

@ -0,0 +1 @@
(async do {x})

View File

@ -0,0 +1,4 @@
{
"throws": "This experimental syntax requires enabling the parser plugin: 'doExpressions' (1:7)",
"plugins": []
}

View File

@ -0,0 +1,5 @@
async
do {
42
}
while (false);

View File

@ -0,0 +1,51 @@
{
"type": "File",
"start":0,"end":32,"loc":{"start":{"line":1,"column":0},"end":{"line":5,"column":14}},
"program": {
"type": "Program",
"start":0,"end":32,"loc":{"start":{"line":1,"column":0},"end":{"line":5,"column":14}},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "ExpressionStatement",
"start":0,"end":5,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":5}},
"expression": {
"type": "Identifier",
"start":0,"end":5,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":5},"identifierName":"async"},
"name": "async"
}
},
{
"type": "DoWhileStatement",
"start":6,"end":32,"loc":{"start":{"line":2,"column":0},"end":{"line":5,"column":14}},
"body": {
"type": "BlockStatement",
"start":9,"end":17,"loc":{"start":{"line":2,"column":3},"end":{"line":4,"column":1}},
"body": [
{
"type": "ExpressionStatement",
"start":13,"end":15,"loc":{"start":{"line":3,"column":2},"end":{"line":3,"column":4}},
"expression": {
"type": "NumericLiteral",
"start":13,"end":15,"loc":{"start":{"line":3,"column":2},"end":{"line":3,"column":4}},
"extra": {
"rawValue": 42,
"raw": "42"
},
"value": 42
}
}
],
"directives": []
},
"test": {
"type": "BooleanLiteral",
"start":25,"end":30,"loc":{"start":{"line":5,"column":7},"end":{"line":5,"column":12}},
"value": false
}
}
],
"directives": []
}
}

View File

@ -0,0 +1,4 @@
async do {
42
}
while (false);

View File

@ -0,0 +1,55 @@
{
"type": "File",
"start":0,"end":32,"loc":{"start":{"line":1,"column":0},"end":{"line":4,"column":14}},
"program": {
"type": "Program",
"start":0,"end":32,"loc":{"start":{"line":1,"column":0},"end":{"line":4,"column":14}},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "ExpressionStatement",
"start":0,"end":17,"loc":{"start":{"line":1,"column":0},"end":{"line":3,"column":1}},
"expression": {
"type": "DoExpression",
"start":6,"end":17,"loc":{"start":{"line":1,"column":6},"end":{"line":3,"column":1}},
"async": true,
"body": {
"type": "BlockStatement",
"start":9,"end":17,"loc":{"start":{"line":1,"column":9},"end":{"line":3,"column":1}},
"body": [
{
"type": "ExpressionStatement",
"start":13,"end":15,"loc":{"start":{"line":2,"column":2},"end":{"line":2,"column":4}},
"expression": {
"type": "NumericLiteral",
"start":13,"end":15,"loc":{"start":{"line":2,"column":2},"end":{"line":2,"column":4}},
"extra": {
"rawValue": 42,
"raw": "42"
},
"value": 42
}
}
],
"directives": []
}
}
},
{
"type": "WhileStatement",
"start":18,"end":32,"loc":{"start":{"line":4,"column":0},"end":{"line":4,"column":14}},
"test": {
"type": "BooleanLiteral",
"start":25,"end":30,"loc":{"start":{"line":4,"column":7},"end":{"line":4,"column":12}},
"value": false
},
"body": {
"type": "EmptyStatement",
"start":31,"end":32,"loc":{"start":{"line":4,"column":13},"end":{"line":4,"column":14}}
}
}
],
"directives": []
}
}

View File

@ -0,0 +1,4 @@
async
do {
42
}

View File

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

View File

@ -0,0 +1,5 @@
let x = async do {
if (foo()) { f() }
else if (bar()) { g() }
else { h() }
};

View File

@ -0,0 +1,132 @@
{
"type": "File",
"start":0,"end":83,"loc":{"start":{"line":1,"column":0},"end":{"line":5,"column":2}},
"program": {
"type": "Program",
"start":0,"end":83,"loc":{"start":{"line":1,"column":0},"end":{"line":5,"column":2}},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "VariableDeclaration",
"start":0,"end":83,"loc":{"start":{"line":1,"column":0},"end":{"line":5,"column":2}},
"declarations": [
{
"type": "VariableDeclarator",
"start":4,"end":82,"loc":{"start":{"line":1,"column":4},"end":{"line":5,"column":1}},
"id": {
"type": "Identifier",
"start":4,"end":5,"loc":{"start":{"line":1,"column":4},"end":{"line":1,"column":5},"identifierName":"x"},
"name": "x"
},
"init": {
"type": "DoExpression",
"start":14,"end":82,"loc":{"start":{"line":1,"column":14},"end":{"line":5,"column":1}},
"async": true,
"body": {
"type": "BlockStatement",
"start":17,"end":82,"loc":{"start":{"line":1,"column":17},"end":{"line":5,"column":1}},
"body": [
{
"type": "IfStatement",
"start":21,"end":80,"loc":{"start":{"line":2,"column":2},"end":{"line":4,"column":14}},
"test": {
"type": "CallExpression",
"start":25,"end":30,"loc":{"start":{"line":2,"column":6},"end":{"line":2,"column":11}},
"callee": {
"type": "Identifier",
"start":25,"end":28,"loc":{"start":{"line":2,"column":6},"end":{"line":2,"column":9},"identifierName":"foo"},
"name": "foo"
},
"arguments": []
},
"consequent": {
"type": "BlockStatement",
"start":32,"end":39,"loc":{"start":{"line":2,"column":13},"end":{"line":2,"column":20}},
"body": [
{
"type": "ExpressionStatement",
"start":34,"end":37,"loc":{"start":{"line":2,"column":15},"end":{"line":2,"column":18}},
"expression": {
"type": "CallExpression",
"start":34,"end":37,"loc":{"start":{"line":2,"column":15},"end":{"line":2,"column":18}},
"callee": {
"type": "Identifier",
"start":34,"end":35,"loc":{"start":{"line":2,"column":15},"end":{"line":2,"column":16},"identifierName":"f"},
"name": "f"
},
"arguments": []
}
}
],
"directives": []
},
"alternate": {
"type": "IfStatement",
"start":47,"end":80,"loc":{"start":{"line":3,"column":7},"end":{"line":4,"column":14}},
"test": {
"type": "CallExpression",
"start":51,"end":56,"loc":{"start":{"line":3,"column":11},"end":{"line":3,"column":16}},
"callee": {
"type": "Identifier",
"start":51,"end":54,"loc":{"start":{"line":3,"column":11},"end":{"line":3,"column":14},"identifierName":"bar"},
"name": "bar"
},
"arguments": []
},
"consequent": {
"type": "BlockStatement",
"start":58,"end":65,"loc":{"start":{"line":3,"column":18},"end":{"line":3,"column":25}},
"body": [
{
"type": "ExpressionStatement",
"start":60,"end":63,"loc":{"start":{"line":3,"column":20},"end":{"line":3,"column":23}},
"expression": {
"type": "CallExpression",
"start":60,"end":63,"loc":{"start":{"line":3,"column":20},"end":{"line":3,"column":23}},
"callee": {
"type": "Identifier",
"start":60,"end":61,"loc":{"start":{"line":3,"column":20},"end":{"line":3,"column":21},"identifierName":"g"},
"name": "g"
},
"arguments": []
}
}
],
"directives": []
},
"alternate": {
"type": "BlockStatement",
"start":73,"end":80,"loc":{"start":{"line":4,"column":7},"end":{"line":4,"column":14}},
"body": [
{
"type": "ExpressionStatement",
"start":75,"end":78,"loc":{"start":{"line":4,"column":9},"end":{"line":4,"column":12}},
"expression": {
"type": "CallExpression",
"start":75,"end":78,"loc":{"start":{"line":4,"column":9},"end":{"line":4,"column":12}},
"callee": {
"type": "Identifier",
"start":75,"end":76,"loc":{"start":{"line":4,"column":9},"end":{"line":4,"column":10},"identifierName":"h"},
"name": "h"
},
"arguments": []
}
}
],
"directives": []
}
}
}
],
"directives": []
}
}
}
],
"kind": "let"
}
],
"directives": []
}
}

View File

@ -0,0 +1,3 @@
async do {
await 42
}

View File

@ -0,0 +1,46 @@
{
"type": "File",
"start":0,"end":23,"loc":{"start":{"line":1,"column":0},"end":{"line":3,"column":1}},
"program": {
"type": "Program",
"start":0,"end":23,"loc":{"start":{"line":1,"column":0},"end":{"line":3,"column":1}},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "ExpressionStatement",
"start":0,"end":23,"loc":{"start":{"line":1,"column":0},"end":{"line":3,"column":1}},
"expression": {
"type": "DoExpression",
"start":6,"end":23,"loc":{"start":{"line":1,"column":6},"end":{"line":3,"column":1}},
"async": true,
"body": {
"type": "BlockStatement",
"start":9,"end":23,"loc":{"start":{"line":1,"column":9},"end":{"line":3,"column":1}},
"body": [
{
"type": "ExpressionStatement",
"start":13,"end":21,"loc":{"start":{"line":2,"column":2},"end":{"line":2,"column":10}},
"expression": {
"type": "AwaitExpression",
"start":13,"end":21,"loc":{"start":{"line":2,"column":2},"end":{"line":2,"column":10}},
"argument": {
"type": "NumericLiteral",
"start":19,"end":21,"loc":{"start":{"line":2,"column":8},"end":{"line":2,"column":10}},
"extra": {
"rawValue": 42,
"raw": "42"
},
"value": 42
}
}
}
],
"directives": []
}
}
}
],
"directives": []
}
}

View File

@ -0,0 +1,8 @@
function iter() {
switch(1) {
default:
var x = async do {
break;
}
}
};

View File

@ -0,0 +1,94 @@
{
"type": "File",
"start":0,"end":99,"loc":{"start":{"line":1,"column":0},"end":{"line":8,"column":2}},
"errors": [
"SyntaxError: Unsyntactic break (5:8)"
],
"program": {
"type": "Program",
"start":0,"end":99,"loc":{"start":{"line":1,"column":0},"end":{"line":8,"column":2}},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "FunctionDeclaration",
"start":0,"end":98,"loc":{"start":{"line":1,"column":0},"end":{"line":8,"column":1}},
"id": {
"type": "Identifier",
"start":9,"end":13,"loc":{"start":{"line":1,"column":9},"end":{"line":1,"column":13},"identifierName":"iter"},
"name": "iter"
},
"generator": false,
"async": false,
"params": [],
"body": {
"type": "BlockStatement",
"start":16,"end":98,"loc":{"start":{"line":1,"column":16},"end":{"line":8,"column":1}},
"body": [
{
"type": "SwitchStatement",
"start":20,"end":96,"loc":{"start":{"line":2,"column":2},"end":{"line":7,"column":3}},
"discriminant": {
"type": "NumericLiteral",
"start":27,"end":28,"loc":{"start":{"line":2,"column":9},"end":{"line":2,"column":10}},
"extra": {
"rawValue": 1,
"raw": "1"
},
"value": 1
},
"cases": [
{
"type": "SwitchCase",
"start":36,"end":92,"loc":{"start":{"line":3,"column":4},"end":{"line":6,"column":7}},
"consequent": [
{
"type": "VariableDeclaration",
"start":51,"end":92,"loc":{"start":{"line":4,"column":6},"end":{"line":6,"column":7}},
"declarations": [
{
"type": "VariableDeclarator",
"start":55,"end":92,"loc":{"start":{"line":4,"column":10},"end":{"line":6,"column":7}},
"id": {
"type": "Identifier",
"start":55,"end":56,"loc":{"start":{"line":4,"column":10},"end":{"line":4,"column":11},"identifierName":"x"},
"name": "x"
},
"init": {
"type": "DoExpression",
"start":65,"end":92,"loc":{"start":{"line":4,"column":20},"end":{"line":6,"column":7}},
"async": true,
"body": {
"type": "BlockStatement",
"start":68,"end":92,"loc":{"start":{"line":4,"column":23},"end":{"line":6,"column":7}},
"body": [
{
"type": "BreakStatement",
"start":78,"end":84,"loc":{"start":{"line":5,"column":8},"end":{"line":5,"column":14}},
"label": null
}
],
"directives": []
}
}
}
],
"kind": "var"
}
],
"test": null
}
]
}
],
"directives": []
}
},
{
"type": "EmptyStatement",
"start":98,"end":99,"loc":{"start":{"line":8,"column":1},"end":{"line":8,"column":2}}
}
],
"directives": []
}
}

View File

@ -0,0 +1,5 @@
async function *iter() {
await async do {
yield 1;
}
};

View File

@ -0,0 +1,81 @@
{
"type": "File",
"start":0,"end":63,"loc":{"start":{"line":1,"column":0},"end":{"line":5,"column":2}},
"errors": [
"SyntaxError: Missing semicolon (3:9)"
],
"program": {
"type": "Program",
"start":0,"end":63,"loc":{"start":{"line":1,"column":0},"end":{"line":5,"column":2}},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "FunctionDeclaration",
"start":0,"end":62,"loc":{"start":{"line":1,"column":0},"end":{"line":5,"column":1}},
"id": {
"type": "Identifier",
"start":16,"end":20,"loc":{"start":{"line":1,"column":16},"end":{"line":1,"column":20},"identifierName":"iter"},
"name": "iter"
},
"generator": true,
"async": true,
"params": [],
"body": {
"type": "BlockStatement",
"start":23,"end":62,"loc":{"start":{"line":1,"column":23},"end":{"line":5,"column":1}},
"body": [
{
"type": "ExpressionStatement",
"start":27,"end":60,"loc":{"start":{"line":2,"column":2},"end":{"line":4,"column":3}},
"expression": {
"type": "AwaitExpression",
"start":27,"end":60,"loc":{"start":{"line":2,"column":2},"end":{"line":4,"column":3}},
"argument": {
"type": "DoExpression",
"start":39,"end":60,"loc":{"start":{"line":2,"column":14},"end":{"line":4,"column":3}},
"async": true,
"body": {
"type": "BlockStatement",
"start":42,"end":60,"loc":{"start":{"line":2,"column":17},"end":{"line":4,"column":3}},
"body": [
{
"type": "ExpressionStatement",
"start":48,"end":53,"loc":{"start":{"line":3,"column":4},"end":{"line":3,"column":9}},
"expression": {
"type": "Identifier",
"start":48,"end":53,"loc":{"start":{"line":3,"column":4},"end":{"line":3,"column":9},"identifierName":"yield"},
"name": "yield"
}
},
{
"type": "ExpressionStatement",
"start":54,"end":56,"loc":{"start":{"line":3,"column":10},"end":{"line":3,"column":12}},
"expression": {
"type": "NumericLiteral",
"start":54,"end":55,"loc":{"start":{"line":3,"column":10},"end":{"line":3,"column":11}},
"extra": {
"rawValue": 1,
"raw": "1"
},
"value": 1
}
}
],
"directives": []
}
}
}
}
],
"directives": []
}
},
{
"type": "EmptyStatement",
"start":62,"end":63,"loc":{"start":{"line":5,"column":1},"end":{"line":5,"column":2}}
}
],
"directives": []
}
}

View File

@ -0,0 +1,5 @@
function iter() {
return async do {
return 1;
}
};

View File

@ -0,0 +1,68 @@
{
"type": "File",
"start":0,"end":58,"loc":{"start":{"line":1,"column":0},"end":{"line":5,"column":2}},
"errors": [
"SyntaxError: 'return' outside of function (3:4)"
],
"program": {
"type": "Program",
"start":0,"end":58,"loc":{"start":{"line":1,"column":0},"end":{"line":5,"column":2}},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "FunctionDeclaration",
"start":0,"end":57,"loc":{"start":{"line":1,"column":0},"end":{"line":5,"column":1}},
"id": {
"type": "Identifier",
"start":9,"end":13,"loc":{"start":{"line":1,"column":9},"end":{"line":1,"column":13},"identifierName":"iter"},
"name": "iter"
},
"generator": false,
"async": false,
"params": [],
"body": {
"type": "BlockStatement",
"start":16,"end":57,"loc":{"start":{"line":1,"column":16},"end":{"line":5,"column":1}},
"body": [
{
"type": "ReturnStatement",
"start":20,"end":55,"loc":{"start":{"line":2,"column":2},"end":{"line":4,"column":3}},
"argument": {
"type": "DoExpression",
"start":33,"end":55,"loc":{"start":{"line":2,"column":15},"end":{"line":4,"column":3}},
"async": true,
"body": {
"type": "BlockStatement",
"start":36,"end":55,"loc":{"start":{"line":2,"column":18},"end":{"line":4,"column":3}},
"body": [
{
"type": "ReturnStatement",
"start":42,"end":51,"loc":{"start":{"line":3,"column":4},"end":{"line":3,"column":13}},
"argument": {
"type": "NumericLiteral",
"start":49,"end":50,"loc":{"start":{"line":3,"column":11},"end":{"line":3,"column":12}},
"extra": {
"rawValue": 1,
"raw": "1"
},
"value": 1
}
}
],
"directives": []
}
}
}
],
"directives": []
}
},
{
"type": "EmptyStatement",
"start":57,"end":58,"loc":{"start":{"line":5,"column":1},"end":{"line":5,"column":2}}
}
],
"directives": []
}
}

View File

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

View File

@ -0,0 +1,4 @@
let x = async do {
let tmp = f();
tmp * tmp + 1
};

View File

@ -0,0 +1,100 @@
{
"type": "File",
"start":0,"end":54,"loc":{"start":{"line":1,"column":0},"end":{"line":4,"column":2}},
"program": {
"type": "Program",
"start":0,"end":54,"loc":{"start":{"line":1,"column":0},"end":{"line":4,"column":2}},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "VariableDeclaration",
"start":0,"end":54,"loc":{"start":{"line":1,"column":0},"end":{"line":4,"column":2}},
"declarations": [
{
"type": "VariableDeclarator",
"start":4,"end":53,"loc":{"start":{"line":1,"column":4},"end":{"line":4,"column":1}},
"id": {
"type": "Identifier",
"start":4,"end":5,"loc":{"start":{"line":1,"column":4},"end":{"line":1,"column":5},"identifierName":"x"},
"name": "x"
},
"init": {
"type": "DoExpression",
"start":14,"end":53,"loc":{"start":{"line":1,"column":14},"end":{"line":4,"column":1}},
"async": true,
"body": {
"type": "BlockStatement",
"start":17,"end":53,"loc":{"start":{"line":1,"column":17},"end":{"line":4,"column":1}},
"body": [
{
"type": "VariableDeclaration",
"start":21,"end":35,"loc":{"start":{"line":2,"column":2},"end":{"line":2,"column":16}},
"declarations": [
{
"type": "VariableDeclarator",
"start":25,"end":34,"loc":{"start":{"line":2,"column":6},"end":{"line":2,"column":15}},
"id": {
"type": "Identifier",
"start":25,"end":28,"loc":{"start":{"line":2,"column":6},"end":{"line":2,"column":9},"identifierName":"tmp"},
"name": "tmp"
},
"init": {
"type": "CallExpression",
"start":31,"end":34,"loc":{"start":{"line":2,"column":12},"end":{"line":2,"column":15}},
"callee": {
"type": "Identifier",
"start":31,"end":32,"loc":{"start":{"line":2,"column":12},"end":{"line":2,"column":13},"identifierName":"f"},
"name": "f"
},
"arguments": []
}
}
],
"kind": "let"
},
{
"type": "ExpressionStatement",
"start":38,"end":51,"loc":{"start":{"line":3,"column":2},"end":{"line":3,"column":15}},
"expression": {
"type": "BinaryExpression",
"start":38,"end":51,"loc":{"start":{"line":3,"column":2},"end":{"line":3,"column":15}},
"left": {
"type": "BinaryExpression",
"start":38,"end":47,"loc":{"start":{"line":3,"column":2},"end":{"line":3,"column":11}},
"left": {
"type": "Identifier",
"start":38,"end":41,"loc":{"start":{"line":3,"column":2},"end":{"line":3,"column":5},"identifierName":"tmp"},
"name": "tmp"
},
"operator": "*",
"right": {
"type": "Identifier",
"start":44,"end":47,"loc":{"start":{"line":3,"column":8},"end":{"line":3,"column":11},"identifierName":"tmp"},
"name": "tmp"
}
},
"operator": "+",
"right": {
"type": "NumericLiteral",
"start":50,"end":51,"loc":{"start":{"line":3,"column":14},"end":{"line":3,"column":15}},
"extra": {
"rawValue": 1,
"raw": "1"
},
"value": 1
}
}
}
],
"directives": []
}
}
}
],
"kind": "let"
}
],
"directives": []
}
}

View File

@ -22,6 +22,7 @@
"init": {
"type": "DoExpression",
"start":8,"end":76,"loc":{"start":{"line":1,"column":8},"end":{"line":5,"column":1}},
"async": false,
"body": {
"type": "BlockStatement",
"start":11,"end":76,"loc":{"start":{"line":1,"column":11},"end":{"line":5,"column":1}},

View File

@ -22,6 +22,7 @@
"init": {
"type": "DoExpression",
"start":8,"end":47,"loc":{"start":{"line":1,"column":8},"end":{"line":4,"column":1}},
"async": false,
"body": {
"type": "BlockStatement",
"start":11,"end":47,"loc":{"start":{"line":1,"column":11},"end":{"line":4,"column":1}},

View File

@ -28,6 +28,10 @@
"argument": {
"type": "JSXElement",
"start":32,"end":216,"loc":{"start":{"line":3,"column":4},"end":{"line":14,"column":10}},
"extra": {
"parenthesized": true,
"parenStart": 26
},
"openingElement": {
"type": "JSXOpeningElement",
"start":32,"end":37,"loc":{"start":{"line":3,"column":4},"end":{"line":3,"column":9}},
@ -90,6 +94,7 @@
"expression": {
"type": "DoExpression",
"start":69,"end":197,"loc":{"start":{"line":6,"column":8},"end":{"line":12,"column":9}},
"async": false,
"body": {
"type": "BlockStatement",
"start":72,"end":197,"loc":{"start":{"line":6,"column":11},"end":{"line":12,"column":9}},
@ -173,11 +178,7 @@
},
"value": "\n "
}
],
"extra": {
"parenthesized": true,
"parenStart": 26
}
]
}
}
],

View File

@ -29,6 +29,7 @@
"expression": {
"type": "DoExpression",
"start":9,"end":42,"loc":{"start":{"line":1,"column":9},"end":{"line":1,"column":42}},
"async": false,
"body": {
"type": "BlockStatement",
"start":12,"end":42,"loc":{"start":{"line":1,"column":12},"end":{"line":1,"column":42}},

View File

@ -44,6 +44,7 @@
"expression": {
"type": "DoExpression",
"start":34,"end":75,"loc":{"start":{"line":2,"column":11},"end":{"line":2,"column":52}},
"async": false,
"body": {
"type": "BlockStatement",
"start":37,"end":75,"loc":{"start":{"line":2,"column":14},"end":{"line":2,"column":52}},

View File

@ -29,6 +29,7 @@
"expression": {
"type": "DoExpression",
"start":9,"end":49,"loc":{"start":{"line":1,"column":9},"end":{"line":1,"column":49}},
"async": false,
"body": {
"type": "BlockStatement",
"start":12,"end":49,"loc":{"start":{"line":1,"column":12},"end":{"line":1,"column":49}},

View File

@ -29,6 +29,7 @@
"expression": {
"type": "DoExpression",
"start":9,"end":36,"loc":{"start":{"line":1,"column":9},"end":{"line":1,"column":36}},
"async": false,
"body": {
"type": "BlockStatement",
"start":12,"end":36,"loc":{"start":{"line":1,"column":12},"end":{"line":1,"column":36}},

View File

@ -29,6 +29,7 @@
"expression": {
"type": "DoExpression",
"start":9,"end":38,"loc":{"start":{"line":1,"column":9},"end":{"line":1,"column":38}},
"async": false,
"body": {
"type": "BlockStatement",
"start":12,"end":38,"loc":{"start":{"line":1,"column":12},"end":{"line":1,"column":38}},

View File

@ -28,6 +28,7 @@
"expression": {
"type": "DoExpression",
"start":9,"end":78,"loc":{"start":{"line":1,"column":9},"end":{"line":4,"column":1}},
"async": false,
"body": {
"type": "BlockStatement",
"start":12,"end":78,"loc":{"start":{"line":1,"column":12},"end":{"line":4,"column":1}},

View File

@ -29,6 +29,7 @@
"expression": {
"type": "DoExpression",
"start":9,"end":38,"loc":{"start":{"line":1,"column":9},"end":{"line":1,"column":38}},
"async": false,
"body": {
"type": "BlockStatement",
"start":12,"end":38,"loc":{"start":{"line":1,"column":12},"end":{"line":1,"column":38}},

View File

@ -29,6 +29,7 @@
"expression": {
"type": "DoExpression",
"start":9,"end":28,"loc":{"start":{"line":1,"column":9},"end":{"line":1,"column":28}},
"async": false,
"body": {
"type": "BlockStatement",
"start":12,"end":28,"loc":{"start":{"line":1,"column":12},"end":{"line":1,"column":28}},

View File

@ -25,6 +25,7 @@
"expression": {
"type": "DoExpression",
"start":9,"end":41,"loc":{"start":{"line":1,"column":9},"end":{"line":1,"column":41}},
"async": false,
"body": {
"type": "BlockStatement",
"start":12,"end":41,"loc":{"start":{"line":1,"column":12},"end":{"line":1,"column":41}},

View File

@ -40,6 +40,7 @@
"expression": {
"type": "DoExpression",
"start":34,"end":68,"loc":{"start":{"line":2,"column":11},"end":{"line":2,"column":45}},
"async": false,
"body": {
"type": "BlockStatement",
"start":37,"end":68,"loc":{"start":{"line":2,"column":14},"end":{"line":2,"column":45}},

View File

@ -25,6 +25,7 @@
"expression": {
"type": "DoExpression",
"start":9,"end":59,"loc":{"start":{"line":1,"column":9},"end":{"line":1,"column":59}},
"async": false,
"body": {
"type": "BlockStatement",
"start":12,"end":59,"loc":{"start":{"line":1,"column":12},"end":{"line":1,"column":59}},

View File

@ -25,6 +25,7 @@
"expression": {
"type": "DoExpression",
"start":9,"end":31,"loc":{"start":{"line":1,"column":9},"end":{"line":1,"column":31}},
"async": false,
"body": {
"type": "BlockStatement",
"start":12,"end":31,"loc":{"start":{"line":1,"column":12},"end":{"line":1,"column":31}},

View File

@ -25,6 +25,7 @@
"expression": {
"type": "DoExpression",
"start":9,"end":31,"loc":{"start":{"line":1,"column":9},"end":{"line":1,"column":31}},
"async": false,
"body": {
"type": "BlockStatement",
"start":12,"end":31,"loc":{"start":{"line":1,"column":12},"end":{"line":1,"column":31}},

View File

@ -25,6 +25,7 @@
"expression": {
"type": "DoExpression",
"start":9,"end":18,"loc":{"start":{"line":1,"column":9},"end":{"line":1,"column":18}},
"async": false,
"body": {
"type": "BlockStatement",
"start":12,"end":18,"loc":{"start":{"line":1,"column":12},"end":{"line":1,"column":18}},

View File

@ -25,6 +25,7 @@
"expression": {
"type": "DoExpression",
"start":9,"end":46,"loc":{"start":{"line":1,"column":9},"end":{"line":1,"column":46}},
"async": false,
"body": {
"type": "BlockStatement",
"start":12,"end":46,"loc":{"start":{"line":1,"column":12},"end":{"line":1,"column":46}},

View File

@ -25,6 +25,7 @@
"expression": {
"type": "DoExpression",
"start":9,"end":38,"loc":{"start":{"line":1,"column":9},"end":{"line":1,"column":38}},
"async": false,
"body": {
"type": "BlockStatement",
"start":12,"end":38,"loc":{"start":{"line":1,"column":12},"end":{"line":1,"column":38}},

View File

@ -25,6 +25,7 @@
"expression": {
"type": "DoExpression",
"start":9,"end":27,"loc":{"start":{"line":1,"column":9},"end":{"line":1,"column":27}},
"async": false,
"body": {
"type": "BlockStatement",
"start":12,"end":27,"loc":{"start":{"line":1,"column":12},"end":{"line":1,"column":27}},

View File

@ -25,6 +25,7 @@
"expression": {
"type": "DoExpression",
"start":9,"end":33,"loc":{"start":{"line":1,"column":9},"end":{"line":1,"column":33}},
"async": false,
"body": {
"type": "BlockStatement",
"start":12,"end":33,"loc":{"start":{"line":1,"column":12},"end":{"line":1,"column":33}},

View File

@ -25,6 +25,7 @@
"expression": {
"type": "DoExpression",
"start":9,"end":94,"loc":{"start":{"line":1,"column":9},"end":{"line":7,"column":1}},
"async": false,
"body": {
"type": "BlockStatement",
"start":12,"end":94,"loc":{"start":{"line":1,"column":12},"end":{"line":7,"column":1}},

View File

@ -25,6 +25,7 @@
"expression": {
"type": "DoExpression",
"start":9,"end":83,"loc":{"start":{"line":1,"column":9},"end":{"line":7,"column":1}},
"async": false,
"body": {
"type": "BlockStatement",
"start":12,"end":83,"loc":{"start":{"line":1,"column":12},"end":{"line":7,"column":1}},

View File

@ -25,6 +25,7 @@
"expression": {
"type": "DoExpression",
"start":9,"end":117,"loc":{"start":{"line":1,"column":9},"end":{"line":5,"column":1}},
"async": false,
"body": {
"type": "BlockStatement",
"start":12,"end":117,"loc":{"start":{"line":1,"column":12},"end":{"line":5,"column":1}},
@ -50,12 +51,12 @@
"start":22,"end":26,"loc":{"start":{"line":2,"column":8},"end":{"line":2,"column":12},"identifierName":"JSON"},
"name": "JSON"
},
"computed": false,
"property": {
"type": "Identifier",
"start":27,"end":32,"loc":{"start":{"line":2,"column":13},"end":{"line":2,"column":18},"identifierName":"parse"},
"name": "parse"
},
"computed": false
}
},
"arguments": [
{
@ -95,12 +96,12 @@
"start":64,"end":71,"loc":{"start":{"line":3,"column":18},"end":{"line":3,"column":25},"identifierName":"console"},
"name": "console"
},
"computed": false,
"property": {
"type": "Identifier",
"start":72,"end":77,"loc":{"start":{"line":3,"column":26},"end":{"line":3,"column":31},"identifierName":"error"},
"name": "error"
},
"computed": false
}
},
"arguments": [
{

View File

@ -25,6 +25,7 @@
"expression": {
"type": "DoExpression",
"start":9,"end":109,"loc":{"start":{"line":1,"column":9},"end":{"line":5,"column":1}},
"async": false,
"body": {
"type": "BlockStatement",
"start":12,"end":109,"loc":{"start":{"line":1,"column":12},"end":{"line":5,"column":1}},
@ -50,12 +51,12 @@
"start":22,"end":26,"loc":{"start":{"line":2,"column":8},"end":{"line":2,"column":12},"identifierName":"JSON"},
"name": "JSON"
},
"computed": false,
"property": {
"type": "Identifier",
"start":27,"end":32,"loc":{"start":{"line":2,"column":13},"end":{"line":2,"column":18},"identifierName":"parse"},
"name": "parse"
},
"computed": false
}
},
"arguments": [
{
@ -94,12 +95,12 @@
"start":57,"end":64,"loc":{"start":{"line":3,"column":18},"end":{"line":3,"column":25},"identifierName":"console"},
"name": "console"
},
"computed": false,
"property": {
"type": "Identifier",
"start":65,"end":70,"loc":{"start":{"line":3,"column":26},"end":{"line":3,"column":31},"identifierName":"error"},
"name": "error"
},
"computed": false
}
},
"arguments": [
{

View File

@ -25,6 +25,7 @@
"expression": {
"type": "DoExpression",
"start":9,"end":82,"loc":{"start":{"line":1,"column":9},"end":{"line":4,"column":1}},
"async": false,
"body": {
"type": "BlockStatement",
"start":12,"end":82,"loc":{"start":{"line":1,"column":12},"end":{"line":4,"column":1}},
@ -50,12 +51,12 @@
"start":22,"end":26,"loc":{"start":{"line":2,"column":8},"end":{"line":2,"column":12},"identifierName":"JSON"},
"name": "JSON"
},
"computed": false,
"property": {
"type": "Identifier",
"start":27,"end":32,"loc":{"start":{"line":2,"column":13},"end":{"line":2,"column":18},"identifierName":"parse"},
"name": "parse"
},
"computed": false
}
},
"arguments": [
{
@ -94,12 +95,12 @@
"start":57,"end":64,"loc":{"start":{"line":3,"column":18},"end":{"line":3,"column":25},"identifierName":"console"},
"name": "console"
},
"computed": false,
"property": {
"type": "Identifier",
"start":65,"end":70,"loc":{"start":{"line":3,"column":26},"end":{"line":3,"column":31},"identifierName":"error"},
"name": "error"
},
"computed": false
}
},
"arguments": [
{

View File

@ -25,6 +25,7 @@
"expression": {
"type": "DoExpression",
"start":9,"end":37,"loc":{"start":{"line":1,"column":9},"end":{"line":1,"column":37}},
"async": false,
"body": {
"type": "BlockStatement",
"start":12,"end":37,"loc":{"start":{"line":1,"column":12},"end":{"line":1,"column":37}},

View File

@ -108,6 +108,7 @@ export interface ParserOptions {
}
export type ParserPlugin =
| "asyncDoExpressions"
| "asyncGenerators"
| "bigInt"
| "classPrivateMethods"

View File

@ -11,7 +11,12 @@ export default declare(api => {
visitor: {
DoExpression: {
exit(path) {
const body = path.node.body.body;
const { node } = path;
if (node.async) {
// Async do expressions are not yet supported
return;
}
const body = node.body.body;
if (body.length) {
path.replaceExpressionWithStatements(body);
} else {

View File

@ -0,0 +1 @@
(async do {});

View File

@ -0,0 +1,3 @@
{
"plugins": ["syntax-async-do-expressions", "proposal-do-expressions"]
}

View File

@ -0,0 +1,3 @@
src
test
*.log

View File

@ -0,0 +1,19 @@
# @babel/plugin-syntax-async-do-expressions
> Allow parsing of async do expressions
See our website [@babel/plugin-syntax-async-do-expressions](https://babel.dev/docs/en/babel-plugin-syntax-async-do-expressions) for more information.
## Install
Using npm:
```sh
npm install --save-dev @babel/plugin-syntax-async-do-expressions
```
or using yarn:
```sh
yarn add @babel/plugin-syntax-async-do-expressions --dev
```

View File

@ -0,0 +1,31 @@
{
"name": "@babel/plugin-syntax-async-do-expressions",
"version": "7.13.0",
"description": "Allow parsing of async do expressions",
"repository": {
"type": "git",
"url": "https://github.com/babel/babel.git",
"directory": "packages/babel-plugin-syntax-async-do-expressions"
},
"license": "MIT",
"publishConfig": {
"access": "public"
},
"main": "./lib/index.js",
"exports": {
".": "./lib/index.js"
},
"keywords": [
"babel-plugin"
],
"dependencies": {
"@babel/helper-plugin-utils": "workspace:^7.12.13"
},
"peerDependencies": {
"@babel/core": "^7.0.0-0"
},
"devDependencies": {
"@babel/core": "workspace:*"
},
"homepage": "https://babel.dev/docs/en/next/babel-plugin-syntax-async-do-expressions"
}

View File

@ -0,0 +1,13 @@
import { declare } from "@babel/helper-plugin-utils";
export default declare(api => {
api.assertVersion(7);
return {
name: "syntax-async-do-expressions",
manipulateOptions(opts, parserOpts) {
parserOpts.plugins.push("asyncDoExpressions", "doExpressions");
},
};
});

View File

@ -1609,6 +1609,7 @@ export interface Decorator extends BaseNode {
export interface DoExpression extends BaseNode {
type: "DoExpression";
body: BlockStatement;
async: boolean;
}
export interface ExportDefaultSpecifier extends BaseNode {

View File

@ -1051,7 +1051,10 @@ export function importAttribute(
export function decorator(expression: t.Expression): t.Decorator {
return builder("Decorator", ...arguments);
}
export function doExpression(body: t.BlockStatement): t.DoExpression {
export function doExpression(
body: t.BlockStatement,
async?: boolean,
): t.DoExpression {
return builder("DoExpression", ...arguments);
}
export function exportDefaultSpecifier(

View File

@ -188,11 +188,16 @@ defineType("Decorator", {
defineType("DoExpression", {
visitor: ["body"],
builder: ["body", "async"],
aliases: ["Expression"],
fields: {
body: {
validate: assertNodeType("BlockStatement"),
},
async: {
validate: assertValueType("boolean"),
default: false,
},
},
});

View File

@ -1512,6 +1512,17 @@ __metadata:
languageName: unknown
linkType: soft
"@babel/plugin-syntax-async-do-expressions@workspace:packages/babel-plugin-syntax-async-do-expressions":
version: 0.0.0-use.local
resolution: "@babel/plugin-syntax-async-do-expressions@workspace:packages/babel-plugin-syntax-async-do-expressions"
dependencies:
"@babel/core": "workspace:*"
"@babel/helper-plugin-utils": "workspace:^7.12.13"
peerDependencies:
"@babel/core": ^7.0.0-0
languageName: unknown
linkType: soft
"@babel/plugin-syntax-async-generators@npm:^7.8.4":
version: 7.8.4
resolution: "@babel/plugin-syntax-async-generators@npm:7.8.4"