Skip to content

Commit 0e1a776

Browse files
committed
refactor(transformer/arrow-function): wrapping arrow function iife by using wrap_arrow_function_iife
1 parent acfe7d5 commit 0e1a776

File tree

1 file changed

+2
-44
lines changed

1 file changed

+2
-44
lines changed

crates/oxc_transformer/src/common/arrow_function_converter.rs

+2-44
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ use oxc_syntax::{
102102
};
103103
use oxc_traverse::{Ancestor, BoundIdentifier, Traverse, TraverseCtx};
104104

105-
use crate::EnvOptions;
105+
use crate::{utils::ast_builder::wrap_arrow_function_iife, EnvOptions};
106106

107107
type FxIndexMap<K, V> = IndexMap<K, V, FxBuildHasher>;
108108

@@ -419,12 +419,7 @@ impl<'a> Traverse<'a> for ArrowFunctionConverter<'a> {
419419
// prop = (() => { return async () => {} })();
420420
// }
421421
// ```
422-
Some(Self::wrap_arrow_function_with_iife(
423-
arrow.span,
424-
arrow.scope_id(),
425-
expr,
426-
ctx,
427-
))
422+
Some(wrap_arrow_function_iife(expr, ctx))
428423
} else {
429424
return;
430425
}
@@ -1207,43 +1202,6 @@ impl<'a> ArrowFunctionConverter<'a> {
12071202

12081203
statements.insert(0, stmt);
12091204
}
1210-
1211-
/// Wrap an arrow function with IIFE
1212-
///
1213-
/// `() => {}` -> `(() => { return () => {}; })()`
1214-
fn wrap_arrow_function_with_iife(
1215-
span: Span,
1216-
scope_id: ScopeId,
1217-
expr: &mut Expression<'a>,
1218-
ctx: &mut TraverseCtx<'a>,
1219-
) -> Expression<'a> {
1220-
let kind = FormalParameterKind::ArrowFormalParameters;
1221-
let params = ctx.ast.formal_parameters(SPAN, kind, ctx.ast.vec(), NONE);
1222-
let statement = ctx.ast.statement_return(SPAN, Some(ctx.ast.move_expression(expr)));
1223-
let statements = ctx.ast.vec1(statement);
1224-
let body = ctx.ast.function_body(SPAN, ctx.ast.vec(), statements);
1225-
let parent_scope_id = ctx
1226-
.create_child_scope(ctx.current_scope_id(), ScopeFlags::Arrow | ScopeFlags::Function);
1227-
ctx.scopes_mut().change_parent_id(scope_id, Some(parent_scope_id));
1228-
let arrow = ctx.ast.alloc_arrow_function_expression_with_scope_id(
1229-
SPAN,
1230-
false,
1231-
false,
1232-
NONE,
1233-
params,
1234-
NONE,
1235-
body,
1236-
parent_scope_id,
1237-
);
1238-
// IIFE
1239-
ctx.ast.expression_call(
1240-
span,
1241-
Expression::ArrowFunctionExpression(arrow),
1242-
NONE,
1243-
ctx.ast.vec(),
1244-
false,
1245-
)
1246-
}
12471205
}
12481206

12491207
/// Visitor for inserting `this` after `super` in constructor body.

0 commit comments

Comments
 (0)