Skip to content

Commit

Permalink
refactor(transformer): move common utils functions to the root module
Browse files Browse the repository at this point in the history
  • Loading branch information
Dunqing committed Jan 15, 2025
1 parent d6ea754 commit 0d5d4ea
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,17 @@ use oxc_traverse::{
ast_operations::get_var_name_from_node, Ancestor, BoundIdentifier, TraverseCtx,
};

use crate::{common::helper_loader::Helper, TransformCtx};
use crate::{
common::helper_loader::Helper,
utils::ast_builder::{create_bind_call, create_call_call, create_member_callee},
TransformCtx,
};

use super::{
class_details::ResolvedGetSetPrivateProp,
utils::{
create_assignment, create_bind_call, create_call_call, create_member_callee,
create_underscore_ident_name, debug_assert_expr_is_not_parenthesis_or_typescript_syntax,
create_assignment, create_underscore_ident_name,
debug_assert_expr_is_not_parenthesis_or_typescript_syntax,
},
ClassProperties, ResolvedPrivateProp,
};
Expand Down
34 changes: 0 additions & 34 deletions crates/oxc_transformer/src/es2022/class_properties/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,37 +81,3 @@ pub(super) fn create_prototype_member<'a>(
let static_member = ctx.ast.member_expression_static(SPAN, object, property, false);
Expression::from(static_member)
}

/// `object` -> `object.call`.
pub(super) fn create_member_callee<'a>(
object: Expression<'a>,
property: &'static str,
ctx: &mut TraverseCtx<'a>,
) -> Expression<'a> {
let property = ctx.ast.identifier_name(SPAN, Atom::from(property));
Expression::from(ctx.ast.member_expression_static(SPAN, object, property, false))
}

/// `object` -> `object.bind(this)`.
pub(super) fn create_bind_call<'a>(
callee: Expression<'a>,
this: Expression<'a>,
span: Span,
ctx: &mut TraverseCtx<'a>,
) -> Expression<'a> {
let callee = create_member_callee(callee, "bind", ctx);
let arguments = ctx.ast.vec1(Argument::from(this));
ctx.ast.expression_call(span, callee, NONE, arguments, false)
}

/// `object` -> `object.call(...arguments)`.
pub(super) fn create_call_call<'a>(
callee: Expression<'a>,
this: Expression<'a>,
span: Span,
ctx: &mut TraverseCtx<'a>,
) -> Expression<'a> {
let callee = create_member_callee(callee, "call", ctx);
let arguments = ctx.ast.vec1(Argument::from(this));
ctx.ast.expression_call(span, callee, NONE, arguments, false)
}
1 change: 1 addition & 0 deletions crates/oxc_transformer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ mod common;
mod compiler_assumptions;
mod context;
mod options;
mod utils;

// Presets: <https://babel.dev/docs/presets>
mod es2015;
Expand Down
37 changes: 37 additions & 0 deletions crates/oxc_transformer/src/utils/ast_builder.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
use oxc_ast::{ast::*, NONE};
use oxc_span::SPAN;
use oxc_traverse::TraverseCtx;

/// `object` -> `object.call`.
pub(crate) fn create_member_callee<'a>(
object: Expression<'a>,
property: &'static str,
ctx: &mut TraverseCtx<'a>,
) -> Expression<'a> {
let property = ctx.ast.identifier_name(SPAN, Atom::from(property));
Expression::from(ctx.ast.member_expression_static(SPAN, object, property, false))
}

/// `object` -> `object.bind(this)`.
pub(crate) fn create_bind_call<'a>(
callee: Expression<'a>,
this: Expression<'a>,
span: Span,
ctx: &mut TraverseCtx<'a>,
) -> Expression<'a> {
let callee = create_member_callee(callee, "bind", ctx);
let arguments = ctx.ast.vec1(Argument::from(this));
ctx.ast.expression_call(span, callee, NONE, arguments, false)
}

/// `object` -> `object.call(...arguments)`.
pub(crate) fn create_call_call<'a>(
callee: Expression<'a>,
this: Expression<'a>,
span: Span,
ctx: &mut TraverseCtx<'a>,
) -> Expression<'a> {
let callee = create_member_callee(callee, "call", ctx);
let arguments = ctx.ast.vec1(Argument::from(this));
ctx.ast.expression_call(span, callee, NONE, arguments, false)
}
1 change: 1 addition & 0 deletions crates/oxc_transformer/src/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub(crate) mod ast_builder;

0 comments on commit 0d5d4ea

Please sign in to comment.