Repository: swc-project/swc (rust)

Issue:
Single quotes in template literal are escaped wrong when compress is enabled
### Describe the bug

I looked for related bug reports first and this bug seems related to https://github.com/swc-project/swc/issues/1488.

Note: the bug does _not_ happen when `compress` is `false`.

### Input code

```typescript
export const a = `You\'ll`
export const b = 'You\'ll'
```


### Config

```json
{
"sourceMaps": true,
  "jsc": {
    "parser": {
      "syntax": "typescript",
      "tsx": true,
      "decorators": true
    },
    "target": "es2017",
    "loose": false,
    "transform": {
      "react": {
        "runtime": "automatic"
      }
    },
    "externalHelpers": true,
    "keepClassNames": false,
    "minify": {
      "compress": true
    }
  },
  "module": {
    "type": "es6"
  },
  "minify": false
}
```


### Playground link (or link to the minimal reproduction)

https://play.swc.rs/?version=1.3.96&code=H4sIAAAAAAAAA0utKMgvKlFIzs8rLlFIVLBVSIjML41Rz8lJ4EpFlkoCSqlDpdQBxGxW3DUAAAA%3D&config=H4sIAAAAAAAAA31VvY7bMAze8xSB5hvaDlega4GiS%2FsKgiJRjq6yaIhULsEh717Kdi6JI2ez%2BPGfH%2BmPjSIs2cIfM5D6seVc4GWz3ao3svL8kE95DCYT5M%2B3SOiU2BxFovg0ANkcBlYvF5TpeONrFDmwmA1jvkQZgfOEKza5A67ugL59%2Bfp9dqUiIoGIvYkEF91sEnnM%2FW0%2BGYzlG0EVlcShr9bKFMbecLBqhs930eHIkJOJvyEOkOkudfUPYPgZDdFf0wMtculDCv50m4jFfshAdJ%2BLlFd6SLywn7GM77Tol8h3iBGk1HVEG9IhMXRT0kvHFmOUqYI%2BmNzwUjM1ORC2QlSwMDg9ZByaeHKBA0rXGqgD47RFBw0oZLAcDtAyk1hilkjKa9Qzwg52petGMi6s4WBiMdyICcdxJJJtw%2BseA7H2JbVaOIErPZjAublLy%2BB1Bi45Pdq9YUgrM6lk07ayLT2y7arhhU9r1v6pZUheKMunBi671qoyQSdN1SH4RmdrZyBzaE0zgysWamdtK50ZXmkfBQcavBeuNFzTe2C7bwWt1wh9A5D5Gt9i1QTozy1cwetCPIF%2FSZXcJtisIfdnv47Sqd9hfBKgB96je6Igo2Bch7NcieOwjpfkQKgBrqlSaAQej4AsAKOO4%2B1%2B4Iash3jUXcTd9UzcX%2BDNfIVVj66MWz%2F%2Fcuogp%2F%2FBq7oqXc7tmOHm%2FB%2F4%2B2x5vgYAAA%3D%3D

### SWC Info output

_No response_

### Expected behavior

```js
export const a = "You'll";  // or `You\'ll`;
export const b = 'You\'ll';
``` 

### Actual behavior

with `compress: true`, swc returns:
```js
export const a = "You\\'ll";
export const b = 'You\'ll';
```

with `compress: false`, swc returns:
```js
export const a = `You\'ll`;
export const b = 'You\'ll';
```

### Version

1.3.96

### Additional context

This [bug report](https://github.com/swc-project/swc/issues/1280) looks related to escaping
```js
`''`
```
. Could that be related?

Pull request:
fix(es/minifier): Use `cooked` while converting tpls into strings
**Description:**


**Related issue:**

 - Closes #8228

Patch:
diff --git a/crates/swc_ecma_minifier/src/compress/pure/strings.rs b/crates/swc_ecma_minifier/src/compress/pure/strings.rs
index 7c290773aa..0bde969305 100644
--- a/crates/swc_ecma_minifier/src/compress/pure/strings.rs
+++ b/crates/swc_ecma_minifier/src/compress/pure/strings.rs
@@ -189,6 +189,23 @@ impl Pure<'_> {
     pub(super) fn convert_tpl_to_str(&mut self, e: &mut Expr) {
         match e {
             Expr::Tpl(t) if t.quasis.len() == 1 && t.exprs.is_empty() => {
+                if let Some(value) = &t.quasis[0].cooked {
+                    if value.chars().all(|c| match c {
+                        '\u{0020}'..='\u{007e}' => true,
+                        '\n' | '\r' => self.config.force_str_for_tpl,
+                        _ => false,
+                    }) {
+                        report_change!("converting a template literal to a string literal");
+
+                        *e = Expr::Lit(Lit::Str(Str {
+                            span: t.span,
+                            raw: None,
+                            value: value.clone(),
+                        }));
+                        return;
+                    }
+                }
+
                 let c = &t.quasis[0].raw;
 
                 if c.chars().all(|c| match c {
@@ -209,6 +226,8 @@ impl Pure<'_> {
                         .replace("\\r", "\r")
                         .replace("\\\\", "\\");
 
+                    report_change!("converting a template literal to a string literal");
+
                     *e = Expr::Lit(Lit::Str(Str {
                         span: t.span,
                         raw: None,


Test patch:
diff --git a/crates/swc/tests/fixture/issues-8xxx/8228/input/.swcrc b/crates/swc/tests/fixture/issues-8xxx/8228/input/.swcrc
new file mode 100644
index 0000000000..8b41c9b98a
--- /dev/null
+++ b/crates/swc/tests/fixture/issues-8xxx/8228/input/.swcrc
@@ -0,0 +1,25 @@
+{
+    "jsc": {
+        "parser": {
+            "syntax": "typescript",
+            "tsx": true,
+            "decorators": true
+        },
+        "target": "es2017",
+        "loose": false,
+        "transform": {
+            "react": {
+                "runtime": "automatic"
+            }
+        },
+        "externalHelpers": true,
+        "keepClassNames": false,
+        "minify": {
+            "compress": true
+        }
+    },
+    "module": {
+        "type": "es6"
+    },
+    "minify": false
+}
\ No newline at end of file
diff --git a/crates/swc/tests/fixture/issues-8xxx/8228/input/1.js b/crates/swc/tests/fixture/issues-8xxx/8228/input/1.js
new file mode 100644
index 0000000000..e10b7a1d43
--- /dev/null
+++ b/crates/swc/tests/fixture/issues-8xxx/8228/input/1.js
@@ -0,0 +1,2 @@
+export const a = `You\'ll`
+export const b = 'You\'ll'
diff --git a/crates/swc/tests/fixture/issues-8xxx/8228/output/1.js b/crates/swc/tests/fixture/issues-8xxx/8228/output/1.js
new file mode 100644
index 0000000000..866b5ee10e
--- /dev/null
+++ b/crates/swc/tests/fixture/issues-8xxx/8228/output/1.js
@@ -0,0 +1,2 @@
+export const a = "You'll";
+export const b = 'You\'ll';
diff --git a/crates/swc_ecma_minifier/tests/fixture/issues/8228/input.js b/crates/swc_ecma_minifier/tests/fixture/issues/8228/input.js
new file mode 100644
index 0000000000..d9411b3d74
--- /dev/null
+++ b/crates/swc_ecma_minifier/tests/fixture/issues/8228/input.js
@@ -0,0 +1,2 @@
+export const a = `You\'ll`
+export const b = 'You\'ll'
\ No newline at end of file
diff --git a/crates/swc_ecma_minifier/tests/fixture/issues/8228/output.js b/crates/swc_ecma_minifier/tests/fixture/issues/8228/output.js
new file mode 100644
index 0000000000..866b5ee10e
--- /dev/null
+++ b/crates/swc_ecma_minifier/tests/fixture/issues/8228/output.js
@@ -0,0 +1,2 @@
+export const a = "You'll";
+export const b = 'You\'ll';
diff --git a/crates/swc_ecma_minifier/tests/terser/compress/template_string/respect_inline_script/output.js b/crates/swc_ecma_minifier/tests/terser/compress/template_string/respect_inline_script/output.js
index f012493935..fe2642fb36 100644
--- a/crates/swc_ecma_minifier/tests/terser/compress/template_string/respect_inline_script/output.js
+++ b/crates/swc_ecma_minifier/tests/terser/compress/template_string/respect_inline_script/output.js
@@ -1,3 +1,3 @@
 var foo = `<\/script>${content}`;
-var bar = `\x3c!--`;
-var baz = `--\x3e`;
+var bar = "<!--";
+var baz = "-->";