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 compilation of parameters in async generators #15146

Merged
merged 2 commits into from
Nov 7, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
24 changes: 17 additions & 7 deletions packages/babel-plugin-transform-parameters/src/params.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,8 @@ export default function convertFunctionParams(
// ensure it's a block, useful for arrow functions
path.ensureBlock();

if (state.needsOuterBinding || shadowedParams.size > 0 || node.generator) {
const { async, generator } = node;
if (generator || state.needsOuterBinding || shadowedParams.size > 0) {
body.push(buildScopeIIFE(shadowedParams, path.node.body));

path.set("body", t.blockStatement(body as t.Statement[]));
Expand All @@ -169,12 +170,21 @@ export default function convertFunctionParams(
// This is an IIFE, so we don't need to worry about the noNewArrows assumption
arrowPath.arrowFunctionToExpression();

arrowPath.node.generator = path.node.generator;
arrowPath.node.async = path.node.async;

// We don't reset "async" because if the default value of a parameter
// throws, it must reject asynchronously.
path.node.generator = false;
arrowPath.node.generator = generator;
arrowPath.node.async = async;

node.generator = false;
node.async = false;
if (async) {
// If the default value of a parameter throws, it must reject asynchronously.
path.node.body = template.statement.ast`{
try {
${path.node.body.body}
} catch (e) {
return Promise.reject(e);
}
}` as t.BlockStatement;
}
} else {
path.get("body").unshiftContainer("body", body);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
async function * foo (p = 1) {
yield "hello";
yield p;
}

return (async () => {
const res = [];
for await (const x of foo()) res.push(x);
expect(res).toEqual(["hello", 1]);
})();
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"plugins": ["transform-parameters"],
"parserOpts": {
"allowReturnOutsideFunction": true
},
"minNodeVersion": "10.0.0"
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,25 @@ function f() {
return a;
}(a);
}
async function g() {
let a = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 1;
return async function (a) {
var a = await a;
return a;
}(a);
function g() {
try {
let a = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 1;
return async function (a) {
var a = await a;
return a;
}(a);
} catch (e) {
return Promise.reject(e);
}
}
async function h() {
let a = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 1;
return async function* (a) {
var a = await (yield a);
return a;
}(a);
function h() {
try {
let a = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 1;
return async function* (a) {
var a = await (yield a);
return a;
}(a);
} catch (e) {
return Promise.reject(e);
}
}