Skip to content

Commit

Permalink
[TS] Correctly transform computed strings and templates in enums (#10555
Browse files Browse the repository at this point in the history
)

* [TS] Correctly transform computed strings and templates in enums

* Typo [skip ci]
  • Loading branch information
nicolo-ribaudo authored and JLHwung committed Nov 4, 2019
1 parent 5c0d8a9 commit abce0ef
Show file tree
Hide file tree
Showing 7 changed files with 16 additions and 6 deletions.
10 changes: 7 additions & 3 deletions packages/babel-plugin-transform-typescript/src/enum.js
Expand Up @@ -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":
Expand All @@ -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;
}
Expand Down
@@ -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 = {}));

0 comments on commit abce0ef

Please sign in to comment.