Repository: swc-project/swc (rust)

Issue:
Compiling `import * as`  results incorrect output in typescript 
### Describe the bug

I tried this typescript code to compile es2017. It results wrong code. Output code throws error `Refference Error`.

### Input code

```typescript
import * as deff from 'sdf';


export default {
  deff
}
```


### Config

```json
{
  "jsc": {
    "parser": {
      "syntax": "typescript",
      "tsx": false,
      "decorators": true
    },
    "target": "es2017"
  },
  "sourceMaps": true,
  "module": {
    "type": "commonjs"
  }
}
```


### Playground link

https://play.swc.rs/?version=1.2.206&code=H4sIAAAAAAAAA1PIzC3ILypR0FJILFZISU1LU0grys9VUC9OSVO35uLiSq0ASwNlEktzShSquRTAqrhqASevvRo5AAAA&config=H4sIAAAAAAAAAy2NwQrDMAxD%2F0XnHLZdBvmHfYRxvbHS1MF2YKXk35dAb0J6kk6szsgnKpmLTeXHHvRDRhxVnO1bAwnhw3rT5pKwCKtRqDlyWJM%2BYrKPxCiJP273J4bl2ozlRfWiEooubZP5MacHzFqK7quj9z%2Fc9hIXiQAAAA%3D%3D

### Expected behavior

Output code doesn't have to throw any error.

### Actual behavior

It throws an error. Because `deff` doesn't exist

### Version

1.2.206

### Additional context

_No response_

Pull request:
fix(es/module): handle module ref in binding ident
<!-- 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):**
- fix #5054 

Patch:
diff --git a/crates/swc_ecma_transforms_module/src/module_ref_rewriter.rs b/crates/swc_ecma_transforms_module/src/module_ref_rewriter.rs
index b72cef18d2..e43c83749a 100644
--- a/crates/swc_ecma_transforms_module/src/module_ref_rewriter.rs
+++ b/crates/swc_ecma_transforms_module/src/module_ref_rewriter.rs
@@ -4,7 +4,7 @@ use swc_common::{
     util::take::Take,
     DUMMY_SP,
 };
-use swc_ecma_ast::{Id, Ident, *};
+use swc_ecma_ast::*;
 use swc_ecma_utils::{undefined, ExprFactory, IntoIndirectCall};
 use swc_ecma_visit::{noop_visit_mut_type, VisitMut, VisitMutWith};
 
@@ -41,32 +41,28 @@ pub(crate) struct ModuleRefRewriter {
 impl VisitMut for ModuleRefRewriter {
     noop_visit_mut_type!();
 
+    /// replace bar in binding pattern
+    /// const foo = { bar }
+    fn visit_mut_prop(&mut self, n: &mut Prop) {
+        match n {
+            Prop::Shorthand(shorthand) => {
+                if let Some(expr) = self.map_module_ref_ident(shorthand) {
+                    *n = KeyValueProp {
+                        key: shorthand.take().into(),
+                        value: Box::new(expr),
+                    }
+                    .into()
+                }
+            }
+            _ => n.visit_mut_children_with(self),
+        }
+    }
+
     fn visit_mut_expr(&mut self, n: &mut Expr) {
         match n {
             Expr::Ident(ref_ident) => {
-                if let Some((mod_ident, mod_prop)) = self.import_map.get(&ref_ident.to_id()) {
-                    let mut mod_ident = mod_ident.clone();
-                    let span = ref_ident.span.with_ctxt(mod_ident.span.ctxt);
-                    mod_ident.span = span;
-
-                    let mod_expr = if self.lazy_record.contains(&mod_ident.to_id()) {
-                        mod_ident.as_call(span, Default::default())
-                    } else {
-                        mod_ident.into()
-                    };
-
-                    if let Some(imported_name) = mod_prop {
-                        let prop = prop_name(imported_name, DUMMY_SP).into();
-
-                        *n = MemberExpr {
-                            obj: Box::new(mod_expr),
-                            span,
-                            prop,
-                        }
-                        .into();
-                    } else {
-                        *n = mod_expr;
-                    }
+                if let Some(expr) = self.map_module_ref_ident(ref_ident) {
+                    *n = expr;
                 }
             }
 
@@ -136,4 +132,33 @@ impl ModuleRefRewriter {
         n.visit_mut_with(self);
         self.top_level = top_level;
     }
+
+    fn map_module_ref_ident(&mut self, ref_ident: &Ident) -> Option<Expr> {
+        self.import_map
+            .get(&ref_ident.to_id())
+            .map(|(mod_ident, mod_prop)| -> Expr {
+                let mut mod_ident = mod_ident.clone();
+                let span = ref_ident.span.with_ctxt(mod_ident.span.ctxt);
+                mod_ident.span = span;
+
+                let mod_expr = if self.lazy_record.contains(&mod_ident.to_id()) {
+                    mod_ident.as_call(span, Default::default())
+                } else {
+                    mod_ident.into()
+                };
+
+                if let Some(imported_name) = mod_prop {
+                    let prop = prop_name(imported_name, DUMMY_SP).into();
+
+                    MemberExpr {
+                        obj: Box::new(mod_expr),
+                        span,
+                        prop,
+                    }
+                    .into()
+                } else {
+                    mod_expr
+                }
+            })
+    }
 }


Test patch:
diff --git a/crates/swc_ecma_transforms_module/tests/fixture/common/issue-332/output.amd.js b/crates/swc_ecma_transforms_module/tests/fixture/common/issue-332/output.amd.js
index 140958eae6..1ecd5cb13d 100644
--- a/crates/swc_ecma_transforms_module/tests/fixture/common/issue-332/output.amd.js
+++ b/crates/swc_ecma_transforms_module/tests/fixture/common/issue-332/output.amd.js
@@ -13,6 +13,6 @@ define([
     });
     _foo = _interopRequireDefault(_foo);
     const bar = {
-        foo
+        foo: _foo.default
     };
 });
diff --git a/crates/swc_ecma_transforms_module/tests/fixture/common/issue-332/output.cjs b/crates/swc_ecma_transforms_module/tests/fixture/common/issue-332/output.cjs
index f323d599df..5ed391b623 100644
--- a/crates/swc_ecma_transforms_module/tests/fixture/common/issue-332/output.cjs
+++ b/crates/swc_ecma_transforms_module/tests/fixture/common/issue-332/output.cjs
@@ -8,5 +8,5 @@ Object.defineProperty(exports, "bar", {
 });
 const _foo = _interopRequireDefault(require("foo"));
 const bar = {
-    foo
+    foo: _foo.default
 };
diff --git a/crates/swc_ecma_transforms_module/tests/fixture/common/issue-332/output.umd.js b/crates/swc_ecma_transforms_module/tests/fixture/common/issue-332/output.umd.js
index fcf11c5653..885de8cccd 100644
--- a/crates/swc_ecma_transforms_module/tests/fixture/common/issue-332/output.umd.js
+++ b/crates/swc_ecma_transforms_module/tests/fixture/common/issue-332/output.umd.js
@@ -16,6 +16,6 @@
     });
     _foo = _interopRequireDefault(_foo);
     const bar = {
-        foo
+        foo: _foo.default
     };
 });
diff --git a/crates/swc_ecma_transforms_module/tests/fixture/common/issue-5054/1/input.js b/crates/swc_ecma_transforms_module/tests/fixture/common/issue-5054/1/input.js
new file mode 100644
index 0000000000..56ff00a183
--- /dev/null
+++ b/crates/swc_ecma_transforms_module/tests/fixture/common/issue-5054/1/input.js
@@ -0,0 +1,7 @@
+import foo from "foo";
+import { bar } from "bar";
+import * as baz from "baz";
+
+export default { foo, baz, baz };
+const x = { foo, bar, baz };
+export const y = { foo, bar, baz };
diff --git a/crates/swc_ecma_transforms_module/tests/fixture/common/issue-5054/1/output.amd.js b/crates/swc_ecma_transforms_module/tests/fixture/common/issue-5054/1/output.amd.js
new file mode 100644
index 0000000000..47ab4b7c4d
--- /dev/null
+++ b/crates/swc_ecma_transforms_module/tests/fixture/common/issue-5054/1/output.amd.js
@@ -0,0 +1,39 @@
+define([
+    "require",
+    "exports",
+    "foo",
+    "bar",
+    "baz"
+], function(require, exports, _foo, _bar, _baz) {
+    "use strict";
+    Object.defineProperty(exports, "__esModule", {
+        value: true
+    });
+    function _export(target, all) {
+        for(var name in all)Object.defineProperty(target, name, {
+            get: all[name],
+            enumerable: true
+        });
+    }
+    _export(exports, {
+        default: ()=>_default,
+        y: ()=>y
+    });
+    _foo = _interopRequireDefault(_foo);
+    _baz = _interopRequireWildcard(_baz);
+    var _default = {
+        foo: _foo.default,
+        baz: _baz,
+        baz: _baz
+    };
+    const x = {
+        foo: _foo.default,
+        bar: _bar.bar,
+        baz: _baz
+    };
+    const y = {
+        foo: _foo.default,
+        bar: _bar.bar,
+        baz: _baz
+    };
+});
diff --git a/crates/swc_ecma_transforms_module/tests/fixture/common/issue-5054/1/output.cjs b/crates/swc_ecma_transforms_module/tests/fixture/common/issue-5054/1/output.cjs
new file mode 100644
index 0000000000..cfde32e2f5
--- /dev/null
+++ b/crates/swc_ecma_transforms_module/tests/fixture/common/issue-5054/1/output.cjs
@@ -0,0 +1,32 @@
+"use strict";
+Object.defineProperty(exports, "__esModule", {
+    value: true
+});
+function _export(target, all) {
+    for(var name in all)Object.defineProperty(target, name, {
+        get: all[name],
+        enumerable: true
+    });
+}
+_export(exports, {
+    default: ()=>_default,
+    y: ()=>y
+});
+const _foo = _interopRequireDefault(require("foo"));
+const _bar = require("bar");
+const _baz = _interopRequireWildcard(require("baz"));
+var _default = {
+    foo: _foo.default,
+    baz: _baz,
+    baz: _baz
+};
+const x = {
+    foo: _foo.default,
+    bar: _bar.bar,
+    baz: _baz
+};
+const y = {
+    foo: _foo.default,
+    bar: _bar.bar,
+    baz: _baz
+};
diff --git a/crates/swc_ecma_transforms_module/tests/fixture/common/issue-5054/1/output.umd.js b/crates/swc_ecma_transforms_module/tests/fixture/common/issue-5054/1/output.umd.js
new file mode 100644
index 0000000000..1cceb2bbcf
--- /dev/null
+++ b/crates/swc_ecma_transforms_module/tests/fixture/common/issue-5054/1/output.umd.js
@@ -0,0 +1,42 @@
+(function(global, factory) {
+    if (typeof module === "object" && typeof module.exports === "object") factory(exports, require("foo"), require("bar"), require("baz"));
+    else if (typeof define === "function" && define.amd) define([
+        "exports",
+        "foo",
+        "bar",
+        "baz"
+    ], factory);
+    else if (global = typeof globalThis !== "undefined" ? globalThis : global || self) factory(global.input = {}, global.foo, global.bar, global.baz);
+})(this, function(exports, _foo, _bar, _baz) {
+    "use strict";
+    Object.defineProperty(exports, "__esModule", {
+        value: true
+    });
+    function _export(target, all) {
+        for(var name in all)Object.defineProperty(target, name, {
+            get: all[name],
+            enumerable: true
+        });
+    }
+    _export(exports, {
+        default: ()=>_default,
+        y: ()=>y
+    });
+    _foo = _interopRequireDefault(_foo);
+    _baz = _interopRequireWildcard(_baz);
+    var _default = {
+        foo: _foo.default,
+        baz: _baz,
+        baz: _baz
+    };
+    const x = {
+        foo: _foo.default,
+        bar: _bar.bar,
+        baz: _baz
+    };
+    const y = {
+        foo: _foo.default,
+        bar: _bar.bar,
+        baz: _baz
+    };
+});