Skip to content

Commit bae4940

Browse files
authored
fix(es/minifier): Properly handle object shorthand syntax during compression (#10467)
**Related issue:** - Closes #10466
1 parent 7d49097 commit bae4940

File tree

4 files changed

+32
-22
lines changed

4 files changed

+32
-22
lines changed

crates/swc_ecma_minifier/src/compress/optimize/mod.rs

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2278,28 +2278,6 @@ impl VisitMut for Optimizer<'_> {
22782278
n.retain(|p| !p.pat.is_invalid());
22792279
}
22802280

2281-
#[cfg_attr(feature = "debug", tracing::instrument(level = "debug", skip_all))]
2282-
fn visit_mut_prop(&mut self, n: &mut Prop) {
2283-
n.visit_mut_children_with(self);
2284-
2285-
if let Prop::Shorthand(i) = n {
2286-
if self.vars.has_pending_inline_for(&i.to_id()) {
2287-
let mut e: Expr = i.clone().into();
2288-
e.visit_mut_with(self);
2289-
2290-
*n = Prop::KeyValue(KeyValueProp {
2291-
key: PropName::Ident(i.clone().into()),
2292-
value: Box::new(e),
2293-
});
2294-
}
2295-
}
2296-
2297-
#[cfg(debug_assertions)]
2298-
{
2299-
n.visit_with(&mut AssertValid);
2300-
}
2301-
}
2302-
23032281
#[cfg_attr(feature = "debug", tracing::instrument(level = "debug", skip_all))]
23042282
fn visit_mut_return_stmt(&mut self, n: &mut ReturnStmt) {
23052283
n.visit_mut_children_with(self);

crates/swc_ecma_minifier/src/compress/optimize/util.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,20 @@ impl VisitMut for Finalizer<'_> {
453453

454454
n.retain(|v| !v.name.is_invalid());
455455
}
456+
457+
fn visit_mut_prop(&mut self, n: &mut Prop) {
458+
n.visit_mut_children_with(self);
459+
460+
if let Prop::Shorthand(i) = n {
461+
if let Some(expr) = self.lits.get(&i.to_id()) {
462+
*n = Prop::KeyValue(KeyValueProp {
463+
key: i.take().into(),
464+
value: expr.clone(),
465+
});
466+
self.changed = true;
467+
}
468+
}
469+
}
456470
}
457471

458472
pub(crate) struct NormalMultiReplacer<'a> {
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
const G = {
2+
setPackageName({ packageName }) {
3+
if ("string" == typeof packageName) this.packageName = packageName;
4+
return this;
5+
}
6+
};
7+
var packageName;
8+
packageName = "@clerk/clerk-react", G.setPackageName({
9+
packageName
10+
}), console.log(G.packageName);
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
const G = {
2+
setPackageName ({ packageName }) {
3+
return "string" == typeof packageName && (this.packageName = packageName), this;
4+
}
5+
};
6+
G.setPackageName({
7+
packageName: "@clerk/clerk-react"
8+
}), console.log(G.packageName);

0 commit comments

Comments
 (0)