fix await and yield for do expression (#10072)

This commit is contained in:
Tan Li Hau 2019-07-03 22:54:39 +08:00 committed by Brian Ng
parent 5b86353b35
commit d50e78d45b
5 changed files with 46 additions and 0 deletions

View File

@ -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);

View File

@ -0,0 +1,8 @@
async function p(x) {
const y = do {
let z;
await x;
};
return y;
}

View File

@ -0,0 +1,3 @@
{
"minNodeVersion": "8.0.0"
}

View File

@ -0,0 +1,7 @@
async function p(x) {
const y = await async function () {
let z;
return await x;
}();
return y;
}

View File

@ -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, []));
@ -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");
}