Skip to content

Commit e7358e0

Browse files
committedJul 12, 2024
refactor(es): Use into for AST construction (#9197)
**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.
1 parent a7d236c commit e7358e0

File tree

138 files changed

+3686
-2790
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

138 files changed

+3686
-2790
lines changed
 

‎crates/swc_bundler/src/bundler/chunk/cjs.rs

+7-9
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ fn wrap_module(
108108
}
109109

110110
// ... body of foo
111-
let module_fn = Expr::Fn(FnExpr {
111+
let module_fn: Expr = FnExpr {
112112
ident: None,
113113
function: Box::new(Function {
114114
params: vec![
@@ -145,7 +145,8 @@ fn wrap_module(
145145
is_async: false,
146146
..Default::default()
147147
}),
148-
});
148+
}
149+
.into();
149150

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

@@ -355,15 +356,12 @@ impl VisitMut for DefaultHandler {
355356

356357
if let Expr::Ident(i) = e {
357358
if i.sym == "default" {
358-
*e = Expr::Member(MemberExpr {
359+
*e = MemberExpr {
359360
span: i.span,
360-
obj: Box::new(Expr::Ident(Ident::new(
361-
"module".into(),
362-
DUMMY_SP,
363-
self.local_ctxt,
364-
))),
361+
obj: Ident::new("module".into(), DUMMY_SP, self.local_ctxt).into(),
365362
prop: MemberProp::Ident(quote_ident!("exports")),
366-
});
363+
}
364+
.into();
367365
}
368366
}
369367
}

‎crates/swc_bundler/src/bundler/chunk/computed_key.rs

+17-11
Original file line numberDiff line numberDiff line change
@@ -114,10 +114,13 @@ where
114114

115115
let return_stmt = Stmt::Return(ReturnStmt {
116116
span: DUMMY_SP,
117-
arg: Some(Box::new(Expr::Object(ObjectLit {
118-
span: DUMMY_SP,
119-
props: take(&mut export_visitor.return_props),
120-
}))),
117+
arg: Some(
118+
ObjectLit {
119+
span: DUMMY_SP,
120+
props: take(&mut export_visitor.return_props),
121+
}
122+
.into(),
123+
),
121124
});
122125

123126
module.iter().for_each(|(_, v)| {
@@ -131,7 +134,7 @@ where
131134
}
132135
});
133136

134-
let module_fn = Expr::Fn(FnExpr {
137+
let module_fn: Expr = FnExpr {
135138
function: Box::new(Function {
136139
params: Default::default(),
137140
body: Some(BlockStmt {
@@ -144,20 +147,23 @@ where
144147
..Default::default()
145148
}),
146149
ident: None,
147-
});
150+
}
151+
.into();
148152

149-
let mut module_expr = Expr::Call(CallExpr {
153+
let mut module_expr = CallExpr {
150154
span: DUMMY_SP,
151155
callee: module_fn.as_callee(),
152156
args: Default::default(),
153157
..Default::default()
154-
});
158+
}
159+
.into();
155160

156161
if is_async {
157-
module_expr = Expr::Await(AwaitExpr {
162+
module_expr = AwaitExpr {
158163
span: DUMMY_SP,
159164
arg: Box::new(module_expr),
160-
});
165+
}
166+
.into();
161167
}
162168

163169
let var_decl = VarDecl {
@@ -208,7 +214,7 @@ impl ExportToReturn {
208214
self.return_props
209215
.push(PropOrSpread::Prop(Box::new(Prop::KeyValue(KeyValueProp {
210216
key: PropName::Ident(key.into()),
211-
value: Box::new(Expr::Ident(value)),
217+
value: value.into(),
212218
}))));
213219
}
214220
}

0 commit comments

Comments
 (0)
Please sign in to comment.