From 8b1859a2ea3d78aa2198b95bb4080ac27365409b Mon Sep 17 00:00:00 2001 From: pshu Date: Tue, 16 Jul 2024 15:27:46 +0800 Subject: [PATCH 1/6] =?UTF-8?q?test:=20=E2=9C=85=20add=20failure=20case=20?= =?UTF-8?q?for=20pure=20nested=20tpl=20with=20//?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- crates/swc_ecma_minifier/tests/exec.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/crates/swc_ecma_minifier/tests/exec.rs b/crates/swc_ecma_minifier/tests/exec.rs index 122fb672b3b6..43254a4ae637 100644 --- a/crates/swc_ecma_minifier/tests/exec.rs +++ b/crates/swc_ecma_minifier/tests/exec.rs @@ -11325,3 +11325,13 @@ fn issue_9010() { "#, ); } + +#[test] +fn issue_9184() { + run_default_exec_test( + r#" + let pi= Math.random() >1.1 ? "foo": "bar"; + console.log(`(${`${pi}`} - ${`\\*${pi}`})`) +"#, + ); +} \ No newline at end of file From 5a001a40d54276a8655af106c8567f763e91be34 Mon Sep 17 00:00:00 2001 From: pshu Date: Tue, 16 Jul 2024 15:29:46 +0800 Subject: [PATCH 2/6] =?UTF-8?q?fix:=20=F0=9F=90=9B=20convert=20cooked=20st?= =?UTF-8?q?r=20back=20to=20raw?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/compress/pure/strings.rs | 31 ++++++++++--------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/crates/swc_ecma_minifier/src/compress/pure/strings.rs b/crates/swc_ecma_minifier/src/compress/pure/strings.rs index a3273251ee8e..df7d70227ee3 100644 --- a/crates/swc_ecma_minifier/src/compress/pure/strings.rs +++ b/crates/swc_ecma_minifier/src/compress/pure/strings.rs @@ -121,13 +121,13 @@ impl Pure<'_> { quasis: Default::default(), exprs: Default::default(), }; - let mut cur_str_value = String::new(); + let mut cur_cooked_str = String::new(); for idx in 0..(tpl.quasis.len() + tpl.exprs.len()) { if idx % 2 == 0 { let q = tpl.quasis[idx / 2].take(); - cur_str_value.push_str(q.cooked.as_deref().unwrap_or(&*q.raw)); + cur_cooked_str.push_str(q.cooked.as_deref().unwrap_or(&*q.raw)); } else { let mut e = tpl.exprs[idx / 2].take(); self.eval_nested_tpl(&mut e); @@ -141,16 +141,17 @@ impl Pure<'_> { if idx % 2 == 0 { let q = e.quasis[idx / 2].take(); - cur_str_value.push_str(q.cooked.as_deref().unwrap_or(&*q.raw)); + cur_cooked_str.push_str(q.cooked.as_deref().unwrap_or(&*q.raw)); } else { - let s = Atom::from(&*cur_str_value); - cur_str_value.clear(); + let cooked = Atom::from(&*cur_cooked_str); + let raw = Atom::from(convert_str_value_to_tpl_raw(&cooked)); + cur_cooked_str.clear(); new_tpl.quasis.push(TplElement { span: DUMMY_SP, tail: false, - cooked: Some(s.clone()), - raw: s, + cooked: Some(cooked), + raw, }); let e = e.exprs[idx / 2].take(); @@ -160,14 +161,15 @@ impl Pure<'_> { } } _ => { - let s = Atom::from(&*cur_str_value); - cur_str_value.clear(); + let cooked = Atom::from(&*cur_cooked_str); + let raw = Atom::from(convert_str_value_to_tpl_raw(&cooked)); + cur_cooked_str.clear(); new_tpl.quasis.push(TplElement { span: DUMMY_SP, tail: false, - cooked: Some(s.clone()), - raw: s, + cooked: Some(cooked), + raw, }); new_tpl.exprs.push(e); @@ -176,12 +178,13 @@ impl Pure<'_> { } } - let s = Atom::from(&*cur_str_value); + let cooked = Atom::from(&*cur_cooked_str); + let raw = Atom::from(convert_str_value_to_tpl_raw(&cooked)); new_tpl.quasis.push(TplElement { span: DUMMY_SP, tail: false, - cooked: Some(s.clone()), - raw: s, + cooked: Some(cooked), + raw, }); *e = new_tpl.into(); From 9e9c06dc97e67540a191d8cb7532f4e6505ceb8b Mon Sep 17 00:00:00 2001 From: pshu Date: Tue, 16 Jul 2024 15:37:47 +0800 Subject: [PATCH 3/6] =?UTF-8?q?chore:=20=F0=9F=9A=A8=20lint=20happy?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- crates/swc_ecma_minifier/tests/exec.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/swc_ecma_minifier/tests/exec.rs b/crates/swc_ecma_minifier/tests/exec.rs index 43254a4ae637..e8564959477e 100644 --- a/crates/swc_ecma_minifier/tests/exec.rs +++ b/crates/swc_ecma_minifier/tests/exec.rs @@ -11333,5 +11333,5 @@ fn issue_9184() { let pi= Math.random() >1.1 ? "foo": "bar"; console.log(`(${`${pi}`} - ${`\\*${pi}`})`) "#, - ); -} \ No newline at end of file + ); +} From 0d8780a0573176d5b96eca3125c373aa5deec841 Mon Sep 17 00:00:00 2001 From: pshu Date: Tue, 16 Jul 2024 17:58:41 +0800 Subject: [PATCH 4/6] =?UTF-8?q?fix:=20=F0=9F=90=9B=20collect=20cooked=20an?= =?UTF-8?q?d=20raw=20string=20seperately?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- crates/swc_ecma_minifier/src/compress/pure/strings.rs | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/crates/swc_ecma_minifier/src/compress/pure/strings.rs b/crates/swc_ecma_minifier/src/compress/pure/strings.rs index df7d70227ee3..fa9d28c9ec71 100644 --- a/crates/swc_ecma_minifier/src/compress/pure/strings.rs +++ b/crates/swc_ecma_minifier/src/compress/pure/strings.rs @@ -122,12 +122,14 @@ impl Pure<'_> { exprs: Default::default(), }; let mut cur_cooked_str = String::new(); + let mut cur_raw_str = String::new(); for idx in 0..(tpl.quasis.len() + tpl.exprs.len()) { if idx % 2 == 0 { let q = tpl.quasis[idx / 2].take(); cur_cooked_str.push_str(q.cooked.as_deref().unwrap_or(&*q.raw)); + cur_raw_str.push_str(&q.raw); } else { let mut e = tpl.exprs[idx / 2].take(); self.eval_nested_tpl(&mut e); @@ -142,10 +144,12 @@ impl Pure<'_> { let q = e.quasis[idx / 2].take(); cur_cooked_str.push_str(q.cooked.as_deref().unwrap_or(&*q.raw)); + cur_raw_str.push_str(&q.raw); } else { let cooked = Atom::from(&*cur_cooked_str); - let raw = Atom::from(convert_str_value_to_tpl_raw(&cooked)); + let raw = Atom::from(&*cur_raw_str); cur_cooked_str.clear(); + cur_raw_str.clear(); new_tpl.quasis.push(TplElement { span: DUMMY_SP, @@ -162,8 +166,9 @@ impl Pure<'_> { } _ => { let cooked = Atom::from(&*cur_cooked_str); - let raw = Atom::from(convert_str_value_to_tpl_raw(&cooked)); + let raw = Atom::from(&*cur_raw_str); cur_cooked_str.clear(); + cur_raw_str.clear(); new_tpl.quasis.push(TplElement { span: DUMMY_SP, @@ -179,7 +184,7 @@ impl Pure<'_> { } let cooked = Atom::from(&*cur_cooked_str); - let raw = Atom::from(convert_str_value_to_tpl_raw(&cooked)); + let raw = Atom::from(&*cur_raw_str); new_tpl.quasis.push(TplElement { span: DUMMY_SP, tail: false, From 7661d2fde62c2651be7af3467927aa2baf634285 Mon Sep 17 00:00:00 2001 From: pshu Date: Tue, 16 Jul 2024 20:46:24 +0800 Subject: [PATCH 5/6] =?UTF-8?q?test:=20=E2=9C=85=20add=20reverse=20case?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- crates/swc_ecma_minifier/tests/exec.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/crates/swc_ecma_minifier/tests/exec.rs b/crates/swc_ecma_minifier/tests/exec.rs index e8564959477e..6512fdbc95c8 100644 --- a/crates/swc_ecma_minifier/tests/exec.rs +++ b/crates/swc_ecma_minifier/tests/exec.rs @@ -11335,3 +11335,13 @@ fn issue_9184() { "#, ); } + +#[test] +fn issue_9184_2() { + run_default_exec_test( + r#" + let pi= Math.random() < -1 ? "foo": "bar"; + console.log(`(${`${pi}`} - ${`\\*${pi}`})`) +"#, + ); +} From bc7d584860849b3f8c23df3ec5f762dbefef6606 Mon Sep 17 00:00:00 2001 From: pshu Date: Tue, 16 Jul 2024 22:19:07 +0800 Subject: [PATCH 6/6] =?UTF-8?q?feat:=20=E2=9C=A8=20generate=20cooked=20fro?= =?UTF-8?q?m=20raw=20using=20Str::from=5Ftpl=5Fraw?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- crates/swc_ecma_minifier/src/compress/pure/strings.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/crates/swc_ecma_minifier/src/compress/pure/strings.rs b/crates/swc_ecma_minifier/src/compress/pure/strings.rs index fa9d28c9ec71..268f23778781 100644 --- a/crates/swc_ecma_minifier/src/compress/pure/strings.rs +++ b/crates/swc_ecma_minifier/src/compress/pure/strings.rs @@ -127,8 +127,8 @@ impl Pure<'_> { for idx in 0..(tpl.quasis.len() + tpl.exprs.len()) { if idx % 2 == 0 { let q = tpl.quasis[idx / 2].take(); - - cur_cooked_str.push_str(q.cooked.as_deref().unwrap_or(&*q.raw)); + + cur_cooked_str.push_str(&Str::from_tpl_raw(&q.raw)); cur_raw_str.push_str(&q.raw); } else { let mut e = tpl.exprs[idx / 2].take(); @@ -143,7 +143,7 @@ impl Pure<'_> { if idx % 2 == 0 { let q = e.quasis[idx / 2].take(); - cur_cooked_str.push_str(q.cooked.as_deref().unwrap_or(&*q.raw)); + cur_cooked_str.push_str(Str::from_tpl_raw(&q.raw).as_ref()); cur_raw_str.push_str(&q.raw); } else { let cooked = Atom::from(&*cur_cooked_str);