Skip to content

Commit

Permalink
feat(transformer/async-to-generator): support inferring the function …
Browse files Browse the repository at this point in the history
…name from the ObjectPropertyValue's key
  • Loading branch information
Dunqing committed Nov 8, 2024
1 parent 729936f commit 9d4633c
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 16 deletions.
33 changes: 19 additions & 14 deletions crates/oxc_transformer/src/es2017/async_to_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,9 +287,7 @@ 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 = id.or_else(|| {
Self::infer_function_id_from_variable_declarator(wrapper_scope_id, ctx)
});
let id = id.or_else(|| Self::infer_function_id_from_parent_node(wrapper_scope_id, ctx));
Self::create_function(id, params, body, scope_id, ctx)
};

Expand Down Expand Up @@ -425,7 +423,7 @@ 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();
ctx.scopes_mut().get_flags_mut(generator_function_id).remove(ScopeFlags::Arrow);
let function_name = Self::get_function_name_from_parent_variable_declarator(ctx);
let function_name = Self::infer_function_name_from_parent_node(ctx);

if function_name.is_none() && !Self::is_function_length_affected(&params) {
return self.create_async_to_generator_call(
Expand Down Expand Up @@ -479,25 +477,32 @@ impl<'a, 'ctx> AsyncGeneratorExecutor<'a, 'ctx> {
}
}

/// Infers the function id from [`Ancestor::VariableDeclaratorInit`].
fn infer_function_id_from_variable_declarator(
/// Infers the function id from [`TraverseCtx::parent`].
fn infer_function_id_from_parent_node(
scope_id: ScopeId,
ctx: &mut TraverseCtx<'a>,
) -> Option<BindingIdentifier<'a>> {
let name = Self::get_function_name_from_parent_variable_declarator(ctx)?;
let name = Self::infer_function_name_from_parent_node(ctx)?;
Some(
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())
/// Infers the function name from the [`TraverseCtx::parent`].
fn infer_function_name_from_parent_node(ctx: &mut TraverseCtx<'a>) -> Option<Atom<'a>> {
match ctx.parent() {
// infer `foo` from `const foo = async function() {}`
Ancestor::VariableDeclaratorInit(declarator) => {
declarator.id().get_binding_identifier().map(|id| id.name.clone())
}
// infer `foo` from `({ foo: async function() {} })`
Ancestor::ObjectPropertyValue(property) if !*property.method() => property
.key()
.static_name()
.map(|name| ctx.ast.atom(if name.is_empty() { "_" } else { &name })),
_ => None,
}
}

/// Creates a [`Function`] with the specified params, body and scope_id.
Expand Down
8 changes: 6 additions & 2 deletions tasks/transform_conformance/snapshots/oxc.snap.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
commit: d20b314c

Passed: 79/88
Passed: 79/89

# All Passed:
* babel-plugin-transform-class-static-block
* babel-plugin-transform-nullish-coalescing-operator
* babel-plugin-transform-optional-catch-binding
* babel-plugin-transform-async-generator-functions
* babel-plugin-transform-async-to-generator
* babel-plugin-transform-exponentiation-operator
* babel-plugin-transform-arrow-functions
* babel-preset-typescript
* babel-plugin-transform-react-jsx-source
* regexp


# babel-plugin-transform-async-to-generator (8/9)
* object/property-with-function/input.js
x Output mismatch


# babel-plugin-transform-typescript (2/9)
* class-property-definition/input.ts
Unresolved references mismatch:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const Normal = {
foo: async () => {
console.log(log)
}
}

const StringLiteralKey = {
['bar']: async () => {
}
}
const EmptyStringLiteralKey = {
['']: async () => {
console.log(this)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
var _this = this;
const Normal = {
foo: function () {
var _ref = babelHelpers.asyncToGenerator(function* () {
console.log(log);
});
return function foo() {
return _ref.apply(this, arguments);
};
}()
};
const StringLiteralKey = {
['bar']: function () {
var _ref2 = babelHelpers.asyncToGenerator(function* () {
});
return function bar() {
return _ref2.apply(this, arguments);
};
}()
};
const EmptyStringLiteralKey = {
['']: function () {
var _ref3 = babelHelpers.asyncToGenerator(function* () {
console.log(_this);
});
return function _() {
return _ref3.apply(this, arguments);
};
}()
};

0 comments on commit 9d4633c

Please sign in to comment.