Skip to content

Commit eb4dfe7

Browse files
author
Emanuel Hoogeveen
authored
[ts] Handle exponentiation operator in constant folding (#15418)
1 parent 8d1486d commit eb4dfe7

3 files changed

Lines changed: 45 additions & 23 deletions

File tree

packages/babel-plugin-transform-typescript/src/enum.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ export function translateEnumValues(
146146
const initializer = member.initializer;
147147
let value: t.Expression;
148148
if (initializer) {
149-
constValue = evaluate(initializer, seen);
149+
constValue = computeConstantValue(initializer, seen);
150150
if (constValue !== undefined) {
151151
seen.set(name, constValue);
152152
if (typeof constValue === "number") {
@@ -193,14 +193,14 @@ export function translateEnumValues(
193193
});
194194
}
195195

196-
// Based on the TypeScript repository's `evalConstant` in `checker.ts`.
197-
function evaluate(
196+
// Based on the TypeScript repository's `computeConstantValue` in `checker.ts`.
197+
function computeConstantValue(
198198
expr: t.Node,
199199
seen: PreviousEnumMembers,
200200
): number | string | typeof undefined {
201-
return evalConstant(expr);
201+
return evaluate(expr);
202202

203-
function evalConstant(expr: t.Node): number | typeof undefined {
203+
function evaluate(expr: t.Node): number | typeof undefined {
204204
switch (expr.type) {
205205
case "StringLiteral":
206206
return expr.value;
@@ -211,7 +211,7 @@ function evaluate(
211211
case "NumericLiteral":
212212
return expr.value;
213213
case "ParenthesizedExpression":
214-
return evalConstant(expr.expression);
214+
return evaluate(expr.expression);
215215
case "Identifier":
216216
return seen.get(expr.name);
217217
case "TemplateLiteral":
@@ -228,7 +228,7 @@ function evaluate(
228228
argument,
229229
operator,
230230
}: t.UnaryExpression): number | typeof undefined {
231-
const value = evalConstant(argument);
231+
const value = evaluate(argument);
232232
if (value === undefined) {
233233
return undefined;
234234
}
@@ -246,11 +246,11 @@ function evaluate(
246246
}
247247

248248
function evalBinaryExpression(expr: t.BinaryExpression): number | undefined {
249-
const left = evalConstant(expr.left);
249+
const left = evaluate(expr.left);
250250
if (left === undefined) {
251251
return undefined;
252252
}
253-
const right = evalConstant(expr.right);
253+
const right = evaluate(expr.right);
254254
if (right === undefined) {
255255
return undefined;
256256
}
@@ -278,6 +278,8 @@ function evaluate(
278278
return left - right;
279279
case "%":
280280
return left % right;
281+
case "**":
282+
return left ** right;
281283
default:
282284
return undefined;
283285
}
Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,20 @@
11
enum E {
22
a,
3-
b = 2 + 3,
4-
c = 2 - 3,
5-
d = 2 * 3,
6-
e = 2 / 3,
7-
f = -1,
8-
g = 1 + 2 - 3 * 4 / -5,
9-
h,
3+
b = 1 | 2,
4+
c = 1 & 3,
5+
d = 4 >> 1,
6+
e = 8 >>> 1,
7+
f = 1 << 3,
8+
g = 2 ^ 7,
9+
h = 2 * 3,
10+
i = 2 / 3,
11+
j = 2 + 5,
12+
k = 2 - 4,
13+
l = 2.5 % 2,
14+
m = 2 ** 33,
15+
n = +9,
16+
o = -1,
17+
p = ~2,
18+
q = 1 + 2 - 3 * 4 / -5,
19+
r,
1020
}
Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,21 @@
11
var E;
22
(function (E) {
33
E[E["a"] = 0] = "a";
4-
E[E["b"] = 5] = "b";
5-
E[E["c"] = -1] = "c";
6-
E[E["d"] = 6] = "d";
7-
E[E["e"] = 0.6666666666666666] = "e";
8-
E[E["f"] = -1] = "f";
9-
E[E["g"] = 5.4] = "g";
10-
E[E["h"] = 6.4] = "h";
4+
E[E["b"] = 3] = "b";
5+
E[E["c"] = 1] = "c";
6+
E[E["d"] = 2] = "d";
7+
E[E["e"] = 4] = "e";
8+
E[E["f"] = 8] = "f";
9+
E[E["g"] = 5] = "g";
10+
E[E["h"] = 6] = "h";
11+
E[E["i"] = 0.6666666666666666] = "i";
12+
E[E["j"] = 7] = "j";
13+
E[E["k"] = -2] = "k";
14+
E[E["l"] = 0.5] = "l";
15+
E[E["m"] = 8589934592] = "m";
16+
E[E["n"] = 9] = "n";
17+
E[E["o"] = -1] = "o";
18+
E[E["p"] = -3] = "p";
19+
E[E["q"] = 5.4] = "q";
20+
E[E["r"] = 6.4] = "r";
1121
})(E || (E = {}));

0 commit comments

Comments
 (0)