Skip to content

Commit

Permalink
fix await and yield for do expression (#10072)
Browse files Browse the repository at this point in the history
  • Loading branch information
tanhauhau authored and existentialism committed Jul 3, 2019
1 parent 5b86353 commit d50e78d
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 0 deletions.
@@ -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);
@@ -0,0 +1,8 @@
async function p(x) {
const y = do {
let z;
await x;
};

return y;
}
@@ -0,0 +1,3 @@
{
"minNodeVersion": "8.0.0"
}
@@ -0,0 +1,7 @@
async function p(x) {
const y = await async function () {
let z;
return await x;
}();
return y;
}
17 changes: 17 additions & 0 deletions packages/babel-traverse/src/path/replacement.js
Expand Up @@ -215,6 +215,10 @@ export function replaceExpressionWithStatements(nodes: Array<Object>) {
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, []));
Expand Down Expand Up @@ -255,6 +259,19 @@ export function replaceExpressionWithStatements(nodes: Array<Object>) {
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");
}

Expand Down

0 comments on commit d50e78d

Please sign in to comment.