Repository: swc-project/swc (rust)

Issue:
SWC produces incorrect code
### Describe the bug

With the provided input code, `option` variable is being accessed before it is defined, causing `TypeError: Cannot read properties of undefined (reading '__labelPrefix')`

### Input code

```typescript
function getDescription(option, parentGroup) {
    return [
        parentGroup && parentGroup.label,
        option.__labelPrefix,
    ]
        .concat(option.tags)
}

function printDescription() {
  const option = {__labelPrefix: 'test', tags: []};
  const parent = null
  const desc = getDescription(option, parent);
  console.log(desc);
}

printDescription();
```


### Config

```json
{
  "jsc": {
    "parser": {
      "syntax": "ecmascript",
      "jsx": false
    },
    "target": "es5",
    "loose": false,
    "minify": {
      "compress": {
        "arguments": false,
        "arrows": true,
        "booleans": true,
        "booleans_as_integers": false,
        "collapse_vars": true,
        "comparisons": true,
        "computed_props": true,
        "conditionals": true,
        "dead_code": true,
        "directives": true,
        "drop_console": false,
        "drop_debugger": true,
        "evaluate": true,
        "expression": false,
        "hoist_funs": false,
        "hoist_props": true,
        "hoist_vars": false,
        "if_return": true,
        "join_vars": true,
        "keep_classnames": false,
        "keep_fargs": true,
        "keep_fnames": false,
        "keep_infinity": false,
        "loops": true,
        "negate_iife": true,
        "properties": true,
        "reduce_funcs": false,
        "reduce_vars": false,
        "side_effects": true,
        "switches": true,
        "typeofs": true,
        "unsafe": false,
        "unsafe_arrows": false,
        "unsafe_comps": false,
        "unsafe_Function": false,
        "unsafe_math": false,
        "unsafe_symbols": false,
        "unsafe_methods": false,
        "unsafe_proto": false,
        "unsafe_regexp": false,
        "unsafe_undefined": false,
        "unused": true,
        "const_to_let": true,
        "pristine_globals": true
      },
      "mangle": false
    }
  },
  "module": {
    "type": "es6"
  },
  "minify": false,
  "isModule": true
}
```


### Playground link

https://play.swc.rs/?version=1.3.37&code=H4sIAAAAAAAAA32QTQqDMBCF9znFrPwByQGU7grddi8iNh0lEBKJEyiId280GpRCZxPmZd6bj%2BmdFiSNhgHpjpOwclzbzGxPAWNnUdPDGjfmMDPwZZGc1VBvzVqnGUiSc8tV90JVxMmQytt2058We%2FkJv02c4cJo0dFOwKkbppwtjPUH6WilvrAGMG%2BbaN8AN5gvS0pICSdKC1jzSqibpYqeAOw92ikV1bff4LW%2Fd8mPEKOQKzNkq8mLHveXsmLsCxkjU49uAQAA&config=H4sIAAAAAAAAA22TQXLDIAxF7%2BJ1tu2iB%2BiuZ2AUEA4tRh4k0ngyuXuFY%2BM46c56fMniS1y7b7bdx7UbITPm%2BsVTErh0Hx3aAdjmMEp3UJkiD5HxdugEco9SJfymZ5GIcTk9dENIwU%2B1kqVhzMhcvzWjDJiEmw5ypl8NJReNjkQRIb3EBtiEJNhj3lItxQgjozlDbhn1b5ADU9qhIujMmGl8oMkFCZS02MocgjOWHDYQMloJZ9wkWkMlibWx1skMHR5L31f37ko8QywgrRZeZhv0jy3vRIHF%2BJL4Ce06vaP7JRdV8CajlJxWzTeFtPPhB1H7jMCcYMAtc%2BZe57BX%2Bn9UIXkdokyN6oS3rhL2ejkTgm83rF1jlrC5ldEVi%2FWGdiu%2BwN2FODg06L263ZL5N4g9bcVkGpF8C9U18NsM7qFZ92lP6wq8wE%2FtSh7HsfAB5PTMeBqOFF9KDCgnci9YjRB6hlm39zI%2B05Icqs3oHg4K17CtqQ5fyMT61FafdSE0x%2FSRjm199UUOkPq2lrcKyJUKrrN581N9727b61zXib8W4VzoD8zDcjEPBAAA

### Expected behavior

Expected to be minified code something like this (or anything else matching the original structure)

```
!function() {
    var option = {
        __labelPrefix: "test",
        tags: []
    };
    console.log([
        null,
        option.__labelPrefix
    ].concat(option.tags));
}();
```

### Actual behavior

I am getting this

```js
!function() {
    var option;
    console.log([
        null,
        option.__labelPrefix
    ].concat((option = {
        __labelPrefix: "test",
        tags: []
    }).tags));
}();
```

### Version

1.3.37

### Additional context

Originally reported as an issue to our lib: https://github.com/cloudscape-design/components/issues/785

Pull request description:
fix(es/minifier): Seq Inline wrongly callee with side effect
<!-- Note: CI script will automatically rebase your PR so please do not rebase unless required -->

<!-- Thanks for submitting a pull request! Please provide enough information so that others can review your pull request. -->

**Description:**
Closes #7004 

**BREAKING CHANGE:**

<!--
If this PR introduces a breaking change, it must contain a notice for it to be included in the CHANGELOG. Add description or remove entirely if not breaking.

You may need to update `MIGRATION.md` for the breaking changes.
-->

**Related issue (if exists):**


Patch:
diff --git a/crates/swc_ecma_minifier/src/compress/optimize/sequences.rs b/crates/swc_ecma_minifier/src/compress/optimize/sequences.rs
index f05b426296..685a5ec96e 100644
--- a/crates/swc_ecma_minifier/src/compress/optimize/sequences.rs
+++ b/crates/swc_ecma_minifier/src/compress/optimize/sequences.rs
@@ -1068,11 +1068,15 @@ where
 
         match e {
             Expr::Ident(e) => {
-                if e.span.ctxt == self.expr_ctx.unresolved_ctxt
-                    && !is_global_var_with_pure_property_access(&e.sym)
-                {
-                    log_abort!("Undeclared");
-                    return false;
+                if e.span.ctxt == self.expr_ctx.unresolved_ctxt {
+                    return if self.options.pristine_globals
+                        && is_global_var_with_pure_property_access(&e.sym)
+                    {
+                        true
+                    } else {
+                        log_abort!("Undeclared");
+                        return false;
+                    };
                 }
 
                 if let Some(a) = a {
@@ -1185,6 +1189,10 @@ where
             }
 
             Expr::Member(MemberExpr { obj, prop, .. }) => {
+                if !self.is_skippable_for_seq(a, obj) {
+                    return false;
+                }
+
                 if !self.should_preserve_property_access(
                     obj,
                     PropertyAccessOpts {


Test patch:
diff --git a/crates/swc/tests/fixture/issues-6xxx/6217/1/output/input.js b/crates/swc/tests/fixture/issues-6xxx/6217/1/output/input.js
index 65197608af..f758508350 100644
--- a/crates/swc/tests/fixture/issues-6xxx/6217/1/output/input.js
+++ b/crates/swc/tests/fixture/issues-6xxx/6217/1/output/input.js
@@ -1,4 +1,4 @@
 let b;
-for(let i = 0; i < 2; ++i)console.log(0, b = 0 === i ? 1 : 2);
+for(let i = 0; i < 2; ++i)b = 0 === i ? 1 : 2, console.log(0, b);
 let c = 1, d;
 while(c--);
diff --git a/crates/swc/tests/fixture/issues-6xxx/6217/2/output/input.js b/crates/swc/tests/fixture/issues-6xxx/6217/2/output/input.js
index a4b4240ad9..0c994a3b84 100644
--- a/crates/swc/tests/fixture/issues-6xxx/6217/2/output/input.js
+++ b/crates/swc/tests/fixture/issues-6xxx/6217/2/output/input.js
@@ -1,4 +1,4 @@
 let b, c, d;
-for(let i = 0; i < 2; ++i)console.log(0, b = 0 === i ? 1 : 2);
+for(let i = 0; i < 2; ++i)b = 0 === i ? 1 : 2, console.log(0, b);
 c = 1;
 while(c--);
diff --git a/crates/swc_ecma_minifier/tests/benches-full/jquery.js b/crates/swc_ecma_minifier/tests/benches-full/jquery.js
index f0f8b3bdb8..34a06f7e46 100644
--- a/crates/swc_ecma_minifier/tests/benches-full/jquery.js
+++ b/crates/swc_ecma_minifier/tests/benches-full/jquery.js
@@ -1834,8 +1834,8 @@
                 return computeStyleTests(), scrollboxSizeVal;
             },
             reliableTrDimensions: function() {
-                var table, tr, trChild, trStyle;
-                return null == reliableTrDimensionsVal && (table = document.createElement("table"), tr = document.createElement("tr"), trChild = document.createElement("div"), table.style.cssText = "position:absolute;left:-11111px", tr.style.height = "1px", trChild.style.height = "9px", documentElement.appendChild(table).appendChild(tr).appendChild(trChild), trStyle = window1.getComputedStyle(tr), reliableTrDimensionsVal = parseInt(trStyle.height) > 3, documentElement.removeChild(table)), reliableTrDimensionsVal;
+                var table, tr, trChild;
+                return null == reliableTrDimensionsVal && (table = document.createElement("table"), tr = document.createElement("tr"), trChild = document.createElement("div"), table.style.cssText = "position:absolute;left:-11111px", tr.style.height = "1px", trChild.style.height = "9px", documentElement.appendChild(table).appendChild(tr).appendChild(trChild), reliableTrDimensionsVal = parseInt(window1.getComputedStyle(tr).height) > 3, documentElement.removeChild(table)), reliableTrDimensionsVal;
             }
         }));
     }();
diff --git a/crates/swc_ecma_minifier/tests/benches-full/terser.js b/crates/swc_ecma_minifier/tests/benches-full/terser.js
index 1c0f40ee4e..9ad4d1c288 100644
--- a/crates/swc_ecma_minifier/tests/benches-full/terser.js
+++ b/crates/swc_ecma_minifier/tests/benches-full/terser.js
@@ -1190,11 +1190,11 @@
         }
         function switch_body_() {
             expect("{");
-            for(var tmp, a = [], cur = null, branch = null; !is("punc", "}");)is("eof") && unexpected(), is("keyword", "case") ? (branch && (branch.end = prev()), cur = [], a.push(branch = new AST_Case({
+            for(var tmp, a = [], cur = null, branch = null; !is("punc", "}");)is("eof") && unexpected(), is("keyword", "case") ? (branch && (branch.end = prev()), cur = [], branch = new AST_Case({
                 start: (tmp = S.token, next(), tmp),
                 expression: expression(!0),
                 body: cur
-            })), expect(":")) : is("keyword", "default") ? (branch && (branch.end = prev()), cur = [], a.push(branch = new AST_Default({
+            }), a.push(branch), expect(":")) : is("keyword", "default") ? (branch && (branch.end = prev()), cur = [], a.push(branch = new AST_Default({
                 start: (tmp = S.token, next(), expect(":"), tmp),
                 body: cur
             }))) : (cur || unexpected(), cur.push(statement()));
diff --git a/crates/swc_ecma_minifier/tests/benches-full/victory.js b/crates/swc_ecma_minifier/tests/benches-full/victory.js
index 4c6a83f403..34e861ad3c 100644
--- a/crates/swc_ecma_minifier/tests/benches-full/victory.js
+++ b/crates/swc_ecma_minifier/tests/benches-full/victory.js
@@ -9640,9 +9640,9 @@
                     {
                         key: "render",
                         value: function() {
-                            var arr, animationWhitelist = VictoryAxis.animationWhitelist, props = victory_core__WEBPACK_IMPORTED_MODULE_4__.Axis.modifyProps(this.props, fallbackProps);
+                            var animationWhitelist = VictoryAxis.animationWhitelist, props = victory_core__WEBPACK_IMPORTED_MODULE_4__.Axis.modifyProps(this.props, fallbackProps);
                             if (this.shouldAnimate()) return this.animateComponent(props, animationWhitelist);
-                            var gridAndTicks = this.renderGridAndTicks(props), children = [
+                            var gridAndTicks = this.renderGridAndTicks(props), modifiedGridAndTicks = props.fixLabelOverlap ? this.fixLabelOverlap(gridAndTicks, props) : gridAndTicks, children = [
                                 this.renderLine(props),
                                 this.renderLabel(props)
                             ].concat(function(arr) {
@@ -9650,9 +9650,9 @@
                                     for(var i = 0, arr2 = Array(arr.length); i < arr.length; i++)arr2[i] = arr[i];
                                     return arr2;
                                 }
-                            }(arr = props.fixLabelOverlap ? this.fixLabelOverlap(gridAndTicks, props) : gridAndTicks) || function(iter) {
+                            }(modifiedGridAndTicks) || function(iter) {
                                 if (Symbol.iterator in Object(iter) || "[object Arguments]" === Object.prototype.toString.call(iter)) return Array.from(iter);
-                            }(arr) || function() {
+                            }(modifiedGridAndTicks) || function() {
                                 throw TypeError("Invalid attempt to spread non-iterable instance");
                             }());
                             return props.standalone ? this.renderContainer(props.containerComponent, children) : react__WEBPACK_IMPORTED_MODULE_3___default.a.cloneElement(props.groupComponent, {}, children);
diff --git a/crates/swc_ecma_minifier/tests/fixture/issues/6422/1/output.js b/crates/swc_ecma_minifier/tests/fixture/issues/6422/1/output.js
index e3d96e8a2c..fedd3a3445 100644
--- a/crates/swc_ecma_minifier/tests/fixture/issues/6422/1/output.js
+++ b/crates/swc_ecma_minifier/tests/fixture/issues/6422/1/output.js
@@ -8,11 +8,12 @@ let proto = {
         setter_effect = 'PASS';
     }
 };
-({
+let obj2 = {
     __proto__: proto
-}).foo;
+};
 ({
     __proto__: proto
-}).bar = 0;
+}).foo;
+obj2.bar = 0;
 assert.strictEqual(getter_effect, 'PASS');
 assert.strictEqual(setter_effect, 'PASS');
diff --git a/crates/swc_ecma_minifier/tests/fixture/issues/7004/input.js b/crates/swc_ecma_minifier/tests/fixture/issues/7004/input.js
new file mode 100644
index 0000000000..524bb00564
--- /dev/null
+++ b/crates/swc_ecma_minifier/tests/fixture/issues/7004/input.js
@@ -0,0 +1,16 @@
+function getDescription(option, parentGroup) {
+    return [
+        parentGroup && parentGroup.label,
+        option.__labelPrefix,
+    ]
+        .concat(option.tags)
+}
+
+function printDescription() {
+  const option = {__labelPrefix: 'test', tags: []};
+  const parent = null
+  const desc = getDescription(option, parent);
+  console.log(desc);
+}
+
+printDescription();
\ No newline at end of file
diff --git a/crates/swc_ecma_minifier/tests/fixture/issues/7004/output.js b/crates/swc_ecma_minifier/tests/fixture/issues/7004/output.js
new file mode 100644
index 0000000000..a6e3e83e5a
--- /dev/null
+++ b/crates/swc_ecma_minifier/tests/fixture/issues/7004/output.js
@@ -0,0 +1,11 @@
+!function() {
+    var option;
+    const desc = [
+        null,
+        (option = {
+            __labelPrefix: 'test',
+            tags: []
+        }).__labelPrefix
+    ].concat(option.tags);
+    console.log(desc);
+}();
diff --git a/crates/swc_ecma_minifier/tests/fixture/next/react-pdf-renderer/output.js b/crates/swc_ecma_minifier/tests/fixture/next/react-pdf-renderer/output.js
index aeab649e7a..a60e370740 100644
--- a/crates/swc_ecma_minifier/tests/fixture/next/react-pdf-renderer/output.js
+++ b/crates/swc_ecma_minifier/tests/fixture/next/react-pdf-renderer/output.js
@@ -2639,7 +2639,7 @@
                 if (!e) return [];
                 for(var n = rH(e, t, r), i = [
                     n[0]
-                ], o = n[1]; null !== o;)i.push((n = rH(o, t + i.length, r))[0]), o = n[1];
+                ], o = n[1]; null !== o;)n = rH(o, t + i.length, r), i.push(n[0]), o = n[1];
                 return i;
             }, rY = function(e) {
                 return function(t) {
diff --git a/crates/swc_ecma_minifier/tests/fixture/next/syncfusion/933-e9f9a6bf671b96fc/output.js b/crates/swc_ecma_minifier/tests/fixture/next/syncfusion/933-e9f9a6bf671b96fc/output.js
index 69b1b629ed..45fb00929f 100644
--- a/crates/swc_ecma_minifier/tests/fixture/next/syncfusion/933-e9f9a6bf671b96fc/output.js
+++ b/crates/swc_ecma_minifier/tests/fixture/next/syncfusion/933-e9f9a6bf671b96fc/output.js
@@ -6747,7 +6747,7 @@
                     return value > this.max ? this.max : value < this.min ? this.min : value;
                 }, NumericTextBox.prototype.roundNumber = function(value, precision) {
                     var result = value, decimals = precision || 0, result1 = result.toString().split('e'), result2 = (result = Math.round(Number(result1[0] + 'e' + (result1[1] ? Number(result1[1]) + decimals : decimals)))).toString().split('e');
-                    return result = Number(result2[0] + 'e' + (result2[1] ? Number(result2[1]) - decimals : -decimals)), Number(result.toFixed(decimals));
+                    return Number((result = Number(result2[0] + 'e' + (result2[1] ? Number(result2[1]) - decimals : -decimals))).toFixed(decimals));
                 }, NumericTextBox.prototype.cancelEvent = function(event) {
                     return event.preventDefault(), !1;
                 }, NumericTextBox.prototype.keyPressHandler = function(event) {