Repository: swc-project/swc (rust)

Issue:
SWC `compress` removes string literal’s `.toUpperCase()` and produces incorrect code
### Describe the bug

The attached playground shows a stripped-down version of a code file that appears in my next.js project.

The SWC minifier/“compressor” removes `.toUpperCase()` from a string literal without uppercasing the string literal. The result is incorrect output.

### [Playground link](https://play.swc.rs/?version=1.4.14&code=H4sIAAAAAAAAA2WPvQrDMAyEdz%2BFhoJjCIF4LZ0KfYD%2BbB3iJiIEXDvIKg0Ev3vz11DSTZ843emwaz0xlNaEAGd0FRIS9AKAJriw4aY8kXliEpgaV%2BcpzINWkw6g9C4w2MZhfsWO4QDFrl%2FEEYyr4Is6Fhn7W9siHU3ARO03BnoxkCOABh5Qbk9%2BbnjNW%2BPj3c00ecVijiDkF7lJPy6iiEKMFt5iZn2dOHyv9ROV%2FZeXRqYgH1IND3wAKJlsZzUBAAA%3D&config=H4sIAAAAAAAAA32UO3LjMAyG%2B5zCo3qLHRcpcoB0ewYOLYIysxShIUDHmozvHlCSH4khdRI%2B%2FAAJgPh62e2aD2qbt92XfMrPYDNBvv2LhcbE9iyWhscBqM1h4ObPlTJV5G0kmEyXmTRscwdcVUD7v%2Fv9omgiIsFVsdj6kIIfH3O22A8ZiB5sYpWQpYfE9FO%2FsIyfFXAuj%2FYDYgSbNoixZEJi6CBrgVuM0Q4E5mSzEqWe1OZAqKWosDA4M2QcVJ5c4IBJcj5TB9aZFh0oKGRoOZxAk0kukSWS6yn3mbCDQ%2Bm6qc%2B%2F1HCysVhWcsJ5aomcVol6xEBsfElaCWe4UoMZLsX9rQzeZOCS07PuA0Na6cl%2FAKlAtETJ9qDFnTy8zNOa2m8qQ%2FIysjwqXOZbu2WCTopqQvBKZWtlIHPQupnBlRZqZVvtOAteKR8FBwa8l1lRQtNn4PaoJa0PHb0CpL%2FWa1M1A3N7hSu8PogN%2FC63ZH3AFo%2Fe8nGd0tgfMG4k6IGP6DYcpBWM6zjLljgP67wkBzIa4FSXQhN4XgLyABhNnPbl02zI85CIpot4uK%2BJxeFy28O9Td39vc%2Br%2BGVxaHp0ZYLLkq%2F9nVfza3N3um7h28GbQP%2Buyinp5RvxWkerMAYAAA%3D%3D)


### Input code

```typescript
export class Renderer {
  renderStaticFrame(string1, string2) {
    const line1Text = `${string1} and ${string2}`.toUpperCase();
    const line2Text = 'line 2 text'.toUpperCase();

    const text = `${line1Text}\n${line2Text}`;
    return text;
  }
}

console.log(new Renderer().renderStaticFrame('a', 'b'));
```

### Output code

```js
export class Renderer {
  renderStaticFrame(string1, string2) {
      let line1Text = `${string1} and ${string2}`.toUpperCase();
      return `${line1Text}\nline 2 text`;
  }
}
console.log(new Renderer().renderStaticFrame('a', 'b'));
```

### Expected behavior
Both lines are uppercase.
```diff
❯ node swc-input.js   
A AND B
LINE 2 TEXT
```

### Actual behavior
The second line is printed in lowercase, because the `toUpperCase` function was removed in the SWC output.
```diff
❯ node swc-output.js     
A AND B
line 2 text
```


<details>
<summary>
<h3>SWC Info output</h3>
</summary>

<pre><code>    Operating System:
        Platform: darwin
        Arch: arm64
        Machine Type: arm64
        Version: Darwin Kernel Version 23.4.0: Fri Mar 15 00:10:42 PDT 2024; root:xnu-10063.101.17~1/RELEASE_ARM64_T6000
        CPU: (10 cores)
            Models: Apple M1 Pro

    Binaries:
        Node: 20.12.2
        npm: 10.5.0
        Yarn: 1.22.19
        pnpm: N/A

    Relevant Packages:
        @swc/core: N/A
        @swc/helpers: 0.5.5
        @swc/types: N/A
        typescript: 5.4.5
        next: 14.2.0

    SWC Config:
        output: N/A
        .swcrc path: N/A

    Next.js info:
        output: N/A
</code></pre>
</details>


### Version

1.4.14

### Additional context

From playing with the version selector in the playground, it appears this issue was introduced in version 1.3.105.

Pull request:
fix(es/minifier): Remove `raw` of strings after modification
**Related issue:**

 - Closes #8864

Patch:
diff --git a/crates/swc_ecma_minifier/src/compress/pure/evaluate.rs b/crates/swc_ecma_minifier/src/compress/pure/evaluate.rs
index 1c29a585a2..ee3e60ba70 100644
--- a/crates/swc_ecma_minifier/src/compress/pure/evaluate.rs
+++ b/crates/swc_ecma_minifier/src/compress/pure/evaluate.rs
@@ -742,9 +742,10 @@ impl Pure<'_> {
         };
 
         self.changed = true;
-        report_change!("evaluate: Evaluated `{}` of a string literal", method);
+        report_change!("evaluate: Evaluated `{method}` of a string literal");
         *e = Expr::Lit(Lit::Str(Str {
             value: new_val.into(),
+            raw: None,
             ..s
         }));
     }


Test patch:
diff --git a/crates/swc_ecma_minifier/tests/exec.rs b/crates/swc_ecma_minifier/tests/exec.rs
index 59374233ba..c7d625194b 100644
--- a/crates/swc_ecma_minifier/tests/exec.rs
+++ b/crates/swc_ecma_minifier/tests/exec.rs
@@ -11193,3 +11193,22 @@ fn issue_8246_1() {
         false,
     );
 }
+
+#[test]
+fn issue_8864_1() {
+    run_default_exec_test(
+        "
+        class Renderer {
+            renderStaticFrame(string1, string2) {
+            const line1Text = `${string1} and ${string2}`.toUpperCase();
+            const line2Text = 'line 2 text'.toUpperCase();
+        
+            const text = `${line1Text}\n${line2Text}`;
+            return text;
+            }
+        }
+      
+        console.log(new Renderer().renderStaticFrame('a', 'b'));
+      ",
+    )
+}