Skip to content

Commit

Permalink
refactor(transformer/async-to-generator): move handling of `MethodDef…
Browse files Browse the repository at this point in the history
…inition`'s value to `exit_function` (#7105)

Part of #7074

We need to handle this before the `arrow_function` plugin inserts `_this = this` because, in the `async-to-generator` plugin, we will move the body to an inner generator function. However, we don't want `_this = this` moved to the inner generator function as well. So as long as we move first, and then insert, we can fix this problem.

The new semantic error is related to another tricky problem, I will fix it in another PR
  • Loading branch information
Dunqing committed Nov 4, 2024
1 parent 50646a4 commit f80085c
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 17 deletions.
11 changes: 3 additions & 8 deletions crates/oxc_transformer/src/es2017/async_to_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,14 +130,9 @@ impl<'a, 'ctx> Traverse<'a> for AsyncToGenerator<'a, 'ctx> {
}
}

fn exit_method_definition(
&mut self,
node: &mut MethodDefinition<'a>,
ctx: &mut TraverseCtx<'a>,
) {
let function = &mut node.value;
if function.r#async && !function.generator && !function.is_typescript_syntax() {
self.executor.transform_function_for_method_definition(function, ctx);
fn exit_function(&mut self, func: &mut Function<'a>, ctx: &mut TraverseCtx<'a>) {
if func.r#async && ctx.parent().is_method_definition() {
self.executor.transform_function_for_method_definition(func, ctx);
}
}
}
Expand Down
10 changes: 3 additions & 7 deletions crates/oxc_transformer/src/es2017/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
mod async_to_generator;
mod options;

use oxc_ast::ast::{Expression, Statement};
use oxc_ast::ast::{Expression, Function, Statement};
use oxc_traverse::{Traverse, TraverseCtx};

use crate::{es2017::async_to_generator::AsyncToGenerator, TransformCtx};
Expand Down Expand Up @@ -30,13 +30,9 @@ impl<'a, 'ctx> Traverse<'a> for ES2017<'a, 'ctx> {
}
}

fn exit_method_definition(
&mut self,
node: &mut oxc_ast::ast::MethodDefinition<'a>,
ctx: &mut TraverseCtx<'a>,
) {
fn exit_function(&mut self, node: &mut Function<'a>, ctx: &mut TraverseCtx<'a>) {
if self.options.async_to_generator {
self.async_to_generator.exit_method_definition(node, ctx);
self.async_to_generator.exit_function(node, ctx);
}
}

Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_transformer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,7 @@ impl<'a, 'ctx> Traverse<'a> for TransformerImpl<'a, 'ctx> {
}
self.x1_jsx.exit_function(func, ctx);
self.x2_es2018.exit_function(func, ctx);
self.x2_es2017.exit_function(func, ctx);
self.x3_es2015.exit_function(func, ctx);
}

Expand Down Expand Up @@ -333,7 +334,6 @@ impl<'a, 'ctx> Traverse<'a> for TransformerImpl<'a, 'ctx> {
typescript.exit_method_definition(def, ctx);
}
self.x2_es2018.exit_method_definition(def, ctx);
self.x2_es2017.exit_method_definition(def, ctx);
}

fn enter_new_expression(&mut self, expr: &mut NewExpression<'a>, ctx: &mut TraverseCtx<'a>) {
Expand Down
10 changes: 9 additions & 1 deletion tasks/transform_conformance/snapshots/babel.snap.md
Original file line number Diff line number Diff line change
Expand Up @@ -1672,7 +1672,15 @@ x Output mismatch
x Output mismatch

* regression/regression-2765/input.js
x Output mismatch
Bindings mismatch:
after transform: ScopeId(10): []
rebuilt : ScopeId(6): ["_this2"]
Bindings mismatch:
after transform: ScopeId(4): ["_this2", "c"]
rebuilt : ScopeId(7): ["c"]
Symbol scope ID mismatch for "_this2":
after transform: SymbolId(8): ScopeId(4)
rebuilt : SymbolId(6): ScopeId(6)


# babel-plugin-transform-exponentiation-operator (3/4)
Expand Down

0 comments on commit f80085c

Please sign in to comment.