Skip to content

Commit

Permalink
refactor(es): Use into for AST construction (#9197)
Browse files Browse the repository at this point in the history
**Description:**

This PR changes the AST node construction code to use `.into()` or `::from()` to make changing the boxed-ness of the AST node easier. I used `ast-grep` to make large changes across codebase.
  • Loading branch information
kdy1 committed Jul 12, 2024
1 parent a7d236c commit e7358e0
Show file tree
Hide file tree
Showing 138 changed files with 3,686 additions and 2,790 deletions.
16 changes: 7 additions & 9 deletions crates/swc_bundler/src/bundler/chunk/cjs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ fn wrap_module(
}

// ... body of foo
let module_fn = Expr::Fn(FnExpr {
let module_fn: Expr = FnExpr {
ident: None,
function: Box::new(Function {
params: vec![
Expand Down Expand Up @@ -145,7 +145,8 @@ fn wrap_module(
is_async: false,
..Default::default()
}),
});
}
.into();

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

Expand Down Expand Up @@ -355,15 +356,12 @@ impl VisitMut for DefaultHandler {

if let Expr::Ident(i) = e {
if i.sym == "default" {
*e = Expr::Member(MemberExpr {
*e = MemberExpr {
span: i.span,
obj: Box::new(Expr::Ident(Ident::new(
"module".into(),
DUMMY_SP,
self.local_ctxt,
))),
obj: Ident::new("module".into(), DUMMY_SP, self.local_ctxt).into(),
prop: MemberProp::Ident(quote_ident!("exports")),
});
}
.into();
}
}
}
Expand Down
28 changes: 17 additions & 11 deletions crates/swc_bundler/src/bundler/chunk/computed_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,13 @@ where

let return_stmt = Stmt::Return(ReturnStmt {
span: DUMMY_SP,
arg: Some(Box::new(Expr::Object(ObjectLit {
span: DUMMY_SP,
props: take(&mut export_visitor.return_props),
}))),
arg: Some(
ObjectLit {
span: DUMMY_SP,
props: take(&mut export_visitor.return_props),
}
.into(),
),
});

module.iter().for_each(|(_, v)| {
Expand All @@ -131,7 +134,7 @@ where
}
});

let module_fn = Expr::Fn(FnExpr {
let module_fn: Expr = FnExpr {
function: Box::new(Function {
params: Default::default(),
body: Some(BlockStmt {
Expand All @@ -144,20 +147,23 @@ where
..Default::default()
}),
ident: None,
});
}
.into();

let mut module_expr = Expr::Call(CallExpr {
let mut module_expr = CallExpr {
span: DUMMY_SP,
callee: module_fn.as_callee(),
args: Default::default(),
..Default::default()
});
}
.into();

if is_async {
module_expr = Expr::Await(AwaitExpr {
module_expr = AwaitExpr {
span: DUMMY_SP,
arg: Box::new(module_expr),
});
}
.into();
}

let var_decl = VarDecl {
Expand Down Expand Up @@ -208,7 +214,7 @@ impl ExportToReturn {
self.return_props
.push(PropOrSpread::Prop(Box::new(Prop::KeyValue(KeyValueProp {
key: PropName::Ident(key.into()),
value: Box::new(Expr::Ident(value)),
value: value.into(),
}))));
}
}
Expand Down
41 changes: 20 additions & 21 deletions crates/swc_bundler/src/bundler/chunk/merge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -693,7 +693,7 @@ where
))
}
None => {
let init = Expr::Class(c);
let init = c;
new.push(init.assign_to(local.clone()).into_module_item(
injected_ctxt,
"prepare -> export default decl -> class -> without \
Expand Down Expand Up @@ -974,17 +974,20 @@ where

vars.push(VarDeclarator {
span: DUMMY_SP,
name: Pat::Ident(mod_var.clone().into()),
init: Some(Box::new(Expr::Call(CallExpr {
span: DUMMY_SP,
callee: Ident::new(
"load".into(),
DUMMY_SP,
dep.export_ctxt(),
)
.as_callee(),
..Default::default()
}))),
name: mod_var.clone().into(),
init: Some(
CallExpr {
span: DUMMY_SP,
callee: Ident::new(
"load".into(),
DUMMY_SP,
dep.export_ctxt(),
)
.as_callee(),
..Default::default()
}
.into(),
),
definite: Default::default(),
});
for s in specifiers {
Expand All @@ -994,10 +997,8 @@ where
ModuleExportName::Ident(name) => {
vars.push(VarDeclarator {
span: s.span,
name: Pat::Ident(name.clone().into()),
init: Some(Box::new(Expr::Ident(
mod_var.clone(),
))),
name: name.clone().into(),
init: Some(mod_var.clone().into()),
definite: Default::default(),
});
}
Expand All @@ -1011,7 +1012,7 @@ where
ExportSpecifier::Default(s) => {
vars.push(VarDeclarator {
span: DUMMY_SP,
name: Pat::Ident(s.exported.clone().into()),
name: s.exported.clone().into(),
init: Some(
mod_var
.clone()
Expand Down Expand Up @@ -1042,9 +1043,7 @@ where
};
vars.push(VarDeclarator {
span: s.span,
name: Pat::Ident(
exported.clone().unwrap().into(),
),
name: exported.clone().unwrap().into(),
init: Some(
mod_var
.clone()
Expand Down Expand Up @@ -1339,7 +1338,7 @@ impl VisitMut for ImportMetaHandler<'_, '_> {
..
}) = e
{
*e = Expr::Ident(self.inline_ident.clone());
*e = self.inline_ident.clone().into();
self.occurred = true;
}
}
Expand Down
26 changes: 15 additions & 11 deletions crates/swc_bundler/src/bundler/finalize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ where
"default".into(),
DUMMY_SP,
)),
value: Box::new(Expr::Ident(s.exported)),
value: s.exported.into(),
},
))));
}
Expand All @@ -225,7 +225,7 @@ where
props.push(PropOrSpread::Prop(Box::new(
Prop::KeyValue(KeyValueProp {
key: PropName::Ident(exported.into()),
value: Box::new(Expr::Ident(orig)),
value: orig.into(),
}),
)));
}
Expand Down Expand Up @@ -262,7 +262,7 @@ where
"default".into(),
export.span,
)),
value: Box::new(Expr::Ident(ident.clone())),
value: ident.clone().into(),
},
))));

Expand All @@ -283,7 +283,7 @@ where
"default".into(),
export.span,
)),
value: Box::new(Expr::Ident(ident.clone())),
value: ident.clone().into(),
},
))));

Expand All @@ -302,7 +302,7 @@ where
))));
let var = VarDeclarator {
span: DUMMY_SP,
name: Pat::Ident(default_var.into()),
name: default_var.into(),
init: Some(export.expr),
definite: false,
};
Expand All @@ -323,10 +323,13 @@ where
};
body.stmts.push(Stmt::Return(ReturnStmt {
span: DUMMY_SP,
arg: Some(Box::new(Expr::Object(ObjectLit {
span: DUMMY_SP,
props,
}))),
arg: Some(
ObjectLit {
span: DUMMY_SP,
props,
}
.into(),
),
}));

let f = Function {
Expand All @@ -342,12 +345,13 @@ where
function: Box::new(f),
};

let iife = Box::new(Expr::Call(CallExpr {
let iife = CallExpr {
span: DUMMY_SP,
callee: invoked_fn_expr.as_callee(),
args: Default::default(),
..Default::default()
}));
}
.into();

Module {
span: DUMMY_SP,
Expand Down
2 changes: 1 addition & 1 deletion crates/swc_bundler/src/bundler/import/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ where
};
prop.ctxt = self.imported_idents.get(&obj.to_id()).copied().unwrap();

*e = Expr::Ident(prop);
*e = prop.into();
}
}

Expand Down
11 changes: 6 additions & 5 deletions crates/swc_bundler/src/bundler/keywords.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,17 +91,18 @@ impl VisitMut for KeywordRenamer {
Some(default) => {
*n = ObjectPatProp::KeyValue(KeyValuePatProp {
key: PropName::Ident(pat.key.take().into()),
value: Box::new(Pat::Assign(AssignPat {
value: AssignPat {
span: pat.span,
left: Box::new(Pat::Ident(renamed.into())),
left: Box::new(renamed.into()),
right: default.take(),
})),
}
.into(),
});
}
None => {
*n = ObjectPatProp::KeyValue(KeyValuePatProp {
key: PropName::Ident(pat.key.take().into()),
value: Box::new(Pat::Ident(renamed.into())),
value: renamed.into(),
})
}
}
Expand Down Expand Up @@ -134,7 +135,7 @@ impl VisitMut for KeywordRenamer {
if let Some(renamed) = self.renamed(i) {
*n = Prop::KeyValue(KeyValueProp {
key: PropName::Ident(i.clone().into()),
value: Box::new(Expr::Ident(renamed)),
value: renamed.into(),
});
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/swc_bundler/src/id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,6 @@ impl From<Id> for Ident {
impl From<Id> for Expr {
#[inline]
fn from(id: Id) -> Self {
Expr::Ident(id.into_ident())
id.into_ident().into()
}
}
2 changes: 1 addition & 1 deletion crates/swc_bundler/src/inline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ impl VisitMut for Inliner {
if i.sym != orig.sym {
*n = Prop::KeyValue(KeyValueProp {
key: PropName::Ident(orig.into()),
value: Box::new(Expr::Ident(i.clone())),
value: i.clone().into(),
});
}
}
Expand Down
7 changes: 4 additions & 3 deletions crates/swc_bundler/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ pub(crate) trait ExprExt: Into<Expr> {

VarDeclarator {
span: DUMMY_SP,
name: Pat::Ident(Ident::new(lhs.0, DUMMY_SP, lhs.1).into()),
name: Ident::new(lhs.0, DUMMY_SP, lhs.1).into(),
init: Some(Box::new(init)),
definite: false,
}
Expand Down Expand Up @@ -184,11 +184,12 @@ impl<T> IntoParallelIterator for T where T: IntoIterator {}
fn metadata(key: &str, value: &str) -> PropOrSpread {
PropOrSpread::Prop(Box::new(Prop::KeyValue(KeyValueProp {
key: PropName::Ident(IdentName::new(key.into(), DUMMY_SP)),
value: Box::new(Expr::Lit(Lit::Str(Str {
value: Lit::Str(Str {
span: DUMMY_SP,
value: value.into(),
raw: None,
}))),
})
.into(),
})))
}

Expand Down
Loading

0 comments on commit e7358e0

Please sign in to comment.