Skip to content

Commit fb1a5f7

Browse files
committed
fix(transformer/typescript): incorrect evaluation of TemplateLiteral in enum
1 parent 591d87b commit fb1a5f7

File tree

5 files changed

+88
-16
lines changed

5 files changed

+88
-16
lines changed

crates/oxc_transformer/src/typescript/enum.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -417,10 +417,18 @@ impl<'a> TypeScriptEnum<'a> {
417417
Expression::NumericLiteral(lit) => Some(ConstantValue::Number(lit.value)),
418418
Expression::StringLiteral(lit) => Some(ConstantValue::String(lit.value.to_string())),
419419
Expression::TemplateLiteral(lit) => {
420-
let mut value = String::new();
421-
for part in &lit.quasis {
422-
value.push_str(&part.value.raw);
423-
}
420+
let value = if let Some(quasi) = lit.single_quasi() {
421+
quasi.to_string()
422+
} else {
423+
let mut value = String::new();
424+
for (quasi, expr) in lit.quasis.iter().zip(&lit.expressions) {
425+
value.push_str(&quasi.value.cooked.unwrap_or(quasi.value.raw));
426+
if let ConstantValue::String(str) = self.evaluate(expr, prev_members)? {
427+
value.push_str(&str);
428+
}
429+
}
430+
value
431+
};
424432
Some(ConstantValue::String(value))
425433
}
426434
Expression::ParenthesizedExpression(expr) => {

tasks/coverage/snapshots/semantic_typescript.snap

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3776,41 +3776,39 @@ rebuilt : SymbolId(0): Span { start: 0, end: 0 }
37763776

37773777
semantic Error: tasks/coverage/typescript/tests/cases/compiler/computedEnumMemberSyntacticallyString.ts
37783778
Missing ReferenceId: "Foo"
3779+
Missing ReferenceId: "Foo"
3780+
Missing ReferenceId: "Foo"
37793781
Bindings mismatch:
37803782
after transform: ScopeId(1): ["A", "B", "C", "F", "Foo", "G", "H", "I", "J"]
37813783
rebuilt : ScopeId(1): ["Foo"]
37823784
Scope flags mismatch:
37833785
after transform: ScopeId(1): ScopeFlags(0x0)
37843786
rebuilt : ScopeId(1): ScopeFlags(Function)
3785-
Symbol reference IDs mismatch for "BAR":
3786-
after transform: SymbolId(0): [ReferenceId(0), ReferenceId(1), ReferenceId(2), ReferenceId(3), ReferenceId(4), ReferenceId(7)]
3787-
rebuilt : SymbolId(0): [ReferenceId(3), ReferenceId(7), ReferenceId(10), ReferenceId(15)]
37883787
Symbol flags mismatch for "Foo":
37893788
after transform: SymbolId(1): SymbolFlags(RegularEnum)
37903789
rebuilt : SymbolId(1): SymbolFlags(FunctionScopedVariable)
37913790
Symbol reference IDs mismatch for "Foo":
3792-
after transform: SymbolId(10): [ReferenceId(9), ReferenceId(10), ReferenceId(11), ReferenceId(12), ReferenceId(13), ReferenceId(14), ReferenceId(15), ReferenceId(16), ReferenceId(17), ReferenceId(18), ReferenceId(19), ReferenceId(20), ReferenceId(21)]
3793-
rebuilt : SymbolId(2): [ReferenceId(0), ReferenceId(1), ReferenceId(2), ReferenceId(4), ReferenceId(5), ReferenceId(6), ReferenceId(8), ReferenceId(9), ReferenceId(11), ReferenceId(12), ReferenceId(13), ReferenceId(14), ReferenceId(16), ReferenceId(17)]
3791+
after transform: SymbolId(10): [ReferenceId(9), ReferenceId(10), ReferenceId(11), ReferenceId(12), ReferenceId(13), ReferenceId(14), ReferenceId(15), ReferenceId(16), ReferenceId(17), ReferenceId(18), ReferenceId(19), ReferenceId(20), ReferenceId(21), ReferenceId(22), ReferenceId(23), ReferenceId(24)]
3792+
rebuilt : SymbolId(2): [ReferenceId(0), ReferenceId(2), ReferenceId(3), ReferenceId(5), ReferenceId(6), ReferenceId(8), ReferenceId(9), ReferenceId(11), ReferenceId(12), ReferenceId(14), ReferenceId(15), ReferenceId(16), ReferenceId(17), ReferenceId(18), ReferenceId(19), ReferenceId(21), ReferenceId(22), ReferenceId(23), ReferenceId(24)]
37943793

37953794
semantic Error: tasks/coverage/typescript/tests/cases/compiler/computedEnumMemberSyntacticallyString2.ts
37963795
Missing ReferenceId: "Foo"
37973796
Missing ReferenceId: "Foo"
37983797
Missing ReferenceId: "Foo"
3798+
Missing ReferenceId: "Foo"
3799+
Missing ReferenceId: "Foo"
37993800
Bindings mismatch:
38003801
after transform: ScopeId(1): ["A", "B", "C", "D", "E1", "E2", "F", "Foo", "G", "H", "I", "J"]
38013802
rebuilt : ScopeId(1): ["Foo"]
38023803
Scope flags mismatch:
38033804
after transform: ScopeId(1): ScopeFlags(StrictMode)
38043805
rebuilt : ScopeId(1): ScopeFlags(StrictMode | Function)
3805-
Symbol reference IDs mismatch for "BAR":
3806-
after transform: SymbolId(0): [ReferenceId(0), ReferenceId(4), ReferenceId(5), ReferenceId(6), ReferenceId(7), ReferenceId(10)]
3807-
rebuilt : SymbolId(0): [ReferenceId(12), ReferenceId(15), ReferenceId(18), ReferenceId(21), ReferenceId(26)]
38083806
Symbol flags mismatch for "Foo":
38093807
after transform: SymbolId(2): SymbolFlags(RegularEnum)
38103808
rebuilt : SymbolId(2): SymbolFlags(FunctionScopedVariable)
38113809
Symbol reference IDs mismatch for "Foo":
3812-
after transform: SymbolId(14): [ReferenceId(12), ReferenceId(13), ReferenceId(14), ReferenceId(15), ReferenceId(16), ReferenceId(17), ReferenceId(18), ReferenceId(19), ReferenceId(20), ReferenceId(21), ReferenceId(22), ReferenceId(23), ReferenceId(24), ReferenceId(25), ReferenceId(26), ReferenceId(27), ReferenceId(28), ReferenceId(29), ReferenceId(30), ReferenceId(31)]
3813-
rebuilt : SymbolId(3): [ReferenceId(0), ReferenceId(1), ReferenceId(2), ReferenceId(4), ReferenceId(5), ReferenceId(6), ReferenceId(7), ReferenceId(8), ReferenceId(9), ReferenceId(10), ReferenceId(11), ReferenceId(13), ReferenceId(14), ReferenceId(16), ReferenceId(17), ReferenceId(19), ReferenceId(20), ReferenceId(22), ReferenceId(23), ReferenceId(24), ReferenceId(25), ReferenceId(27), ReferenceId(28)]
3810+
after transform: SymbolId(14): [ReferenceId(12), ReferenceId(13), ReferenceId(14), ReferenceId(15), ReferenceId(16), ReferenceId(17), ReferenceId(18), ReferenceId(19), ReferenceId(20), ReferenceId(21), ReferenceId(22), ReferenceId(23), ReferenceId(24), ReferenceId(25), ReferenceId(26), ReferenceId(27), ReferenceId(28), ReferenceId(29), ReferenceId(30), ReferenceId(31), ReferenceId(32), ReferenceId(33)]
3811+
rebuilt : SymbolId(3): [ReferenceId(0), ReferenceId(2), ReferenceId(3), ReferenceId(5), ReferenceId(6), ReferenceId(7), ReferenceId(8), ReferenceId(9), ReferenceId(10), ReferenceId(11), ReferenceId(12), ReferenceId(14), ReferenceId(15), ReferenceId(17), ReferenceId(18), ReferenceId(20), ReferenceId(21), ReferenceId(23), ReferenceId(24), ReferenceId(25), ReferenceId(26), ReferenceId(27), ReferenceId(28), ReferenceId(30), ReferenceId(31), ReferenceId(32), ReferenceId(33)]
38143812

38153813
semantic Error: tasks/coverage/typescript/tests/cases/compiler/computedEnumTypeWidening.ts
38163814
Bindings mismatch:

tasks/transform_conformance/snapshots/oxc.snap.md

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
commit: 1d4546bc
22

3-
Passed: 180/298
3+
Passed: 180/299
44

55
# All Passed:
66
* babel-plugin-transform-class-static-block
@@ -44,7 +44,7 @@ after transform: SymbolId(0): [ReferenceId(0), ReferenceId(2), ReferenceId(6), R
4444
rebuilt : SymbolId(0): [ReferenceId(0), ReferenceId(2), ReferenceId(6), ReferenceId(10)]
4545

4646

47-
# babel-plugin-transform-typescript (5/26)
47+
# babel-plugin-transform-typescript (5/27)
4848
* allow-declare-fields-false/input.ts
4949
Unresolved references mismatch:
5050
after transform: ["dce"]
@@ -169,6 +169,41 @@ Symbol reference IDs mismatch for "NestInner":
169169
after transform: SymbolId(18): [ReferenceId(31), ReferenceId(32), ReferenceId(33), ReferenceId(34), ReferenceId(35)]
170170
rebuilt : SymbolId(9): [ReferenceId(25), ReferenceId(26), ReferenceId(28), ReferenceId(29), ReferenceId(30), ReferenceId(31)]
171171

172+
* enum-template-literal/input.ts
173+
Bindings mismatch:
174+
after transform: ScopeId(1): ["LARGE", "SMALL", "Size"]
175+
rebuilt : ScopeId(1): ["Size"]
176+
Scope flags mismatch:
177+
after transform: ScopeId(1): ScopeFlags(0x0)
178+
rebuilt : ScopeId(1): ScopeFlags(Function)
179+
Bindings mismatch:
180+
after transform: ScopeId(2): ["Animal", "CAT", "DOG"]
181+
rebuilt : ScopeId(2): ["Animal"]
182+
Scope flags mismatch:
183+
after transform: ScopeId(2): ScopeFlags(0x0)
184+
rebuilt : ScopeId(2): ScopeFlags(Function)
185+
Bindings mismatch:
186+
after transform: ScopeId(3): ["AnimalSize", "LARGE_DOG", "SMALL_CAT"]
187+
rebuilt : ScopeId(3): ["AnimalSize"]
188+
Scope flags mismatch:
189+
after transform: ScopeId(3): ScopeFlags(0x0)
190+
rebuilt : ScopeId(3): ScopeFlags(Function)
191+
Symbol flags mismatch for "Size":
192+
after transform: SymbolId(0): SymbolFlags(RegularEnum)
193+
rebuilt : SymbolId(0): SymbolFlags(FunctionScopedVariable)
194+
Symbol reference IDs mismatch for "Size":
195+
after transform: SymbolId(0): [ReferenceId(0), ReferenceId(2), ReferenceId(7)]
196+
rebuilt : SymbolId(0): [ReferenceId(3)]
197+
Symbol flags mismatch for "Animal":
198+
after transform: SymbolId(3): SymbolFlags(RegularEnum)
199+
rebuilt : SymbolId(2): SymbolFlags(FunctionScopedVariable)
200+
Symbol reference IDs mismatch for "Animal":
201+
after transform: SymbolId(3): [ReferenceId(1), ReferenceId(3), ReferenceId(11)]
202+
rebuilt : SymbolId(2): [ReferenceId(7)]
203+
Symbol flags mismatch for "AnimalSize":
204+
after transform: SymbolId(6): SymbolFlags(RegularEnum)
205+
rebuilt : SymbolId(4): SymbolFlags(FunctionScopedVariable)
206+
172207
* export-elimination/input.ts
173208
Bindings mismatch:
174209
after transform: ScopeId(0): ["Bar", "Foo", "Func", "Im", "Name", "Ok"]
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
enum Size {
2+
SMALL = "SMALL",
3+
LARGE = "LARGE",
4+
}
5+
6+
enum Animal {
7+
CAT = "CAT",
8+
DOG = "DOG",
9+
}
10+
11+
enum AnimalSize {
12+
SMALL_CAT = `${Size.SMALL}_${Animal.CAT}`,
13+
LARGE_DOG = `${Size.LARGE}_${Animal.DOG}`,
14+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
var Size = /* @__PURE__ */ function(Size) {
2+
Size["SMALL"] = "SMALL";
3+
Size["LARGE"] = "LARGE";
4+
return Size;
5+
}(Size || {});
6+
7+
var Animal = /* @__PURE__ */ function(Animal) {
8+
Animal["CAT"] = "CAT";
9+
Animal["DOG"] = "DOG";
10+
return Animal;
11+
}(Animal || {});
12+
13+
var AnimalSize = /* @__PURE__ */ function(AnimalSize) {
14+
AnimalSize["SMALL_CAT"] = "SMALL_CAT";
15+
AnimalSize["LARGE_DOG"] = "LARGE_DOG";
16+
return AnimalSize;
17+
}(AnimalSize || {});

0 commit comments

Comments
 (0)