Repository: babel/babel (TypeScript)

Issue:
[Bug]: Wrong export value of `__proto__` in CommonJS
### 💻

- [X] Would you like to work on a fix?

### How are you using Babel?

Programmatic API (`babel.transform`, `babel.parse`)

### Input code

```JavaScript
export const __proto__ = null;

import { __proto__ as p } from "./index.js";

console.log(p);
```

[Playground](https://babeljs.io/repl#?browsers=chrome%20100&build=&builtIns=false&corejs=3.21&spec=false&loose=false&code_lz=KYDwDg9gTgLgBAYwgOwM7wPobFCMJZwC8cyArgDYUDcAULQJYC2kscA3nFjngRnAENUcMHAC-cAGa4mcAEQA6APQNkAE1AKAVqjl1aSNBArAFFCAHMAFGACUdIA&debug=false&forceAllTransforms=false&modules=commonjs&shippedProposals=false&circleciRepo=&evaluate=false&fileSize=false&timeTravel=false&sourceType=module&lineWrap=false&presets=env%2Cstage-3&prettier=true&targets=&version=7.23.1&externalPlugins=&assumptions=%7B%7D)

### Configuration file name

_No response_

### Configuration

_No response_

### Current and expected behavior

run `node index.js`

Expected behavior:
`null` should be printed.
Actual behavior:
`undefined` is printed.

### Environment

latest version

### Possible solution

Use `[[Define]]` semantic for `__proto__` export name.

### Additional context

_No response_


Hints:

Patch:
diff --git a/packages/babel-helper-module-transforms/src/index.ts b/packages/babel-helper-module-transforms/src/index.ts
--- a/packages/babel-helper-module-transforms/src/index.ts
+++ b/packages/babel-helper-module-transforms/src/index.ts
@@ -557,6 +557,7 @@ function buildExportInitializationStatements(
 const InitTemplate = {
   computed: template.expression`EXPORTS["NAME"] = VALUE`,
   default: template.expression`EXPORTS.NAME = VALUE`,
+  define: template.expression`Object.defineProperty(EXPORTS, "NAME", { enumerable:true, value: void 0, writable: true })["NAME"] = VALUE`,
 };
 
 function buildInitStatement(
@@ -572,11 +573,16 @@ function buildInitStatement(
         NAME: exportName,
         VALUE: acc,
       };
+
+      if (exportName === "__proto__") {
+        return InitTemplate.define(params);
+      }
+
       if (stringSpecifiers.has(exportName)) {
         return InitTemplate.computed(params);
-      } else {
-        return InitTemplate.default(params);
       }
+
+      return InitTemplate.default(params);
     }, initExpr),
   );
 }


Test patch:
diff --git a/packages/babel-plugin-transform-modules-amd/test/fixtures/amd/export-named-6/input.mjs b/packages/babel-plugin-transform-modules-amd/test/fixtures/amd/export-named-6/input.mjs
new file mode 100644
--- /dev/null
+++ b/packages/babel-plugin-transform-modules-amd/test/fixtures/amd/export-named-6/input.mjs
@@ -0,0 +1,7 @@
+export const __proto__ = null;
+export const a = 1;
+export const _ = 2;
+
+import { __proto__ as p } from "./input.js";
+
+console.log(p);
diff --git a/packages/babel-plugin-transform-modules-amd/test/fixtures/amd/export-named-6/output.js b/packages/babel-plugin-transform-modules-amd/test/fixtures/amd/export-named-6/output.js
new file mode 100644
--- /dev/null
+++ b/packages/babel-plugin-transform-modules-amd/test/fixtures/amd/export-named-6/output.js
@@ -0,0 +1,16 @@
+define(["exports", "./input.js"], function (_exports, _input) {
+  "use strict";
+
+  Object.defineProperty(_exports, "__esModule", {
+    value: true
+  });
+  _exports.a = Object.defineProperty(_exports, "__proto__", {
+    enumerable: true,
+    value: void 0,
+    writable: true
+  })["__proto__"] = _exports._ = void 0;
+  const __proto__ = _exports.__proto__ = null;
+  const a = _exports.a = 1;
+  const _ = _exports._ = 2;
+  console.log(_input.__proto__);
+});
diff --git a/packages/babel-plugin-transform-modules-commonjs/test/fixtures/interop/export-from-9/input.mjs b/packages/babel-plugin-transform-modules-commonjs/test/fixtures/interop/export-from-9/input.mjs
new file mode 100644
--- /dev/null
+++ b/packages/babel-plugin-transform-modules-commonjs/test/fixtures/interop/export-from-9/input.mjs
@@ -0,0 +1 @@
+export { __proto__ } from "xyz";
diff --git a/packages/babel-plugin-transform-modules-commonjs/test/fixtures/interop/export-from-9/output.js b/packages/babel-plugin-transform-modules-commonjs/test/fixtures/interop/export-from-9/output.js
new file mode 100644
--- /dev/null
+++ b/packages/babel-plugin-transform-modules-commonjs/test/fixtures/interop/export-from-9/output.js
@@ -0,0 +1,12 @@
+"use strict";
+
+Object.defineProperty(exports, "__esModule", {
+  value: true
+});
+Object.defineProperty(exports, "__proto__", {
+  enumerable: true,
+  get: function () {
+    return _xyz.__proto__;
+  }
+});
+var _xyz = require("xyz");
diff --git a/packages/babel-plugin-transform-modules-commonjs/test/fixtures/interop/export-named-6/input.mjs b/packages/babel-plugin-transform-modules-commonjs/test/fixtures/interop/export-named-6/input.mjs
new file mode 100644
--- /dev/null
+++ b/packages/babel-plugin-transform-modules-commonjs/test/fixtures/interop/export-named-6/input.mjs
@@ -0,0 +1,7 @@
+export const __proto__ = null;
+export const a = 1;
+export const _ = 2;
+
+import { __proto__ as p } from "./input.js";
+
+console.log(p);
diff --git a/packages/babel-plugin-transform-modules-commonjs/test/fixtures/interop/export-named-6/output.js b/packages/babel-plugin-transform-modules-commonjs/test/fixtures/interop/export-named-6/output.js
new file mode 100644
--- /dev/null
+++ b/packages/babel-plugin-transform-modules-commonjs/test/fixtures/interop/export-named-6/output.js
@@ -0,0 +1,15 @@
+"use strict";
+
+Object.defineProperty(exports, "__esModule", {
+  value: true
+});
+exports.a = Object.defineProperty(exports, "__proto__", {
+  enumerable: true,
+  value: void 0,
+  writable: true
+})["__proto__"] = exports._ = void 0;
+var _input = require("./input.js");
+const __proto__ = exports.__proto__ = null;
+const a = exports.a = 1;
+const _ = exports._ = 2;
+console.log(_input.__proto__);
diff --git a/packages/babel-plugin-transform-modules-umd/test/fixtures/umd/export-named-6/input.mjs b/packages/babel-plugin-transform-modules-umd/test/fixtures/umd/export-named-6/input.mjs
new file mode 100644
--- /dev/null
+++ b/packages/babel-plugin-transform-modules-umd/test/fixtures/umd/export-named-6/input.mjs
@@ -0,0 +1,7 @@
+export const __proto__ = null;
+export const a = 1;
+export const _ = 2;
+
+import { __proto__ as p } from "./input.js";
+
+console.log(p);
diff --git a/packages/babel-plugin-transform-modules-umd/test/fixtures/umd/export-named-6/output.js b/packages/babel-plugin-transform-modules-umd/test/fixtures/umd/export-named-6/output.js
new file mode 100644
--- /dev/null
+++ b/packages/babel-plugin-transform-modules-umd/test/fixtures/umd/export-named-6/output.js
@@ -0,0 +1,28 @@
+(function (global, factory) {
+  if (typeof define === "function" && define.amd) {
+    define(["exports", "./input.js"], factory);
+  } else if (typeof exports !== "undefined") {
+    factory(exports, require("./input.js"));
+  } else {
+    var mod = {
+      exports: {}
+    };
+    factory(mod.exports, global.input);
+    global.input = mod.exports;
+  }
+})(typeof globalThis !== "undefined" ? globalThis : typeof self !== "undefined" ? self : this, function (_exports, _input) {
+  "use strict";
+
+  Object.defineProperty(_exports, "__esModule", {
+    value: true
+  });
+  _exports.a = Object.defineProperty(_exports, "__proto__", {
+    enumerable: true,
+    value: void 0,
+    writable: true
+  })["__proto__"] = _exports._ = void 0;
+  const __proto__ = _exports.__proto__ = null;
+  const a = _exports.a = 1;
+  const _ = _exports._ = 2;
+  console.log(_input.__proto__);
+});