Repository: swc-project/swc (rust)

Issue:
Object destructuring with default assigns on default
### Describe the bug

swc/ES5 transpiles into code that assigns on the default symbol

### Input code

```typescript
var bar;
const baz = 'ok';
({
    foo: bar = baz
} = {});

console.log("result:", bar, baz);
// expected result: ok, ok
```


### Config

_No response_

### Playground link

https://play.swc.rs/?version=1.2.130&code=H4sIAAAAAAAAAytLLFJISiyy5krOzysuATKrFGwV1POz1a25NKq5FIAgLT%2FfCqQEKA6U5aoF0tW1mtZcYB35Oal6OfnpGkpFqcWlOSVWSjogpSCiCqhEX18htaIgNbkkNUUBqkAhP1sHiAFLEcycdwAAAA%3D%3D&config=H4sIAAAAAAAAA0WMTQrEIAxG75K1286id5hDBCctFv9IUhgR714tlu7C915ehUMsrBUyshCPS0pU%2FMMKZAOKZZcVTNf6tKEXagYUeScdiiyd%2BZSEJjUQXHRbGSWbQmYSeRHG3T9m66GQfucYKmjJdAc%2F0N7G%2FHPynaLySe0Cj9ke9LUAAAA%3D

### Expected behavior

properly fall back to default, don't assign on default

### Actual behavior

var bar;
var baz = 'ok';
var ref;
ref = {}, baz = ref.foo, bar = baz === void 0 ? baz : baz, ref;
console.log("result:", bar, baz);
// => actual result: undefined, undefined

### Version

1.2.130

### Additional context

_No response_

Pull request:
fix(es/compat): Fix `destructuring`
**Description:**

It is a regression introduced in #3104 .

**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):**
- fix #3315 

Patch:
diff --git a/crates/swc_ecma_transforms_compat/src/es2015/destructuring.rs b/crates/swc_ecma_transforms_compat/src/es2015/destructuring.rs
index d612936ed7..06b59bc875 100644
--- a/crates/swc_ecma_transforms_compat/src/es2015/destructuring.rs
+++ b/crates/swc_ecma_transforms_compat/src/es2015/destructuring.rs
@@ -900,11 +900,7 @@ impl VisitMut for AssignFolder {
                     });
                 }
                 Pat::Assign(pat) => {
-                    let ref_ident = match *pat.right {
-                        Expr::Ident(ref i) => i.clone(),
-
-                        _ => make_ref_ident(self.c, &mut self.vars, None),
-                    };
+                    let ref_ident = make_ref_ident(self.c, &mut self.vars, None);
 
                     let mut exprs = vec![Box::new(
                         AssignExpr {


Test patch:
diff --git a/crates/swc/tests/tsc-references/destructuringObjectAssignmentPatternWithNestedSpread_es5.1.normal.js b/crates/swc/tests/tsc-references/destructuringObjectAssignmentPatternWithNestedSpread_es5.1.normal.js
index aeac467a69..24dd2ee38f 100644
--- a/crates/swc/tests/tsc-references/destructuringObjectAssignmentPatternWithNestedSpread_es5.1.normal.js
+++ b/crates/swc/tests/tsc-references/destructuringObjectAssignmentPatternWithNestedSpread_es5.1.normal.js
@@ -34,7 +34,7 @@ var a, b, c = {
     }
 }, d;
 var _c;
-var ref;
+var ref, ref1;
 _c = c, b = _objectWithoutProperties(_c.x, [
     "a"
-]), ref = _c, d = ref.x, a = (d === void 0 ? d : d).a, ref, _c;
+]), ref = _c, ref1 = ref.x, a = (ref1 === void 0 ? d : ref1).a, ref, _c;
diff --git a/crates/swc/tests/tsc-references/destructuringObjectAssignmentPatternWithNestedSpread_es5.2.minified.js b/crates/swc/tests/tsc-references/destructuringObjectAssignmentPatternWithNestedSpread_es5.2.minified.js
index f1c5e6cf87..1c365f54f9 100644
--- a/crates/swc/tests/tsc-references/destructuringObjectAssignmentPatternWithNestedSpread_es5.2.minified.js
+++ b/crates/swc/tests/tsc-references/destructuringObjectAssignmentPatternWithNestedSpread_es5.2.minified.js
@@ -1,4 +1,4 @@
-var _c;
+var _c, ref, d;
 (function(source, excluded) {
     if (null == source) return {};
     var key, i, target = function(source, excluded) {
@@ -18,4 +18,4 @@ var _c;
     }
 }).x, [
     "a"
-]), _c.x.a;
+]), (void 0 === (ref = _c.x) ? d : ref).a;
diff --git a/crates/swc_ecma_transforms_compat/tests/es2015_destructuring.rs b/crates/swc_ecma_transforms_compat/tests/es2015_destructuring.rs
index 698f9880d3..d82e517cd1 100644
--- a/crates/swc_ecma_transforms_compat/tests/es2015_destructuring.rs
+++ b/crates/swc_ecma_transforms_compat/tests/es2015_destructuring.rs
@@ -1962,3 +1962,33 @@ test!(
     NODE_ENV = process.env.NODE_ENV;
     "
 );
+
+test!(
+    syntax(),
+    |_| tr(),
+    issue_3315_1,
+    "\
+    var baz = 1;
+    ({ foo: bar = baz } = {});
+    ",
+    "\
+    var baz = 1;
+    var ref, ref1;
+    ref = {}, ref1 = ref.foo, bar = ref1 === void 0 ? baz : ref1, ref;
+    "
+);
+
+test!(
+    syntax(),
+    |_| tr(),
+    issue_3315_2,
+    "\
+    var baz = 1;
+    [bar = baz] = [];    
+    ",
+    "\
+    var baz = 1;
+    var ref, ref1;
+    ref = [], ref1 = ref[0], bar = ref1 === void 0 ? baz : ref1, ref;
+    "
+);