refactor(transformer): wrap_statements_in_arrow_function_iife utility function (#8548)

Move utility function for creating an IIFE from `ClassStaticBlock` transform to utils module.
This commit is contained in:
overlookmotel 2025-01-16 14:55:52 +00:00
parent b552f5cfd3
commit 712633f0bc
3 changed files with 32 additions and 47 deletions

View file

@ -7,10 +7,12 @@ use oxc_ast::{
ast::*, ast::*,
visit::{walk_mut, VisitMut}, visit::{walk_mut, VisitMut},
}; };
use oxc_span::SPAN;
use oxc_syntax::scope::{ScopeFlags, ScopeId}; use oxc_syntax::scope::{ScopeFlags, ScopeId};
use oxc_traverse::TraverseCtx; use oxc_traverse::TraverseCtx;
use super::super::ClassStaticBlock; use crate::utils::ast_builder::wrap_statements_in_arrow_function_iife;
use super::{ use super::{
super_converter::{ClassPropertiesSuperConverter, ClassPropertiesSuperConverterMode}, super_converter::{ClassPropertiesSuperConverter, ClassPropertiesSuperConverterMode},
ClassProperties, ClassProperties,
@ -88,7 +90,7 @@ impl<'a> ClassProperties<'a, '_> {
let outer_scope_id = ctx.current_scope_id(); let outer_scope_id = ctx.current_scope_id();
ctx.scopes_mut().change_parent_id(scope_id, Some(outer_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( fn convert_static_block_with_single_expression_to_expression(

View file

@ -41,12 +41,14 @@
use itoa::Buffer as ItoaBuffer; 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_ast::{ast::*, NONE};
use oxc_span::SPAN; use oxc_span::SPAN;
use oxc_syntax::scope::{ScopeFlags, ScopeId}; use oxc_syntax::scope::{ScopeFlags, ScopeId};
use oxc_traverse::{Traverse, TraverseCtx}; use oxc_traverse::{Traverse, TraverseCtx};
use crate::utils::ast_builder::wrap_statements_in_arrow_function_iife;
pub struct ClassStaticBlock; pub struct ClassStaticBlock;
impl ClassStaticBlock { impl ClassStaticBlock {
@ -157,32 +159,7 @@ impl ClassStaticBlock {
// Always strict mode since we're in a class. // Always strict mode since we're in a class.
*ctx.scopes_mut().get_flags_mut(scope_id) = *ctx.scopes_mut().get_flags_mut(scope_id) =
ScopeFlags::Function | ScopeFlags::Arrow | ScopeFlags::StrictMode; ScopeFlags::Function | ScopeFlags::Arrow | ScopeFlags::StrictMode;
wrap_statements_in_arrow_function_iife(ctx.ast.move_vec(stmts), scope_id, SPAN, ctx)
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)
} }
/// Convert static block to expression which will be value of private field, /// Convert static block to expression which will be value of private field,

View file

@ -1,5 +1,6 @@
use oxc_allocator::Vec as ArenaVec;
use oxc_ast::{ast::*, NONE}; use oxc_ast::{ast::*, NONE};
use oxc_semantic::ScopeFlags; use oxc_semantic::{ScopeFlags, ScopeId};
use oxc_span::{GetSpan, SPAN}; use oxc_span::{GetSpan, SPAN};
use oxc_traverse::TraverseCtx; use oxc_traverse::TraverseCtx;
@ -40,28 +41,33 @@ pub(crate) fn create_call_call<'a>(
/// Wrap an `Expression` in an arrow function IIFE (immediately invoked function expression) /// Wrap an `Expression` in an arrow function IIFE (immediately invoked function expression)
/// with a body block. /// with a body block.
/// ///
/// `expr` -> `(() => { return expr; })()` /// `expr` -> `(() => { return expr; })()`
pub(crate) fn wrap_expression_in_arrow_function_iife<'a>( pub(crate) fn wrap_expression_in_arrow_function_iife<'a>(
expr: Expression<'a>, expr: Expression<'a>,
ctx: &mut TraverseCtx<'a>, ctx: &mut TraverseCtx<'a>,
) -> Expression<'a> { ) -> Expression<'a> {
let scope_id = let scope_id =
ctx.insert_scope_below_expression(&expr, ScopeFlags::Arrow | ScopeFlags::Function); ctx.insert_scope_below_expression(&expr, ScopeFlags::Arrow | ScopeFlags::Function);
let span = expr.span(); let span = expr.span();
let kind = FormalParameterKind::ArrowFormalParameters; let stmts = ctx.ast.vec1(ctx.ast.statement_return(SPAN, Some(expr)));
let params = ctx.ast.formal_parameters(SPAN, kind, ctx.ast.vec(), NONE); wrap_statements_in_arrow_function_iife(stmts, scope_id, span, ctx)
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( /// Wrap statements in an IIFE (immediately invoked function expression).
SPAN, false, false, NONE, params, NONE, body, scope_id, ///
); /// `x; y; z;` -> `(() => { x; y; z; })()`
// IIFE pub(crate) fn wrap_statements_in_arrow_function_iife<'a>(
ctx.ast.expression_call( stmts: ArenaVec<'a, Statement<'a>>,
span, scope_id: ScopeId,
Expression::ArrowFunctionExpression(arrow), span: Span,
NONE, ctx: &mut TraverseCtx<'a>,
ctx.ast.vec(), ) -> Expression<'a> {
false, let kind = FormalParameterKind::ArrowFormalParameters;
) 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)
} }