mirror of
https://github.com/swc-project/swc.git
synced 2024-11-23 17:54:15 +03:00
fix(es/minifier): Check if object shorthand is skippable for seq inliner (#8036)
**Related issue:** - Closes #7984
This commit is contained in:
parent
c0625361a1
commit
01391e3c13
@ -1894,12 +1894,29 @@ impl Optimizer<'_> {
|
||||
}
|
||||
PropOrSpread::Prop(prop) => {
|
||||
// Inline into key
|
||||
let key = match &mut **prop {
|
||||
let computed = match &mut **prop {
|
||||
Prop::Shorthand(_) | Prop::Assign(_) => None,
|
||||
Prop::KeyValue(prop) => prop.key.as_mut_computed(),
|
||||
Prop::Getter(prop) => prop.key.as_mut_computed(),
|
||||
Prop::Setter(prop) => prop.key.as_mut_computed(),
|
||||
Prop::Method(prop) => prop.key.as_mut_computed(),
|
||||
};
|
||||
|
||||
if let Some(computed) = computed {
|
||||
if self.merge_sequential_expr(a, &mut computed.expr)? {
|
||||
return Ok(true);
|
||||
}
|
||||
|
||||
if !self.is_skippable_for_seq(Some(a), &computed.expr) {
|
||||
return Ok(false);
|
||||
}
|
||||
}
|
||||
|
||||
match &mut **prop {
|
||||
Prop::Shorthand(shorthand) => {
|
||||
// We can't ignore shorthand properties
|
||||
//
|
||||
// https://github.com/swc-project/swc/issues/6914
|
||||
|
||||
let mut new_b = Box::new(Expr::Ident(shorthand.clone()));
|
||||
if self.merge_sequential_expr(a, &mut new_b)? {
|
||||
*prop = Box::new(Prop::KeyValue(KeyValueProp {
|
||||
@ -1908,40 +1925,15 @@ impl Optimizer<'_> {
|
||||
shorthand.span.with_ctxt(SyntaxContext::empty()),
|
||||
)
|
||||
.into(),
|
||||
value: new_b,
|
||||
value: new_b.clone(),
|
||||
}));
|
||||
}
|
||||
|
||||
continue;
|
||||
}
|
||||
Prop::KeyValue(prop) => Some(&mut prop.key),
|
||||
Prop::Assign(_) => None,
|
||||
Prop::Getter(prop) => Some(&mut prop.key),
|
||||
Prop::Setter(prop) => Some(&mut prop.key),
|
||||
Prop::Method(prop) => Some(&mut prop.key),
|
||||
};
|
||||
|
||||
if let Some(PropName::Computed(key)) = key {
|
||||
if self.merge_sequential_expr(a, &mut key.expr)? {
|
||||
return Ok(true);
|
||||
}
|
||||
|
||||
if !self.is_skippable_for_seq(Some(a), &key.expr) {
|
||||
return Ok(false);
|
||||
}
|
||||
}
|
||||
|
||||
match &mut **prop {
|
||||
Prop::KeyValue(prop) => {
|
||||
if self.merge_sequential_expr(a, &mut prop.value)? {
|
||||
return Ok(true);
|
||||
}
|
||||
|
||||
if !self.is_skippable_for_seq(Some(a), &prop.value) {
|
||||
if !self.is_skippable_for_seq(Some(a), &new_b) {
|
||||
return Ok(false);
|
||||
}
|
||||
}
|
||||
Prop::Assign(prop) => {
|
||||
Prop::KeyValue(prop) => {
|
||||
if self.merge_sequential_expr(a, &mut prop.value)? {
|
||||
return Ok(true);
|
||||
}
|
||||
|
17
crates/swc_ecma_minifier/tests/fixture/issues/7984/input.js
Normal file
17
crates/swc_ecma_minifier/tests/fixture/issues/7984/input.js
Normal file
@ -0,0 +1,17 @@
|
||||
getInitialProps = (code) => {
|
||||
let statusCode, message;
|
||||
|
||||
if (code) {
|
||||
statusCode = code;
|
||||
}
|
||||
|
||||
switch (statusCode) {
|
||||
case 404:
|
||||
message = "404";
|
||||
break;
|
||||
default:
|
||||
message = "500";
|
||||
}
|
||||
|
||||
return { statusCode, message };
|
||||
};
|
@ -0,0 +1,7 @@
|
||||
getInitialProps = (code)=>{
|
||||
let statusCode, message;
|
||||
return code && (statusCode = code), message = 404 === statusCode ? "404" : "500", {
|
||||
statusCode,
|
||||
message
|
||||
};
|
||||
};
|
Loading…
Reference in New Issue
Block a user