fix(es/minifier): Check if object shorthand is skippable for seq inliner (#8036)

**Related issue:**

 - Closes #7984
This commit is contained in:
Austaras 2023-09-30 19:40:46 +08:00 committed by GitHub
parent c0625361a1
commit 01391e3c13
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 46 additions and 30 deletions

View File

@ -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);
}

View 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 };
};

View File

@ -0,0 +1,7 @@
getInitialProps = (code)=>{
let statusCode, message;
return code && (statusCode = code), message = 404 === statusCode ? "404" : "500", {
statusCode,
message
};
};