Skip to content

Commit

Permalink
[babel 8] Turn const enums into const variables (#16396)
Browse files Browse the repository at this point in the history
Co-authored-by: Babel Bot <30521560+liuxingbaoyu@users.noreply.github.com>
  • Loading branch information
samualtnorman and liuxingbaoyu authored Apr 1, 2024
1 parent 22725e9 commit dd699a8
Show file tree
Hide file tree
Showing 26 changed files with 134 additions and 7 deletions.
4 changes: 3 additions & 1 deletion packages/babel-plugin-transform-typescript/src/const-enum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ export default function transpileConstEnum(
);
} else {
path.replaceWith(
t.variableDeclaration("var", [t.variableDeclarator(path.node.id, obj)]),
t.variableDeclaration(process.env.BABEL_8_BREAKING ? "const" : "var", [
t.variableDeclarator(path.node.id, obj),
]),
);
path.scope.registerDeclaration(path);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export const enum A {
x = 3,
y = "f",
z = 4 << 2,
w = y
}

A.x;
A.y;
A.z;
A.w;
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"plugins": [["transform-typescript", { "optimizeConstEnums": true }]],
"sourceType": "module",
"BABEL_8_BREAKING": false
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export var A = {
x: 3,
y: "f",
z: 16,
w: "f"
};
A.x;
A.y;
A.z;
A.w;
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"plugins": [["transform-typescript", { "optimizeConstEnums": true }]],
"sourceType": "module",
"BABEL_8_BREAKING": true
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export var A = {
export const A = {
x: 3,
y: "f",
z: 16,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const enum A { y }

let x = A.y;
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"BABEL_8_BREAKING": false
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export var A = {
y: 0
};
let x = A.y;
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const enum WhitespaceFlag {
before = 1 << 0,
after = 1 << 1,
}

export type { WhitespaceFlag as WF1 };
export { WhitespaceFlag as WF2 };

export const before = WhitespaceFlag.before;
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"plugins": [["transform-typescript", { "optimizeConstEnums": true }]],
"sourceType": "module",
"validateLogs": true,
"BABEL_8_BREAKING": false
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
var WhitespaceFlag = {
before: 1,
after: 2
};
export { WhitespaceFlag as WF2 };
export const before = WhitespaceFlag.before;
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"plugins": [["transform-typescript", { "optimizeConstEnums": true }]],
"sourceType": "module",
"validateLogs": true
"validateLogs": true,
"BABEL_8_BREAKING": true
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var WhitespaceFlag = {
const WhitespaceFlag = {
before: 1,
after: 2
};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"BABEL_8_BREAKING": true
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export var A = {
export const A = {
y: 0
};
let x = A.y;
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const enum A { y }

let x = A.y;

export { A };
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"plugins": [["transform-typescript", { "optimizeConstEnums": true }]],
"sourceType": "module",
"validateLogs": true,
"BABEL_8_BREAKING": false
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
var A = {
y: 0
};
let x = A.y;
export { A };
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"plugins": [["transform-typescript", { "optimizeConstEnums": true }]],
"sourceType": "module",
"validateLogs": true,
"BABEL_8_BREAKING": true
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var A = {
const A = {
y: 0
};
let x = A.y;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export const enum A {
x, y
}

export const enum A {
z
}

A.x;
A["y"];
A.z;
A.w;
A;
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"plugins": [["transform-typescript", { "optimizeConstEnums": true }]],
"sourceType": "module",
"validateLogs": true,
"BABEL_8_BREAKING": false
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export var A = {
x: 0,
y: 1
};
Object.assign(A, {
z: 0
});
A.x;
A["y"];
A.z;
A.w;
A;
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"plugins": [["transform-typescript", { "optimizeConstEnums": true }]],
"sourceType": "module",
"validateLogs": true,
"BABEL_8_BREAKING": true
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export var A = {
export const A = {
x: 0,
y: 1
};
Expand Down

0 comments on commit dd699a8

Please sign in to comment.