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

fix(transformer/async-to-generator): output is incorrect when arrow function without params #7052

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 18 additions & 7 deletions crates/oxc_transformer/src/es2017/async_to_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -410,8 +410,9 @@ impl<'a, 'ctx> AsyncGeneratorExecutor<'a, 'ctx> {
let params = ctx.alloc(ctx.ast.move_formal_parameters(&mut arrow.params));
let generator_function_id = arrow.scope_id.get().unwrap();
ctx.scopes_mut().get_flags_mut(generator_function_id).remove(ScopeFlags::Arrow);
let function_name = Self::get_function_name_from_parent_variable_declarator(ctx);

if !Self::is_function_length_affected(&params) {
if function_name.is_none() && !Self::is_function_length_affected(&params) {
return self.create_async_to_generator_call(
params,
ctx.ast.alloc(body),
Expand All @@ -421,6 +422,7 @@ impl<'a, 'ctx> AsyncGeneratorExecutor<'a, 'ctx> {
}

let wrapper_scope_id = ctx.create_child_scope(ctx.current_scope_id(), ScopeFlags::Function);

// The generator function will move to inside wrapper, so we need
// to change the parent scope of the generator function to the wrapper function.
ctx.scopes_mut().change_parent_id(generator_function_id, Some(wrapper_scope_id));
Expand All @@ -432,7 +434,10 @@ impl<'a, 'ctx> AsyncGeneratorExecutor<'a, 'ctx> {
let params = Self::create_placeholder_params(&params, scope_id, ctx);
let statements = ctx.ast.vec1(Self::create_apply_call_statement(&bound_ident, ctx));
let body = ctx.ast.alloc_function_body(SPAN, ctx.ast.vec(), statements);
let id = Self::infer_function_id_from_variable_declarator(wrapper_scope_id, ctx);
let id = function_name.map(|name| {
ctx.generate_binding(name, wrapper_scope_id, SymbolFlags::FunctionScopedVariable)
.create_binding_identifier(ctx)
});
let function = Self::create_function(id, params, body, scope_id, ctx);
let argument = Some(ctx.ast.expression_from_function(function));
ctx.ast.statement_return(SPAN, argument)
Expand Down Expand Up @@ -464,16 +469,22 @@ impl<'a, 'ctx> AsyncGeneratorExecutor<'a, 'ctx> {
scope_id: ScopeId,
ctx: &mut TraverseCtx<'a>,
) -> Option<BindingIdentifier<'a>> {
let Ancestor::VariableDeclaratorInit(declarator) = ctx.parent() else {
return None;
};
let Some(id) = declarator.id().get_binding_identifier() else { unreachable!() };
let name = Self::get_function_name_from_parent_variable_declarator(ctx)?;
Some(
ctx.generate_binding(id.name.clone(), scope_id, SymbolFlags::FunctionScopedVariable)
ctx.generate_binding(name, scope_id, SymbolFlags::FunctionScopedVariable)
.create_binding_identifier(ctx),
)
}

fn get_function_name_from_parent_variable_declarator(
ctx: &mut TraverseCtx<'a>,
) -> Option<Atom<'a>> {
let Ancestor::VariableDeclaratorInit(declarator) = ctx.parent() else {
return None;
};
declarator.id().get_binding_identifier().map(|id| id.name.clone())
}

/// Creates a [`Function`] with the specified params, body and scope_id.
#[inline]
fn create_function(
Expand Down
2 changes: 1 addition & 1 deletion tasks/transform_conformance/snapshots/oxc.snap.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
commit: d20b314c

Passed: 74/83
Passed: 75/84

# All Passed:
* babel-plugin-transform-class-static-block
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
let g = async () => {
console.log("Good")
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
let g = function() {
var _ref = babelHelpers.asyncToGenerator(function* () {
console.log("Good");
});
return function g() {
return _ref.apply(this, arguments);
};
}();
Loading