Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ts] Handle exponentiation operator in constant folding #15418

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 = {}));