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

Optimize transform-async-to-generator output #14122

merged 3 commits into from Jan 8, 2022

Conversation

magic-akari
Copy link
Contributor

@magic-akari magic-akari commented Jan 8, 2022

  • remove wrapper if the function length is zero.
  • remove wrapper if the assumptions.ignoreFunctionLength is true.
Q                       A
Fixed Issues? Fixes #14121
Patch: Bug Fix?
Major: Breaking Change?
Minor: New Feature?
Tests Added + Pass? Yes
Documentation PR Link
Any Dependency Changes?
License MIT

- remove wrapper if the function length is zero.
- remove wrapper if the `assumptions.ignoreFunctionLength` is `true`.
@nicolo-ribaudo nicolo-ribaudo added the PR: Output optimization 🔬 A type of pull request used for our changelog categories label Jan 8, 2022
Comment on lines 95 to 109
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"));
}

})();

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.

foo(async function ({}) {});

export default async ([...x]) => {};

Copy link
Member

Choose a reason for hiding this comment

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

There is one more case that we should test:

export default async function (x) {}

The function should still be wrapped, because it is a function declaration and it needs to be hoisted.

Why?

Otherwise this code doesn't work, when a.js is the entrypoint:

// a.js

import x from "./b.js";

x.then(console.log);

export default async function () { return 1 }
// b.js

import f from "./a.js";
export default f();

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done. test added.

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!

@nicolo-ribaudo nicolo-ribaudo merged commit 910ece5 into babel:main Jan 8, 2022
@magic-akari magic-akari deleted the fix/async branch February 4, 2022 03:26
@github-actions github-actions bot added the outdated A closed issue/PR that is archived due to age. Recommended to make a new issue label May 6, 2022
@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 6, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
outdated A closed issue/PR that is archived due to age. Recommended to make a new issue PR: Output optimization 🔬 A type of pull request used for our changelog categories
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Bug]: Babel generate redundant code and does not respect assumptions.ignoreFunctionLength
3 participants