diff --git a/packages/babel-plugin-proposal-do-expressions/test/fixtures/do-expressions/await/exec.js b/packages/babel-plugin-proposal-do-expressions/test/fixtures/do-expressions/await/exec.js new file mode 100644 index 000000000000..84d1e1cd4962 --- /dev/null +++ b/packages/babel-plugin-proposal-do-expressions/test/fixtures/do-expressions/await/exec.js @@ -0,0 +1,11 @@ +async function p(x) { + const y = do { + let z; + await x; + }; + + return y; +} + +const promise = Promise.resolve(5); +expect(p(promise)).resolves.toBe(5); \ No newline at end of file diff --git a/packages/babel-plugin-proposal-do-expressions/test/fixtures/do-expressions/await/input.js b/packages/babel-plugin-proposal-do-expressions/test/fixtures/do-expressions/await/input.js new file mode 100644 index 000000000000..7b8cb4939a69 --- /dev/null +++ b/packages/babel-plugin-proposal-do-expressions/test/fixtures/do-expressions/await/input.js @@ -0,0 +1,8 @@ +async function p(x) { + const y = do { + let z; + await x; + }; + + return y; +} \ No newline at end of file diff --git a/packages/babel-plugin-proposal-do-expressions/test/fixtures/do-expressions/await/options.json b/packages/babel-plugin-proposal-do-expressions/test/fixtures/do-expressions/await/options.json new file mode 100644 index 000000000000..7edb6d2fc815 --- /dev/null +++ b/packages/babel-plugin-proposal-do-expressions/test/fixtures/do-expressions/await/options.json @@ -0,0 +1,3 @@ +{ + "minNodeVersion": "8.0.0" +} \ No newline at end of file diff --git a/packages/babel-plugin-proposal-do-expressions/test/fixtures/do-expressions/await/output.js b/packages/babel-plugin-proposal-do-expressions/test/fixtures/do-expressions/await/output.js new file mode 100644 index 000000000000..55ab68b17442 --- /dev/null +++ b/packages/babel-plugin-proposal-do-expressions/test/fixtures/do-expressions/await/output.js @@ -0,0 +1,7 @@ +async function p(x) { + const y = await async function () { + let z; + return await x; + }(); + return y; +} diff --git a/packages/babel-traverse/src/path/replacement.js b/packages/babel-traverse/src/path/replacement.js index 0c411cb40163..f0e74dbc1172 100644 --- a/packages/babel-traverse/src/path/replacement.js +++ b/packages/babel-traverse/src/path/replacement.js @@ -215,6 +215,10 @@ export function replaceExpressionWithStatements(nodes: Array) { if (toSequenceExpression) { return this.replaceWith(toSequenceExpression)[0].get("expressions"); } + + const functionParent = this.getFunctionParent(); + const isParentAsync = functionParent && functionParent.is("async"); + const container = t.arrowFunctionExpression([], t.blockStatement(nodes)); this.replaceWith(t.callExpression(container, [])); @@ -255,6 +259,19 @@ export function replaceExpressionWithStatements(nodes: Array) { const callee = this.get("callee"); callee.arrowFunctionToExpression(); + // (() => await xxx)() -> await (async () => await xxx)(); + if ( + isParentAsync && + traverse.hasType( + this.get("callee.body").node, + "AwaitExpression", + t.FUNCTION_TYPES, + ) + ) { + callee.set("async", true); + this.replaceWith(t.awaitExpression(this.node)); + } + return callee.get("body.body"); }