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

Optimize transform-async-to-generator output #14122

Merged
merged 3 commits into from Jan 8, 2022
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
8 changes: 7 additions & 1 deletion packages/babel-helper-remap-async-to-generator/src/index.ts
Expand Up @@ -36,6 +36,7 @@ export default function (
wrapAwait?: any;
},
noNewArrows?: boolean,
ignoreFunctionLength?: boolean,
) {
path.traverse(awaitVisitor, {
wrapAwait: helpers.wrapAwait,
Expand All @@ -46,7 +47,12 @@ export default function (
path.node.async = false;
path.node.generator = true;

wrapFunction(path, cloneNode(helpers.wrapAsync), noNewArrows);
wrapFunction(
path,
cloneNode(helpers.wrapAsync),
noNewArrows,
ignoreFunctionLength,
);

const isProperty =
path.isObjectMethod() ||
Expand Down
48 changes: 29 additions & 19 deletions packages/babel-helper-wrap-function/src/index.ts
Expand Up @@ -66,7 +66,12 @@ function classOrObjectMethod(
).unwrapFunctionEnvironment();
}

function plainFunction(path: NodePath<any>, callId: any, noNewArrows: boolean) {
function plainFunction(
path: NodePath<any>,
callId: any,
noNewArrows: boolean,
ignoreFunctionLength: boolean,
) {
const node = path.node;
const isDeclaration = path.isFunctionDeclaration();
const functionId = node.id;
Expand All @@ -87,26 +92,26 @@ function plainFunction(path: NodePath<any>, callId: any, noNewArrows: boolean) {
}

const built = callExpression(callId, [node]);
const params = node.params.reduce(
(acc, param) => {
acc.done = acc.done || isAssignmentPattern(param) || isRestElement(param);

if (!acc.done) {
acc.params.push(path.scope.generateUidIdentifier("x"));
}

return acc;
},
{
params: [],
done: false,
},
).params;
Copy link
Member

Choose a reason for hiding this comment

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

I know that you didn't write this code but you just moved it from a few lines below, however it's an example of "overusing reduce" and we can probably simplify it since we are touching these lines anyway:

Suggested change
const params = node.params.reduce(
(acc, param) => {
acc.done = acc.done || isAssignmentPattern(param) || isRestElement(param);
if (!acc.done) {
acc.params.push(path.scope.generateUidIdentifier("x"));
}
return acc;
},
{
params: [],
done: false,
},
).params;
const params = [];
for (const param of node.params) {
if (isAssignmentPattern(param) || isRestElement(param)) break;
params.push(path.scope.generateUidIdentifier("x"));
}

const container = wrapper({
NAME: functionId || null,
REF: path.scope.generateUidIdentifier(functionId ? functionId.name : "ref"),
FUNCTION: built,
PARAMS: node.params.reduce(
(acc, param) => {
acc.done =
acc.done || isAssignmentPattern(param) || isRestElement(param);

if (!acc.done) {
acc.params.push(path.scope.generateUidIdentifier("x"));
}

return acc;
},
{
params: [],
done: false,
},
).params,
PARAMS: params,
});

if (isDeclaration) {
Expand All @@ -123,7 +128,11 @@ function plainFunction(path: NodePath<any>, callId: any, noNewArrows: boolean) {
});
}

if (!retFunction || retFunction.id || node.params.length) {
if (
!retFunction ||
retFunction.id ||
Copy link
Member

Choose a reason for hiding this comment

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

Probably we can do it by adding || isFunctionDeclaration(retFunction) here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We don't need to change it.
default export use a different template builder.

The wrapper is in another function.
You can check the test case.

Copy link
Member

Choose a reason for hiding this comment

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

Oh awesome, thanks for the test!

(!ignoreFunctionLength && params.length)
) {
// we have an inferred function id or params so we need this wrapper
// @ts-expect-error todo(flow->ts) separate `wrapper` for `isDeclaration` and `else` branches
path.replaceWith(container);
Expand All @@ -139,10 +148,11 @@ export default function wrapFunction(
callId: any,
// TODO(Babel 8): Consider defaulting to false for spec compliancy
noNewArrows: boolean = true,
ignoreFunctionLength: boolean = false,
) {
if (path.isMethod()) {
classOrObjectMethod(path, callId);
} else {
plainFunction(path, callId, noNewArrows);
plainFunction(path, callId, noNewArrows, ignoreFunctionLength);
}
}
Expand Up @@ -8,6 +8,7 @@ export default declare((api, options) => {

const { method, module } = options;
const noNewArrows = api.assumption("noNewArrows");
const ignoreFunctionLength = api.assumption("ignoreFunctionLength");

if (method && module) {
return {
Expand All @@ -24,7 +25,12 @@ export default declare((api, options) => {
wrapAsync = state.methodWrapper = addNamed(path, method, module);
}

remapAsyncToGenerator(path, { wrapAsync }, noNewArrows);
remapAsyncToGenerator(
path,
{ wrapAsync },
noNewArrows,
ignoreFunctionLength,
);
},
},
};
Expand All @@ -41,6 +47,7 @@ export default declare((api, options) => {
path,
{ wrapAsync: state.addHelper("asyncToGenerator") },
noNewArrows,
ignoreFunctionLength,
);
},
},
Expand Down
@@ -0,0 +1,7 @@
async (x = 1) => 1;
(async function (x = 2) {
return 2;
})();

foo(async (...x) => {});
foo(async (...[...y]) => {});
Copy link
Member

Choose a reason for hiding this comment

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

All these functions would have length: 0 anyway: to test the behavior of ignoreFunctionLength, we shuold use functions with .length >= 1.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks.
I was writing on the first case and accidentally mixed it up.
I will update this test case.

@@ -0,0 +1,6 @@
{
"plugins": ["transform-async-to-generator"],
"assumptions": {
"ignoreFunctionLength": true
}
}
@@ -0,0 +1,9 @@
/*#__PURE__*/
babelHelpers.asyncToGenerator(function* (x = 1) {
return 1;
});
babelHelpers.asyncToGenerator(function* (x = 2) {
return 2;
})();
foo( /*#__PURE__*/babelHelpers.asyncToGenerator(function* (...x) {}));
foo( /*#__PURE__*/babelHelpers.asyncToGenerator(function* (...[...y]) {}));
@@ -1,3 +1,11 @@
foo(async function () {

});

bar(async function (x = 1) {

});

baz(async function (...y) {

});
@@ -1 +1,5 @@
foo( /*#__PURE__*/babelHelpers.asyncToGenerator(function* () {}));
bar( /*#__PURE__*/babelHelpers.asyncToGenerator(function* () {
let x = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 1;
}));
baz( /*#__PURE__*/babelHelpers.asyncToGenerator(function* () {}));