Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
fix
  • Loading branch information
liuxingbaoyu committed Jul 30, 2026
commit 7b400dc3d67d8be1c49f415025781c4d561f9be3
3 changes: 2 additions & 1 deletion packages/babel-plugin-transform-typescript/src/const-enum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export default function transpileConstEnum(
),
);

if (path.scope.hasOwnBinding(name)) {
if (!(path.scope.getOwnBinding(name)?.kind === "unknown")) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Preserve compatibility with cores that do not bind enums

When this plugin version is loaded by an older but supported @babel/core/@babel/traverse (the plugin's peer range is ^8.0.0), enums have no scope binding, so with optimizeConstEnums an input such as export const enum E { A } takes this branch and becomes Object.assign(E, ...) without ever declaring E, throwing during module evaluation. A missing binding must follow the declaration-creation path, and the paired traverse change must likewise remain compatible with older plugin versions.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am surprised that AI has also discovered this.

(parentIsExport ? path.parentPath : path).replaceWith(
t.expressionStatement(
t.callExpression(
Expand All @@ -53,6 +53,7 @@ export default function transpileConstEnum(
),
);
} else {
path.scope.removeOwnBinding(name);
path.replaceWith(
t.variableDeclaration("const", [
t.variableDeclarator(path.node.id, obj),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

export { Enum };

const enum Enum {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"plugins": [["transform-typescript"]],
"sourceType": "module",
"validateLogs": true
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export { Enum };
var Enum = /*#__PURE__*/function (Enum) {
return Enum;
}(Enum || {});
Original file line number Diff line number Diff line change
@@ -1,16 +1 @@
The exported identifier "WhitespaceFlag" is not declared in Babel's scope tracker
as a JavaScript value binding, and "@babel/plugin-transform-typescript"
never encountered it as a TypeScript type declaration.
It will be treated as a JavaScript value.

This problem is likely caused by another plugin injecting
"WhitespaceFlag" without registering it in the scope tracker. If you are the author
of that plugin, please use "scope.registerDeclaration(declarationPath)".
The exported identifier "WhitespaceFlag" is not declared in Babel's scope tracker
as a JavaScript value binding, and "@babel/plugin-transform-typescript"
never encountered it as a TypeScript type declaration.
It will be treated as a JavaScript value.

This problem is likely caused by another plugin injecting
"WhitespaceFlag" without registering it in the scope tracker. If you are the author
of that plugin, please use "scope.registerDeclaration(declarationPath)".
6 changes: 6 additions & 0 deletions packages/babel-traverse/src/scope/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -755,6 +755,12 @@ class Scope {
return;
}

if (path.isTSEnumDeclaration() && !path.node.declare) {
const id = path.get("id");
this.registerBinding(kind, id, path);
Comment on lines +758 to +760

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Clear bindings when inlining local const enums

With optimizeConstEnums, a non-exported const enum merged with a following namespace now leaves this newly registered binding behind when const-enum.ts removes the enum; the generic removal hook cannot clean it because getBindingIdentifiers has no TSEnumDeclaration entry. For a module containing const enum E { A } followed by namespace E { export const B = 1 }, transpileNamespace consequently sees hasOwnBinding("E") and omits its let E declaration, producing an IIFE that reads and assigns undeclared E and throws at runtime. Remove the enum binding before the inlining branch removes the declaration.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While adding tests, I discovered that const enum cannot be merged with a namespace, but removing it is fine.

Comment on lines +758 to +760

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Register nested enums in their block scope

When an enum appears in a nested block and an enclosing function/program has a same-named binding, this new special case registers the enum in that enclosing scope because the collector routes non-BlockScoped declarations to the function/program scope. Consequently, valid shadowing such as let E; { enum E {} } throws Duplicate declaration "E" during scope crawling, even though the enum transform emits a block-local let; register enum declarations in their block parent (or classify them as block-scoped) instead.

Useful? React with 👍 / 👎.

return;
}

const parent = this.getProgramParent();
const ids = path.getOuterBindingIdentifiers(true);

Expand Down
Loading