Repository: swc-project/swc (rust)

Issue:
compress bug for evaluate array inside template string
### Describe the bug

`` `${[1, 1]}` `` should be `"1,1"` not `"11,"`

### Input code

```typescript
console.log(`${[1, 1]}`)
```


### Config

```json
{
  "jsc": {
    "parser": {
      "syntax": "ecmascript",
      "jsx": false
    },
    "target": "es2015",
    "loose": false,
    "minify": {
      "mangle": false,
      "compress": {
        "evaluate": true
      }
    }
  },
  "module": {
    "type": "es6"
  },
  "minify": false,
  "isModule": true
}
```


### Playground link

https://play.swc.rs/?version=1.3.71&code=H4sIAAAAAAAAA0vOzyvOz0nVy8lP10hQqY421FEwjK1N0AQAnOq1zBgAAAA%3D&config=H4sIAAAAAAAAA12PTQ7CIBCF9z1FM2sXaqIL7%2BAhJjhtaKAQZmpsGu4upVAbN4T53nvzszRtCwMreLRL%2BqbCY2AKe50Iz6PgJxEgZZFV0F7gVNWBV6lDw5RR3BQQDD1JTvH1fLmVBBjnmGqiMKtH3c3HmRbH3vzZElbO%2BkDMB2ui9EYzoax2CRMVITb1zSuBda8ptyyHyuxpW%2B8OP1PdZJ8Lmp81mbvHLw8fKR00AQAA

### Expected behavior

`console.log("1,1");`

### Actual behavior

`console.log("11,");`

### Version

1.3.71

### Additional context

_No response_

Pull request:
fix(es/utils): Fix string evaluation of array literals
**Description:**


**Related issue:**

 - Closes #7714.

Patch:
diff --git a/crates/swc_ecma_utils/src/lib.rs b/crates/swc_ecma_utils/src/lib.rs
index d6a8ca25ca..c43f0d7d3c 100644
--- a/crates/swc_ecma_utils/src/lib.rs
+++ b/crates/swc_ecma_utils/src/lib.rs
@@ -1032,10 +1032,11 @@ pub trait ExprExt {
                 Unknown => return Value::Unknown,
             })),
             Expr::Array(ArrayLit { ref elems, .. }) => {
-                let mut first = true;
                 let mut buf = String::new();
+                let len = elems.len();
                 // null, undefined is "" in array literal.
-                for elem in elems {
+                for (idx, elem) in elems.iter().enumerate() {
+                    let last = idx == len - 1;
                     let e = match *elem {
                         Some(ref elem) => {
                             let ExprOrSpread { ref expr, .. } = *elem;
@@ -1055,9 +1056,7 @@ pub trait ExprExt {
                     };
                     buf.push_str(&e);
 
-                    if first {
-                        first = false;
-                    } else {
+                    if !last {
                         buf.push(',');
                     }
                 }


Test patch:
diff --git a/crates/swc_ecma_minifier/tests/fixture/issues/7714/1/config.json b/crates/swc_ecma_minifier/tests/fixture/issues/7714/1/config.json
new file mode 100644
index 0000000000..7990f01208
--- /dev/null
+++ b/crates/swc_ecma_minifier/tests/fixture/issues/7714/1/config.json
@@ -0,0 +1,4 @@
+{
+    "defaults": true,
+    "evaluate": true
+}
diff --git a/crates/swc_ecma_minifier/tests/fixture/issues/7714/1/input.js b/crates/swc_ecma_minifier/tests/fixture/issues/7714/1/input.js
new file mode 100644
index 0000000000..0ea1ca9e08
--- /dev/null
+++ b/crates/swc_ecma_minifier/tests/fixture/issues/7714/1/input.js
@@ -0,0 +1,1 @@
+console.log(`${[1, 1]}`)
diff --git a/crates/swc_ecma_minifier/tests/fixture/issues/7714/1/output.js b/crates/swc_ecma_minifier/tests/fixture/issues/7714/1/output.js
new file mode 100644
index 0000000000..cb5fdd9ec1
--- /dev/null
+++ b/crates/swc_ecma_minifier/tests/fixture/issues/7714/1/output.js
@@ -0,0 +1,1 @@
+console.log("1,1");