Skip to content

Commit 0acecbf

Browse files
Dunqingoverlookmotel
authored andcommitted
refactor(transformer): move wrap_arrow_function_iife to root utils module
1 parent f413bb5 commit 0acecbf

File tree

2 files changed

+32
-32
lines changed

2 files changed

+32
-32
lines changed

crates/oxc_transformer/src/es2020/optional_chaining.rs

+3-32
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,10 @@ use std::mem;
5151

5252
use oxc_allocator::CloneIn;
5353
use oxc_ast::{ast::*, NONE};
54-
use oxc_semantic::ScopeFlags;
5554
use oxc_span::SPAN;
5655
use oxc_traverse::{Ancestor, BoundIdentifier, MaybeBoundIdentifier, Traverse, TraverseCtx};
5756

58-
use crate::TransformCtx;
57+
use crate::{utils::ast_builder::wrap_arrow_function_iife, TransformCtx};
5958

6059
#[derive(Debug)]
6160
enum CallContext<'a> {
@@ -243,34 +242,6 @@ impl<'a> OptionalChaining<'a, '_> {
243242
ctx.ast.expression_conditional(SPAN, test, consequent, alternate)
244243
}
245244

246-
/// Wrap the expression with an arrow function
247-
///
248-
/// `expr` -> `(() => { return expr; })()`
249-
fn wrap_arrow_function_iife(
250-
expr: &mut Expression<'a>,
251-
ctx: &mut TraverseCtx<'a>,
252-
) -> Expression<'a> {
253-
let scope_id =
254-
ctx.insert_scope_below_expression(expr, ScopeFlags::Arrow | ScopeFlags::Function);
255-
256-
let kind = FormalParameterKind::ArrowFormalParameters;
257-
let params = ctx.ast.formal_parameters(SPAN, kind, ctx.ast.vec(), NONE);
258-
let statements =
259-
ctx.ast.vec1(ctx.ast.statement_return(SPAN, Some(ctx.ast.move_expression(expr))));
260-
let body = ctx.ast.function_body(SPAN, ctx.ast.vec(), statements);
261-
let arrow = ctx.ast.alloc_arrow_function_expression_with_scope_id(
262-
SPAN, false, false, NONE, params, NONE, body, scope_id,
263-
);
264-
// IIFE
265-
ctx.ast.expression_call(
266-
SPAN,
267-
Expression::ArrowFunctionExpression(arrow),
268-
NONE,
269-
ctx.ast.vec(),
270-
false,
271-
)
272-
}
273-
274245
/// Convert chain expression to expression
275246
///
276247
/// - [ChainElement::CallExpression] -> [Expression::CallExpression]
@@ -312,7 +283,7 @@ impl<'a> OptionalChaining<'a, '_> {
312283
// To insert the temp binding in the correct scope, we wrap the expression with
313284
// an arrow function. During the chain expression transformation, the temp binding
314285
// will be inserted into the arrow function's body.
315-
Self::wrap_arrow_function_iife(expr, ctx)
286+
wrap_arrow_function_iife(expr, ctx)
316287
} else {
317288
self.transform_chain_expression_impl(false, expr, ctx)
318289
}
@@ -326,7 +297,7 @@ impl<'a> OptionalChaining<'a, '_> {
326297
) {
327298
*expr = if self.is_inside_function_parameter {
328299
// Same as the above `transform_chain_expression` explanation
329-
Self::wrap_arrow_function_iife(expr, ctx)
300+
wrap_arrow_function_iife(expr, ctx)
330301
} else {
331302
// Unfortunately no way to get compiler to see that this branch is provably unreachable.
332303
// We don't want to inline this function, to keep `enter_expression` as small as possible.

crates/oxc_transformer/src/utils/ast_builder.rs

+29
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use oxc_ast::{ast::*, NONE};
2+
use oxc_semantic::ScopeFlags;
23
use oxc_span::SPAN;
34
use oxc_traverse::TraverseCtx;
45

@@ -35,3 +36,31 @@ pub(crate) fn create_call_call<'a>(
3536
let arguments = ctx.ast.vec1(Argument::from(this));
3637
ctx.ast.expression_call(span, callee, NONE, arguments, false)
3738
}
39+
40+
/// Wrap the expression with an arrow function iife.
41+
///
42+
/// `expr` -> `(() => { return expr; })()`
43+
pub(crate) fn wrap_arrow_function_iife<'a>(
44+
expr: &mut Expression<'a>,
45+
ctx: &mut TraverseCtx<'a>,
46+
) -> Expression<'a> {
47+
let scope_id =
48+
ctx.insert_scope_below_expression(expr, ScopeFlags::Arrow | ScopeFlags::Function);
49+
50+
let kind = FormalParameterKind::ArrowFormalParameters;
51+
let params = ctx.ast.formal_parameters(SPAN, kind, ctx.ast.vec(), NONE);
52+
let statements =
53+
ctx.ast.vec1(ctx.ast.statement_return(SPAN, Some(ctx.ast.move_expression(expr))));
54+
let body = ctx.ast.function_body(SPAN, ctx.ast.vec(), statements);
55+
let arrow = ctx.ast.alloc_arrow_function_expression_with_scope_id(
56+
SPAN, false, false, NONE, params, NONE, body, scope_id,
57+
);
58+
// IIFE
59+
ctx.ast.expression_call(
60+
SPAN,
61+
Expression::ArrowFunctionExpression(arrow),
62+
NONE,
63+
ctx.ast.vec(),
64+
false,
65+
)
66+
}

0 commit comments

Comments
 (0)