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 1 commit
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;
}
18 changes: 18 additions & 0 deletions packages/babel-traverse/src/path/introspection.js
Expand Up @@ -109,6 +109,24 @@ export function canSwapBetweenExpressionAndStatement(replacement) {
return false;
}

/**
* Check whether the current path contains an `await` expression
*/

export function containsAwaitExpression(): boolean {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can/should use hasType instead:

const hasAsync = traverse.hasType(
fn.body,
"AwaitExpression",
t.FUNCTION_TYPES,
);

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Disclaimer: I didn't know about that method until 5 mins ago. Usually, when someone adds a new method to @babel/traverse there is a 50% probability that it already exists.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried to look for it too... But I guess you can find things better than me

let foundAwait = false;
this.traverse({
Function(path) {
path.skip();
},
AwaitExpression(path) {
foundAwait = true;
path.stop();
},
});
return foundAwait;
}

/**
* Check whether the current path references a completion record
*/
Expand Down
10 changes: 10 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,12 @@ export function replaceExpressionWithStatements(nodes: Array<Object>) {
const callee = this.get("callee");
callee.arrowFunctionToExpression();

// (() => await xxx)() -> await (async () => await xxx)();
if (isParentAsync && this.get("callee.body").containsAwaitExpression()) {
callee.set("async", true);
this.replaceWith(t.awaitExpression(this.node));
}

return callee.get("body.body");
}

Expand Down