Skip to content

Commit

Permalink
refactor(es): Use Into and From for AST construction (#9201)
Browse files Browse the repository at this point in the history
**Description:**

This PR will help modifying boxedness of AST nodes.
  • Loading branch information
kdy1 committed Jul 11, 2024
1 parent 728417c commit 28b6369
Show file tree
Hide file tree
Showing 88 changed files with 2,223 additions and 1,765 deletions.
37 changes: 20 additions & 17 deletions crates/swc_bundler/src/bundler/chunk/cjs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,14 @@ where
});
module.sort(info.id, &ctx.graph, &ctx.cycles, &self.cm);

let stmt = ModuleItem::Stmt(wrap_module(
let stmt = wrap_module(
SyntaxContext::empty(),
SyntaxContext::empty().apply_mark(self.unresolved_mark),
info.local_ctxt(),
load_var,
module.into(),
));
)
.into();

let wrapped = Modules::from(
info.id,
Expand Down Expand Up @@ -150,7 +151,7 @@ fn wrap_module(

// var load = __swcpack_require__.bind(void 0, moduleDecl)

Stmt::Decl(Decl::Var(Box::new(VarDecl {
VarDecl {
span: DUMMY_SP,
kind: VarDeclKind::Var,
declare: false,
Expand All @@ -168,7 +169,8 @@ fn wrap_module(
definite: false,
}],
..Default::default()
})))
}
.into()
}

struct RequireReplacer<'a, 'b, L, R>
Expand Down Expand Up @@ -248,16 +250,15 @@ where
// Side effect import
if i.specifiers.is_empty() {
self.replaced = true;
*node = ModuleItem::Stmt(
CallExpr {
span: DUMMY_SP,
callee: load_var.as_callee(),
args: vec![],
*node = CallExpr {
span: DUMMY_SP,
callee: load_var.as_callee(),
args: vec![],

..Default::default()
}
.into_stmt(),
);
..Default::default()
}
.into_stmt()
.into();
return;
}

Expand Down Expand Up @@ -291,7 +292,7 @@ where
}
ImportSpecifier::Namespace(ns) => {
self.replaced = true;
*node = ModuleItem::Stmt(Stmt::Decl(Decl::Var(Box::new(VarDecl {
*node = VarDecl {
span: i.span,
kind: VarDeclKind::Var,
declare: false,
Expand All @@ -311,14 +312,15 @@ where
definite: false,
}],
..Default::default()
}))));
}
.into();
return;
}
}
}

self.replaced = true;
*node = ModuleItem::Stmt(Stmt::Decl(Decl::Var(Box::new(VarDecl {
*node = VarDecl {
span: i.span,
kind: VarDeclKind::Var,
declare: false,
Expand All @@ -339,7 +341,8 @@ where
definite: false,
}],
..Default::default()
}))));
}
.into();
}
}
}
Expand Down
55 changes: 29 additions & 26 deletions crates/swc_bundler/src/bundler/chunk/computed_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ where
});
additional_items.push((
module_id,
ModuleItem::ModuleDecl(ModuleDecl::ExportNamed(NamedExport {
NamedExport {
span: DUMMY_SP,
specifiers: vec![specifier],
src: None,
Expand All @@ -94,7 +94,8 @@ where
}
.into_with(),
),
})),
}
.into(),
));
}
}
Expand All @@ -112,7 +113,7 @@ where

module.append_all(additional_items);

let return_stmt = Stmt::Return(ReturnStmt {
let return_stmt = ReturnStmt {
span: DUMMY_SP,
arg: Some(
ObjectLit {
Expand All @@ -121,7 +122,8 @@ where
}
.into(),
),
});
}
.into();

module.iter().for_each(|(_, v)| {
if let ModuleItem::ModuleDecl(ModuleDecl::ExportAll(ref export)) = v {
Expand Down Expand Up @@ -179,10 +181,7 @@ where
}],
};

module.append(
id,
ModuleItem::Stmt(Stmt::Decl(Decl::Var(Box::new(var_decl)))),
);
module.append(id, var_decl.into());

// print_hygiene(
// "wrap",
Expand Down Expand Up @@ -233,7 +232,7 @@ impl Fold for ExportToReturn {
};

let stmt = match decl {
ModuleDecl::Import(_) => return ModuleItem::ModuleDecl(decl),
ModuleDecl::Import(_) => return decl.into(),
ModuleDecl::ExportDecl(export) => {
match &export.decl {
Decl::Class(ClassDecl { ident, .. }) | Decl::Fn(FnDecl { ident, .. }) => {
Expand All @@ -246,7 +245,7 @@ impl Fold for ExportToReturn {
_ => unreachable!(),
}

Some(Stmt::Decl(export.decl))
Some(ModuleItem::from(export.decl))
}

ModuleDecl::ExportDefaultDecl(export) => match export.decl {
Expand All @@ -259,11 +258,14 @@ impl Fold for ExportToReturn {
ident.clone(),
);

Some(Stmt::Decl(Decl::Class(ClassDecl {
ident,
class: expr.class,
declare: false,
})))
Some(
ClassDecl {
ident,
class: expr.class,
declare: false,
}
.into(),
)
}
DefaultDecl::Fn(expr) => {
let ident = expr.ident;
Expand All @@ -274,18 +276,19 @@ impl Fold for ExportToReturn {
ident.clone(),
);

Some(Stmt::Decl(Decl::Fn(FnDecl {
ident,
function: expr.function,
declare: false,
})))
Some(
FnDecl {
ident,
function: expr.function,
declare: false,
}
.into(),
)
}
DefaultDecl::TsInterfaceDecl(_) => None,
},
ModuleDecl::ExportDefaultExpr(_) => None,
ModuleDecl::ExportAll(export) => {
return ModuleItem::ModuleDecl(ModuleDecl::ExportAll(export))
}
ModuleDecl::ExportAll(export) => return export.into(),
ModuleDecl::ExportNamed(export) => {
for specifier in &export.specifiers {
match specifier {
Expand Down Expand Up @@ -322,7 +325,7 @@ impl Fold for ExportToReturn {
{
None
} else {
return ModuleItem::ModuleDecl(ModuleDecl::ExportNamed(export));
return export.into();
}
}
ModuleDecl::TsImportEquals(_) => None,
Expand All @@ -331,9 +334,9 @@ impl Fold for ExportToReturn {
};

if let Some(stmt) = stmt {
ModuleItem::Stmt(stmt)
stmt
} else {
ModuleItem::Stmt(Stmt::Empty(EmptyStmt { span: DUMMY_SP }))
EmptyStmt { span: DUMMY_SP }.into()
}
}
}
Loading

0 comments on commit 28b6369

Please sign in to comment.