From 05990a98fd3f06a3c03bd1e795800acf22f16035 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Donny/=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Thu, 17 Aug 2023 13:35:07 +0900 Subject: [PATCH] fix(es/minifier): Preserve `cooked` while compressing template literals (#7773) **Related issue:** - Closes #7770 --- .../src/compress/pure/strings.rs | 19 +++++++++++++----- .../tests/fixture/issues/7770/1/input.js | 13 ++++++++++++ .../tests/fixture/issues/7770/1/output.js | 11 ++++++++++ .../tests/fixture/issues/7770/2/input.js | 20 +++++++++++++++++++ .../tests/fixture/issues/7770/2/output.js | 13 ++++++++++++ 5 files changed, 71 insertions(+), 5 deletions(-) create mode 100644 crates/swc_ecma_minifier/tests/fixture/issues/7770/1/input.js create mode 100644 crates/swc_ecma_minifier/tests/fixture/issues/7770/1/output.js create mode 100644 crates/swc_ecma_minifier/tests/fixture/issues/7770/2/input.js create mode 100644 crates/swc_ecma_minifier/tests/fixture/issues/7770/2/output.js diff --git a/crates/swc_ecma_minifier/src/compress/pure/strings.rs b/crates/swc_ecma_minifier/src/compress/pure/strings.rs index ea387a53300..0884eef6c20 100644 --- a/crates/swc_ecma_minifier/src/compress/pure/strings.rs +++ b/crates/swc_ecma_minifier/src/compress/pure/strings.rs @@ -240,7 +240,7 @@ impl Pure<'_> { let mut quasis = vec![]; let mut exprs = vec![]; let mut cur_raw = String::new(); - let mut cur_cooked = String::new(); + let mut cur_cooked = Some(String::new()); for i in 0..(tpl.exprs.len() + tpl.quasis.len()) { if i % 2 == 0 { @@ -249,7 +249,13 @@ impl Pure<'_> { cur_raw.push_str(&q.raw); if let Some(cooked) = q.cooked { - cur_cooked.push_str(&cooked); + if let Some(cur_cooked) = &mut cur_cooked { + cur_cooked.push_str(&cooked); + } + } else { + // If cooked is None, it means that the template literal contains invalid escape + // sequences. + cur_cooked = None; } } else { let i = i / 2; @@ -258,15 +264,18 @@ impl Pure<'_> { match *e { Expr::Lit(Lit::Str(s)) => { cur_raw.push_str(&convert_str_value_to_tpl_raw(&s.value)); - cur_cooked.push_str(&convert_str_value_to_tpl_cooked(&s.value)); + if let Some(cur_cooked) = &mut cur_cooked { + cur_cooked.push_str(&convert_str_value_to_tpl_cooked(&s.value)); + } } _ => { quasis.push(TplElement { span: DUMMY_SP, tail: true, - cooked: Some(Atom::from(&*cur_cooked)), + cooked: cur_cooked.take().map(From::from), raw: take(&mut cur_raw).into(), }); + cur_cooked = Some(String::new()); exprs.push(e); } @@ -279,7 +288,7 @@ impl Pure<'_> { quasis.push(TplElement { span: DUMMY_SP, tail: true, - cooked: None, + cooked: cur_cooked.map(From::from), raw: cur_raw.into(), }); diff --git a/crates/swc_ecma_minifier/tests/fixture/issues/7770/1/input.js b/crates/swc_ecma_minifier/tests/fixture/issues/7770/1/input.js new file mode 100644 index 00000000000..18ba50c6394 --- /dev/null +++ b/crates/swc_ecma_minifier/tests/fixture/issues/7770/1/input.js @@ -0,0 +1,13 @@ +exports.MainCSS1 = foo` + \x<-invalid, so no cooked + ${'this breaks splits the quasis'} + this should have a cooked +`; + +exports.MainCSS2 = foo` + \x<-invalid, so no cooked + ${'this breaks splits the quasis'} + this should have a cooked + ${'this breaks splits the quasis'} + this should also have a cooked +`; \ No newline at end of file diff --git a/crates/swc_ecma_minifier/tests/fixture/issues/7770/1/output.js b/crates/swc_ecma_minifier/tests/fixture/issues/7770/1/output.js new file mode 100644 index 00000000000..2c4b9c3bd75 --- /dev/null +++ b/crates/swc_ecma_minifier/tests/fixture/issues/7770/1/output.js @@ -0,0 +1,11 @@ +exports.MainCSS1 = foo` + \x<-invalid, so no cooked + ${'this breaks splits the quasis'} + this should have a cooked +`, exports.MainCSS2 = foo` + \x<-invalid, so no cooked + ${'this breaks splits the quasis'} + this should have a cooked + ${'this breaks splits the quasis'} + this should also have a cooked +`; diff --git a/crates/swc_ecma_minifier/tests/fixture/issues/7770/2/input.js b/crates/swc_ecma_minifier/tests/fixture/issues/7770/2/input.js new file mode 100644 index 00000000000..40e40bec228 --- /dev/null +++ b/crates/swc_ecma_minifier/tests/fixture/issues/7770/2/input.js @@ -0,0 +1,20 @@ +const sWidth = "asdasd"; +function absolute() { + return ` + `; +} +function flex() { + return ` + `; +} +exports.MainCSS = ` +.ThisshouldOnlyBeonTop { + ${absolute()} +} +.abcBlablaOne .asdsad { + ${flex()} +} +.aasdasdasd .asdsada { + we: ${sWidth} !important; +} +`; \ No newline at end of file diff --git a/crates/swc_ecma_minifier/tests/fixture/issues/7770/2/output.js b/crates/swc_ecma_minifier/tests/fixture/issues/7770/2/output.js new file mode 100644 index 00000000000..fc934390bb3 --- /dev/null +++ b/crates/swc_ecma_minifier/tests/fixture/issues/7770/2/output.js @@ -0,0 +1,13 @@ +exports.MainCSS = ` +.ThisshouldOnlyBeonTop { + + +} +.abcBlablaOne .asdsad { + + +} +.aasdasdasd .asdsada { + we: asdasd !important; +} +`;