Skip to content

Commit

Permalink
feat(transformer/async-to-generator): do not transform await expressi…
Browse files Browse the repository at this point in the history
…on if is not inside async function
  • Loading branch information
Dunqing committed Nov 6, 2024
1 parent f5aa14b commit 71a854d
Showing 1 changed file with 22 additions and 3 deletions.
25 changes: 22 additions & 3 deletions crates/oxc_transformer/src/es2017/async_to_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,25 @@ impl<'a, 'ctx> Traverse<'a> for AsyncToGenerator<'a, 'ctx> {
}

impl<'a, 'ctx> AsyncToGenerator<'a, 'ctx> {
/// Check whether the current node is inside an async function.
fn is_inside_async_function(ctx: &mut TraverseCtx<'a>) -> bool {
// Early return if current scope is top because we don't need to transform top-level await expression.
if ctx.current_scope_flags().is_top() {
return false;
}

for ancestor in ctx.ancestors() {
match ancestor {
Ancestor::FunctionBody(func) => return *func.r#async(),
Ancestor::ArrowFunctionExpressionBody(func) => {
return *func.r#async();
}
_ => {}
}
}
false
}

/// Transforms `await` expressions to `yield` expressions.
/// Ignores top-level await expressions.
#[allow(clippy::unused_self)]
Expand All @@ -154,14 +173,14 @@ impl<'a, 'ctx> AsyncToGenerator<'a, 'ctx> {
ctx: &mut TraverseCtx<'a>,
) -> Option<Expression<'a>> {
// We don't need to handle top-level await.
if ctx.parent().is_program() {
None
} else {
if Self::is_inside_async_function(ctx) {
Some(ctx.ast.expression_yield(
SPAN,
false,
Some(ctx.ast.move_expression(&mut expr.argument)),
))
} else {
None
}
}
}
Expand Down

0 comments on commit 71a854d

Please sign in to comment.