Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix await and yield for do expression #10072

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -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,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