Skip to content

Commit

Permalink
feat(es/typescript): Inline typescript enums (#3647)
Browse files Browse the repository at this point in the history
  • Loading branch information
magic-akari authored Feb 24, 2022
1 parent 0bda251 commit 1743302
Show file tree
Hide file tree
Showing 60 changed files with 712 additions and 348 deletions.
11 changes: 10 additions & 1 deletion crates/swc/src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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)]
Expand Down
2 changes: 1 addition & 1 deletion crates/swc/tests/fixture/issue-1515/case1/output/input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}
}
Expand Down
7 changes: 7 additions & 0 deletions crates/swc/tests/fixture/ts-inline-enum/default/input/.swcrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"jsc": {
"parser": {
"syntax": "typescript"
}
}
}
18 changes: 18 additions & 0 deletions crates/swc/tests/fixture/ts-inline-enum/default/input/index.ts
Original file line number Diff line number Diff line change
@@ -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" = `你好`,
}
17 changes: 17 additions & 0 deletions crates/swc/tests/fixture/ts-inline-enum/default/output/index.ts
Original file line number Diff line number Diff line change
@@ -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 = {}));
7 changes: 7 additions & 0 deletions crates/swc/tests/fixture/ts-inline-enum/lhs/input/.swcrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"jsc": {
"parser": {
"syntax": "typescript"
}
}
}
8 changes: 8 additions & 0 deletions crates/swc/tests/fixture/ts-inline-enum/lhs/input/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const enum Foo {
hello = 42,
}

let x;
[x = Foo.hello] = [,];

console.log(x);
8 changes: 8 additions & 0 deletions crates/swc/tests/fixture/ts-inline-enum/lhs/output/index.ts
Original file line number Diff line number Diff line change
@@ -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);
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"jsc": {
"parser": {
"syntax": "typescript"
},
"transform": {
"treatConstEnumAsEnum": false
}
}
}
Original file line number Diff line number Diff line change
@@ -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);
Original file line number Diff line number Diff line change
@@ -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");
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"jsc": {
"parser": {
"syntax": "typescript"
},
"transform": {
"treatConstEnumAsEnum": true
}
}
}
Original file line number Diff line number Diff line change
@@ -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);
Original file line number Diff line number Diff line change
@@ -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);
4 changes: 2 additions & 2 deletions crates/swc/tests/tsc-references/constEnum3_es2015.1.normal.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Original file line number Diff line number Diff line change
Expand Up @@ -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");
4 changes: 2 additions & 2 deletions crates/swc/tests/tsc-references/constEnum3_es5.1.normal.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Original file line number Diff line number Diff line change
Expand Up @@ -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]() {
Expand Down
Original file line number Diff line number Diff line change
@@ -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];
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Original file line number Diff line number Diff line change
Expand Up @@ -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) {}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Loading

1 comment on commit 1743302

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Benchmark

Benchmark suite Current: 1743302 Previous: 8c94ea9 Ratio
full_es2015 194464632 ns/iter (± 25131345) 172796660 ns/iter (± 5523368) 1.13
full_es2016 209792989 ns/iter (± 22258159) 177884497 ns/iter (± 9630153) 1.18
full_es2017 211067325 ns/iter (± 23599133) 176898710 ns/iter (± 6029938) 1.19
full_es2018 209207663 ns/iter (± 21307513) 175306615 ns/iter (± 5707664) 1.19
full_es2019 207111165 ns/iter (± 27030514) 174867699 ns/iter (± 5929635) 1.18
full_es2020 178077004 ns/iter (± 20117811) 160847944 ns/iter (± 5009194) 1.11
full_es3 253228959 ns/iter (± 24655142) 220222784 ns/iter (± 6855941) 1.15
full_es5 255734069 ns/iter (± 26422005) 216630191 ns/iter (± 9765541) 1.18
parser 825401 ns/iter (± 200602) 698479 ns/iter (± 28796) 1.18

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.