Repository: swc-project/swc (rust)

Issue:
minifier: self-referential var declaration makes inlining invalid
### Describe the bug

When var declaration has itself as a rhs expression, minifier inlines rhs expression without handling possible ReferenceError.

### Input code

```typescript
const k = (() => {
    var x = x;
    return x;
})();
```


### Config

_No response_

### Playground link (or link to the minimal reproduction)

https://play.swc.rs/?version=1.4.13&code=H4sIAAAAAAAAA0vOzysuUchWsFXQ0NBUsLVTqOZSAIKyxCKFCqBghTWYW5RaUlqUB%2BLVampoWnMBAHUc1Rk2AAAA&config=H4sIAAAAAAAAA32UO3LjMAyG%2B5zCozrFjostcoDtcgYOLYIyvSShIUDHmozvvtDDj40hdRI%2B%2FAAJgPh%2B2%2B2aE7XNx%2B5bPuWnt4Wg3P%2FFQkNmexFLA22y1JbQc%2FN%2BoycakbeRYDJdZ9KwLR3wpKL9r%2F1%2BUTQRkeCmWGwp5OCH55wtpr4A0ZNNrBKyJshM%2F%2BsXVvBrBFzqs%2F2AGMHmDWIsmZAZOiha4BZjtD2BOduiRBlPaksg1FKMsDI40xfsVZ5d4IBZcr5SB9aZFh0oKBRoOZxBk0kukWWS6yn3mbCDQ%2B26qc8%2F1HC2sVpWcsJlaomcVol6xEBsfM1aCWe4UoMZLsX9qQzeFOBa8qvuhCGv9OQvgFQgWqJsE2hxJw8v87Sm9pvKkL2MLA8Kl%2FnWbpmhk6KaELxS2bEyUDho3SzgagtjZVvtOAteKR8FBwa8l1lRQtNX4PaoJeWhB%2FQKkP5ar03VDMz9Fa7w8UFs4D9yS9YHbPFIlo%2FrlIZ0wLiRIAEf0W04SCsY13GRLXHp13nNDmQ0wKkulSbwugTkATCaOO3Ll9mQ5yERTRfx8FgTi8P1voeTzd3jvc%2Br%2BG1xaBK6OsFlyY%2F9nVfz7%2BbhdNvC94M3gT5vyjns9R%2F7ntJqMQYAAA%3D%3D

### SWC Info output

_No response_

### Expected behavior

```javascript
let k = void 0;
```

### Actual behavior

```javascript
let k = x;
```

The result occurs ReferenceError due to a dangling usage of `x`.

### Version

1.4.13

### Additional context

_No response_

Pull request description:
fix(es/minifier): Abort seq inline on recursive usage
**Related issue:**

 - Closes #8841

Patch:
diff --git a/crates/swc_ecma_minifier/src/compress/optimize/sequences.rs b/crates/swc_ecma_minifier/src/compress/optimize/sequences.rs
index 2a90e3c432..b1422fcc87 100644
--- a/crates/swc_ecma_minifier/src/compress/optimize/sequences.rs
+++ b/crates/swc_ecma_minifier/src/compress/optimize/sequences.rs
@@ -2308,7 +2308,7 @@ impl Optimizer<'_> {
                         can_take_init = true;
                     }
 
-                    if usage.inline_prevented {
+                    if usage.inline_prevented || usage.used_recursively {
                         return Ok(false);
                     }
 


Test patch:
diff --git a/crates/swc_ecma_minifier/tests/fixture/issues/8841/input.js b/crates/swc_ecma_minifier/tests/fixture/issues/8841/input.js
new file mode 100644
index 0000000000..c34cb561be
--- /dev/null
+++ b/crates/swc_ecma_minifier/tests/fixture/issues/8841/input.js
@@ -0,0 +1,4 @@
+export const k = (() => {
+    var x = x;
+    return x;
+})();
diff --git a/crates/swc_ecma_minifier/tests/fixture/issues/8841/output.js b/crates/swc_ecma_minifier/tests/fixture/issues/8841/output.js
new file mode 100644
index 0000000000..45a235405a
--- /dev/null
+++ b/crates/swc_ecma_minifier/tests/fixture/issues/8841/output.js
@@ -0,0 +1,2 @@
+var x;
+export const k = x;