Skip to content

Commit

Permalink
[ts] Handle exponentiation operator in constant folding (#15418)
Browse files Browse the repository at this point in the history
  • Loading branch information
ehoogeveen-medweb committed Feb 12, 2023
1 parent 8d1486d commit eb4dfe7
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 23 deletions.
20 changes: 11 additions & 9 deletions packages/babel-plugin-transform-typescript/src/enum.ts
Expand Up @@ -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") {
Expand Down Expand Up @@ -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;
Expand All @@ -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":
Expand All @@ -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;
}
Expand All @@ -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;
}
Expand Down Expand Up @@ -278,6 +278,8 @@ function evaluate(
return left - right;
case "%":
return left % right;
case "**":
return left ** right;
default:
return undefined;
}
Expand Down
@@ -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,
}
@@ -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 = {}));

0 comments on commit eb4dfe7

Please sign in to comment.