Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(es/minifier): Collect raw str values for new Tpl element #9261

Merged
merged 8 commits into from
Jul 17, 2024
36 changes: 22 additions & 14 deletions crates/swc_ecma_minifier/src/compress/pure/strings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,13 +121,15 @@ impl Pure<'_> {
quasis: Default::default(),
exprs: Default::default(),
};
let mut cur_str_value = String::new();
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_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));
stormslowly marked this conversation as resolved.
Show resolved Hide resolved
cur_raw_str.push_str(&q.raw);
} else {
let mut e = tpl.exprs[idx / 2].take();
self.eval_nested_tpl(&mut e);
Expand All @@ -141,16 +143,19 @@ 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));
cur_raw_str.push_str(&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(&*cur_raw_str);
cur_cooked_str.clear();
cur_raw_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();
Expand All @@ -160,14 +165,16 @@ impl Pure<'_> {
}
}
_ => {
let s = Atom::from(&*cur_str_value);
cur_str_value.clear();
let cooked = Atom::from(&*cur_cooked_str);
let raw = Atom::from(&*cur_raw_str);
cur_cooked_str.clear();
cur_raw_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);
Expand All @@ -176,12 +183,13 @@ impl Pure<'_> {
}
}

let s = Atom::from(&*cur_str_value);
let cooked = Atom::from(&*cur_cooked_str);
let raw = Atom::from(&*cur_raw_str);
new_tpl.quasis.push(TplElement {
span: DUMMY_SP,
tail: false,
cooked: Some(s.clone()),
raw: s,
cooked: Some(cooked),
raw,
});

*e = new_tpl.into();
Expand Down
10 changes: 10 additions & 0 deletions crates/swc_ecma_minifier/tests/exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11325,3 +11325,13 @@ fn issue_9010() {
"#,
);
}

#[test]
fn issue_9184() {
run_default_exec_test(
r#"
let pi= Math.random() >1.1 ? "foo": "bar";
kdy1 marked this conversation as resolved.
Show resolved Hide resolved
console.log(`(${`${pi}`} - ${`\\*${pi}`})`)
"#,
);
}
Loading