From 01391e3c13e42b7f42f80ab13b396cad182942ff Mon Sep 17 00:00:00 2001 From: Austaras Date: Sat, 30 Sep 2023 19:40:46 +0800 Subject: [PATCH] fix(es/minifier): Check if object shorthand is skippable for seq inliner (#8036) **Related issue:** - Closes #7984 --- .../src/compress/optimize/sequences.rs | 52 ++++++++----------- .../tests/fixture/issues/7984/input.js | 17 ++++++ .../tests/fixture/issues/7984/output.js | 7 +++ 3 files changed, 46 insertions(+), 30 deletions(-) create mode 100644 crates/swc_ecma_minifier/tests/fixture/issues/7984/input.js create mode 100644 crates/swc_ecma_minifier/tests/fixture/issues/7984/output.js diff --git a/crates/swc_ecma_minifier/src/compress/optimize/sequences.rs b/crates/swc_ecma_minifier/src/compress/optimize/sequences.rs index 18bc9afd406a..5a7a526d6ae1 100644 --- a/crates/swc_ecma_minifier/src/compress/optimize/sequences.rs +++ b/crates/swc_ecma_minifier/src/compress/optimize/sequences.rs @@ -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); } diff --git a/crates/swc_ecma_minifier/tests/fixture/issues/7984/input.js b/crates/swc_ecma_minifier/tests/fixture/issues/7984/input.js new file mode 100644 index 000000000000..b8620aec7f21 --- /dev/null +++ b/crates/swc_ecma_minifier/tests/fixture/issues/7984/input.js @@ -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 }; +}; diff --git a/crates/swc_ecma_minifier/tests/fixture/issues/7984/output.js b/crates/swc_ecma_minifier/tests/fixture/issues/7984/output.js new file mode 100644 index 000000000000..e3abb638b7e8 --- /dev/null +++ b/crates/swc_ecma_minifier/tests/fixture/issues/7984/output.js @@ -0,0 +1,7 @@ +getInitialProps = (code)=>{ + let statusCode, message; + return code && (statusCode = code), message = 404 === statusCode ? "404" : "500", { + statusCode, + message + }; +};