From abce0ef49d4f3b204b9d570b763839101772a56a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=B2=20Ribaudo?= Date: Mon, 4 Nov 2019 19:22:49 +0100 Subject: [PATCH] [TS] Correctly transform computed strings and templates in enums (#10555) * [TS] Correctly transform computed strings and templates in enums * Typo [skip ci] --- packages/babel-plugin-transform-typescript/src/enum.js | 10 +++++++--- .../test/fixtures/enum/string-value-template/input.ts | 3 +++ .../test/fixtures/enum/string-value-template/output.js | 5 +++++ .../enum/{string-values => string-value}/input.ts | 0 .../enum/{string-values => string-value}/output.js | 0 .../test/fixtures/enum/string-values-computed/input.ts | 1 - .../fixtures/enum/string-values-computed/output.js | 3 +-- 7 files changed, 16 insertions(+), 6 deletions(-) create mode 100644 packages/babel-plugin-transform-typescript/test/fixtures/enum/string-value-template/input.ts create mode 100644 packages/babel-plugin-transform-typescript/test/fixtures/enum/string-value-template/output.js rename packages/babel-plugin-transform-typescript/test/fixtures/enum/{string-values => string-value}/input.ts (100%) rename packages/babel-plugin-transform-typescript/test/fixtures/enum/{string-values => string-value}/output.js (100%) diff --git a/packages/babel-plugin-transform-typescript/src/enum.js b/packages/babel-plugin-transform-typescript/src/enum.js index 11c7b5376d..3b604e2e8f 100644 --- a/packages/babel-plugin-transform-typescript/src/enum.js +++ b/packages/babel-plugin-transform-typescript/src/enum.js @@ -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; } diff --git a/packages/babel-plugin-transform-typescript/test/fixtures/enum/string-value-template/input.ts b/packages/babel-plugin-transform-typescript/test/fixtures/enum/string-value-template/input.ts new file mode 100644 index 0000000000..01086419ea --- /dev/null +++ b/packages/babel-plugin-transform-typescript/test/fixtures/enum/string-value-template/input.ts @@ -0,0 +1,3 @@ +enum E { + A = `Hey` +} diff --git a/packages/babel-plugin-transform-typescript/test/fixtures/enum/string-value-template/output.js b/packages/babel-plugin-transform-typescript/test/fixtures/enum/string-value-template/output.js new file mode 100644 index 0000000000..dfc0f72fcd --- /dev/null +++ b/packages/babel-plugin-transform-typescript/test/fixtures/enum/string-value-template/output.js @@ -0,0 +1,5 @@ +var E; + +(function (E) { + E["A"] = "Hey"; +})(E || (E = {})); diff --git a/packages/babel-plugin-transform-typescript/test/fixtures/enum/string-values/input.ts b/packages/babel-plugin-transform-typescript/test/fixtures/enum/string-value/input.ts similarity index 100% rename from packages/babel-plugin-transform-typescript/test/fixtures/enum/string-values/input.ts rename to packages/babel-plugin-transform-typescript/test/fixtures/enum/string-value/input.ts diff --git a/packages/babel-plugin-transform-typescript/test/fixtures/enum/string-values/output.js b/packages/babel-plugin-transform-typescript/test/fixtures/enum/string-value/output.js similarity index 100% rename from packages/babel-plugin-transform-typescript/test/fixtures/enum/string-values/output.js rename to packages/babel-plugin-transform-typescript/test/fixtures/enum/string-value/output.js diff --git a/packages/babel-plugin-transform-typescript/test/fixtures/enum/string-values-computed/input.ts b/packages/babel-plugin-transform-typescript/test/fixtures/enum/string-values-computed/input.ts index ac52806fb9..06e9b00f06 100644 --- a/packages/babel-plugin-transform-typescript/test/fixtures/enum/string-values-computed/input.ts +++ b/packages/babel-plugin-transform-typescript/test/fixtures/enum/string-values-computed/input.ts @@ -1,4 +1,3 @@ -// Not type-correct code enum E { A = "HALLO" + "WERLD" } diff --git a/packages/babel-plugin-transform-typescript/test/fixtures/enum/string-values-computed/output.js b/packages/babel-plugin-transform-typescript/test/fixtures/enum/string-values-computed/output.js index e449a1ca23..a0f3ea2ca8 100644 --- a/packages/babel-plugin-transform-typescript/test/fixtures/enum/string-values-computed/output.js +++ b/packages/babel-plugin-transform-typescript/test/fixtures/enum/string-values-computed/output.js @@ -1,6 +1,5 @@ -// Not type-correct code var E; (function (E) { - E[E["A"] = "HALLO" + "WERLD"] = "A"; + E["A"] = "HALLOWERLD"; })(E || (E = {}));