Repository: swc-project/swc (rust)

Issue:
Invalid code generated when using Symbol.toStringTag
### Describe the bug

With very specific code input, the swc generated code does not run and makes nodejs crash on parsing.

### Input code

```typescript
var test1 = class X {
  [Symbol.toStringTag]() {}
}

function a() {
  const b = class Y {
    x() {
    }
  }
}
```


### Config

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


### Playground link

https://play.swc.rs/?version=1.2.172&code=H4sIAAAAAAAAAytLLFIoSS0uMVSwVUjOSSwuVohQqOZSUIgOrsxNys%2FRK8kPLinKzEsPSUyP1dBUqK7lquXiSivNSy7JzM9TSAQJAVUn5%2BcVlygkwc2IBIsqKFRA5RUUarlAuBYAZGQUj28AAAA%3D&config=H4sIAAAAAAAAA0WMOwrEMAxE76LaxW7K3GEPIbxKcPAPSYEY47vHBodUEjNvXoVDLKwVMrIQj09KVLxgBbIBxbLLCqZjPdrQCzUDiryTDkSWz%2FLttU9JaAIGgotuK0NmU8hMIm%2BFcfcP2borpP85ggpacr9jElI8BNormmMnv0krn9RuC1DXWb0AAAA%3D

### Expected behavior

Declaring a function after the class should not modify the `let` position.

### Actual behavior

When this exact code is used or a derivative, the produced code does not run with "use strict"

### Version

1.2.172

### Additional context

Happens in production with Prisma generated client and swc/jest

Pull request:
fix(es/compat): insert statements position `class_properties`
<!-- 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:**

<!-- Explain the **motivation** for making this change. What existing problem does the pull request solve? -->

**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):**
closes #4473 

Patch:
diff --git a/crates/swc_ecma_transforms_compat/src/es2022/class_properties/mod.rs b/crates/swc_ecma_transforms_compat/src/es2022/class_properties/mod.rs
index 05866a069e..83cfa4efbf 100644
--- a/crates/swc_ecma_transforms_compat/src/es2022/class_properties/mod.rs
+++ b/crates/swc_ecma_transforms_compat/src/es2022/class_properties/mod.rs
@@ -157,9 +157,12 @@ impl<C: Comments> VisitMut for ClassProperties<C> {
     }
 
     fn visit_mut_stmts(&mut self, n: &mut Vec<Stmt>) {
+        let old = self.extra.take();
         self.visit_mut_stmt_like(n);
 
-        self.extra.take().prepend_with(n)
+        self.extra.take().prepend_with(n);
+
+        self.extra = old;
     }
 
     fn visit_mut_block_stmt_or_expr(&mut self, body: &mut BlockStmtOrExpr) {
diff --git a/crates/swc_ecma_transforms_compat/src/es2022/private_in_object.rs b/crates/swc_ecma_transforms_compat/src/es2022/private_in_object.rs
index cb7d01a2f3..ab975f56e2 100644
--- a/crates/swc_ecma_transforms_compat/src/es2022/private_in_object.rs
+++ b/crates/swc_ecma_transforms_compat/src/es2022/private_in_object.rs
@@ -11,7 +11,6 @@ use swc_ecma_ast::*;
 use swc_ecma_transforms_base::pass::JsPass;
 use swc_ecma_utils::{
     default_constructor, ident::IdentLike, prepend, private_ident, quote_ident, ExprFactory, Id,
-    HANDLER,
 };
 use swc_ecma_visit::{
     as_folder, noop_visit_mut_type, noop_visit_type, Visit, VisitMut, VisitMutWith, VisitWith,
@@ -368,15 +367,6 @@ impl VisitMut for PrivateInObject {
                 });
             }
 
-            Expr::Member(MemberExpr {
-                span,
-                obj: _,
-                prop: MemberProp::PrivateName(PrivateName { .. }),
-            }) => {
-                let error = "Illegal access to private class property.";
-                HANDLER.with(|h| h.struct_span_err(*span, error).emit());
-            }
-
             _ => {}
         }
     }


Test patch:
diff --git a/crates/swc/tests/errors/lints/check-private-property/1/output.swc-stderr b/crates/swc/tests/errors/lints/check-private-property/1/output.swc-stderr
deleted file mode 100644
index d69ad15079..0000000000
--- a/crates/swc/tests/errors/lints/check-private-property/1/output.swc-stderr
+++ /dev/null
@@ -1,11 +0,0 @@
-  x Illegal access to private class property.
-    ,----
- 18 | foo.#bar;
-    : ^^^^^^^^
-    `----
-
-  x Illegal access to private class property.
-    ,----
- 19 | foo.#privateMethod();
-    : ^^^^^^^^^^^^^^^^^^
-    `----
diff --git a/crates/swc/tests/errors/lints/check-private-property/1/private.js b/crates/swc/tests/errors/lints/check-private-property/1/private.js
deleted file mode 100644
index 3466ea5a22..0000000000
--- a/crates/swc/tests/errors/lints/check-private-property/1/private.js
+++ /dev/null
@@ -1,21 +0,0 @@
-class Foo {
-	#bar = 'i\'m a private field!';
-
-  #privateMethod() {
-    return 'i\'m a private method!';
-  }
-
-  foo() {
-    return this.#bar;
-  }
-
-  bar() {
-    return this.#privateMethod();
-  }
-}
-
-var f = new Foo();
-console.log(f.foo());
-console.log(f.bar());
-foo.#bar;
-foo.#privateMethod();
diff --git a/crates/swc/tests/fixture/issues-4xxx/4464/1/input/exec.js b/crates/swc/tests/fixture/issues-4xxx/4464/1/input/exec.js
deleted file mode 100644
index 488eca3aae..0000000000
--- a/crates/swc/tests/fixture/issues-4xxx/4464/1/input/exec.js
+++ /dev/null
@@ -1,19 +0,0 @@
-class Foo {
-        #bar = 'i\'m a private field!';
-
-  #privateMethod() {
-    return 'i\'m a private method!';
-  }
-
-  foo() {
-    return this.#bar;
-  }
-
-  bar() {
-    return this.#privateMethod();
-  }
-}
-
-var f = new Foo();
-console.log(f.foo());
-console.log(f.bar());
\ No newline at end of file
diff --git a/crates/swc/tests/fixture/issues-4xxx/4464/1/output/exec.js b/crates/swc/tests/fixture/issues-4xxx/4464/1/output/exec.js
deleted file mode 100644
index 69b97496de..0000000000
--- a/crates/swc/tests/fixture/issues-4xxx/4464/1/output/exec.js
+++ /dev/null
@@ -1,34 +0,0 @@
-import * as swcHelpers from "@swc/helpers";
-var _bar = /*#__PURE__*/ new WeakMap(), _privateMethod = /*#__PURE__*/ new WeakSet();
-var Foo = /*#__PURE__*/ function() {
-    "use strict";
-    function Foo() {
-        swcHelpers.classCallCheck(this, Foo);
-        swcHelpers.classPrivateMethodInit(this, _privateMethod);
-        swcHelpers.classPrivateFieldInit(this, _bar, {
-            writable: true,
-            value: "i'm a private field!"
-        });
-    }
-    swcHelpers.createClass(Foo, [
-        {
-            key: "foo",
-            value: function foo() {
-                return swcHelpers.classPrivateFieldGet(this, _bar);
-            }
-        },
-        {
-            key: "bar",
-            value: function bar() {
-                return swcHelpers.classPrivateMethodGet(this, _privateMethod, privateMethod).call(this);
-            }
-        }
-    ]);
-    return Foo;
-}();
-function privateMethod() {
-    return "i'm a private method!";
-}
-var f = new Foo();
-console.log(f.foo());
-console.log(f.bar());
diff --git a/crates/swc/tests/tsc-references/superInStaticMembers1_es2015.1.normal.js b/crates/swc/tests/tsc-references/superInStaticMembers1_es2015.1.normal.js
index 71229e784f..83f04171e7 100644
--- a/crates/swc/tests/tsc-references/superInStaticMembers1_es2015.1.normal.js
+++ b/crates/swc/tests/tsc-references/superInStaticMembers1_es2015.1.normal.js
@@ -1,4 +1,5 @@
 import * as swcHelpers from "@swc/helpers";
+var _Reflect, __, _Reflect1, __1;
 class Reflect1 {
 }
 // @target: es5, es2015, es2021, es2022, esnext
@@ -66,7 +67,7 @@ C._ = [
         swcHelpers.get(swcHelpers.getPrototypeOf(C), "w", C).call(C);
     })(), 
 ];
-var __32 = {
+var __2 = {
     writable: true,
     value: (()=>{
         var { Reflect  } = {
@@ -75,7 +76,7 @@ var __32 = {
         super.w();
     })()
 };
-var __1 = {
+var __111 = {
     writable: true,
     value: (()=>{
         var [Reflect] = [
@@ -410,21 +411,20 @@ _Reflect = class Reflect {
         C1._ = swcHelpers.get(swcHelpers.getPrototypeOf(C1), "w", C1).call(C1);
     })()
 }, _Reflect;
-_Reflect = class Reflect {
-}, __ = {
+_Reflect1 = class Reflect {
+}, __1 = {
     writable: true,
     value: (()=>{
-        var _Reflect, __;
         class C extends B {
         }
-        var __33 = {
+        var __ = {
             writable: true,
             value: (()=>{
                 super.w();
             })()
         };
     })()
-}, _Reflect;
+}, _Reflect1;
 (function Reflect() {}); // no collision
 class C extends B {
 }
@@ -439,7 +439,6 @@ var __31 = {
     })()
 };
 (function Reflect() {
-    var _Reflect, __;
     class C2 extends B {
     }
     C2._ = swcHelpers.get(swcHelpers.getPrototypeOf(C2), "w", C2).call(C2);
diff --git a/crates/swc/tests/tsc-references/superInStaticMembers1_es2015.2.minified.js b/crates/swc/tests/tsc-references/superInStaticMembers1_es2015.2.minified.js
index 43d1debac2..07b62a4ec5 100644
--- a/crates/swc/tests/tsc-references/superInStaticMembers1_es2015.2.minified.js
+++ b/crates/swc/tests/tsc-references/superInStaticMembers1_es2015.2.minified.js
@@ -171,22 +171,11 @@ class C extends B {
 C._ = swcHelpers.get(swcHelpers.getPrototypeOf(C), "w", C).call(C);
 class C extends B {
 }
-super.w(), _Reflect = class {
-}, __ = {
-    writable: !0,
-    value: (()=>{
-        class C1 extends B {
-        }
-        C1._ = swcHelpers.get(swcHelpers.getPrototypeOf(C1), "w", C1).call(C1);
-    })()
-}, _Reflect = class {
-}, __ = {
-    writable: !0,
-    value: (()=>{
-        var _Reflect, __;
-        super.w();
-    })()
-};
+super.w(), (()=>{
+    class C1 extends B {
+    }
+    C1._ = swcHelpers.get(swcHelpers.getPrototypeOf(C1), "w", C1).call(C1);
+})(), super.w();
 class C extends B {
 }
 C._ = swcHelpers.get(swcHelpers.getPrototypeOf(C), "w", C).call(C);
diff --git a/crates/swc/tests/tsc-references/superInStaticMembers1_es5.1.normal.js b/crates/swc/tests/tsc-references/superInStaticMembers1_es5.1.normal.js
index 31b4678943..8e40c8f27b 100644
--- a/crates/swc/tests/tsc-references/superInStaticMembers1_es5.1.normal.js
+++ b/crates/swc/tests/tsc-references/superInStaticMembers1_es5.1.normal.js
@@ -1,6 +1,7 @@
 import * as swcHelpers from "@swc/helpers";
 var _this = this, _superprop_get_w = ()=>super.w
 ;
+var _Reflect, __, _Reflect1, __1;
 // @target: es5, es2015, es2021, es2022, esnext
 // @noTypesAndSymbols: true
 // @filename: external.ts
@@ -83,7 +84,7 @@ C._ = [
         swcHelpers.get(swcHelpers.getPrototypeOf(C), "w", C).call(C);
     }(), 
 ];
-var _$__22 = {
+var __2 = {
     writable: true,
     value: function() {
         var Reflect = {
@@ -92,7 +93,7 @@ var _$__22 = {
         _superprop_get_w().call(_this);
     }()
 };
-var __1 = {
+var __111 = {
     writable: true,
     value: function() {
         var Reflect = null; // collision (es2015-es2021 only)
@@ -223,7 +224,7 @@ var C = /*#__PURE__*/ function(B) {
     }
     return C;
 }(B);
-var _$__1 = {
+var __11 = {
     writable: true,
     value: function() {
         _superprop_get_w().call(_this);
@@ -242,7 +243,7 @@ var C = /*#__PURE__*/ function(B) {
     }
     return C;
 }(B);
-var _$__2 = {
+var __12 = {
     writable: true,
     value: function() {
         _superprop_get_w().call(_this);
@@ -259,7 +260,7 @@ var C = /*#__PURE__*/ function(B) {
     }
     return C;
 }(B);
-var _$__3 = {
+var __13 = {
     writable: true,
     value: function() {
         _superprop_get_w().call(_this);
@@ -296,7 +297,7 @@ var C = /*#__PURE__*/ function(B) {
     }
     return C;
 }(B);
-var _$__4 = {
+var __14 = {
     writable: true,
     value: function() {
         _superprop_get_w().call(_this);
@@ -325,7 +326,7 @@ var C = /*#__PURE__*/ function(B) {
     }
     return C;
 }(B);
-var _$__5 = {
+var __15 = {
     writable: true,
     value: function() {
         _superprop_get_w().call(_this);
@@ -352,7 +353,7 @@ var C = /*#__PURE__*/ function(B) {
     }
     return C;
 }(B);
-var _$__6 = {
+var __16 = {
     writable: true,
     value: function() {
         _superprop_get_w().call(_this);
@@ -381,7 +382,7 @@ var C = /*#__PURE__*/ function(B) {
     }
     return C;
 }(B);
-var _$__7 = {
+var __17 = {
     writable: true,
     value: function() {
         _superprop_get_w().call(_this);
@@ -410,7 +411,7 @@ var C = /*#__PURE__*/ function(B) {
     }
     return C;
 }(B);
-var _$__8 = {
+var __18 = {
     writable: true,
     value: function() {
         _superprop_get_w().call(_this);
@@ -437,7 +438,7 @@ var C = /*#__PURE__*/ function(B) {
     }
     return C;
 }(B);
-var _$__9 = {
+var __19 = {
     writable: true,
     value: function() {
         _superprop_get_w().call(_this);
@@ -464,7 +465,7 @@ var C = /*#__PURE__*/ function(B) {
     }
     return C;
 }(B);
-var _$__10 = {
+var __20 = {
     writable: true,
     value: function() {
         _superprop_get_w().call(_this);
@@ -491,7 +492,7 @@ var C = /*#__PURE__*/ function(B) {
     }
     return C;
 }(B);
-var _$__11 = {
+var __21 = {
     writable: true,
     value: function() {
         _superprop_get_w().call(_this);
@@ -518,7 +519,7 @@ var C = /*#__PURE__*/ function(B) {
     }
     return C;
 }(B);
-var _$__12 = {
+var __22 = {
     writable: true,
     value: function() {
         _superprop_get_w().call(_this);
@@ -545,7 +546,7 @@ var C = /*#__PURE__*/ function(B) {
     }
     return C;
 }(B);
-var _$__13 = {
+var __23 = {
     writable: true,
     value: function() {
         _superprop_get_w().call(_this);
@@ -572,7 +573,7 @@ var C = /*#__PURE__*/ function(B) {
     }
     return C;
 }(B);
-var _$__14 = {
+var __24 = {
     writable: true,
     value: function() {
         _superprop_get_w().call(_this);
@@ -599,7 +600,7 @@ var C = /*#__PURE__*/ function(B) {
     }
     return C;
 }(B);
-var _$__15 = {
+var __25 = {
     writable: true,
     value: function() {
         _superprop_get_w().call(_this);
@@ -626,7 +627,7 @@ var C = /*#__PURE__*/ function(B) {
     }
     return C;
 }(B);
-var _$__16 = {
+var __26 = {
     writable: true,
     value: function() {
         _superprop_get_w().call(_this);
@@ -653,7 +654,7 @@ var C = /*#__PURE__*/ function(B) {
     }
     return C;
 }(B);
-var _$__17 = {
+var __27 = {
     writable: true,
     value: function() {
         _superprop_get_w().call(_this);
@@ -680,7 +681,7 @@ var C = /*#__PURE__*/ function(B) {
     }
     return C;
 }(B);
-var _$__18 = {
+var __28 = {
     writable: true,
     value: function() {
         _superprop_get_w().call(_this);
@@ -707,7 +708,7 @@ var C = /*#__PURE__*/ function(B) {
     }
     return C;
 }(B);
-var _$__19 = {
+var __29 = {
     writable: true,
     value: function() {
         _superprop_get_w().call(_this);
@@ -742,16 +743,16 @@ var C = /*#__PURE__*/ function(B) {
     }
     return C;
 }(B);
-var _$__20 = {
+var __30 = {
     writable: true,
     value: function() {
         _superprop_get_w().call(_this);
     }()
 };
-_$_Reflect = function Reflect() {
+_Reflect = function Reflect() {
     "use strict";
     swcHelpers.classCallCheck(this, Reflect);
-}, _$__ = {
+}, __ = {
     writable: true,
     value: function() {
         var C = /*#__PURE__*/ function(B) {
@@ -766,16 +767,15 @@ _$_Reflect = function Reflect() {
         }(B);
         C._ = swcHelpers.get(swcHelpers.getPrototypeOf(C), "w", C).call(C);
     }()
-}, _$_Reflect;
-_$_Reflect = function Reflect() {
+}, _Reflect;
+_Reflect1 = function Reflect() {
     "use strict";
     swcHelpers.classCallCheck(this, Reflect);
-}, _$__ = {
+}, __1 = {
     writable: true,
     value: function() {
         var _this1 = _this, _superprop_get_w1 = ()=>_superprop_get_w()
         ;
-        var _$_Reflect, _$__;
         var C = /*#__PURE__*/ function(B) {
             "use strict";
             swcHelpers.inherits(C, B);
@@ -786,14 +786,14 @@ _$_Reflect = function Reflect() {
             }
             return C;
         }(B);
-        var _$__23 = {
+        var __ = {
             writable: true,
             value: function() {
                 _superprop_get_w1().call(_this1);
             }()
         };
     }()
-}, _$_Reflect;
+}, _Reflect1;
 (function Reflect() {}); // no collision
 var C = /*#__PURE__*/ function(B) {
     "use strict";
@@ -817,14 +817,13 @@ var C = /*#__PURE__*/ function(B) {
     }
     return C;
 }(B);
-var _$__21 = {
+var __31 = {
     writable: true,
     value: function() {
         _superprop_get_w().call(_this);
     }()
 };
 (function Reflect() {
-    var _$_Reflect, _$__;
     var C = /*#__PURE__*/ function(B) {
         "use strict";
         swcHelpers.inherits(C, B);
@@ -850,7 +849,7 @@ var _$__21 = {
         }
         return C;
     }(B);
-    var _$__ = {
+    var __ = {
         writable: true,
         value: function() {
             _superprop_get_w2().call(_this2);
diff --git a/crates/swc/tests/tsc-references/superInStaticMembers1_es5.2.minified.js b/crates/swc/tests/tsc-references/superInStaticMembers1_es5.2.minified.js
index 76f331d17d..c389021443 100644
--- a/crates/swc/tests/tsc-references/superInStaticMembers1_es5.2.minified.js
+++ b/crates/swc/tests/tsc-references/superInStaticMembers1_es5.2.minified.js
@@ -464,41 +464,29 @@ var C = function(B) {
     }
     return C;
 }(B);
-_superprop_get_w().call(_this), _$_Reflect = function Reflect7() {
-    "use strict";
-    swcHelpers.classCallCheck(this, Reflect7);
-}, _$__ = {
-    writable: !0,
-    value: function() {
-        var C = function(B) {
-            "use strict";
-            swcHelpers.inherits(C, B);
-            var _super = swcHelpers.createSuper(C);
-            function C() {
-                return swcHelpers.classCallCheck(this, C), _super.apply(this, arguments);
-            }
-            return C;
-        }(B);
-        C._ = swcHelpers.get(swcHelpers.getPrototypeOf(C), "w", C).call(C);
-    }()
-}, _$_Reflect = function Reflect8() {
-    "use strict";
-    swcHelpers.classCallCheck(this, Reflect8);
-}, _$__ = {
-    writable: !0,
-    value: function() {
-        var _$_Reflect, _$__, C = function(B) {
-            "use strict";
-            swcHelpers.inherits(C, B);
-            var _super = swcHelpers.createSuper(C);
-            function C() {
-                return swcHelpers.classCallCheck(this, C), _super.apply(this, arguments);
-            }
-            return C;
-        }(B);
-        _superprop_get_w().call(_this);
-    }()
-};
+_superprop_get_w().call(_this), function() {
+    var C = function(B) {
+        "use strict";
+        swcHelpers.inherits(C, B);
+        var _super = swcHelpers.createSuper(C);
+        function C() {
+            return swcHelpers.classCallCheck(this, C), _super.apply(this, arguments);
+        }
+        return C;
+    }(B);
+    C._ = swcHelpers.get(swcHelpers.getPrototypeOf(C), "w", C).call(C);
+}(), function() {
+    var C = function(B) {
+        "use strict";
+        swcHelpers.inherits(C, B);
+        var _super = swcHelpers.createSuper(C);
+        function C() {
+            return swcHelpers.classCallCheck(this, C), _super.apply(this, arguments);
+        }
+        return C;
+    }(B);
+    _superprop_get_w().call(_this);
+}();
 var C = function(B) {
     "use strict";
     swcHelpers.inherits(C, B);
diff --git a/crates/swc_ecma_transforms_compat/tests/es2022_class_properties.rs b/crates/swc_ecma_transforms_compat/tests/es2022_class_properties.rs
index c598a81dee..2a0d3651aa 100644
--- a/crates/swc_ecma_transforms_compat/tests/es2022_class_properties.rs
+++ b/crates/swc_ecma_transforms_compat/tests/es2022_class_properties.rs
@@ -5965,6 +5965,35 @@ var _prop2 = {
 "
 );
 
+test!(
+    syntax(),
+    |t| class_properties(Some(t.comments.clone()), Default::default()),
+    issue_4473,
+    "
+var test1 = class X {
+  [Symbol.toStringTag]() {}
+}
+
+function a() {
+  const b = class Y {
+    x() {
+    }
+  }
+}
+",
+    "
+let _toStringTag;
+var test1 = (_toStringTag = Symbol.toStringTag, class X {
+    [_toStringTag]() {}
+});
+function a() {
+    const b = class Y {
+        x() {}
+    };
+}
+"
+);
+
 test!(
     syntax(),
     |t| class_properties(