fix await and yield for do expression (#10072)
This commit is contained in:
parent
5b86353b35
commit
d50e78d45b
11
packages/babel-plugin-proposal-do-expressions/test/fixtures/do-expressions/await/exec.js
vendored
Normal file
11
packages/babel-plugin-proposal-do-expressions/test/fixtures/do-expressions/await/exec.js
vendored
Normal 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);
|
||||||
8
packages/babel-plugin-proposal-do-expressions/test/fixtures/do-expressions/await/input.js
vendored
Normal file
8
packages/babel-plugin-proposal-do-expressions/test/fixtures/do-expressions/await/input.js
vendored
Normal file
@ -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"
|
||||||
|
}
|
||||||
7
packages/babel-plugin-proposal-do-expressions/test/fixtures/do-expressions/await/output.js
vendored
Normal file
7
packages/babel-plugin-proposal-do-expressions/test/fixtures/do-expressions/await/output.js
vendored
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
async function p(x) {
|
||||||
|
const y = await async function () {
|
||||||
|
let z;
|
||||||
|
return await x;
|
||||||
|
}();
|
||||||
|
return y;
|
||||||
|
}
|
||||||
@ -215,6 +215,10 @@ export function replaceExpressionWithStatements(nodes: Array<Object>) {
|
|||||||
if (toSequenceExpression) {
|
if (toSequenceExpression) {
|
||||||
return this.replaceWith(toSequenceExpression)[0].get("expressions");
|
return this.replaceWith(toSequenceExpression)[0].get("expressions");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const functionParent = this.getFunctionParent();
|
||||||
|
const isParentAsync = functionParent && functionParent.is("async");
|
||||||
|
|
||||||
const container = t.arrowFunctionExpression([], t.blockStatement(nodes));
|
const container = t.arrowFunctionExpression([], t.blockStatement(nodes));
|
||||||
|
|
||||||
this.replaceWith(t.callExpression(container, []));
|
this.replaceWith(t.callExpression(container, []));
|
||||||
@ -255,6 +259,19 @@ export function replaceExpressionWithStatements(nodes: Array<Object>) {
|
|||||||
const callee = this.get("callee");
|
const callee = this.get("callee");
|
||||||
callee.arrowFunctionToExpression();
|
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");
|
return callee.get("body.body");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user