diff --git a/crates/swc/src/config/mod.rs b/crates/swc/src/config/mod.rs index 2902e6f9f414..7660e7c44e9c 100644 --- a/crates/swc/src/config/mod.rs +++ b/crates/swc/src/config/mod.rs @@ -53,7 +53,9 @@ use swc_ecma_transforms::{ proposals::{decorators, export_default_from, import_assertions}, react, resolver::ts_resolver, - resolver_with_mark, typescript, Assumptions, + resolver_with_mark, + typescript::{self, TSEnumConfig}, + Assumptions, }; use swc_ecma_transforms_compat::es2015::regenerator; use swc_ecma_transforms_optimization::{inline_globals2, GlobalExprMap}; @@ -468,6 +470,10 @@ impl Options { typescript::Config { pragma: Some(transform.react.pragma.clone()), pragma_frag: Some(transform.react.pragma_frag.clone()), + ts_enum_config: TSEnumConfig { + treat_const_enum_as_enum: transform.treat_const_enum_as_enum, + ts_enum_is_readonly: assumptions.ts_enum_is_readonly, + }, ..Default::default() }, comments, @@ -1170,6 +1176,9 @@ pub struct TransformConfig { #[serde(default)] pub regenerator: regenerator::Config, + + #[serde(default)] + pub treat_const_enum_as_enum: bool, } #[derive(Debug, Default, Clone, Serialize, Deserialize)] diff --git a/crates/swc/tests/fixture/issue-1515/case1/output/input.ts b/crates/swc/tests/fixture/issue-1515/case1/output/input.ts index 86706e81eaca..1dba257b80b1 100644 --- a/crates/swc/tests/fixture/issue-1515/case1/output/input.ts +++ b/crates/swc/tests/fixture/issue-1515/case1/output/input.ts @@ -25,7 +25,7 @@ exports.ServiceError = ServiceError; constructor(...args){ super(...args); // Service was probably not registered, or using the wrong channel - this.code = Code.serviceNotFound; + this.code = 404; this.name = "ServiceError.ServiceNotFound"; } } diff --git a/crates/swc/tests/fixture/ts-inline-enum/default/input/.swcrc b/crates/swc/tests/fixture/ts-inline-enum/default/input/.swcrc new file mode 100644 index 000000000000..097ae6ef11ee --- /dev/null +++ b/crates/swc/tests/fixture/ts-inline-enum/default/input/.swcrc @@ -0,0 +1,7 @@ +{ + "jsc": { + "parser": { + "syntax": "typescript" + } + } +} diff --git a/crates/swc/tests/fixture/ts-inline-enum/default/input/index.ts b/crates/swc/tests/fixture/ts-inline-enum/default/input/index.ts new file mode 100644 index 000000000000..8ea010fc7d90 --- /dev/null +++ b/crates/swc/tests/fixture/ts-inline-enum/default/input/index.ts @@ -0,0 +1,18 @@ +enum Foo { + hello = 42, +} + +enum Foo2 { + hello = "42", +} + +console.log(Foo.hello, Foo2.hello); + +console.log(Hello.en, Hello["ja-JP"], Hello[`ko-KR`], Hello['zh-CN']); + +const enum Hello { + en = "hello", + 'ja-JP' = "こんにちは", + "ko-KR" = '안녕하세요', + "zh-CN" = `你好`, +} diff --git a/crates/swc/tests/fixture/ts-inline-enum/default/output/index.ts b/crates/swc/tests/fixture/ts-inline-enum/default/output/index.ts new file mode 100644 index 000000000000..cfa4a3e755c0 --- /dev/null +++ b/crates/swc/tests/fixture/ts-inline-enum/default/output/index.ts @@ -0,0 +1,17 @@ +var Foo; +(function(Foo) { + Foo[Foo["hello"] = 42] = "hello"; +})(Foo || (Foo = {})); +var Foo2; +(function(Foo2) { + Foo2["hello"] = "42"; +})(Foo2 || (Foo2 = {})); +console.log(Foo.hello, Foo2.hello); +console.log("hello", "こんにちは", '안녕하세요', "你好"); +var Hello; +(function(Hello) { + Hello["en"] = "hello"; + Hello['ja-JP'] = "こんにちは"; + Hello["ko-KR"] = '안녕하세요'; + Hello["zh-CN"] = "你好"; +})(Hello || (Hello = {})); diff --git a/crates/swc/tests/fixture/ts-inline-enum/lhs/input/.swcrc b/crates/swc/tests/fixture/ts-inline-enum/lhs/input/.swcrc new file mode 100644 index 000000000000..097ae6ef11ee --- /dev/null +++ b/crates/swc/tests/fixture/ts-inline-enum/lhs/input/.swcrc @@ -0,0 +1,7 @@ +{ + "jsc": { + "parser": { + "syntax": "typescript" + } + } +} diff --git a/crates/swc/tests/fixture/ts-inline-enum/lhs/input/index.ts b/crates/swc/tests/fixture/ts-inline-enum/lhs/input/index.ts new file mode 100644 index 000000000000..04137fcbf7b5 --- /dev/null +++ b/crates/swc/tests/fixture/ts-inline-enum/lhs/input/index.ts @@ -0,0 +1,8 @@ +const enum Foo { + hello = 42, +} + +let x; +[x = Foo.hello] = [,]; + +console.log(x); diff --git a/crates/swc/tests/fixture/ts-inline-enum/lhs/output/index.ts b/crates/swc/tests/fixture/ts-inline-enum/lhs/output/index.ts new file mode 100644 index 000000000000..8d6268a6fbe8 --- /dev/null +++ b/crates/swc/tests/fixture/ts-inline-enum/lhs/output/index.ts @@ -0,0 +1,8 @@ +var Foo; +(function(Foo) { + Foo[Foo["hello"] = 42] = "hello"; +})(Foo || (Foo = {})); +var x; +var ref; +ref = void 0, x = ref === void 0 ? 42 : ref; +console.log(x); diff --git a/crates/swc/tests/fixture/ts-inline-enum/treatConstEnumAsEnum-2/input/.swcrc b/crates/swc/tests/fixture/ts-inline-enum/treatConstEnumAsEnum-2/input/.swcrc new file mode 100644 index 000000000000..5a3d1d08b18d --- /dev/null +++ b/crates/swc/tests/fixture/ts-inline-enum/treatConstEnumAsEnum-2/input/.swcrc @@ -0,0 +1,10 @@ +{ + "jsc": { + "parser": { + "syntax": "typescript" + }, + "transform": { + "treatConstEnumAsEnum": false + } + } +} diff --git a/crates/swc/tests/fixture/ts-inline-enum/treatConstEnumAsEnum-2/input/index.ts b/crates/swc/tests/fixture/ts-inline-enum/treatConstEnumAsEnum-2/input/index.ts new file mode 100644 index 000000000000..90835253541d --- /dev/null +++ b/crates/swc/tests/fixture/ts-inline-enum/treatConstEnumAsEnum-2/input/index.ts @@ -0,0 +1,18 @@ +enum Foo { + hello = 42, +} + +enum Foo2 { + hello = "42", +} + +const enum Bar { + hello = 42, +} + +const enum Bar2 { + hello = "42", +} + +console.log(Foo.hello, Foo2.hello); +console.log(Bar.hello, Bar2.hello); diff --git a/crates/swc/tests/fixture/ts-inline-enum/treatConstEnumAsEnum-2/output/index.ts b/crates/swc/tests/fixture/ts-inline-enum/treatConstEnumAsEnum-2/output/index.ts new file mode 100644 index 000000000000..94e0d2630e06 --- /dev/null +++ b/crates/swc/tests/fixture/ts-inline-enum/treatConstEnumAsEnum-2/output/index.ts @@ -0,0 +1,18 @@ +var Foo; +(function(Foo) { + Foo[Foo["hello"] = 42] = "hello"; +})(Foo || (Foo = {})); +var Foo2; +(function(Foo2) { + Foo2["hello"] = "42"; +})(Foo2 || (Foo2 = {})); +var Bar; +(function(Bar) { + Bar[Bar["hello"] = 42] = "hello"; +})(Bar || (Bar = {})); +var Bar2; +(function(Bar2) { + Bar2["hello"] = "42"; +})(Bar2 || (Bar2 = {})); +console.log(Foo.hello, Foo2.hello); +console.log(42, "42"); diff --git a/crates/swc/tests/fixture/ts-inline-enum/treatConstEnumAsEnum/input/.swcrc b/crates/swc/tests/fixture/ts-inline-enum/treatConstEnumAsEnum/input/.swcrc new file mode 100644 index 000000000000..a92b42429529 --- /dev/null +++ b/crates/swc/tests/fixture/ts-inline-enum/treatConstEnumAsEnum/input/.swcrc @@ -0,0 +1,10 @@ +{ + "jsc": { + "parser": { + "syntax": "typescript" + }, + "transform": { + "treatConstEnumAsEnum": true + } + } +} diff --git a/crates/swc/tests/fixture/ts-inline-enum/treatConstEnumAsEnum/input/index.ts b/crates/swc/tests/fixture/ts-inline-enum/treatConstEnumAsEnum/input/index.ts new file mode 100644 index 000000000000..90835253541d --- /dev/null +++ b/crates/swc/tests/fixture/ts-inline-enum/treatConstEnumAsEnum/input/index.ts @@ -0,0 +1,18 @@ +enum Foo { + hello = 42, +} + +enum Foo2 { + hello = "42", +} + +const enum Bar { + hello = 42, +} + +const enum Bar2 { + hello = "42", +} + +console.log(Foo.hello, Foo2.hello); +console.log(Bar.hello, Bar2.hello); diff --git a/crates/swc/tests/fixture/ts-inline-enum/treatConstEnumAsEnum/output/index.ts b/crates/swc/tests/fixture/ts-inline-enum/treatConstEnumAsEnum/output/index.ts new file mode 100644 index 000000000000..cea9922cd91b --- /dev/null +++ b/crates/swc/tests/fixture/ts-inline-enum/treatConstEnumAsEnum/output/index.ts @@ -0,0 +1,18 @@ +var Foo; +(function(Foo) { + Foo[Foo["hello"] = 42] = "hello"; +})(Foo || (Foo = {})); +var Foo2; +(function(Foo2) { + Foo2["hello"] = "42"; +})(Foo2 || (Foo2 = {})); +var Bar; +(function(Bar) { + Bar[Bar["hello"] = 42] = "hello"; +})(Bar || (Bar = {})); +var Bar2; +(function(Bar2) { + Bar2["hello"] = "42"; +})(Bar2 || (Bar2 = {})); +console.log(Foo.hello, Foo2.hello); +console.log(Bar.hello, Bar2.hello); diff --git a/crates/swc/tests/tsc-references/constEnum3_es2015.1.normal.js b/crates/swc/tests/tsc-references/constEnum3_es2015.1.normal.js index f9ce8daabcba..24953802a35d 100644 --- a/crates/swc/tests/tsc-references/constEnum3_es2015.1.normal.js +++ b/crates/swc/tests/tsc-references/constEnum3_es2015.1.normal.js @@ -5,7 +5,7 @@ var TestType; })(TestType || (TestType = {})); function f1(f) {} function f2(f) {} -f1(TestType.foo); -f1(TestType.bar); +f1(0); +f1(1); f2('foo'); f2('bar'); diff --git a/crates/swc/tests/tsc-references/constEnum3_es2015.2.minified.js b/crates/swc/tests/tsc-references/constEnum3_es2015.2.minified.js index 6b8e8b83cff0..0092d37ed31e 100644 --- a/crates/swc/tests/tsc-references/constEnum3_es2015.2.minified.js +++ b/crates/swc/tests/tsc-references/constEnum3_es2015.2.minified.js @@ -3,4 +3,4 @@ function f1(f) {} function f2(f) {} !function(TestType) { TestType[TestType.foo = 0] = "foo", TestType[TestType.bar = 1] = "bar"; -}(TestType || (TestType = {})), f1(TestType.foo), f1(TestType.bar), f2("foo"), f2("bar"); +}(TestType || (TestType = {})), f1(0), f1(1), f2("foo"), f2("bar"); diff --git a/crates/swc/tests/tsc-references/constEnum3_es5.1.normal.js b/crates/swc/tests/tsc-references/constEnum3_es5.1.normal.js index f9ce8daabcba..24953802a35d 100644 --- a/crates/swc/tests/tsc-references/constEnum3_es5.1.normal.js +++ b/crates/swc/tests/tsc-references/constEnum3_es5.1.normal.js @@ -5,7 +5,7 @@ var TestType; })(TestType || (TestType = {})); function f1(f) {} function f2(f) {} -f1(TestType.foo); -f1(TestType.bar); +f1(0); +f1(1); f2('foo'); f2('bar'); diff --git a/crates/swc/tests/tsc-references/constEnum3_es5.2.minified.js b/crates/swc/tests/tsc-references/constEnum3_es5.2.minified.js index 6b8e8b83cff0..0092d37ed31e 100644 --- a/crates/swc/tests/tsc-references/constEnum3_es5.2.minified.js +++ b/crates/swc/tests/tsc-references/constEnum3_es5.2.minified.js @@ -3,4 +3,4 @@ function f1(f) {} function f2(f) {} !function(TestType) { TestType[TestType.foo = 0] = "foo", TestType[TestType.bar = 1] = "bar"; -}(TestType || (TestType = {})), f1(TestType.foo), f1(TestType.bar), f2("foo"), f2("bar"); +}(TestType || (TestType = {})), f1(0), f1(1), f2("foo"), f2("bar"); diff --git a/crates/swc/tests/tsc-references/constEnumPropertyAccess1_es2015.1.normal.js b/crates/swc/tests/tsc-references/constEnumPropertyAccess1_es2015.1.normal.js index cb26c6a5deda..d4e0e0a939ce 100644 --- a/crates/swc/tests/tsc-references/constEnumPropertyAccess1_es2015.1.normal.js +++ b/crates/swc/tests/tsc-references/constEnumPropertyAccess1_es2015.1.normal.js @@ -13,10 +13,10 @@ G; var o = { 1: true }; -var a = G.A; -var a1 = G["A"]; -var g = o[G.A]; -var tmp = G.A, tmp1 = G.B, tmp2 = G.B; +var a = 1; +var a1 = 1; +var g = o[1]; +var tmp = 1, tmp1 = 2, tmp2 = 2; class C { [tmp]() {} get [tmp1]() { diff --git a/crates/swc/tests/tsc-references/constEnumPropertyAccess1_es2015.2.minified.js b/crates/swc/tests/tsc-references/constEnumPropertyAccess1_es2015.2.minified.js index 809f404c23ae..1b8518bb3128 100644 --- a/crates/swc/tests/tsc-references/constEnumPropertyAccess1_es2015.2.minified.js +++ b/crates/swc/tests/tsc-references/constEnumPropertyAccess1_es2015.2.minified.js @@ -1,6 +1,6 @@ var G; -(function(G) { +!function(G) { G[G.A = 1] = "A", G[G.B = 2] = "B", G[G.C = 3] = "C", G[G.D = 2] = "D"; -})(G || (G = {})), G.A, G.A, ({ +}(G || (G = {})), ({ 1: !0 -})[G.A], G.A, G.B, G.B; +})[1]; diff --git a/crates/swc/tests/tsc-references/constEnumPropertyAccess1_es5.1.normal.js b/crates/swc/tests/tsc-references/constEnumPropertyAccess1_es5.1.normal.js index 99f49284d1f2..c6a262169df8 100644 --- a/crates/swc/tests/tsc-references/constEnumPropertyAccess1_es5.1.normal.js +++ b/crates/swc/tests/tsc-references/constEnumPropertyAccess1_es5.1.normal.js @@ -32,10 +32,10 @@ G; var o = { 1: true }; -var a = G.A; -var a1 = G["A"]; -var g = o[G.A]; -var tmp = G.A, tmp1 = G.B, tmp2 = G.B; +var a = 1; +var a1 = 1; +var g = o[1]; +var tmp = 1, tmp1 = 2, tmp2 = 2; var C = /*#__PURE__*/ function() { "use strict"; function C() { diff --git a/crates/swc/tests/tsc-references/constEnumPropertyAccess1_es5.2.minified.js b/crates/swc/tests/tsc-references/constEnumPropertyAccess1_es5.2.minified.js index 00f56c5c97ca..358fac227748 100644 --- a/crates/swc/tests/tsc-references/constEnumPropertyAccess1_es5.2.minified.js +++ b/crates/swc/tests/tsc-references/constEnumPropertyAccess1_es5.2.minified.js @@ -6,10 +6,10 @@ function _defineProperties(target, props) { } !function(G) { G[G.A = 1] = "A", G[G.B = 2] = "B", G[G.C = 3] = "C", G[G.D = 2] = "D"; -}(G || (G = {})), G.A, G.A, ({ +}(G || (G = {})), ({ 1: !0 -})[G.A]; -var G, tmp = G.A, tmp1 = G.B, tmp2 = G.B, C = function() { +})[1]; +var G, C = function() { "use strict"; var Constructor, protoProps, staticProps; function C() { @@ -19,17 +19,17 @@ var G, tmp = G.A, tmp1 = G.B, tmp2 = G.B, C = function() { } return Constructor = C, protoProps = [ { - key: tmp, + key: 1, value: function() {} }, { - key: tmp1, + key: 2, get: function() { return !0; } }, { - key: tmp2, + key: 2, set: function(x) {} } ], _defineProperties(Constructor.prototype, protoProps), staticProps && _defineProperties(Constructor, staticProps), C; diff --git a/crates/swc/tests/tsc-references/constEnumPropertyAccess2_es2015.1.normal.js b/crates/swc/tests/tsc-references/constEnumPropertyAccess2_es2015.1.normal.js index 98fba94ff895..970410132faf 100644 --- a/crates/swc/tests/tsc-references/constEnumPropertyAccess2_es2015.1.normal.js +++ b/crates/swc/tests/tsc-references/constEnumPropertyAccess2_es2015.1.normal.js @@ -11,7 +11,7 @@ G; })(G || (G = {})); // Error from referring constant enum in any other context than a property access var z = G; -var z1 = G[G.A]; +var z1 = G[1]; var g; g = "string"; function foo(x) {} diff --git a/crates/swc/tests/tsc-references/constEnumPropertyAccess2_es2015.2.minified.js b/crates/swc/tests/tsc-references/constEnumPropertyAccess2_es2015.2.minified.js index b8892ea9ec9f..149e5d0953d3 100644 --- a/crates/swc/tests/tsc-references/constEnumPropertyAccess2_es2015.2.minified.js +++ b/crates/swc/tests/tsc-references/constEnumPropertyAccess2_es2015.2.minified.js @@ -1,4 +1,4 @@ var G; (function(G) { G[G.A = 1] = "A", G[G.B = 2] = "B", G[G.C = 3] = "C", G[G.D = 2] = "D"; -})(G || (G = {})), G[G.A], G.B = 3; +})(G || (G = {})), G[1], G.B = 3; diff --git a/crates/swc/tests/tsc-references/constEnumPropertyAccess2_es5.1.normal.js b/crates/swc/tests/tsc-references/constEnumPropertyAccess2_es5.1.normal.js index 98fba94ff895..970410132faf 100644 --- a/crates/swc/tests/tsc-references/constEnumPropertyAccess2_es5.1.normal.js +++ b/crates/swc/tests/tsc-references/constEnumPropertyAccess2_es5.1.normal.js @@ -11,7 +11,7 @@ G; })(G || (G = {})); // Error from referring constant enum in any other context than a property access var z = G; -var z1 = G[G.A]; +var z1 = G[1]; var g; g = "string"; function foo(x) {} diff --git a/crates/swc/tests/tsc-references/constEnumPropertyAccess2_es5.2.minified.js b/crates/swc/tests/tsc-references/constEnumPropertyAccess2_es5.2.minified.js index b8892ea9ec9f..149e5d0953d3 100644 --- a/crates/swc/tests/tsc-references/constEnumPropertyAccess2_es5.2.minified.js +++ b/crates/swc/tests/tsc-references/constEnumPropertyAccess2_es5.2.minified.js @@ -1,4 +1,4 @@ var G; (function(G) { G[G.A = 1] = "A", G[G.B = 2] = "B", G[G.C = 3] = "C", G[G.D = 2] = "D"; -})(G || (G = {})), G[G.A], G.B = 3; +})(G || (G = {})), G[1], G.B = 3; diff --git a/crates/swc/tests/tsc-references/destructuringParameterDeclaration3ES5_es2015.1.normal.js b/crates/swc/tests/tsc-references/destructuringParameterDeclaration3ES5_es2015.1.normal.js index 94546eeafe3e..054d60ad4806 100644 --- a/crates/swc/tests/tsc-references/destructuringParameterDeclaration3ES5_es2015.1.normal.js +++ b/crates/swc/tests/tsc-references/destructuringParameterDeclaration3ES5_es2015.1.normal.js @@ -73,4 +73,4 @@ var E1; })(E1 || (E1 = {})); function foo1(...a) {} foo1(1, 2, 3, E.a); -foo1(1, 2, 3, E1.a, E.b); +foo1(1, 2, 3, 0, E.b); diff --git a/crates/swc/tests/tsc-references/destructuringParameterDeclaration3ES5_es2015.2.minified.js b/crates/swc/tests/tsc-references/destructuringParameterDeclaration3ES5_es2015.2.minified.js index 0d9df2eb8a89..eab9b7740f14 100644 --- a/crates/swc/tests/tsc-references/destructuringParameterDeclaration3ES5_es2015.2.minified.js +++ b/crates/swc/tests/tsc-references/destructuringParameterDeclaration3ES5_es2015.2.minified.js @@ -25,4 +25,4 @@ a10([ E[E.a = 0] = "a", E[E.b = 1] = "b"; })(E || (E = {})), (function(E1) { E1[E1.a = 0] = "a", E1[E1.b = 1] = "b"; -})(E1 || (E1 = {})), foo1(1, 2, 3, E.a), foo1(1, 2, 3, E1.a, E.b); +})(E1 || (E1 = {})), foo1(1, 2, 3, E.a), foo1(1, 2, 3, 0, E.b); diff --git a/crates/swc/tests/tsc-references/destructuringParameterDeclaration3ES5_es5.1.normal.js b/crates/swc/tests/tsc-references/destructuringParameterDeclaration3ES5_es5.1.normal.js index ab8e0ee25f82..6c6db7ac3e60 100644 --- a/crates/swc/tests/tsc-references/destructuringParameterDeclaration3ES5_es5.1.normal.js +++ b/crates/swc/tests/tsc-references/destructuringParameterDeclaration3ES5_es5.1.normal.js @@ -166,4 +166,4 @@ function foo1() { } } foo1(1, 2, 3, E.a); -foo1(1, 2, 3, E1.a, E.b); +foo1(1, 2, 3, 0, E.b); diff --git a/crates/swc/tests/tsc-references/destructuringParameterDeclaration3ES5_es5.2.minified.js b/crates/swc/tests/tsc-references/destructuringParameterDeclaration3ES5_es5.2.minified.js index 38fc41d15ace..9f9f60bd36f5 100644 --- a/crates/swc/tests/tsc-references/destructuringParameterDeclaration3ES5_es5.2.minified.js +++ b/crates/swc/tests/tsc-references/destructuringParameterDeclaration3ES5_es5.2.minified.js @@ -111,4 +111,4 @@ function foo1() { E[E.a = 0] = "a", E[E.b = 1] = "b"; })(E || (E = {})), (function(E1) { E1[E1.a = 0] = "a", E1[E1.b = 1] = "b"; -})(E1 || (E1 = {})), foo1(1, 2, 3, E.a), foo1(1, 2, 3, E1.a, E.b); +})(E1 || (E1 = {})), foo1(1, 2, 3, E.a), foo1(1, 2, 3, 0, E.b); diff --git a/crates/swc/tests/tsc-references/destructuringParameterDeclaration3ES5iterable_es2015.1.normal.js b/crates/swc/tests/tsc-references/destructuringParameterDeclaration3ES5iterable_es2015.1.normal.js index 94546eeafe3e..054d60ad4806 100644 --- a/crates/swc/tests/tsc-references/destructuringParameterDeclaration3ES5iterable_es2015.1.normal.js +++ b/crates/swc/tests/tsc-references/destructuringParameterDeclaration3ES5iterable_es2015.1.normal.js @@ -73,4 +73,4 @@ var E1; })(E1 || (E1 = {})); function foo1(...a) {} foo1(1, 2, 3, E.a); -foo1(1, 2, 3, E1.a, E.b); +foo1(1, 2, 3, 0, E.b); diff --git a/crates/swc/tests/tsc-references/destructuringParameterDeclaration3ES5iterable_es2015.2.minified.js b/crates/swc/tests/tsc-references/destructuringParameterDeclaration3ES5iterable_es2015.2.minified.js index 0d9df2eb8a89..eab9b7740f14 100644 --- a/crates/swc/tests/tsc-references/destructuringParameterDeclaration3ES5iterable_es2015.2.minified.js +++ b/crates/swc/tests/tsc-references/destructuringParameterDeclaration3ES5iterable_es2015.2.minified.js @@ -25,4 +25,4 @@ a10([ E[E.a = 0] = "a", E[E.b = 1] = "b"; })(E || (E = {})), (function(E1) { E1[E1.a = 0] = "a", E1[E1.b = 1] = "b"; -})(E1 || (E1 = {})), foo1(1, 2, 3, E.a), foo1(1, 2, 3, E1.a, E.b); +})(E1 || (E1 = {})), foo1(1, 2, 3, E.a), foo1(1, 2, 3, 0, E.b); diff --git a/crates/swc/tests/tsc-references/destructuringParameterDeclaration3ES5iterable_es5.1.normal.js b/crates/swc/tests/tsc-references/destructuringParameterDeclaration3ES5iterable_es5.1.normal.js index ab8e0ee25f82..6c6db7ac3e60 100644 --- a/crates/swc/tests/tsc-references/destructuringParameterDeclaration3ES5iterable_es5.1.normal.js +++ b/crates/swc/tests/tsc-references/destructuringParameterDeclaration3ES5iterable_es5.1.normal.js @@ -166,4 +166,4 @@ function foo1() { } } foo1(1, 2, 3, E.a); -foo1(1, 2, 3, E1.a, E.b); +foo1(1, 2, 3, 0, E.b); diff --git a/crates/swc/tests/tsc-references/destructuringParameterDeclaration3ES5iterable_es5.2.minified.js b/crates/swc/tests/tsc-references/destructuringParameterDeclaration3ES5iterable_es5.2.minified.js index 38fc41d15ace..9f9f60bd36f5 100644 --- a/crates/swc/tests/tsc-references/destructuringParameterDeclaration3ES5iterable_es5.2.minified.js +++ b/crates/swc/tests/tsc-references/destructuringParameterDeclaration3ES5iterable_es5.2.minified.js @@ -111,4 +111,4 @@ function foo1() { E[E.a = 0] = "a", E[E.b = 1] = "b"; })(E || (E = {})), (function(E1) { E1[E1.a = 0] = "a", E1[E1.b = 1] = "b"; -})(E1 || (E1 = {})), foo1(1, 2, 3, E.a), foo1(1, 2, 3, E1.a, E.b); +})(E1 || (E1 = {})), foo1(1, 2, 3, E.a), foo1(1, 2, 3, 0, E.b); diff --git a/crates/swc/tests/tsc-references/destructuringParameterDeclaration3ES6_es2015.1.normal.js b/crates/swc/tests/tsc-references/destructuringParameterDeclaration3ES6_es2015.1.normal.js index 94546eeafe3e..054d60ad4806 100644 --- a/crates/swc/tests/tsc-references/destructuringParameterDeclaration3ES6_es2015.1.normal.js +++ b/crates/swc/tests/tsc-references/destructuringParameterDeclaration3ES6_es2015.1.normal.js @@ -73,4 +73,4 @@ var E1; })(E1 || (E1 = {})); function foo1(...a) {} foo1(1, 2, 3, E.a); -foo1(1, 2, 3, E1.a, E.b); +foo1(1, 2, 3, 0, E.b); diff --git a/crates/swc/tests/tsc-references/destructuringParameterDeclaration3ES6_es2015.2.minified.js b/crates/swc/tests/tsc-references/destructuringParameterDeclaration3ES6_es2015.2.minified.js index 0d9df2eb8a89..eab9b7740f14 100644 --- a/crates/swc/tests/tsc-references/destructuringParameterDeclaration3ES6_es2015.2.minified.js +++ b/crates/swc/tests/tsc-references/destructuringParameterDeclaration3ES6_es2015.2.minified.js @@ -25,4 +25,4 @@ a10([ E[E.a = 0] = "a", E[E.b = 1] = "b"; })(E || (E = {})), (function(E1) { E1[E1.a = 0] = "a", E1[E1.b = 1] = "b"; -})(E1 || (E1 = {})), foo1(1, 2, 3, E.a), foo1(1, 2, 3, E1.a, E.b); +})(E1 || (E1 = {})), foo1(1, 2, 3, E.a), foo1(1, 2, 3, 0, E.b); diff --git a/crates/swc/tests/tsc-references/destructuringParameterDeclaration3ES6_es5.1.normal.js b/crates/swc/tests/tsc-references/destructuringParameterDeclaration3ES6_es5.1.normal.js index ab8e0ee25f82..6c6db7ac3e60 100644 --- a/crates/swc/tests/tsc-references/destructuringParameterDeclaration3ES6_es5.1.normal.js +++ b/crates/swc/tests/tsc-references/destructuringParameterDeclaration3ES6_es5.1.normal.js @@ -166,4 +166,4 @@ function foo1() { } } foo1(1, 2, 3, E.a); -foo1(1, 2, 3, E1.a, E.b); +foo1(1, 2, 3, 0, E.b); diff --git a/crates/swc/tests/tsc-references/destructuringParameterDeclaration3ES6_es5.2.minified.js b/crates/swc/tests/tsc-references/destructuringParameterDeclaration3ES6_es5.2.minified.js index 38fc41d15ace..9f9f60bd36f5 100644 --- a/crates/swc/tests/tsc-references/destructuringParameterDeclaration3ES6_es5.2.minified.js +++ b/crates/swc/tests/tsc-references/destructuringParameterDeclaration3ES6_es5.2.minified.js @@ -111,4 +111,4 @@ function foo1() { E[E.a = 0] = "a", E[E.b = 1] = "b"; })(E || (E = {})), (function(E1) { E1[E1.a = 0] = "a", E1[E1.b = 1] = "b"; -})(E1 || (E1 = {})), foo1(1, 2, 3, E.a), foo1(1, 2, 3, E1.a, E.b); +})(E1 || (E1 = {})), foo1(1, 2, 3, E.a), foo1(1, 2, 3, 0, E.b); diff --git a/crates/swc/tests/tsc-references/enumLiteralTypes1_es2015.1.normal.js b/crates/swc/tests/tsc-references/enumLiteralTypes1_es2015.1.normal.js index 8b125f7faf5e..5ce2c5479044 100644 --- a/crates/swc/tests/tsc-references/enumLiteralTypes1_es2015.1.normal.js +++ b/crates/swc/tests/tsc-references/enumLiteralTypes1_es2015.1.normal.js @@ -41,8 +41,8 @@ function f4(a, b) { b++; } function f5(a, b, c) { - var z1 = g(Choice.Yes); - var z2 = g(Choice.No); + var z1 = g(1); + var z2 = g(2); var z3 = g(a); var z4 = g(b); var z5 = g(c); @@ -52,17 +52,17 @@ function assertNever(x) { } function f10(x) { switch(x){ - case Choice.Yes: + case 1: return "true"; - case Choice.No: + case 2: return "false"; } } function f11(x) { switch(x){ - case Choice.Yes: + case 1: return "true"; - case Choice.No: + case 2: return "false"; } return assertNever(x); @@ -75,7 +75,7 @@ function f12(x) { } } function f13(x) { - if (x === Choice.Yes) { + if (x === 1) { x; } else { x; @@ -83,17 +83,17 @@ function f13(x) { } function f20(x) { switch(x.kind){ - case Choice.Yes: + case 1: return x.a; - case Choice.No: + case 2: return x.b; } } function f21(x) { switch(x.kind){ - case Choice.Yes: + case 1: return x.a; - case Choice.No: + case 2: return x.b; } return assertNever(x); diff --git a/crates/swc/tests/tsc-references/enumLiteralTypes1_es5.1.normal.js b/crates/swc/tests/tsc-references/enumLiteralTypes1_es5.1.normal.js index 8b125f7faf5e..5ce2c5479044 100644 --- a/crates/swc/tests/tsc-references/enumLiteralTypes1_es5.1.normal.js +++ b/crates/swc/tests/tsc-references/enumLiteralTypes1_es5.1.normal.js @@ -41,8 +41,8 @@ function f4(a, b) { b++; } function f5(a, b, c) { - var z1 = g(Choice.Yes); - var z2 = g(Choice.No); + var z1 = g(1); + var z2 = g(2); var z3 = g(a); var z4 = g(b); var z5 = g(c); @@ -52,17 +52,17 @@ function assertNever(x) { } function f10(x) { switch(x){ - case Choice.Yes: + case 1: return "true"; - case Choice.No: + case 2: return "false"; } } function f11(x) { switch(x){ - case Choice.Yes: + case 1: return "true"; - case Choice.No: + case 2: return "false"; } return assertNever(x); @@ -75,7 +75,7 @@ function f12(x) { } } function f13(x) { - if (x === Choice.Yes) { + if (x === 1) { x; } else { x; @@ -83,17 +83,17 @@ function f13(x) { } function f20(x) { switch(x.kind){ - case Choice.Yes: + case 1: return x.a; - case Choice.No: + case 2: return x.b; } } function f21(x) { switch(x.kind){ - case Choice.Yes: + case 1: return x.a; - case Choice.No: + case 2: return x.b; } return assertNever(x); diff --git a/crates/swc/tests/tsc-references/enumLiteralTypes2_es2015.1.normal.js b/crates/swc/tests/tsc-references/enumLiteralTypes2_es2015.1.normal.js index 9567b55da6a3..025b0721fb7f 100644 --- a/crates/swc/tests/tsc-references/enumLiteralTypes2_es2015.1.normal.js +++ b/crates/swc/tests/tsc-references/enumLiteralTypes2_es2015.1.normal.js @@ -42,8 +42,8 @@ function f4(a, b) { b++; } function f5(a, b, c) { - var z1 = g(Choice.Yes); - var z2 = g(Choice.No); + var z1 = g(1); + var z2 = g(2); var z3 = g(a); var z4 = g(b); var z5 = g(c); @@ -53,17 +53,17 @@ function assertNever(x) { } function f10(x) { switch(x){ - case Choice.Yes: + case 1: return "true"; - case Choice.No: + case 2: return "false"; } } function f11(x) { switch(x){ - case Choice.Yes: + case 1: return "true"; - case Choice.No: + case 2: return "false"; } return assertNever(x); @@ -76,7 +76,7 @@ function f12(x) { } } function f13(x) { - if (x === Choice.Yes) { + if (x === 1) { x; } else { x; @@ -84,17 +84,17 @@ function f13(x) { } function f20(x) { switch(x.kind){ - case Choice.Yes: + case 1: return x.a; - case Choice.No: + case 2: return x.b; } } function f21(x) { switch(x.kind){ - case Choice.Yes: + case 1: return x.a; - case Choice.No: + case 2: return x.b; } return assertNever(x); diff --git a/crates/swc/tests/tsc-references/enumLiteralTypes2_es5.1.normal.js b/crates/swc/tests/tsc-references/enumLiteralTypes2_es5.1.normal.js index 9567b55da6a3..025b0721fb7f 100644 --- a/crates/swc/tests/tsc-references/enumLiteralTypes2_es5.1.normal.js +++ b/crates/swc/tests/tsc-references/enumLiteralTypes2_es5.1.normal.js @@ -42,8 +42,8 @@ function f4(a, b) { b++; } function f5(a, b, c) { - var z1 = g(Choice.Yes); - var z2 = g(Choice.No); + var z1 = g(1); + var z2 = g(2); var z3 = g(a); var z4 = g(b); var z5 = g(c); @@ -53,17 +53,17 @@ function assertNever(x) { } function f10(x) { switch(x){ - case Choice.Yes: + case 1: return "true"; - case Choice.No: + case 2: return "false"; } } function f11(x) { switch(x){ - case Choice.Yes: + case 1: return "true"; - case Choice.No: + case 2: return "false"; } return assertNever(x); @@ -76,7 +76,7 @@ function f12(x) { } } function f13(x) { - if (x === Choice.Yes) { + if (x === 1) { x; } else { x; @@ -84,17 +84,17 @@ function f13(x) { } function f20(x) { switch(x.kind){ - case Choice.Yes: + case 1: return x.a; - case Choice.No: + case 2: return x.b; } } function f21(x) { switch(x.kind){ - case Choice.Yes: + case 1: return x.a; - case Choice.No: + case 2: return x.b; } return assertNever(x); diff --git a/crates/swc/tests/tsc-references/enumLiteralTypes3_es2015.1.normal.js b/crates/swc/tests/tsc-references/enumLiteralTypes3_es2015.1.normal.js index ec34f71cfedd..96d2c1973499 100644 --- a/crates/swc/tests/tsc-references/enumLiteralTypes3_es2015.1.normal.js +++ b/crates/swc/tests/tsc-references/enumLiteralTypes3_es2015.1.normal.js @@ -29,32 +29,32 @@ function f4(a, b, c, d) { d = d; } function f5(a, b, c, d) { - a = Choice.Unknown; - a = Choice.Yes; - a = Choice.No; - b = Choice.Unknown; - b = Choice.Yes; - b = Choice.No; - c = Choice.Unknown; - c = Choice.Yes; - c = Choice.No; - d = Choice.Unknown; - d = Choice.Yes; - d = Choice.No; + a = 0; + a = 1; + a = 2; + b = 0; + b = 1; + b = 2; + c = 0; + c = 1; + c = 2; + d = 0; + d = 1; + d = 2; } function f6(a, b, c, d) { - a === Choice.Unknown; - a === Choice.Yes; - a === Choice.No; - b === Choice.Unknown; - b === Choice.Yes; - b === Choice.No; - c === Choice.Unknown; - c === Choice.Yes; - c === Choice.No; - d === Choice.Unknown; - d === Choice.Yes; - d === Choice.No; + a === 0; + a === 1; + a === 2; + b === 0; + b === 1; + b === 2; + c === 0; + c === 1; + c === 2; + d === 0; + d === 1; + d === 2; } function f7(a, b, c, d) { a === a; @@ -76,44 +76,44 @@ function f7(a, b, c, d) { } function f10(x) { switch(x){ - case Choice.Unknown: + case 0: return x; - case Choice.Yes: + case 1: return x; - case Choice.No: + case 2: return x; } return x; } function f11(x) { switch(x){ - case Choice.Unknown: + case 0: return x; - case Choice.Yes: + case 1: return x; - case Choice.No: + case 2: return x; } return x; } function f12(x) { switch(x){ - case Choice.Unknown: + case 0: return x; - case Choice.Yes: + case 1: return x; - case Choice.No: + case 2: return x; } return x; } function f13(x) { switch(x){ - case Choice.Unknown: + case 0: return x; - case Choice.Yes: + case 1: return x; - case Choice.No: + case 2: return x; } return x; diff --git a/crates/swc/tests/tsc-references/enumLiteralTypes3_es5.1.normal.js b/crates/swc/tests/tsc-references/enumLiteralTypes3_es5.1.normal.js index ec34f71cfedd..96d2c1973499 100644 --- a/crates/swc/tests/tsc-references/enumLiteralTypes3_es5.1.normal.js +++ b/crates/swc/tests/tsc-references/enumLiteralTypes3_es5.1.normal.js @@ -29,32 +29,32 @@ function f4(a, b, c, d) { d = d; } function f5(a, b, c, d) { - a = Choice.Unknown; - a = Choice.Yes; - a = Choice.No; - b = Choice.Unknown; - b = Choice.Yes; - b = Choice.No; - c = Choice.Unknown; - c = Choice.Yes; - c = Choice.No; - d = Choice.Unknown; - d = Choice.Yes; - d = Choice.No; + a = 0; + a = 1; + a = 2; + b = 0; + b = 1; + b = 2; + c = 0; + c = 1; + c = 2; + d = 0; + d = 1; + d = 2; } function f6(a, b, c, d) { - a === Choice.Unknown; - a === Choice.Yes; - a === Choice.No; - b === Choice.Unknown; - b === Choice.Yes; - b === Choice.No; - c === Choice.Unknown; - c === Choice.Yes; - c === Choice.No; - d === Choice.Unknown; - d === Choice.Yes; - d === Choice.No; + a === 0; + a === 1; + a === 2; + b === 0; + b === 1; + b === 2; + c === 0; + c === 1; + c === 2; + d === 0; + d === 1; + d === 2; } function f7(a, b, c, d) { a === a; @@ -76,44 +76,44 @@ function f7(a, b, c, d) { } function f10(x) { switch(x){ - case Choice.Unknown: + case 0: return x; - case Choice.Yes: + case 1: return x; - case Choice.No: + case 2: return x; } return x; } function f11(x) { switch(x){ - case Choice.Unknown: + case 0: return x; - case Choice.Yes: + case 1: return x; - case Choice.No: + case 2: return x; } return x; } function f12(x) { switch(x){ - case Choice.Unknown: + case 0: return x; - case Choice.Yes: + case 1: return x; - case Choice.No: + case 2: return x; } return x; } function f13(x) { switch(x){ - case Choice.Unknown: + case 0: return x; - case Choice.Yes: + case 1: return x; - case Choice.No: + case 2: return x; } return x; diff --git a/crates/swc/tests/tsc-references/importsNotUsedAsValues_error_es2015.1.normal.js b/crates/swc/tests/tsc-references/importsNotUsedAsValues_error_es2015.1.normal.js index 9c6725e1a01e..dae3e7618bd1 100644 --- a/crates/swc/tests/tsc-references/importsNotUsedAsValues_error_es2015.1.normal.js +++ b/crates/swc/tests/tsc-references/importsNotUsedAsValues_error_es2015.1.normal.js @@ -20,9 +20,9 @@ console.log(a, b); const a = A; let b; console.log(a, b); -C.One; -let c = C.Two; -let d = C.Two; +0; +let c = 1; +let d = 1; console.log(c, d); let c; let d; @@ -46,5 +46,5 @@ K; module.exports = K; // @Filename: /l.ts const K = require('./k'); -K.One; // @Filename: /j.ts +0; // @Filename: /j.ts // Sad face https://github.com/microsoft/TypeScript/blob/6b04f5039429b9d412696fe2febe39ecc69ad365/src/testRunner/compilerRunner.ts#L207 diff --git a/crates/swc/tests/tsc-references/importsNotUsedAsValues_error_es2015.2.minified.js b/crates/swc/tests/tsc-references/importsNotUsedAsValues_error_es2015.2.minified.js index 3700d98c4a1c..1998a928ab63 100644 --- a/crates/swc/tests/tsc-references/importsNotUsedAsValues_error_es2015.2.minified.js +++ b/crates/swc/tests/tsc-references/importsNotUsedAsValues_error_es2015.2.minified.js @@ -12,11 +12,7 @@ let a, b; console.log(a, b); const a = A; let b; -console.log(a, b), C.One; -let c = C.Two, d = C.Two; -console.log(c, d); -let c, d; -console.log(c, d); +console.log(a, b), console.log(1, 1), console.log(1, 1); class H { } module.exports = H; @@ -27,5 +23,4 @@ const H = require("./h"); K[K.One = 0] = "One", K[K.Two = 1] = "Two"; }(K || (K = {})), module.exports = K; const K = require("./k"); -K.One; export { _class as default }; diff --git a/crates/swc/tests/tsc-references/importsNotUsedAsValues_error_es5.1.normal.js b/crates/swc/tests/tsc-references/importsNotUsedAsValues_error_es5.1.normal.js index a2a04a569c7e..899a60faf323 100644 --- a/crates/swc/tests/tsc-references/importsNotUsedAsValues_error_es5.1.normal.js +++ b/crates/swc/tests/tsc-references/importsNotUsedAsValues_error_es5.1.normal.js @@ -29,9 +29,9 @@ console.log(a, b); var a = A; var b; console.log(a, b); -C.One; -var c = C.Two; -var d = C.Two; +0; +var c = 1; +var d = 1; console.log(c, d); var c; var d; @@ -56,5 +56,5 @@ K; module.exports = K; // @Filename: /l.ts var K = require('./k'); -K.One; // @Filename: /j.ts +0; // @Filename: /j.ts // Sad face https://github.com/microsoft/TypeScript/blob/6b04f5039429b9d412696fe2febe39ecc69ad365/src/testRunner/compilerRunner.ts#L207 diff --git a/crates/swc/tests/tsc-references/importsNotUsedAsValues_error_es5.2.minified.js b/crates/swc/tests/tsc-references/importsNotUsedAsValues_error_es5.2.minified.js index 19ab7572d8fd..35bf31415d01 100644 --- a/crates/swc/tests/tsc-references/importsNotUsedAsValues_error_es5.2.minified.js +++ b/crates/swc/tests/tsc-references/importsNotUsedAsValues_error_es5.2.minified.js @@ -13,8 +13,8 @@ export var A = function() { C[C.One = 0] = "One", C[C.Two = 1] = "Two"; }(C || (C = {})), console.log(a, b), console.log(a, b); var a = A; -console.log(a, b), C.One; -var c = C.Two, d = C.Two; +console.log(a, b); +var c = 1, d = 1; console.log(c, d), console.log(c, d); var H = function() { "use strict"; @@ -28,5 +28,4 @@ var H = require("./h"); K[K.One = 0] = "One", K[K.Two = 1] = "Two"; }(K || (K = {})), module.exports = K; var K = require("./k"); -K.One; export { _class as default }; diff --git a/crates/swc/tests/tsc-references/stringEnumLiteralTypes1_es2015.1.normal.js b/crates/swc/tests/tsc-references/stringEnumLiteralTypes1_es2015.1.normal.js index 21a0089b14fd..316fe1006eaa 100644 --- a/crates/swc/tests/tsc-references/stringEnumLiteralTypes1_es2015.1.normal.js +++ b/crates/swc/tests/tsc-references/stringEnumLiteralTypes1_es2015.1.normal.js @@ -28,8 +28,8 @@ function f3(a, b) { var y = !b; } function f5(a, b, c) { - var z1 = g(Choice.Yes); - var z2 = g(Choice.No); + var z1 = g("yes"); + var z2 = g("no"); var z3 = g(a); var z4 = g(b); var z5 = g(c); @@ -39,17 +39,17 @@ function assertNever(x) { } function f10(x) { switch(x){ - case Choice.Yes: + case "yes": return "true"; - case Choice.No: + case "no": return "false"; } } function f11(x) { switch(x){ - case Choice.Yes: + case "yes": return "true"; - case Choice.No: + case "no": return "false"; } return assertNever(x); @@ -62,7 +62,7 @@ function f12(x) { } } function f13(x) { - if (x === Choice.Yes) { + if (x === "yes") { x; } else { x; @@ -70,17 +70,17 @@ function f13(x) { } function f20(x) { switch(x.kind){ - case Choice.Yes: + case "yes": return x.a; - case Choice.No: + case "no": return x.b; } } function f21(x) { switch(x.kind){ - case Choice.Yes: + case "yes": return x.a; - case Choice.No: + case "no": return x.b; } return assertNever(x); diff --git a/crates/swc/tests/tsc-references/stringEnumLiteralTypes1_es5.1.normal.js b/crates/swc/tests/tsc-references/stringEnumLiteralTypes1_es5.1.normal.js index 21a0089b14fd..316fe1006eaa 100644 --- a/crates/swc/tests/tsc-references/stringEnumLiteralTypes1_es5.1.normal.js +++ b/crates/swc/tests/tsc-references/stringEnumLiteralTypes1_es5.1.normal.js @@ -28,8 +28,8 @@ function f3(a, b) { var y = !b; } function f5(a, b, c) { - var z1 = g(Choice.Yes); - var z2 = g(Choice.No); + var z1 = g("yes"); + var z2 = g("no"); var z3 = g(a); var z4 = g(b); var z5 = g(c); @@ -39,17 +39,17 @@ function assertNever(x) { } function f10(x) { switch(x){ - case Choice.Yes: + case "yes": return "true"; - case Choice.No: + case "no": return "false"; } } function f11(x) { switch(x){ - case Choice.Yes: + case "yes": return "true"; - case Choice.No: + case "no": return "false"; } return assertNever(x); @@ -62,7 +62,7 @@ function f12(x) { } } function f13(x) { - if (x === Choice.Yes) { + if (x === "yes") { x; } else { x; @@ -70,17 +70,17 @@ function f13(x) { } function f20(x) { switch(x.kind){ - case Choice.Yes: + case "yes": return x.a; - case Choice.No: + case "no": return x.b; } } function f21(x) { switch(x.kind){ - case Choice.Yes: + case "yes": return x.a; - case Choice.No: + case "no": return x.b; } return assertNever(x); diff --git a/crates/swc/tests/tsc-references/stringEnumLiteralTypes2_es2015.1.normal.js b/crates/swc/tests/tsc-references/stringEnumLiteralTypes2_es2015.1.normal.js index dfb35bbf57fc..123e0de66280 100644 --- a/crates/swc/tests/tsc-references/stringEnumLiteralTypes2_es2015.1.normal.js +++ b/crates/swc/tests/tsc-references/stringEnumLiteralTypes2_es2015.1.normal.js @@ -29,8 +29,8 @@ function f3(a, b) { var y = !b; } function f5(a, b, c) { - var z1 = g(Choice.Yes); - var z2 = g(Choice.No); + var z1 = g("yes"); + var z2 = g("no"); var z3 = g(a); var z4 = g(b); var z5 = g(c); @@ -40,17 +40,17 @@ function assertNever(x) { } function f10(x) { switch(x){ - case Choice.Yes: + case "yes": return "true"; - case Choice.No: + case "no": return "false"; } } function f11(x) { switch(x){ - case Choice.Yes: + case "yes": return "true"; - case Choice.No: + case "no": return "false"; } return assertNever(x); @@ -63,7 +63,7 @@ function f12(x) { } } function f13(x) { - if (x === Choice.Yes) { + if (x === "yes") { x; } else { x; @@ -71,17 +71,17 @@ function f13(x) { } function f20(x) { switch(x.kind){ - case Choice.Yes: + case "yes": return x.a; - case Choice.No: + case "no": return x.b; } } function f21(x) { switch(x.kind){ - case Choice.Yes: + case "yes": return x.a; - case Choice.No: + case "no": return x.b; } return assertNever(x); diff --git a/crates/swc/tests/tsc-references/stringEnumLiteralTypes2_es5.1.normal.js b/crates/swc/tests/tsc-references/stringEnumLiteralTypes2_es5.1.normal.js index dfb35bbf57fc..123e0de66280 100644 --- a/crates/swc/tests/tsc-references/stringEnumLiteralTypes2_es5.1.normal.js +++ b/crates/swc/tests/tsc-references/stringEnumLiteralTypes2_es5.1.normal.js @@ -29,8 +29,8 @@ function f3(a, b) { var y = !b; } function f5(a, b, c) { - var z1 = g(Choice.Yes); - var z2 = g(Choice.No); + var z1 = g("yes"); + var z2 = g("no"); var z3 = g(a); var z4 = g(b); var z5 = g(c); @@ -40,17 +40,17 @@ function assertNever(x) { } function f10(x) { switch(x){ - case Choice.Yes: + case "yes": return "true"; - case Choice.No: + case "no": return "false"; } } function f11(x) { switch(x){ - case Choice.Yes: + case "yes": return "true"; - case Choice.No: + case "no": return "false"; } return assertNever(x); @@ -63,7 +63,7 @@ function f12(x) { } } function f13(x) { - if (x === Choice.Yes) { + if (x === "yes") { x; } else { x; @@ -71,17 +71,17 @@ function f13(x) { } function f20(x) { switch(x.kind){ - case Choice.Yes: + case "yes": return x.a; - case Choice.No: + case "no": return x.b; } } function f21(x) { switch(x.kind){ - case Choice.Yes: + case "yes": return x.a; - case Choice.No: + case "no": return x.b; } return assertNever(x); diff --git a/crates/swc/tests/tsc-references/stringEnumLiteralTypes3_es2015.1.normal.js b/crates/swc/tests/tsc-references/stringEnumLiteralTypes3_es2015.1.normal.js index ba542e941a28..917efbaa3ab9 100644 --- a/crates/swc/tests/tsc-references/stringEnumLiteralTypes3_es2015.1.normal.js +++ b/crates/swc/tests/tsc-references/stringEnumLiteralTypes3_es2015.1.normal.js @@ -29,32 +29,32 @@ function f4(a, b, c, d) { d = d; } function f5(a, b, c, d) { - a = Choice.Unknown; - a = Choice.Yes; - a = Choice.No; - b = Choice.Unknown; - b = Choice.Yes; - b = Choice.No; - c = Choice.Unknown; - c = Choice.Yes; - c = Choice.No; - d = Choice.Unknown; - d = Choice.Yes; - d = Choice.No; + a = ""; + a = "yes"; + a = "no"; + b = ""; + b = "yes"; + b = "no"; + c = ""; + c = "yes"; + c = "no"; + d = ""; + d = "yes"; + d = "no"; } function f6(a, b, c, d) { - a === Choice.Unknown; - a === Choice.Yes; - a === Choice.No; - b === Choice.Unknown; - b === Choice.Yes; - b === Choice.No; - c === Choice.Unknown; - c === Choice.Yes; - c === Choice.No; - d === Choice.Unknown; - d === Choice.Yes; - d === Choice.No; + a === ""; + a === "yes"; + a === "no"; + b === ""; + b === "yes"; + b === "no"; + c === ""; + c === "yes"; + c === "no"; + d === ""; + d === "yes"; + d === "no"; } function f7(a, b, c, d) { a === a; @@ -76,44 +76,44 @@ function f7(a, b, c, d) { } function f10(x) { switch(x){ - case Choice.Unknown: + case "": return x; - case Choice.Yes: + case "yes": return x; - case Choice.No: + case "no": return x; } return x; } function f11(x) { switch(x){ - case Choice.Unknown: + case "": return x; - case Choice.Yes: + case "yes": return x; - case Choice.No: + case "no": return x; } return x; } function f12(x) { switch(x){ - case Choice.Unknown: + case "": return x; - case Choice.Yes: + case "yes": return x; - case Choice.No: + case "no": return x; } return x; } function f13(x) { switch(x){ - case Choice.Unknown: + case "": return x; - case Choice.Yes: + case "yes": return x; - case Choice.No: + case "no": return x; } return x; diff --git a/crates/swc/tests/tsc-references/stringEnumLiteralTypes3_es5.1.normal.js b/crates/swc/tests/tsc-references/stringEnumLiteralTypes3_es5.1.normal.js index ba542e941a28..917efbaa3ab9 100644 --- a/crates/swc/tests/tsc-references/stringEnumLiteralTypes3_es5.1.normal.js +++ b/crates/swc/tests/tsc-references/stringEnumLiteralTypes3_es5.1.normal.js @@ -29,32 +29,32 @@ function f4(a, b, c, d) { d = d; } function f5(a, b, c, d) { - a = Choice.Unknown; - a = Choice.Yes; - a = Choice.No; - b = Choice.Unknown; - b = Choice.Yes; - b = Choice.No; - c = Choice.Unknown; - c = Choice.Yes; - c = Choice.No; - d = Choice.Unknown; - d = Choice.Yes; - d = Choice.No; + a = ""; + a = "yes"; + a = "no"; + b = ""; + b = "yes"; + b = "no"; + c = ""; + c = "yes"; + c = "no"; + d = ""; + d = "yes"; + d = "no"; } function f6(a, b, c, d) { - a === Choice.Unknown; - a === Choice.Yes; - a === Choice.No; - b === Choice.Unknown; - b === Choice.Yes; - b === Choice.No; - c === Choice.Unknown; - c === Choice.Yes; - c === Choice.No; - d === Choice.Unknown; - d === Choice.Yes; - d === Choice.No; + a === ""; + a === "yes"; + a === "no"; + b === ""; + b === "yes"; + b === "no"; + c === ""; + c === "yes"; + c === "no"; + d === ""; + d === "yes"; + d === "no"; } function f7(a, b, c, d) { a === a; @@ -76,44 +76,44 @@ function f7(a, b, c, d) { } function f10(x) { switch(x){ - case Choice.Unknown: + case "": return x; - case Choice.Yes: + case "yes": return x; - case Choice.No: + case "no": return x; } return x; } function f11(x) { switch(x){ - case Choice.Unknown: + case "": return x; - case Choice.Yes: + case "yes": return x; - case Choice.No: + case "no": return x; } return x; } function f12(x) { switch(x){ - case Choice.Unknown: + case "": return x; - case Choice.Yes: + case "yes": return x; - case Choice.No: + case "no": return x; } return x; } function f13(x) { switch(x){ - case Choice.Unknown: + case "": return x; - case Choice.Yes: + case "yes": return x; - case Choice.No: + case "no": return x; } return x; diff --git a/crates/swc_ecma_ast/src/typescript.rs b/crates/swc_ecma_ast/src/typescript.rs index 3d15e091f6f2..01dda7038998 100644 --- a/crates/swc_ecma_ast/src/typescript.rs +++ b/crates/swc_ecma_ast/src/typescript.rs @@ -8,6 +8,7 @@ use serde::{ Deserialize, Deserializer, Serialize, }; use string_enum::StringEnum; +use swc_atoms::JsWord; use swc_common::{ast_node, EqIgnoreSpan, Span}; use crate::{ @@ -862,6 +863,15 @@ pub enum TsEnumMemberId { Str(Str), } +impl AsRef for TsEnumMemberId { + fn as_ref(&self) -> &JsWord { + match &self { + TsEnumMemberId::Str(Str { value: ref sym, .. }) + | TsEnumMemberId::Ident(Ident { ref sym, .. }) => sym, + } + } +} + #[ast_node("TsModuleDeclaration")] #[derive(Eq, Hash, EqIgnoreSpan)] #[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))] diff --git a/crates/swc_ecma_transforms_base/src/assumptions.rs b/crates/swc_ecma_transforms_base/src/assumptions.rs index 644cbf26967a..b4eaea0a5379 100644 --- a/crates/swc_ecma_transforms_base/src/assumptions.rs +++ b/crates/swc_ecma_transforms_base/src/assumptions.rs @@ -70,6 +70,8 @@ pub struct Assumptions { /// https://babeljs.io/docs/en/assumptions#superiscallableconstructor pub super_is_callable_constructor: bool, + + pub ts_enum_is_readonly: bool, } impl Assumptions { @@ -96,6 +98,7 @@ impl Assumptions { set_spread_properties: true, skip_for_of_iterator_closing: true, super_is_callable_constructor: true, + ts_enum_is_readonly: true, } } } diff --git a/crates/swc_ecma_transforms_typescript/src/inline_enum.rs b/crates/swc_ecma_transforms_typescript/src/inline_enum.rs new file mode 100644 index 000000000000..fd72d4f6b9c7 --- /dev/null +++ b/crates/swc_ecma_transforms_typescript/src/inline_enum.rs @@ -0,0 +1,147 @@ +use std::{cell::RefCell, rc::Rc}; + +use swc_atoms::JsWord; +use swc_common::collections::AHashMap; +use swc_ecma_ast::{ + AssignExpr, AssignPat, AssignPatProp, ComputedPropName, Expr, Id, Lit, MemberExpr, MemberProp, + Module, Script, Str, Tpl, +}; +use swc_ecma_utils::ident::IdentLike; +use swc_ecma_visit::{as_folder, noop_visit_mut_type, Fold, VisitMut, VisitMutWith}; + +use crate::TSEnumConfig; + +pub(crate) type TSEnumLit = Rc>>>; + +pub(crate) fn inline_enum( + ts_enum_lit: TSEnumLit, + ts_enum_config: TSEnumConfig, +) -> impl Fold + VisitMut { + as_folder(InlineEnum { + ts_enum_lit, + ts_enum_config, + is_lhs: false, + }) +} + +struct InlineEnum { + ts_enum_lit: TSEnumLit, + ts_enum_config: TSEnumConfig, + + is_lhs: bool, +} + +impl VisitMut for InlineEnum { + noop_visit_mut_type!(); + + fn visit_mut_expr(&mut self, e: &mut Expr) { + if !self.is_lhs { + if let Expr::Member(MemberExpr { obj, prop, .. }) = e { + let lit = Self::get_enum_id(obj).and_then(|id| { + let ts_enum_lit = self.ts_enum_lit.borrow(); + let k_v = ts_enum_lit.get(&id)?; + let key = Self::get_member_key(prop)?; + let lit = k_v.get(&key)?; + Some(lit.clone()) + }); + + if let Some(lit) = lit { + *e = lit.into(); + return; + } + } + } + + e.visit_mut_children_with(self); + } + + fn visit_mut_assign_expr(&mut self, a: &mut AssignExpr) { + let is_lhs = self.is_lhs; + + self.is_lhs = true; + a.left.visit_mut_with(self); + + self.is_lhs = false; + a.right.visit_mut_with(self); + + self.is_lhs = is_lhs; + } + + fn visit_mut_assign_pat(&mut self, a: &mut AssignPat) { + let is_lhs = self.is_lhs; + + self.is_lhs = true; + a.left.visit_mut_with(self); + + self.is_lhs = false; + a.right.visit_mut_with(self); + + self.is_lhs = is_lhs; + } + + fn visit_mut_assign_pat_prop(&mut self, a: &mut AssignPatProp) { + let is_lhs = self.is_lhs; + + self.is_lhs = false; + a.value.visit_mut_with(self); + + self.is_lhs = is_lhs; + } + + fn visit_mut_module(&mut self, m: &mut Module) { + if self.skip() || self.ts_enum_lit.borrow().is_empty() { + return; + } + + m.visit_mut_children_with(self) + } + + fn visit_mut_script(&mut self, s: &mut Script) { + if self.skip() || self.ts_enum_lit.borrow().is_empty() { + return; + } + + s.visit_mut_children_with(self) + } +} + +impl InlineEnum { + fn skip(&self) -> bool { + let Self { + ts_enum_config: + TSEnumConfig { + treat_const_enum_as_enum, + ts_enum_is_readonly, + }, + .. + } = self; + + *treat_const_enum_as_enum && !ts_enum_is_readonly + } + + fn get_enum_id(e: &Expr) -> Option { + if let Expr::Ident(ident) = e { + Some(ident.to_id()) + } else { + None + } + } + + fn get_member_key(prop: &MemberProp) -> Option { + match prop { + MemberProp::Ident(ident) => Some(ident.sym.clone()), + MemberProp::Computed(ComputedPropName { expr, .. }) => match &**expr { + Expr::Lit(Lit::Str(Str { value, .. })) => Some(value.clone()), + Expr::Tpl(Tpl { exprs, quasis, .. }) => match (exprs.len(), quasis.len()) { + (0, 1) => quasis[0] + .cooked + .as_ref() + .map(|str_lit| str_lit.value.clone()), + _ => None, + }, + _ => None, + }, + MemberProp::PrivateName(_) => None, + } + } +} diff --git a/crates/swc_ecma_transforms_typescript/src/lib.rs b/crates/swc_ecma_transforms_typescript/src/lib.rs index b52348359214..95ebf29c2602 100644 --- a/crates/swc_ecma_transforms_typescript/src/lib.rs +++ b/crates/swc_ecma_transforms_typescript/src/lib.rs @@ -2,5 +2,5 @@ #![allow(clippy::vec_box)] pub use self::strip::*; - +mod inline_enum; pub mod strip; diff --git a/crates/swc_ecma_transforms_typescript/src/strip.rs b/crates/swc_ecma_transforms_typescript/src/strip.rs index 4e4e3d69d44e..425f45eafc37 100644 --- a/crates/swc_ecma_transforms_typescript/src/strip.rs +++ b/crates/swc_ecma_transforms_typescript/src/strip.rs @@ -3,6 +3,7 @@ use std::mem::take; use serde::{Deserialize, Serialize}; use swc_atoms::{js_word, JsWord}; use swc_common::{ + chain, collections::{AHashMap, AHashSet}, comments::{Comments, NoopComments}, sync::Lrc, @@ -21,6 +22,8 @@ use swc_ecma_visit::{ VisitWith, }; +use crate::inline_enum::{inline_enum, TSEnumLit}; + /// Value does not contain TsLit::Bool type EnumValues = AHashMap>; @@ -80,22 +83,54 @@ pub struct Config { /// Note: this pass handle jsx directives in comments #[serde(default)] pub pragma_frag: Option, + + #[serde(default)] + pub ts_enum_config: TSEnumConfig, +} + +#[derive(Clone, Copy, Debug, Default, Serialize, Deserialize)] +pub struct TSEnumConfig { + /// Note: `const enum` will be inlined if this is false(default) + /// All `const enum` *should be* inlined since it's align to tsc behavior. + /// This option exists to allow you to roll back the old behavior of swc. + #[serde(default)] + pub treat_const_enum_as_enum: bool, + + /// Config from `assumptions` + /// Note: If this is true, all enums will be inlined when possible + /// whether or not you use `enum` or `const enum`. + #[serde(default)] + pub ts_enum_is_readonly: bool, +} + +impl TSEnumConfig { + pub fn should_collect_enum(&self, is_const: bool) -> bool { + self.ts_enum_is_readonly || (!self.treat_const_enum_as_enum && is_const) + } } pub fn strip_with_config(config: Config, top_level_mark: Mark) -> impl Fold + VisitMut { - as_folder(Strip { - config, - comments: NoopComments, - jsx: None, - top_level_ctxt: SyntaxContext::empty().apply_mark(top_level_mark), - non_top_level: Default::default(), - scope: Default::default(), - is_side_effect_import: Default::default(), - is_type_only_export: Default::default(), - uninitialized_vars: Default::default(), - decl_names: Default::default(), - in_var_pat: Default::default(), - }) + let ts_enum_lit = TSEnumLit::default(); + + let ts_enum_config = config.ts_enum_config; + + chain!( + as_folder(Strip { + config, + comments: NoopComments, + jsx: None, + top_level_ctxt: SyntaxContext::empty().apply_mark(top_level_mark), + ts_enum_lit: ts_enum_lit.clone(), + non_top_level: Default::default(), + scope: Default::default(), + is_side_effect_import: Default::default(), + is_type_only_export: Default::default(), + uninitialized_vars: Default::default(), + decl_names: Default::default(), + in_var_pat: Default::default(), + }), + inline_enum(ts_enum_lit, ts_enum_config) + ) } /// Strips type annotations out. @@ -141,24 +176,31 @@ where let pragma_id = id_for_jsx(&pragma); let pragma_frag_id = id_for_jsx(&pragma_frag); - as_folder(Strip { - config, - comments, - jsx: Some(JsxData { - cm, - top_level_mark, - pragma_id, - pragma_frag_id, + let ts_enum_lit = TSEnumLit::default(); + let ts_enum_config = config.ts_enum_config; + + chain!( + as_folder(Strip { + config, + comments, + jsx: Some(JsxData { + cm, + top_level_mark, + pragma_id, + pragma_frag_id, + }), + top_level_ctxt: SyntaxContext::empty().apply_mark(top_level_mark), + ts_enum_lit: ts_enum_lit.clone(), + non_top_level: Default::default(), + scope: Default::default(), + is_side_effect_import: Default::default(), + is_type_only_export: Default::default(), + uninitialized_vars: Default::default(), + decl_names: Default::default(), + in_var_pat: Default::default(), }), - top_level_ctxt: SyntaxContext::empty().apply_mark(top_level_mark), - non_top_level: Default::default(), - scope: Default::default(), - is_side_effect_import: Default::default(), - is_type_only_export: Default::default(), - uninitialized_vars: Default::default(), - decl_names: Default::default(), - in_var_pat: Default::default(), - }) + inline_enum(ts_enum_lit, ts_enum_config) + ) } /// Get an [Id] which will used by expression. @@ -199,6 +241,8 @@ where is_type_only_export: bool, uninitialized_vars: Vec, + ts_enum_lit: TSEnumLit, + /// Names of declarations. /// This is used to prevent emitting a variable with same name multiple /// name. @@ -853,13 +897,8 @@ where } // for m in e.members.iter() { - match m.id { - TsEnumMemberId::Str(Str { value: ref sym, .. }) - | TsEnumMemberId::Ident(Ident { ref sym, .. }) => { - if *sym == id.sym { - return compute(e, span, values, None, m.init.as_deref()); - } - } + if &id.sym == m.id.as_ref() { + return compute(e, span, values, None, m.init.as_deref()); } } return Err(()); @@ -943,13 +982,7 @@ where if let TsLit::Number(n) = val { default = n.value as i64 + 1; } - values.insert( - match &m.id { - TsEnumMemberId::Ident(i) => i.sym.clone(), - TsEnumMemberId::Str(s) => s.value.clone(), - }, - Some(val.clone()), - ); + values.insert(m.id.as_ref().clone(), Some(val.clone())); match val { TsLit::Number(v) => Expr::Lit(Lit::Num(v)), @@ -971,13 +1004,7 @@ where }; visitor.visit_mut_expr(&mut v); - values.insert( - match &m.id { - TsEnumMemberId::Ident(i) => i.sym.clone(), - TsEnumMemberId::Str(s) => s.value.clone(), - }, - None, - ); + values.insert(m.id.as_ref().clone(), None); Ok(v) } @@ -988,6 +1015,25 @@ where .collect::, _>>() .unwrap_or_else(|_| panic!("invalid value for enum is detected")); + let contains_lit = members.iter().any(|(_, v)| matches!(v, Expr::Lit(..))); + let should_collect = self.config.ts_enum_config.should_collect_enum(e.is_const); + + if should_collect && contains_lit { + let mut enums = self.ts_enum_lit.borrow_mut(); + + let k_v = enums.entry(e.id.to_id()).or_default(); + + members + .iter() + .filter(|(_, v)| matches!(v, Expr::Lit(..))) + .for_each(|(m, e)| { + let member_key = m.id.as_ref().clone(); + let value = e.clone().expect_lit(); + + k_v.insert(member_key, value); + }) + } + let init = CallExpr { span: DUMMY_SP, callee: FnExpr { @@ -1007,14 +1053,10 @@ where span: DUMMY_SP, stmts: members .into_iter() - .enumerate() - .map(|(_, (m, val))| { + .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, - }, + Expr::Lit(Lit::Str(s)) => m.id.as_ref() == &s.value, _ => true, }; diff --git a/node-swc/src/types.ts b/node-swc/src/types.ts index 536ac2d10f01..10a5d0bec41c 100644 --- a/node-swc/src/types.ts +++ b/node-swc/src/types.ts @@ -640,6 +640,8 @@ export interface TransformConfig { * https://swc.rs/docs/configuring-swc.html#jsctransformdecoratormetadata */ decoratorMetadata?: boolean; + + treatConstEnumAsEnum?: boolean; } export interface ReactConfig {