[TS] Correctly transform computed strings and templates in enums (#10555)
* [TS] Correctly transform computed strings and templates in enums * Typo [skip ci]
This commit is contained in:
parent
5c0d8a9de7
commit
abce0ef49d
@ -144,13 +144,12 @@ function evaluate(
|
||||
expr,
|
||||
seen: PreviousEnumMembers,
|
||||
): number | string | typeof undefined {
|
||||
if (expr.type === "StringLiteral") {
|
||||
return expr.value;
|
||||
}
|
||||
return evalConstant(expr);
|
||||
|
||||
function evalConstant(expr): number | typeof undefined {
|
||||
switch (expr.type) {
|
||||
case "StringLiteral":
|
||||
return expr.value;
|
||||
case "UnaryExpression":
|
||||
return evalUnaryExpression(expr);
|
||||
case "BinaryExpression":
|
||||
@ -161,6 +160,11 @@ function evaluate(
|
||||
return evalConstant(expr.expression);
|
||||
case "Identifier":
|
||||
return seen[expr.name];
|
||||
case "TemplateLiteral":
|
||||
if (expr.quasis.length === 1) {
|
||||
return expr.quasis[0].value.cooked;
|
||||
}
|
||||
/* falls through */
|
||||
default:
|
||||
return undefined;
|
||||
}
|
||||
|
||||
3
packages/babel-plugin-transform-typescript/test/fixtures/enum/string-value-template/input.ts
vendored
Normal file
3
packages/babel-plugin-transform-typescript/test/fixtures/enum/string-value-template/input.ts
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
enum E {
|
||||
A = `Hey`
|
||||
}
|
||||
@ -0,0 +1,5 @@
|
||||
var E;
|
||||
|
||||
(function (E) {
|
||||
E["A"] = "Hey";
|
||||
})(E || (E = {}));
|
||||
@ -1,4 +1,3 @@
|
||||
// Not type-correct code
|
||||
enum E {
|
||||
A = "HALLO" + "WERLD"
|
||||
}
|
||||
|
||||
@ -1,6 +1,5 @@
|
||||
// Not type-correct code
|
||||
var E;
|
||||
|
||||
(function (E) {
|
||||
E[E["A"] = "HALLO" + "WERLD"] = "A";
|
||||
E["A"] = "HALLOWERLD";
|
||||
})(E || (E = {}));
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user