babel / babel

🐠 Babel is a compiler for writing next generation JavaScript.
https://babel.dev
MIT License
43.27k stars 5.65k forks source link

[Bug]: `optimizeConstEnums` option of `@babel/preset-typescript` not work as well with nampspace #16911

Open spencerHT opened 1 month ago

spencerHT commented 1 month ago

💻

How are you using Babel?

babel-loader (webpack)

Input code

I init a Github repo for reproducing the case. link. How to reproduce the case:

  1. git clone
  2. pnpm i
  3. node babel.js

The code before transformed is:

export namespace Tags {
  export const enum TagsType {
    Unknown = 0,
    PC = 1,
    Mobile = 2,
    Console = 3,
  }
  export interface TagsCategory {
    type?: Tags.TagsType;
    title?: string;
  }
}

Configuration file name

No response

Configuration

No response

Current and expected behavior

The code after babel transformed is:

"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.Tags = void 0;
let Tags = exports.Tags = void 0;
(function (_Tags) {
  _Tags.TagsType = TagsType;
})(Tags || (exports.Tags = Tags = {}));

The TagsType is not defined.

I tried to put TagsType out of the Tags namespace, and then will be fine: Input:

export const enum TagsType {
  Unknown = 0,
  PC = 1,
  Mobile = 2,
  Console = 3,
}

export namespace Tags {
  export interface TagsCategory {
    type?: TagsType;
    title?: string;
  }
}

Output:

"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.TagsType = exports.Tags = void 0;
var TagsType = exports.TagsType = {
  Unknown: 0,
  PC: 1,
  Mobile: 2,
  Console: 3
};
let Tags = exports.Tags = void 0;

Environment

  System:
    OS: macOS 15.0.1
  Binaries:
    Node: 18.20.2 - ~/.nvm/versions/node/v18.20.2/bin/node
    Yarn: 1.22.22 - ~/.nvm/versions/node/v18.20.2/bin/yarn
    npm: 10.5.0 - ~/.nvm/versions/node/v18.20.2/bin/npm
    pnpm: 9.5.0 - ~/.nvm/versions/node/v18.20.2/bin/pnpm
  npmPackages:
    @babel/core: 7.25.8 => 7.25.8 
    @babel/preset-env: 7.25.8 => 7.25.8 
    @babel/preset-typescript: 7.25.7 => 7.25.7 

Possible solution

No response

Additional context

No response

babel-bot commented 1 month ago

Hey @spencerHT! We really appreciate you taking the time to report an issue. The collaborators on this project attempt to help as many people as possible, but we're a limited number of volunteers, so it's possible this won't be addressed swiftly.

If you need any help, or just have general Babel or JavaScript questions, we have a vibrant Slack community that typically always has someone willing to help. You can sign-up here for an invite.