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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -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; | ||
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) { | ||
|
@@ -123,7 +128,11 @@ function plainFunction(path: NodePath<any>, callId: any, noNewArrows: boolean) { | |
}); | ||
} | ||
|
||
if (!retFunction || retFunction.id || node.params.length) { | ||
if ( | ||
!retFunction || | ||
retFunction.id || | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Probably we can do it by adding There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We don't need to change it. The wrapper is in another function. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
|
@@ -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); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
async (x = 1) => 1; | ||
(async function (x = 2) { | ||
return 2; | ||
})(); | ||
|
||
foo(async (...x) => {}); | ||
foo(async (...[...y]) => {}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. All these functions would have There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"plugins": ["transform-async-to-generator"], | ||
"assumptions": { | ||
"ignoreFunctionLength": true | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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]) {})); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,11 @@ | ||
foo(async function () { | ||
|
||
}); | ||
|
||
bar(async function (x = 1) { | ||
|
||
}); | ||
|
||
baz(async function (...y) { | ||
|
||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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* () {})); |
There was a problem hiding this comment.
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: