Skip to content

Commit

Permalink
feat(es/minifier): Detect TypeScript enum initialization pattern (#8986)
Browse files Browse the repository at this point in the history
**Description:**

We can optimize
```js
var Foo;
Foo || Foo = {};
```

This code looks strange, but 
```ts
enum Foo {
    a = 1,
    b = 2,
}
```

transpiles to

```js
var Foo;
(function(Foo) {
    Foo[Foo["a"] = 1] = "a";
    Foo[Foo["b"] = 2] = "b";
})(Foo || (Foo = {}));

```

and after minification it becomes

```js
var Foo, Foo1;
(Foo1 = Foo || (Foo = {}))[Foo1.a = 1] = "a", Foo1[Foo1.b = 2] = "b";

```
  • Loading branch information
kdy1 authored May 30, 2024
1 parent ffc74b8 commit cc8c155
Show file tree
Hide file tree
Showing 384 changed files with 885 additions and 1,049 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
//// [module.d.ts]
//// [classPoint.ts]
var A;
import { _ as _class_call_check } from "@swc/helpers/_/_class_call_check";
(A || (A = {})).Point = function Point(x, y) {
_class_call_check(this, Point), this.x = x, this.y = y;
};
//// [test.ts]
A.Point.Origin, new A.Point(0, 0);
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//// [ClassAndModuleThatMergeWithStaticFunctionAndExportedFunctionThatShareAName.ts]
import { _ as _class_call_check } from "@swc/helpers/_/_class_call_check";
var A, Point, A1, Point1 = function() {
var A, Point, Point1 = function() {
function Point(x, y) {
_class_call_check(this, Point), this.x = x, this.y = y;
}
Expand All @@ -13,7 +13,7 @@ var A, Point, A1, Point1 = function() {
}();
(Point1 || (Point1 = {})).Origin = function() {
return null;
}, A = A1 || (A1 = {}), Point = function() {
}, A = {}, Point = function() {
function Point(x, y) {
_class_call_check(this, Point), this.x = x, this.y = y;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//// [ClassAndModuleThatMergeWithStaticFunctionAndNonExportedFunctionThatShareAName.ts]
import { _ as _class_call_check } from "@swc/helpers/_/_class_call_check";
var A, Point, A1, Point1 = function() {
var A, Point, Point1 = function() {
function Point(x, y) {
_class_call_check(this, Point), this.x = x, this.y = y;
}
Expand All @@ -11,7 +11,7 @@ var A, Point, A1, Point1 = function() {
};
}, Point;
}();
Point1 || (Point1 = {}), A = A1 || (A1 = {}), Point = function() {
Point1 || (Point1 = {}), A = {}, Point = function() {
function Point(x, y) {
_class_call_check(this, Point), this.x = x, this.y = y;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
//// [ClassAndModuleThatMergeWithStaticVariableAndExportedVarThatShareAName.ts]
import { _ as _class_call_check } from "@swc/helpers/_/_class_call_check";
var A, Point, A1, Point1 = function Point(x, y) {
var A, Point, Point1 = function Point(x, y) {
_class_call_check(this, Point), this.x = x, this.y = y;
};
Point1.Origin = {
x: 0,
y: 0
}, (Point1 || (Point1 = {})).Origin = "", A = A1 || (A1 = {}), (Point = function Point(x, y) {
}, (Point1 || (Point1 = {})).Origin = "", A = {}, (Point = function Point(x, y) {
_class_call_check(this, Point), this.x = x, this.y = y;
}).Origin = {
x: 0,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
//// [ClassAndModuleThatMergeWithStaticVariableAndNonExportedVarThatShareAName.ts]
import { _ as _class_call_check } from "@swc/helpers/_/_class_call_check";
var A, Point, A1, Point1 = function Point(x, y) {
var A, Point, Point1 = function Point(x, y) {
_class_call_check(this, Point), this.x = x, this.y = y;
};
Point1.Origin = {
x: 0,
y: 0
}, Point1 || (Point1 = {}), A = A1 || (A1 = {}), (Point = function Point(x, y) {
}, Point1 || (Point1 = {}), A = {}, (Point = function Point(x, y) {
_class_call_check(this, Point), this.x = x, this.y = y;
}).Origin = {
x: 0,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
//// [class.ts]
var X, X1;
var X;
import { _ as _class_call_check } from "@swc/helpers/_/_class_call_check";
((X1 = X || (X = {})).Y || (X1.Y = {})).Point = function Point(x, y) {
((X = {}).Y || (X.Y = {})).Point = function Point(x, y) {
_class_call_check(this, Point), this.x = x, this.y = y;
};
//// [module.ts]
var X, X1, Y, Point;
(Point = (Y = (X1 = X || (X = {})).Y || (X1.Y = {})).Point || (Y.Point = {})).Origin = new Point(0, 0);
var X, Y, Point;
(Point = (Y = (X = {}).Y || (X.Y = {})).Point || (Y.Point = {})).Origin = new Point(0, 0);
//// [test.ts]
new X.Y.Point(1, 1), X.Y.Point.Origin;
//// [simple.ts]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
//// [class.ts]
var X, X1;
((X1 = X || (X = {})).Y || (X1.Y = {})).Point = class {
var X;
((X = {}).Y || (X.Y = {})).Point = class {
constructor(x, y){
this.x = x, this.y = y;
}
};
//// [module.ts]
var X, X1, Y, Point;
(Point = (Y = (X1 = X || (X = {})).Y || (X1.Y = {})).Point || (Y.Point = {})).Origin = new Point(0, 0);
var X, Y, Point;
(Point = (Y = (X = {}).Y || (X.Y = {})).Point || (Y.Point = {})).Origin = new Point(0, 0);
//// [test.ts]
new X.Y.Point(1, 1), X.Y.Point.Origin;
//// [simple.ts]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//// [ES5SymbolProperty2.ts]
var M, M1, _$Symbol, C;
import { _ as _class_call_check } from "@swc/helpers/_/_class_call_check";
M1 = M || (M = {}), C = function() {
M1 = M = {}, C = function() {
function C() {
_class_call_check(this, C);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
//// [ExportClassWhichExtendsInterfaceWithInaccessibleType.ts]
var A, A1, Point2d;
var A, Point2d;
import { _ as _class_call_check } from "@swc/helpers/_/_class_call_check";
A1 = A || (A = {}), Point2d = function() {
A = {}, Point2d = function() {
function Point2d(x, y) {
_class_call_check(this, Point2d), this.x = x, this.y = y;
}
return Point2d.prototype.fromOrigin = function(p) {
return 1;
}, Point2d;
}(), A1.Point2d = Point2d;
}(), A.Point2d = Point2d;
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
//// [ExportClassWithAccessibleTypesInTypeParameterConstraintsClassHeritageListMemberTypeAnnotations.ts]
var A, A1, Point, Point3d;
var A, Point, Point3d;
import { _ as _class_call_check } from "@swc/helpers/_/_class_call_check";
import { _ as _inherits } from "@swc/helpers/_/_inherits";
import { _ as _create_super } from "@swc/helpers/_/_create_super";
A1 = A || (A = {}), Point = function Point() {
A = {}, Point = function Point() {
_class_call_check(this, Point);
}, A1.Point = Point, A1.Origin = {
}, A.Point = Point, A.Origin = {
x: 0,
y: 0
}, Point3d = function(Point) {
Expand All @@ -15,10 +15,10 @@ A1 = A || (A = {}), Point = function Point() {
return _class_call_check(this, Point3d), _super.apply(this, arguments);
}
return Point3d;
}(Point), A1.Point3d = Point3d, A1.Origin3d = {
}(Point), A.Point3d = Point3d, A.Origin3d = {
x: 0,
y: 0,
z: 0
}, A1.Line = function Line(start, end) {
}, A.Line = function Line(start, end) {
_class_call_check(this, Line), this.start = start, this.end = end;
};
Original file line number Diff line number Diff line change
@@ -1,6 +1,2 @@
//// [ExportClassWithInaccessibleTypeInIndexerTypeAnnotations.ts]
var A;
import { _ as _class_call_check } from "@swc/helpers/_/_class_call_check";
(A || (A = {})).points = function points() {
_class_call_check(this, points);
};
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
//// [ExportClassWithInaccessibleTypeInTypeParameterConstraint.ts]
var A, A1, Point3d, Line;
var A, Point3d, Line;
import { _ as _class_call_check } from "@swc/helpers/_/_class_call_check";
import { _ as _inherits } from "@swc/helpers/_/_inherits";
import { _ as _create_super } from "@swc/helpers/_/_create_super";
(A1 = A || (A = {})).Origin = {
(A = {}).Origin = {
x: 0,
y: 0
}, Point3d = function(Point) {
Expand All @@ -15,7 +15,7 @@ import { _ as _create_super } from "@swc/helpers/_/_create_super";
return Point3d;
}(function Point() {
_class_call_check(this, Point);
}), A1.Point3d = Point3d, A1.Origin3d = {
}), A.Point3d = Point3d, A.Origin3d = {
x: 0,
y: 0,
z: 0
Expand All @@ -26,4 +26,4 @@ import { _ as _create_super } from "@swc/helpers/_/_create_super";
return Line.fromorigin2d = function(p) {
return null;
}, Line;
}(), A1.Line = Line;
}(), A.Line = Line;
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
//// [ExportFunctionWithAccessibleTypesInParameterAndReturnTypeAnnotation.ts]
var A, A1, Line;
var A, Line;
import { _ as _class_call_check } from "@swc/helpers/_/_class_call_check";
(A1 = A || (A = {})).Point = function Point() {
(A = {}).Point = function Point() {
_class_call_check(this, Point);
}, Line = function Line(start, end) {
_class_call_check(this, Line), this.start = start, this.end = end;
}, A1.Line = Line, A1.fromOrigin = function(p) {
}, A.Line = Line, A.fromOrigin = function(p) {
return new Line({
x: 0,
y: 0
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
//// [ExportFunctionWithInaccessibleTypesInParameterTypeAnnotation.ts]
var A, A1, Line;
var A, Line;
import { _ as _class_call_check } from "@swc/helpers/_/_class_call_check";
A1 = A || (A = {}), Line = function Line(start, end) {
A = {}, Line = function Line(start, end) {
_class_call_check(this, Line), this.start = start, this.end = end;
}, A1.Line = Line, A1.fromOrigin = function(p) {
}, A.Line = Line, A.fromOrigin = function(p) {
return new Line({
x: 0,
y: 0
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
//// [ExportFunctionWithInaccessibleTypesInReturnTypeAnnotation.ts]
var A, A1, Line;
var A, Line;
import { _ as _class_call_check } from "@swc/helpers/_/_class_call_check";
(A1 = A || (A = {})).Point = function Point() {
(A = {}).Point = function Point() {
_class_call_check(this, Point);
}, Line = function Line(start, end) {
_class_call_check(this, Line), this.start = start, this.end = end;
}, A1.fromOrigin = function(p) {
}, A.fromOrigin = function(p) {
return new Line({
x: 0,
y: 0
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
//// [ExportInterfaceWithAccessibleTypesInTypeParameterConstraintsClassHeritageListMemberTypeAnnotations.ts]
var A, A1;
(A1 = A || (A = {})).Origin = {
var A;
(A = {}).Origin = {
x: 0,
y: 0
}, A1.Origin3d = {
}, A.Origin3d = {
x: 0,
y: 0,
z: 0
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
//// [ExportInterfaceWithInaccessibleTypeInTypeParameterConstraint.ts]
var A, A1;
(A1 = A || (A = {})).Origin = {
var A;
(A = {}).Origin = {
x: 0,
y: 0
}, A1.Origin3d = {
}, A.Origin3d = {
x: 0,
y: 0,
z: 0
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
//// [ExportModuleWithAccessibleTypesOnItsExportedMembers.ts]
var A, A1, Point, B, Line;
var A, Point, B, Line;
import { _ as _class_call_check } from "@swc/helpers/_/_class_call_check";
A1 = A || (A = {}), Point = function Point(x, y) {
A = {}, Point = function Point(x, y) {
_class_call_check(this, Point), this.x = x, this.y = y;
}, A1.Point = Point, (B = A1.B || (A1.B = {})).Origin = new Point(0, 0), Line = function() {
}, A.Point = Point, (B = A.B || (A.B = {})).Origin = new Point(0, 0), Line = function() {
function Line(start, end) {
_class_call_check(this, Line);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
//// [ExportObjectLiteralAndObjectTypeLiteralWithAccessibleTypesInMemberTypeAnnotations.ts]
var A, A1, Point;
var A, Point;
import { _ as _class_call_check } from "@swc/helpers/_/_class_call_check";
A1 = A || (A = {}), Point = function Point(x, y) {
A = {}, Point = function Point(x, y) {
_class_call_check(this, Point), this.x = x, this.y = y;
}, A1.Origin = {
}, A.Origin = {
x: 0,
y: 0
}, A1.Unity = {
}, A.Unity = {
start: new Point(0, 0),
end: new Point(1, 0)
};
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
//// [ExportObjectLiteralAndObjectTypeLiteralWithAccessibleTypesInNestedMemberTypeAnnotations.ts]
var A;
import "@swc/helpers/_/_class_call_check";
(A || (A = {})).UnitSquare = null;
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
//// [ExportVariableOfGenericTypeWithInaccessibleTypeAsTypeArgument.ts]
var A;
import "@swc/helpers/_/_class_call_check";
(A || (A = {})).beez2 = [];
Original file line number Diff line number Diff line change
@@ -1,6 +1 @@
//// [ExportVariableWithAccessibleTypeInTypeAnnotation.ts]
var A;
(A || (A = {})).Origin = {
x: 0,
y: 0
};
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
//// [ExportVariableWithInaccessibleTypeInTypeAnnotation.ts]
var A, A1;
(A1 = A || (A = {})).Origin = {
var A;
(A = {}).Origin = {
x: 0,
y: 0
}, A1.Origin3d = {
}, A.Origin3d = {
x: 0,
y: 0,
z: 0
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
//// [function.ts]
var A;
(A || (A = {})).Point = function() {
return {
x: 0,
y: 0
};
};
//// [module.ts]
var A, A1;
((A1 = A || (A = {})).Point || (A1.Point = {})).Origin = {
var A;
((A = {}).Point || (A.Point = {})).Origin = {
x: 0,
y: 0
};
Expand All @@ -27,4 +20,4 @@ var B;
x: 0,
y: 0
};
}(B || (B = {})), B.Point, B.Point(), B.Point.Origin;
}(B = {}), B.Point, B.Point(), B.Point.Origin;
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
//// [function.ts]
var A;
(A || (A = {})).Point = function() {
return {
x: 0,
y: 0
};
};
//// [module.ts]
var B, B1;
((B1 = B || (B = {})).Point || (B1.Point = {})).Origin = {
var B;
((B = {}).Point || (B.Point = {})).Origin = {
x: 0,
y: 0
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
//// [module.ts]
var X, X1, Y, Point;
(Point = (Y = (X1 = X || (X = {})).Y || (X1.Y = {})).Point || (Y.Point = {})).Origin = new Point(0, 0);
var X, Y, Point;
(Point = (Y = (X = {}).Y || (X.Y = {})).Point || (Y.Point = {})).Origin = new Point(0, 0);
//// [classPoint.ts]
var X, X1;
var X;
import { _ as _class_call_check } from "@swc/helpers/_/_class_call_check";
((X1 = X || (X = {})).Y || (X1.Y = {})).Point = function Point(x, y) {
((X = {}).Y || (X.Y = {})).Point = function Point(x, y) {
_class_call_check(this, Point), this.x = x, this.y = y;
};
//// [simple.ts]
Expand Down
Loading

0 comments on commit cc8c155

Please sign in to comment.