Skip to content

Commit

Permalink
refactor(transformer): wrap_statements_in_arrow_function_iife utili…
Browse files Browse the repository at this point in the history
…ty function (#8548)

Move utility function for creating an IIFE from `ClassStaticBlock` transform to utils module.
  • Loading branch information
overlookmotel committed Jan 16, 2025
1 parent b552f5c commit 712633f
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ use oxc_ast::{
ast::*,
visit::{walk_mut, VisitMut},
};
use oxc_span::SPAN;
use oxc_syntax::scope::{ScopeFlags, ScopeId};
use oxc_traverse::TraverseCtx;

use super::super::ClassStaticBlock;
use crate::utils::ast_builder::wrap_statements_in_arrow_function_iife;

use super::{
super_converter::{ClassPropertiesSuperConverter, ClassPropertiesSuperConverterMode},
ClassProperties,
Expand Down Expand Up @@ -88,7 +90,7 @@ impl<'a> ClassProperties<'a, '_> {
let outer_scope_id = ctx.current_scope_id();
ctx.scopes_mut().change_parent_id(scope_id, Some(outer_scope_id));

ClassStaticBlock::wrap_statements_in_iife(stmts, scope_id, ctx)
wrap_statements_in_arrow_function_iife(ctx.ast.move_vec(stmts), scope_id, SPAN, ctx)
}

fn convert_static_block_with_single_expression_to_expression(
Expand Down
31 changes: 4 additions & 27 deletions crates/oxc_transformer/src/es2022/class_static_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,14 @@
use itoa::Buffer as ItoaBuffer;

use oxc_allocator::{String as ArenaString, Vec as ArenaVec};
use oxc_allocator::String as ArenaString;
use oxc_ast::{ast::*, NONE};
use oxc_span::SPAN;
use oxc_syntax::scope::{ScopeFlags, ScopeId};
use oxc_traverse::{Traverse, TraverseCtx};

use crate::utils::ast_builder::wrap_statements_in_arrow_function_iife;

pub struct ClassStaticBlock;

impl ClassStaticBlock {
Expand Down Expand Up @@ -157,32 +159,7 @@ impl ClassStaticBlock {
// Always strict mode since we're in a class.
*ctx.scopes_mut().get_flags_mut(scope_id) =
ScopeFlags::Function | ScopeFlags::Arrow | ScopeFlags::StrictMode;

Self::wrap_statements_in_iife(stmts, scope_id, ctx)
}

/// Wrap statements in an IIFE.
///
/// This function also used by `ClassProperties` transform.
pub(super) fn wrap_statements_in_iife<'a>(
stmts: &mut ArenaVec<'a, Statement<'a>>,
scope_id: ScopeId,
ctx: &mut TraverseCtx<'a>,
) -> Expression<'a> {
let stmts = ctx.ast.move_vec(stmts);
let params = ctx.ast.alloc_formal_parameters(
SPAN,
FormalParameterKind::ArrowFormalParameters,
ctx.ast.vec(),
NONE,
);
let body = ctx.ast.alloc_function_body(SPAN, ctx.ast.vec(), stmts);
let arrow = Expression::ArrowFunctionExpression(
ctx.ast.alloc_arrow_function_expression_with_scope_id(
SPAN, false, false, NONE, params, NONE, body, scope_id,
),
);
ctx.ast.expression_call(SPAN, arrow, NONE, ctx.ast.vec(), false)
wrap_statements_in_arrow_function_iife(ctx.ast.move_vec(stmts), scope_id, SPAN, ctx)
}

/// Convert static block to expression which will be value of private field,
Expand Down
40 changes: 23 additions & 17 deletions crates/oxc_transformer/src/utils/ast_builder.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use oxc_allocator::Vec as ArenaVec;
use oxc_ast::{ast::*, NONE};
use oxc_semantic::ScopeFlags;
use oxc_semantic::{ScopeFlags, ScopeId};
use oxc_span::{GetSpan, SPAN};
use oxc_traverse::TraverseCtx;

Expand Down Expand Up @@ -40,28 +41,33 @@ pub(crate) fn create_call_call<'a>(
/// Wrap an `Expression` in an arrow function IIFE (immediately invoked function expression)
/// with a body block.
///
/// `expr` -> `(() => { return expr; })()`
/// `expr` -> `(() => { return expr; })()`
pub(crate) fn wrap_expression_in_arrow_function_iife<'a>(
expr: Expression<'a>,
ctx: &mut TraverseCtx<'a>,
) -> Expression<'a> {
let scope_id =
ctx.insert_scope_below_expression(&expr, ScopeFlags::Arrow | ScopeFlags::Function);

let span = expr.span();
let stmts = ctx.ast.vec1(ctx.ast.statement_return(SPAN, Some(expr)));
wrap_statements_in_arrow_function_iife(stmts, scope_id, span, ctx)
}

/// Wrap statements in an IIFE (immediately invoked function expression).
///
/// `x; y; z;` -> `(() => { x; y; z; })()`
pub(crate) fn wrap_statements_in_arrow_function_iife<'a>(
stmts: ArenaVec<'a, Statement<'a>>,
scope_id: ScopeId,
span: Span,
ctx: &mut TraverseCtx<'a>,
) -> Expression<'a> {
let kind = FormalParameterKind::ArrowFormalParameters;
let params = ctx.ast.formal_parameters(SPAN, kind, ctx.ast.vec(), NONE);
let statements = ctx.ast.vec1(ctx.ast.statement_return(SPAN, Some(expr)));
let body = ctx.ast.function_body(SPAN, ctx.ast.vec(), statements);
let arrow = ctx.ast.alloc_arrow_function_expression_with_scope_id(
SPAN, false, false, NONE, params, NONE, body, scope_id,
);
// IIFE
ctx.ast.expression_call(
span,
Expression::ArrowFunctionExpression(arrow),
NONE,
ctx.ast.vec(),
false,
)
let params = ctx.ast.alloc_formal_parameters(SPAN, kind, ctx.ast.vec(), NONE);
let body = ctx.ast.alloc_function_body(SPAN, ctx.ast.vec(), stmts);
let arrow =
Expression::ArrowFunctionExpression(ctx.ast.alloc_arrow_function_expression_with_scope_id(
SPAN, false, false, NONE, params, NONE, body, scope_id,
));
ctx.ast.expression_call(span, arrow, NONE, ctx.ast.vec(), false)
}

0 comments on commit 712633f

Please sign in to comment.