From 9977547e2742050fdd764089174b84e088ad1a96 Mon Sep 17 00:00:00 2001 From: Emanuel Hoogeveen Date: Sat, 11 Feb 2023 18:35:54 +0100 Subject: [PATCH 1/3] Rename functions to match the TypeScript repository. --- .../src/enum.ts | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/packages/babel-plugin-transform-typescript/src/enum.ts b/packages/babel-plugin-transform-typescript/src/enum.ts index 5c46c6e1379b..07c0b3e38167 100644 --- a/packages/babel-plugin-transform-typescript/src/enum.ts +++ b/packages/babel-plugin-transform-typescript/src/enum.ts @@ -146,7 +146,7 @@ export function translateEnumValues( const initializer = member.initializer; let value: t.Expression; if (initializer) { - constValue = evaluate(initializer, seen); + constValue = computeConstantValue(initializer, seen); if (constValue !== undefined) { seen.set(name, constValue); if (typeof constValue === "number") { @@ -193,14 +193,14 @@ export function translateEnumValues( }); } -// Based on the TypeScript repository's `evalConstant` in `checker.ts`. -function evaluate( +// Based on the TypeScript repository's `computeConstantValue` in `checker.ts`. +function computeConstantValue( expr: t.Node, seen: PreviousEnumMembers, ): number | string | typeof undefined { - return evalConstant(expr); + return evaluate(expr); - function evalConstant(expr: t.Node): number | typeof undefined { + function evaluate(expr: t.Node): number | typeof undefined { switch (expr.type) { case "StringLiteral": return expr.value; @@ -211,7 +211,7 @@ function evaluate( case "NumericLiteral": return expr.value; case "ParenthesizedExpression": - return evalConstant(expr.expression); + return evaluate(expr.expression); case "Identifier": return seen.get(expr.name); case "TemplateLiteral": @@ -228,7 +228,7 @@ function evaluate( argument, operator, }: t.UnaryExpression): number | typeof undefined { - const value = evalConstant(argument); + const value = evaluate(argument); if (value === undefined) { return undefined; } @@ -246,11 +246,11 @@ function evaluate( } function evalBinaryExpression(expr: t.BinaryExpression): number | undefined { - const left = evalConstant(expr.left); + const left = evaluate(expr.left); if (left === undefined) { return undefined; } - const right = evalConstant(expr.right); + const right = evaluate(expr.right); if (right === undefined) { return undefined; } From d22f0aa58edcab4bad7a9b89019323bc6078a97d Mon Sep 17 00:00:00 2001 From: Emanuel Hoogeveen Date: Sat, 11 Feb 2023 18:42:07 +0100 Subject: [PATCH 2/3] Add handling for exponentiation operator. --- packages/babel-plugin-transform-typescript/src/enum.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/packages/babel-plugin-transform-typescript/src/enum.ts b/packages/babel-plugin-transform-typescript/src/enum.ts index 07c0b3e38167..95f0481193b4 100644 --- a/packages/babel-plugin-transform-typescript/src/enum.ts +++ b/packages/babel-plugin-transform-typescript/src/enum.ts @@ -278,6 +278,8 @@ function computeConstantValue( return left - right; case "%": return left % right; + case "**": + return left ** right; default: return undefined; } From b0bed926f28803210188fd05d10816c9782567bf Mon Sep 17 00:00:00 2001 From: Emanuel Hoogeveen Date: Sat, 11 Feb 2023 19:03:58 +0100 Subject: [PATCH 3/3] Extend enum constant folding test to cover all handled binary and unary operators. --- .../fixtures/enum/constant-folding/input.ts | 24 +++++++++++++------ .../fixtures/enum/constant-folding/output.js | 24 +++++++++++++------ 2 files changed, 34 insertions(+), 14 deletions(-) diff --git a/packages/babel-plugin-transform-typescript/test/fixtures/enum/constant-folding/input.ts b/packages/babel-plugin-transform-typescript/test/fixtures/enum/constant-folding/input.ts index 3a30334e22fb..2b902c422955 100644 --- a/packages/babel-plugin-transform-typescript/test/fixtures/enum/constant-folding/input.ts +++ b/packages/babel-plugin-transform-typescript/test/fixtures/enum/constant-folding/input.ts @@ -1,10 +1,20 @@ enum E { a, - b = 2 + 3, - c = 2 - 3, - d = 2 * 3, - e = 2 / 3, - f = -1, - g = 1 + 2 - 3 * 4 / -5, - h, + b = 1 | 2, + c = 1 & 3, + d = 4 >> 1, + e = 8 >>> 1, + f = 1 << 3, + g = 2 ^ 7, + h = 2 * 3, + i = 2 / 3, + j = 2 + 5, + k = 2 - 4, + l = 2.5 % 2, + m = 2 ** 33, + n = +9, + o = -1, + p = ~2, + q = 1 + 2 - 3 * 4 / -5, + r, } diff --git a/packages/babel-plugin-transform-typescript/test/fixtures/enum/constant-folding/output.js b/packages/babel-plugin-transform-typescript/test/fixtures/enum/constant-folding/output.js index 9d615cc49a1b..4f53c5fd8b6a 100644 --- a/packages/babel-plugin-transform-typescript/test/fixtures/enum/constant-folding/output.js +++ b/packages/babel-plugin-transform-typescript/test/fixtures/enum/constant-folding/output.js @@ -1,11 +1,21 @@ var E; (function (E) { E[E["a"] = 0] = "a"; - E[E["b"] = 5] = "b"; - E[E["c"] = -1] = "c"; - E[E["d"] = 6] = "d"; - E[E["e"] = 0.6666666666666666] = "e"; - E[E["f"] = -1] = "f"; - E[E["g"] = 5.4] = "g"; - E[E["h"] = 6.4] = "h"; + E[E["b"] = 3] = "b"; + E[E["c"] = 1] = "c"; + E[E["d"] = 2] = "d"; + E[E["e"] = 4] = "e"; + E[E["f"] = 8] = "f"; + E[E["g"] = 5] = "g"; + E[E["h"] = 6] = "h"; + E[E["i"] = 0.6666666666666666] = "i"; + E[E["j"] = 7] = "j"; + E[E["k"] = -2] = "k"; + E[E["l"] = 0.5] = "l"; + E[E["m"] = 8589934592] = "m"; + E[E["n"] = 9] = "n"; + E[E["o"] = -1] = "o"; + E[E["p"] = -3] = "p"; + E[E["q"] = 5.4] = "q"; + E[E["r"] = 6.4] = "r"; })(E || (E = {}));