Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
liuxingbaoyu committed Sep 22, 2023
1 parent 306769e commit 39776f4
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 45 deletions.
17 changes: 12 additions & 5 deletions packages/babel-helper-remap-async-to-generator/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ const {
yieldExpression,
} = t;

const awaitVisitor = traverse.visitors.merge<{ wrapAwait: t.Expression }>([
const awaitVisitor = traverse.visitors.merge<{
wrapAwait: t.Expression | string;
}>([
{
ArrowFunctionExpression(path) {
path.skip();
Expand All @@ -23,7 +25,12 @@ const awaitVisitor = traverse.visitors.merge<{ wrapAwait: t.Expression }>([
path.replaceWith(
yieldExpression(
wrapAwait
? callExpression(cloneNode(wrapAwait), [argument.node])
? callExpression(
typeof wrapAwait === "string"
? path.hub.addHelper(wrapAwait)
: cloneNode(wrapAwait),
[argument.node],
)
: argument.node,
),
);
Expand All @@ -35,9 +42,9 @@ const awaitVisitor = traverse.visitors.merge<{ wrapAwait: t.Expression }>([
export default function (
path: NodePath<t.Function>,
helpers: {
wrapAsync: t.Identifier | string;
wrapAwait?: t.Identifier;
callAsync?: t.Identifier;
wrapAsync: t.Expression | string;
wrapAwait?: t.Expression | string;
callAsync?: string;
},
noNewArrows?: boolean,
ignoreFunctionLength?: boolean,
Expand Down
22 changes: 14 additions & 8 deletions packages/babel-helper-wrap-function/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ export default function wrapFunction(
// TODO(Babel 8): Consider defaulting to false for spec compliance
noNewArrows: boolean = true,
ignoreFunctionLength: boolean = false,
callAsync?: t.Expression,
callAsync?: string,
) {
if (callAsync) {
if (path.isMethod()) {
Expand All @@ -214,7 +214,11 @@ export default function wrapFunction(
blockStatement(body.body),
true,
);
body.body = [returnStatement(callExpression(callAsync, [container]))];
body.body = [
returnStatement(
callExpression(path.hub.addHelper(callAsync), [container]),
),
];

// Regardless of whether or not the wrapped function is a an async method
// or generator the outer function should not be
Expand Down Expand Up @@ -246,15 +250,14 @@ export default function wrapFunction(
} else {
node = path.node as t.FunctionDeclaration | t.FunctionExpression;
}

const isDeclaration = isFunctionDeclaration(node);
const isDeclaration = path.isFunctionDeclaration();

let built = node;
if (!isCallExpression(node)) {
functionId = node.id;
node.id = null;
node.type = "FunctionExpression";
built = callExpression(callAsync, [
built = callExpression(path.hub.addHelper(callAsync), [
node as t.FunctionExpression,
identifier("this"),
identifier("arguments"),
Expand Down Expand Up @@ -289,8 +292,11 @@ export default function wrapFunction(
wrapper.id ||
(!ignoreFunctionLength && params.length)
) {
// Because we previously mutated some `FunctionDeclaration` nodes to `FunctionExpression`.
path.type = isDeclaration
? "FunctionDeclaration"
: "FunctionExpression";
path.replaceWith(wrapper);

markCallWrapped(path);
} else {
// we can omit this wrapper as the conditions it protects for do not apply
Expand All @@ -305,11 +311,11 @@ export default function wrapFunction(
}

if (path.isMethod()) {
classOrObjectMethod(path, callId as t.Identifier);
classOrObjectMethod(path, callId as t.Expression);
} else {
plainFunction(
path as NodePath<Exclude<t.Function, t.Method>>,
callId as t.Identifier,
callId as t.Expression,
noNewArrows,
ignoreFunctionLength,
);
Expand Down
23 changes: 14 additions & 9 deletions packages/babel-plugin-proposal-function-sent/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,20 @@ export default declare(api => {
]),
);

wrapFunction.default(
fnPath,
"skipFirstGeneratorNext",
undefined,
undefined,
state.availableHelper("callSkipFirstGeneratorNext")
? state.addHelper("callSkipFirstGeneratorNext")
: undefined,
);
if (state.availableHelper("callSkipFirstGeneratorNext")) {
wrapFunction.default(
fnPath,
"skipFirstGeneratorNext",
undefined,
undefined,
"callSkipFirstGeneratorNext",
);
} else {
wrapFunction.default(
fnPath,
state.addHelper("skipFirstGeneratorNext"),
);
}
},
},
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,18 @@ export default declare(api => {

// We don't need to pass the noNewArrows assumption, since
// async generators are never arrow functions.
remapAsyncToGenerator.default(path, {
wrapAsync: state.addHelper("wrapAsyncGenerator"),
wrapAwait: state.addHelper("awaitAsyncGenerator"),
callAsync: state.availableHelper("callAsyncGenerator")
? state.addHelper("callAsyncGenerator")
: undefined,
});
if (state.availableHelper("callAsyncGenerator")) {
remapAsyncToGenerator.default(path, {
wrapAsync: "wrapAsyncGenerator",
wrapAwait: "awaitAsyncGenerator",
callAsync: "callAsyncGenerator",
});
} else {
remapAsyncToGenerator.default(path, {
wrapAsync: state.addHelper("wrapAsyncGenerator"),
wrapAwait: state.addHelper("awaitAsyncGenerator"),
});
}
},
};

Expand Down
38 changes: 24 additions & 14 deletions packages/babel-plugin-transform-async-to-generator/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export interface Options {
}

type State = {
methodWrapper?: t.Identifier;
methodWrapper?: t.Expression;
};

export default declare<State>((api, options: Options) => {
Expand Down Expand Up @@ -60,19 +60,29 @@ export default declare<State>((api, options: Options) => {
Function(path, state) {
if (!path.node.async || path.node.generator) return;

remapAsyncToGenerator.default(
path,
{
wrapAsync: state.availableHelper("asyncToGenerator2")
? "asyncToGenerator2"
: "asyncToGenerator",
callAsync: state.availableHelper("callAsync")
? state.addHelper("callAsync")
: undefined,
},
noNewArrows,
ignoreFunctionLength,
);
if (
state.availableHelper("callAsync") &&
state.availableHelper("asyncToGenerator2")
) {
remapAsyncToGenerator.default(
path,
{
wrapAsync: "asyncToGenerator2",
callAsync: "callAsync",
},
noNewArrows,
ignoreFunctionLength,
);
} else {
remapAsyncToGenerator.default(
path,
{
wrapAsync: state.addHelper("asyncToGenerator"),
},
noNewArrows,
ignoreFunctionLength,
);
}
},
},
};
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 39776f4

Please sign in to comment.