[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,
|
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;
|
||||||
}
|
}
|
||||||
|
|||||||
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 {
|
enum E {
|
||||||
A = "HALLO" + "WERLD"
|
A = "HALLO" + "WERLD"
|
||||||
}
|
}
|
||||||
|
|||||||
@ -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 = {}));
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user