[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, expr,
seen: PreviousEnumMembers, seen: PreviousEnumMembers,
): number | string | typeof undefined { ): number | string | typeof undefined {
if (expr.type === "StringLiteral") {
return expr.value;
}
return evalConstant(expr); return evalConstant(expr);
function evalConstant(expr): number | typeof undefined { function evalConstant(expr): number | typeof undefined {
switch (expr.type) { switch (expr.type) {
case "StringLiteral":
return expr.value;
case "UnaryExpression": case "UnaryExpression":
return evalUnaryExpression(expr); return evalUnaryExpression(expr);
case "BinaryExpression": case "BinaryExpression":
@ -161,6 +160,11 @@ function evaluate(
return evalConstant(expr.expression); return evalConstant(expr.expression);
case "Identifier": case "Identifier":
return seen[expr.name]; return seen[expr.name];
case "TemplateLiteral":
if (expr.quasis.length === 1) {
return expr.quasis[0].value.cooked;
}
/* falls through */
default: default:
return undefined; 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 { enum E {
A = "HALLO" + "WERLD" A = "HALLO" + "WERLD"
} }

View File

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