fix(es/typescript): Support heterogeneous enums (#3721)

This commit is contained in:
magic-akari 2022-02-24 16:20:18 +08:00 committed by GitHub
parent fb561123c7
commit 1761cab6ff
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
23 changed files with 112 additions and 104 deletions

View File

@ -38,18 +38,18 @@ var E07;
(function(E07) {
E07[E07["A"] = 0] = "A";
E07[E07["B"] = 1] = "B";
E07[E07["C"] = "hi"] = "hi";
E07[E07["D"] = 10] = 10;
E07["C"] = "hi";
E07[E07["D"] = 10] = "D";
E07[E07["E"] = 11] = "E";
E07[E07["F"] = "bye"] = "bye";
E07["F"] = "bye";
})(E07 || (E07 = {}));
var E08;
(function(E08) {
E08[E08["A"] = 10] = 10;
E08[E08["B"] = "hello"] = "hello";
E08[E08["C"] = 10] = 10;
E08[E08["D"] = "hello"] = "hello";
E08[E08["E"] = 10] = 10;
E08[E08["A"] = 10] = "A";
E08["B"] = "hello";
E08[E08["C"] = 10] = "C";
E08["D"] = "hello";
E08[E08["E"] = 10] = "E";
})(E08 || (E08 = {}));
var // Examples of numeric enum types with only constant members
E10;

View File

@ -12,9 +12,9 @@ var E01, E02, E03, E04, E05, E06, E07, E08, E11, E12, E20;
})(E05 || (E05 = {})), (function(E06) {
E06.A = "one", E06.B = "two", E06.C = "three";
})(E06 || (E06 = {})), (function(E07) {
E07[E07.A = 0] = "A", E07[E07.B = 1] = "B", E07[E07.C = "hi"] = "hi", E07[E07.D = 10] = 10, E07[E07.E = 11] = "E", E07[E07.F = "bye"] = "bye";
E07[E07.A = 0] = "A", E07[E07.B = 1] = "B", E07.C = "hi", E07[E07.D = 10] = "D", E07[E07.E = 11] = "E", E07.F = "bye";
})(E07 || (E07 = {})), (function(E08) {
E08[E08.A = 10] = 10, E08[E08.B = "hello"] = "hello", E08[E08.C = 10] = 10, E08[E08.D = "hello"] = "hello", E08[E08.E = 10] = 10;
E08[E08.A = 10] = "A", E08.B = "hello", E08[E08.C = 10] = "C", E08.D = "hello", E08[E08.E = 10] = "E";
})(E08 || (E08 = {})), (function(E11) {
E11[E11.A = 0] = "A", E11[E11.B = 1] = "B", E11[E11.C = 2] = "C";
})(E11 || (E11 = {})), (function(E12) {

View File

@ -38,18 +38,18 @@ var E07;
(function(E07) {
E07[E07["A"] = 0] = "A";
E07[E07["B"] = 1] = "B";
E07[E07["C"] = "hi"] = "hi";
E07[E07["D"] = 10] = 10;
E07["C"] = "hi";
E07[E07["D"] = 10] = "D";
E07[E07["E"] = 11] = "E";
E07[E07["F"] = "bye"] = "bye";
E07["F"] = "bye";
})(E07 || (E07 = {}));
var E08;
(function(E08) {
E08[E08["A"] = 10] = 10;
E08[E08["B"] = "hello"] = "hello";
E08[E08["C"] = 10] = 10;
E08[E08["D"] = "hello"] = "hello";
E08[E08["E"] = 10] = 10;
E08[E08["A"] = 10] = "A";
E08["B"] = "hello";
E08[E08["C"] = 10] = "C";
E08["D"] = "hello";
E08[E08["E"] = 10] = "E";
})(E08 || (E08 = {}));
var // Examples of numeric enum types with only constant members
E10;

View File

@ -12,9 +12,9 @@ var E01, E02, E03, E04, E05, E06, E07, E08, E11, E12, E20;
})(E05 || (E05 = {})), (function(E06) {
E06.A = "one", E06.B = "two", E06.C = "three";
})(E06 || (E06 = {})), (function(E07) {
E07[E07.A = 0] = "A", E07[E07.B = 1] = "B", E07[E07.C = "hi"] = "hi", E07[E07.D = 10] = 10, E07[E07.E = 11] = "E", E07[E07.F = "bye"] = "bye";
E07[E07.A = 0] = "A", E07[E07.B = 1] = "B", E07.C = "hi", E07[E07.D = 10] = "D", E07[E07.E = 11] = "E", E07.F = "bye";
})(E07 || (E07 = {})), (function(E08) {
E08[E08.A = 10] = 10, E08[E08.B = "hello"] = "hello", E08[E08.C = 10] = 10, E08[E08.D = "hello"] = "hello", E08[E08.E = 10] = 10;
E08[E08.A = 10] = "A", E08.B = "hello", E08[E08.C = 10] = "C", E08.D = "hello", E08[E08.E = 10] = "E";
})(E08 || (E08 = {})), (function(E11) {
E11[E11.A = 0] = "A", E11[E11.B = 1] = "B", E11[E11.C = 2] = "C";
})(E11 || (E11 = {})), (function(E12) {

View File

@ -1,10 +1,10 @@
var T1;
(function(T1) {
T1[T1["a"] = "1"] = "1";
T1[T1["b"] = "12"] = "12";
T1[T1["c"] = "123"] = "123";
T1[T1["d"] = "a" - "a"] = "a" - "a";
T1[T1["e"] = "a1"] = "a1";
T1["a"] = "1";
T1["b"] = "12";
T1["c"] = "123";
T1[T1["d"] = "a" - "a"] = "d";
T1["e"] = "a1";
})(T1 || (T1 = {}));
var T2;
(function(T2) {
@ -13,10 +13,10 @@ var T2;
})(T2 || (T2 = {}));
var T3;
(function(T3) {
T3[T3["a"] = "1"] = "1";
T3[T3["b"] = "12"] = "12";
T3[T3["c"] = 1] = 1;
T3[T3["d"] = 3] = 3;
T3["a"] = "1";
T3["b"] = "12";
T3[T3["c"] = 1] = "c";
T3[T3["d"] = 3] = "d";
})(T3 || (T3 = {}));
var T4;
(function(T4) {

View File

@ -1,10 +1,10 @@
var T1, T2, T3, T4, T5, T6;
!function(T1) {
T1[T1.a = "1"] = "1", T1[T1.b = "12"] = "12", T1[T1.c = "123"] = "123", T1[T1.d = "a" - "a"] = "a" - "a", T1[T1.e = "a1"] = "a1";
T1.a = "1", T1.b = "12", T1.c = "123", T1[T1.d = "a" - "a"] = "d", T1.e = "a1";
}(T1 || (T1 = {})), (function(T2) {
T2.a = "1", T2.b = "12";
})(T2 || (T2 = {})), (function(T3) {
T3[T3.a = "1"] = "1", T3[T3.b = "12"] = "12", T3[T3.c = 1] = 1, T3[T3.d = 3] = 3;
T3.a = "1", T3.b = "12", T3[T3.c = 1] = "c", T3[T3.d = 3] = "d";
})(T3 || (T3 = {})), (function(T4) {
T4.a = "1";
})(T4 || (T4 = {})), (function(T5) {

View File

@ -1,10 +1,10 @@
var T1;
(function(T1) {
T1[T1["a"] = "1"] = "1";
T1[T1["b"] = "12"] = "12";
T1[T1["c"] = "123"] = "123";
T1[T1["d"] = "a" - "a"] = "a" - "a";
T1[T1["e"] = "a1"] = "a1";
T1["a"] = "1";
T1["b"] = "12";
T1["c"] = "123";
T1[T1["d"] = "a" - "a"] = "d";
T1["e"] = "a1";
})(T1 || (T1 = {}));
var T2;
(function(T2) {
@ -13,10 +13,10 @@ var T2;
})(T2 || (T2 = {}));
var T3;
(function(T3) {
T3[T3["a"] = "1"] = "1";
T3[T3["b"] = "12"] = "12";
T3[T3["c"] = 1] = 1;
T3[T3["d"] = 3] = 3;
T3["a"] = "1";
T3["b"] = "12";
T3[T3["c"] = 1] = "c";
T3[T3["d"] = 3] = "d";
})(T3 || (T3 = {}));
var T4;
(function(T4) {

View File

@ -1,10 +1,10 @@
var T1, T2, T3, T4, T5, T6;
!function(T1) {
T1[T1.a = "1"] = "1", T1[T1.b = "12"] = "12", T1[T1.c = "123"] = "123", T1[T1.d = "a" - "a"] = "a" - "a", T1[T1.e = "a1"] = "a1";
T1.a = "1", T1.b = "12", T1.c = "123", T1[T1.d = "a" - "a"] = "d", T1.e = "a1";
}(T1 || (T1 = {})), (function(T2) {
T2.a = "1", T2.b = "12";
})(T2 || (T2 = {})), (function(T3) {
T3[T3.a = "1"] = "1", T3[T3.b = "12"] = "12", T3[T3.c = 1] = 1, T3[T3.d = 3] = 3;
T3.a = "1", T3.b = "12", T3[T3.c = 1] = "c", T3[T3.d = 3] = "d";
})(T3 || (T3 = {})), (function(T4) {
T4.a = "1";
})(T4 || (T4 = {})), (function(T5) {

View File

@ -5,9 +5,9 @@ T1;
})(T1 || (T1 = {}));
var T2;
(function(T2) {
T2[T2["a"] = "1"] = "1";
T2[T2["b"] = "2"] = "2";
T2[T2["c"] = 3] = 3;
T2["a"] = "1";
T2["b"] = "2";
T2[T2["c"] = 3] = "c";
})(T2 || (T2 = {}));
var T3;
(function(T3) {
@ -23,10 +23,10 @@ var T4;
})(T4 || (T4 = {}));
var T5;
(function(T5) {
T5[T5["a"] = "1"] = "1";
T5[T5["b"] = "12"] = "12";
T5[T5["c"] = "123"] = "123";
T5[T5["d"] = 1] = 1;
T5["a"] = "1";
T5["b"] = "12";
T5["c"] = "123";
T5[T5["d"] = 1] = "d";
})(T5 || (T5 = {}));
var T6;
(function(T6) {

View File

@ -2,13 +2,13 @@ var T1, T2, T3, T4, T5, T6, T7;
!function(T1) {
T1.a = "1";
}(T1 || (T1 = {})), (function(T2) {
T2[T2.a = "1"] = "1", T2[T2.b = "2"] = "2", T2[T2.c = 3] = 3;
T2.a = "1", T2.b = "2", T2[T2.c = 3] = "c";
})(T2 || (T2 = {})), (function(T3) {
T3.a = "11";
})(T3 || (T3 = {})), (function(T4) {
T4.a = "1", T4.b = "11", T4.c = "12", T4.d = "21", T4.e = "211";
})(T4 || (T4 = {})), (function(T5) {
T5[T5.a = "1"] = "1", T5[T5.b = "12"] = "12", T5[T5.c = "123"] = "123", T5[T5.d = 1] = 1;
T5.a = "1", T5.b = "12", T5.c = "123", T5[T5.d = 1] = "d";
})(T5 || (T5 = {})), (function(T6) {
T6[T6.a = 1] = "a", T6[T6.b = 2] = "b";
})(T6 || (T6 = {})), (function(T7) {

View File

@ -5,9 +5,9 @@ T1;
})(T1 || (T1 = {}));
var T2;
(function(T2) {
T2[T2["a"] = "1"] = "1";
T2[T2["b"] = "2"] = "2";
T2[T2["c"] = 3] = 3;
T2["a"] = "1";
T2["b"] = "2";
T2[T2["c"] = 3] = "c";
})(T2 || (T2 = {}));
var T3;
(function(T3) {
@ -23,10 +23,10 @@ var T4;
})(T4 || (T4 = {}));
var T5;
(function(T5) {
T5[T5["a"] = "1"] = "1";
T5[T5["b"] = "12"] = "12";
T5[T5["c"] = "123"] = "123";
T5[T5["d"] = 1] = 1;
T5["a"] = "1";
T5["b"] = "12";
T5["c"] = "123";
T5[T5["d"] = 1] = "d";
})(T5 || (T5 = {}));
var T6;
(function(T6) {

View File

@ -2,13 +2,13 @@ var T1, T2, T3, T4, T5, T6, T7;
!function(T1) {
T1.a = "1";
}(T1 || (T1 = {})), (function(T2) {
T2[T2.a = "1"] = "1", T2[T2.b = "2"] = "2", T2[T2.c = 3] = 3;
T2.a = "1", T2.b = "2", T2[T2.c = 3] = "c";
})(T2 || (T2 = {})), (function(T3) {
T3.a = "11";
})(T3 || (T3 = {})), (function(T4) {
T4.a = "1", T4.b = "11", T4.c = "12", T4.d = "21", T4.e = "211";
})(T4 || (T4 = {})), (function(T5) {
T5[T5.a = "1"] = "1", T5[T5.b = "12"] = "12", T5[T5.c = "123"] = "123", T5[T5.d = 1] = 1;
T5.a = "1", T5.b = "12", T5.c = "123", T5[T5.d = 1] = "d";
})(T5 || (T5 = {})), (function(T6) {
T6[T6.a = 1] = "a", T6[T6.b = 2] = "b";
})(T6 || (T6 = {})), (function(T7) {

View File

@ -4,9 +4,9 @@ var T1;
})(T1 || (T1 = {}));
var T2;
(function(T2) {
T2[T2["a"] = "1"] = "1";
T2[T2["b"] = "2"] = "2";
T2[T2["c"] = 3] = 3;
T2["a"] = "1";
T2["b"] = "2";
T2[T2["c"] = 3] = "c";
})(T2 || (T2 = {}));
var T3;
(function(T3) {
@ -22,14 +22,14 @@ var T4;
})(T4 || (T4 = {}));
var T5;
(function(T5) {
T5[T5["a"] = "1"] = "1";
T5[T5["b"] = "12"] = "12";
T5[T5["c"] = "123"] = "123";
T5[T5["d"] = 1] = 1;
T5[T5["e"] = `1` - `1`] = `1` - `1`;
T5[T5["f"] = "11"] = "11";
T5[T5["g"] = `1${"2"}3`] = `1${"2"}3`;
T5[T5["h"] = `1`.length] = `1`.length;
T5["a"] = "1";
T5["b"] = "12";
T5["c"] = "123";
T5[T5["d"] = 1] = "d";
T5[T5["e"] = `1` - `1`] = "e";
T5["f"] = "11";
T5[T5["g"] = `1${"2"}3`] = "g";
T5[T5["h"] = `1`.length] = "h";
})(T5 || (T5 = {}));
var T6;
(function(T6) {

View File

@ -2,13 +2,13 @@ var T1, T2, T3, T4, T5, T6, T7;
!function(T1) {
T1.a = "1";
}(T1 || (T1 = {})), (function(T2) {
T2[T2.a = "1"] = "1", T2[T2.b = "2"] = "2", T2[T2.c = 3] = 3;
T2.a = "1", T2.b = "2", T2[T2.c = 3] = "c";
})(T2 || (T2 = {})), (function(T3) {
T3.a = "11";
})(T3 || (T3 = {})), (function(T4) {
T4.a = "1", T4.b = "11", T4.c = "12", T4.d = "21", T4.e = "211";
})(T4 || (T4 = {})), (function(T5) {
T5[T5.a = "1"] = "1", T5[T5.b = "12"] = "12", T5[T5.c = "123"] = "123", T5[T5.d = 1] = 1, T5[T5.e = "1" - "1"] = "1" - "1", T5[T5.f = "11"] = "11", T5[T5.g = "123"] = "123", T5[T5.h = 1] = 1;
T5.a = "1", T5.b = "12", T5.c = "123", T5[T5.d = 1] = "d", T5[T5.e = "1" - "1"] = "e", T5.f = "11", T5[T5.g = "123"] = "g", T5[T5.h = 1] = "h";
})(T5 || (T5 = {})), (function(T6) {
T6[T6.a = 1] = "a", T6[T6.b = 2] = "b";
})(T6 || (T6 = {})), (function(T7) {

View File

@ -4,9 +4,9 @@ var T1;
})(T1 || (T1 = {}));
var T2;
(function(T2) {
T2[T2["a"] = "1"] = "1";
T2[T2["b"] = "2"] = "2";
T2[T2["c"] = 3] = 3;
T2["a"] = "1";
T2["b"] = "2";
T2[T2["c"] = 3] = "c";
})(T2 || (T2 = {}));
var T3;
(function(T3) {
@ -22,14 +22,14 @@ var T4;
})(T4 || (T4 = {}));
var T5;
(function(T5) {
T5[T5["a"] = "1"] = "1";
T5[T5["b"] = "12"] = "12";
T5[T5["c"] = "123"] = "123";
T5[T5["d"] = 1] = 1;
T5[T5["e"] = "1" - "1"] = "1" - "1";
T5[T5["f"] = "11"] = "11";
T5[T5["g"] = "123"] = "123";
T5[T5["h"] = "1".length] = "1".length;
T5["a"] = "1";
T5["b"] = "12";
T5["c"] = "123";
T5[T5["d"] = 1] = "d";
T5[T5["e"] = "1" - "1"] = "e";
T5["f"] = "11";
T5[T5["g"] = "123"] = "g";
T5[T5["h"] = "1".length] = "h";
})(T5 || (T5 = {}));
var T6;
(function(T6) {

View File

@ -2,13 +2,13 @@ var T1, T2, T3, T4, T5, T6, T7;
!function(T1) {
T1.a = "1";
}(T1 || (T1 = {})), (function(T2) {
T2[T2.a = "1"] = "1", T2[T2.b = "2"] = "2", T2[T2.c = 3] = 3;
T2.a = "1", T2.b = "2", T2[T2.c = 3] = "c";
})(T2 || (T2 = {})), (function(T3) {
T3.a = "11";
})(T3 || (T3 = {})), (function(T4) {
T4.a = "1", T4.b = "11", T4.c = "12", T4.d = "21", T4.e = "211";
})(T4 || (T4 = {})), (function(T5) {
T5[T5.a = "1"] = "1", T5[T5.b = "12"] = "12", T5[T5.c = "123"] = "123", T5[T5.d = 1] = 1, T5[T5.e = "1" - "1"] = "1" - "1", T5[T5.f = "11"] = "11", T5[T5.g = "123"] = "123", T5[T5.h = 1] = 1;
T5.a = "1", T5.b = "12", T5.c = "123", T5[T5.d = 1] = "d", T5[T5.e = "1" - "1"] = "e", T5.f = "11", T5[T5.g = "123"] = "g", T5[T5.h = 1] = "h";
})(T5 || (T5 = {})), (function(T6) {
T6[T6.a = 1] = "a", T6[T6.b = 2] = "b";
})(T6 || (T6 = {})), (function(T7) {

View File

@ -29,8 +29,8 @@ export var H;
})(H || (H = {}));
export var I;
(function(I) {
I[I["A"] = "a"] = "a";
I[I["B"] = 0] = 0;
I["A"] = "a";
I[I["B"] = 0] = "B";
I[I["C"] = 1] = "C";
})(I || (I = {}));
export var J;

View File

@ -7,7 +7,7 @@ var B, C, DD, E, F, G, H, I, J, K, L;
}(G || (G = {})), (function(H) {
H.A = "a", H.B = "b";
})(H || (H = {})), (function(I) {
I[I.A = "a"] = "a", I[I.B = 0] = 0, I[I.C = 1] = "C";
I.A = "a", I[I.B = 0] = "B", I[I.C = 1] = "C";
})(I || (I = {})), (function(J) {
J[J.A = 1] = "A", J[J.B = 2] = "B", J[J.C = 3] = "C";
})(J || (J = {})), (function(K) {

View File

@ -29,8 +29,8 @@ export var H;
})(H || (H = {}));
export var I;
(function(I) {
I[I["A"] = "a"] = "a";
I[I["B"] = 0] = 0;
I["A"] = "a";
I[I["B"] = 0] = "B";
I[I["C"] = 1] = "C";
})(I || (I = {}));
export var J;

View File

@ -7,7 +7,7 @@ var B, C, DD, E, F, G, H, I, J, K, L;
}(G || (G = {})), (function(H) {
H.A = "a", H.B = "b";
})(H || (H = {})), (function(I) {
I[I.A = "a"] = "a", I[I.B = 0] = 0, I[I.C = 1] = "C";
I.A = "a", I[I.B = 0] = "B", I[I.C = 1] = "C";
})(I || (I = {})), (function(J) {
J[J.A = 1] = "A", J[J.B = 2] = "B", J[J.C = 3] = "C";
})(J || (J = {})), (function(K) {

View File

@ -988,18 +988,6 @@ where
.collect::<Result<Vec<_>, _>>()
.unwrap_or_else(|_| panic!("invalid value for enum is detected"));
let is_all_str = members
.iter()
.all(|(_, v)| matches!(v, Expr::Lit(Lit::Str(..))));
let no_init_required = is_all_str;
let rhs_should_be_name = members.iter().all(|(m, v): &(TsEnumMember, Expr)| match v {
Expr::Lit(Lit::Str(s)) => match &m.id {
TsEnumMemberId::Ident(i) => i.sym == s.value,
TsEnumMemberId::Str(s2) => s2.value == s.value,
},
_ => true,
});
let init = CallExpr {
span: DUMMY_SP,
callee: FnExpr {
@ -1021,6 +1009,15 @@ where
.into_iter()
.enumerate()
.map(|(_, (m, val))| {
let no_init_required = matches!(val, Expr::Lit(Lit::Str(..)));
let rhs_should_be_name = match &val {
Expr::Lit(Lit::Str(s)) => match &m.id {
TsEnumMemberId::Ident(i) => i.sym == s.value,
TsEnumMemberId::Str(s2) => s2.value == s.value,
},
_ => true,
};
let value = match m.id {
TsEnumMemberId::Str(s) => s,
TsEnumMemberId::Ident(i) => Str {

View File

@ -0,0 +1,5 @@
enum A {
X1,
X15 = "x15",
X2 = 6,
}

View File

@ -0,0 +1,6 @@
var A;
(function(A) {
A[A["X1"] = 0] = "X1";
A["X15"] = "x15";
A[A["X2"] = 6] = "X2";
})(A || (A = {}));