[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:
Nicolò Ribaudo 2019-11-04 19:22:49 +01:00 committed by Huáng Jùnliàng
parent 5c0d8a9de7
commit abce0ef49d
7 changed files with 16 additions and 6 deletions

View File

@ -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;
}

View File

@ -0,0 +1,3 @@
enum E {
A = `Hey`
}

View File

@ -0,0 +1,5 @@
var E;
(function (E) {
E["A"] = "Hey";
})(E || (E = {}));

View File

@ -1,4 +1,3 @@
// Not type-correct code
enum E {
A = "HALLO" + "WERLD"
}

View File

@ -1,6 +1,5 @@
// Not type-correct code
var E;
(function (E) {
E[E["A"] = "HALLO" + "WERLD"] = "A";
E["A"] = "HALLOWERLD";
})(E || (E = {}));