Repository: swc-project/swc (rust)

Issue:
Imports with the same name exported with different names map to incorrect modules
### Describe the bug

Wasn't able to find an existing issue, lmk if it's a duplicate: 

If two imports have the same export (e.g. A) and it gets renamed during the export (`export { A as Aa }`) then the compiled code incorrectly maps all the requires to one of the modules. See playground and below!

Note that this doesn't seem to happen for the export `default` - only named exports.

### Input code

```typescript
export { A as Aa } from 'a';
export { A as Ab } from 'b';
```


### Config

```json
{
  "jsc": {
    "parser": {
      "syntax": "typescript",
      "tsx": false
    },
    "target": "es2020",
    "loose": false,
    "minify": {
      "compress": false,
      "mangle": false
    }
  },
  "module": {
    "type": "commonjs"
  },
  "minify": false,
  "isModule": true
}
```


### Playground link

https://play.swc.rs/?version=1.2.218&code=H4sIAAAAAAAAA0utKMgvKlGoVnBUSCxWcExUqFVIK8rPVVBPVLfmSkWVTIJLJqlbAwAkJ3d%2BOQAAAA%3D%3D&config=H4sIAAAAAAAAA0WNSwrDMAxE76J1FiHL3KGHMK4SHPxD40CN8d0rF5fsJM2bp0YXLO2NshGwjAk1FvOhnUrNDCsuF1qoQE%2BH8eCui5GTiyKMbd1WjX1K4AksFFx0Rx0ym0IWBp7IxNP%2Fya6ukN73OLTfP3VqJaR4gfojmmWH16SL3Ny%2F3SauU70AAAA%3D

### Expected behavior

```js
_export(exports, {
    Aa: ()=>_a.A,
    Ab: ()=>_b.A
});
const _a = require("a");
const _b = require("b");
```

### Actual behavior

```js
_export(exports, {
    Aa: ()=>_b.A,
    Ab: ()=>_b.A
});
const _a = require("a");
const _b = require("b");
```

### Version

1.2.218

### Additional context

_No response_

Pull request:
fix(es/module): Private context for `export from`
<!-- 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 #5260 

Patch:
diff --git a/crates/swc_common/src/syntax_pos.rs b/crates/swc_common/src/syntax_pos.rs
index fabc27ef6d..6ef73e0c53 100644
--- a/crates/swc_common/src/syntax_pos.rs
+++ b/crates/swc_common/src/syntax_pos.rs
@@ -493,6 +493,11 @@ impl Span {
         mark
     }
 
+    #[inline]
+    pub fn private(self) -> Span {
+        self.apply_mark(Mark::fresh(Mark::root()))
+    }
+
     #[inline]
     pub fn adjust(&mut self, expansion: Mark) -> Option<Mark> {
         let mut span = *self;
diff --git a/crates/swc_ecma_transforms_module/src/module_decl_strip.rs b/crates/swc_ecma_transforms_module/src/module_decl_strip.rs
index 7555a20a1d..fa7bbf6c52 100644
--- a/crates/swc_ecma_transforms_module/src/module_decl_strip.rs
+++ b/crates/swc_ecma_transforms_module/src/module_decl_strip.rs
@@ -455,14 +455,14 @@ impl From<ExportSpecifier> for LinkSpecifier {
                     ModuleExportName::Ident(Ident { span, sym, .. })
                     | ModuleExportName::Str(Str {
                         span, value: sym, ..
-                    }) => (sym, span),
+                    }) => (sym, span.private()),
                 };
 
                 let exported = exported.map(|exported| match exported {
                     ModuleExportName::Ident(Ident { span, sym, .. })
                     | ModuleExportName::Str(Str {
                         span, value: sym, ..
-                    }) => (sym, span),
+                    }) => (sym, span.private()),
                 });
 
                 match orig {


Test patch:
diff --git a/crates/swc_ecma_transforms_module/tests/fixture/common/issue-5260/input.js b/crates/swc_ecma_transforms_module/tests/fixture/common/issue-5260/input.js
new file mode 100644
index 0000000000..38f758528a
--- /dev/null
+++ b/crates/swc_ecma_transforms_module/tests/fixture/common/issue-5260/input.js
@@ -0,0 +1,19 @@
+export { X as a } from "a";
+export { X as b } from "b";
+export { default as c } from "c";
+export { default as d } from "d";
+export * as e from "e";
+export * as f from "f";
+
+// unresolved
+const x = X;
+const _a = a;
+const _c = c;
+const _e = e;
+
+// top level
+const b = 1,
+    d = 2,
+    f = 3;
+
+console.log(b, d, f);
diff --git a/crates/swc_ecma_transforms_module/tests/fixture/common/issue-5260/output.amd.js b/crates/swc_ecma_transforms_module/tests/fixture/common/issue-5260/output.amd.js
new file mode 100644
index 0000000000..2d620299f5
--- /dev/null
+++ b/crates/swc_ecma_transforms_module/tests/fixture/common/issue-5260/output.amd.js
@@ -0,0 +1,41 @@
+define([
+    "require",
+    "exports",
+    "a",
+    "b",
+    "c",
+    "d",
+    "e",
+    "f"
+], function(require, exports, _a, _b, _c, _d, _e, _f) {
+    "use strict";
+    Object.defineProperty(exports, "__esModule", {
+        value: true
+    });
+    function _export(target, all) {
+        for(var name in all)Object.defineProperty(target, name, {
+            enumerable: true,
+            get: all[name]
+        });
+    }
+    _export(exports, {
+        a: ()=>_a.X,
+        b: ()=>_b.X,
+        c: ()=>_c.default,
+        d: ()=>_d.default,
+        e: ()=>_e,
+        f: ()=>_f
+    });
+    _c = /*#__PURE__*/ _interopRequireDefault(_c);
+    _d = /*#__PURE__*/ _interopRequireDefault(_d);
+    _e = /*#__PURE__*/ _interopRequireWildcard(_e);
+    _f = /*#__PURE__*/ _interopRequireWildcard(_f);
+    // unresolved
+    const x = X;
+    const _a1 = a;
+    const _c1 = c;
+    const _e1 = e;
+    // top level
+    const b = 1, d = 2, f = 3;
+    console.log(b, d, f);
+});
diff --git a/crates/swc_ecma_transforms_module/tests/fixture/common/issue-5260/output.cjs b/crates/swc_ecma_transforms_module/tests/fixture/common/issue-5260/output.cjs
new file mode 100644
index 0000000000..5a9ea8ad65
--- /dev/null
+++ b/crates/swc_ecma_transforms_module/tests/fixture/common/issue-5260/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, {
+        enumerable: true,
+        get: all[name]
+    });
+}
+_export(exports, {
+    a: ()=>_a.X,
+    b: ()=>_b.X,
+    c: ()=>_c.default,
+    d: ()=>_d.default,
+    e: ()=>_e,
+    f: ()=>_f
+});
+const _a = require("a");
+const _b = require("b");
+const _c = /*#__PURE__*/ _interopRequireDefault(require("c"));
+const _d = /*#__PURE__*/ _interopRequireDefault(require("d"));
+const _e = /*#__PURE__*/ _interopRequireWildcard(require("e"));
+const _f = /*#__PURE__*/ _interopRequireWildcard(require("f"));
+// unresolved
+const x = X;
+const _a1 = a;
+const _c1 = c;
+const _e1 = e;
+// top level
+const b = 1, d = 2, f = 3;
+console.log(b, d, f);
diff --git a/crates/swc_ecma_transforms_module/tests/fixture/common/issue-5260/output.umd.js b/crates/swc_ecma_transforms_module/tests/fixture/common/issue-5260/output.umd.js
new file mode 100644
index 0000000000..1d00c8d0c2
--- /dev/null
+++ b/crates/swc_ecma_transforms_module/tests/fixture/common/issue-5260/output.umd.js
@@ -0,0 +1,44 @@
+(function(global, factory) {
+    if (typeof module === "object" && typeof module.exports === "object") factory(exports, require("a"), require("b"), require("c"), require("d"), require("e"), require("f"));
+    else if (typeof define === "function" && define.amd) define([
+        "exports",
+        "a",
+        "b",
+        "c",
+        "d",
+        "e",
+        "f"
+    ], factory);
+    else if (global = typeof globalThis !== "undefined" ? globalThis : global || self) factory(global.input = {}, global.a, global.b, global.c, global.d, global.e, global.f);
+})(this, function(exports, _a, _b, _c, _d, _e, _f) {
+    "use strict";
+    Object.defineProperty(exports, "__esModule", {
+        value: true
+    });
+    function _export(target, all) {
+        for(var name in all)Object.defineProperty(target, name, {
+            enumerable: true,
+            get: all[name]
+        });
+    }
+    _export(exports, {
+        a: ()=>_a.X,
+        b: ()=>_b.X,
+        c: ()=>_c.default,
+        d: ()=>_d.default,
+        e: ()=>_e,
+        f: ()=>_f
+    });
+    _c = /*#__PURE__*/ _interopRequireDefault(_c);
+    _d = /*#__PURE__*/ _interopRequireDefault(_d);
+    _e = /*#__PURE__*/ _interopRequireWildcard(_e);
+    _f = /*#__PURE__*/ _interopRequireWildcard(_f);
+    // unresolved
+    const x = X;
+    const _a1 = a;
+    const _c1 = c;
+    const _e1 = e;
+    // top level
+    const b = 1, d = 2, f = 3;
+    console.log(b, d, f);
+});