Skip to content

Commit 6c76f0a

Browse files
authored
feat(es/minifier): Support BinaryExpression for Evaluator (#11390)
**Description:** Improve Evaluator for `@swc/plugin-formatjs` usage. - Support evaluate BinaryExpression. - Add public functions store and resolve_identifier. - Make `eval_as_expr` a public function. **Related issue:** ref: swc-project/plugins#567
1 parent ee40804 commit 6c76f0a

File tree

3 files changed

+55
-6
lines changed

3 files changed

+55
-6
lines changed

.changeset/late-suns-dress.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
swc_ecma_minifier: minor
3+
swc_core: minor
4+
---
5+
6+
feat(es/minifier): Support BinaryExpression for Evaluator

crates/swc_ecma_minifier/src/eval.rs

Lines changed: 48 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,19 @@ impl Evaluator {
100100
}
101101
}
102102

103+
pub fn store(&self, id: Id, value: &Expr) {
104+
self.data.store(id, value);
105+
}
106+
107+
pub fn resolve_identifier(&mut self, ident: &Ident) -> Option<Box<Expr>> {
108+
self.run();
109+
110+
let lock = self.data.store.lock();
111+
let val = lock.cache.get(&ident.to_id())?;
112+
113+
Some(val.clone())
114+
}
115+
103116
pub fn eval(&mut self, e: &Expr) -> Option<EvalResult> {
104117
match e {
105118
Expr::Seq(s) => return self.eval(s.exprs.last()?),
@@ -185,17 +198,46 @@ impl Evaluator {
185198
Some(EvalResult::Lit(self.eval_as_expr(e)?.lit()?))
186199
}
187200

188-
fn eval_as_expr(&mut self, e: &Expr) -> Option<Box<Expr>> {
201+
pub fn eval_as_expr(&mut self, e: &Expr) -> Option<Box<Expr>> {
189202
match e {
190-
Expr::Ident(i) => {
191-
self.run();
203+
Expr::Bin(BinExpr {
204+
span,
205+
op,
206+
left,
207+
right,
208+
}) => {
209+
let l = if left.is_lit() {
210+
left.clone()
211+
} else {
212+
self.eval_as_expr(left)?
213+
};
214+
let r = if right.is_lit() {
215+
right.clone()
216+
} else {
217+
self.eval_as_expr(right)?
218+
};
192219

193-
let lock = self.data.store.lock();
194-
let val = lock.cache.get(&i.to_id())?;
220+
let mut e: Expr = BinExpr {
221+
span: *span,
222+
op: *op,
223+
left: l,
224+
right: r,
225+
}
226+
.into();
195227

196-
return Some(val.clone());
228+
e.visit_mut_with(&mut pure_optimizer(
229+
&Default::default(),
230+
self.marks,
231+
PureOptimizerConfig {
232+
enable_join_vars: true,
233+
force_str_for_tpl: self.data.force_str_for_tpl(),
234+
},
235+
));
236+
return Some(Box::new(e));
197237
}
198238

239+
Expr::Ident(i) => return self.resolve_identifier(i),
240+
199241
Expr::Member(MemberExpr {
200242
span, obj, prop, ..
201243
}) if !prop.is_computed() => {

crates/swc_ecma_minifier/tests/eval.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ fn simple() {
8989
);
9090
assert_eq!(eval("const high = '\\uD83D';", "high").unwrap(), "\\uD83D");
9191
assert_eq!(eval("const low = '\\uDCA9';", "low").unwrap(), "\\uDCA9");
92+
assert_eq!(eval("", "'1' + '2'").unwrap(), "12");
9293
}
9394

9495
#[test]

0 commit comments

Comments
 (0)