Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(es/minifier): Drop expressions using sequential inliner #6936

Merged
merged 26 commits into from
Apr 29, 2023
Merged
Show file tree
Hide file tree
Changes from 25 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,5 @@ Point || (Point = {}), function(A) {
};
}, Point;
}();
A.Point = Point, Point = A.Point || (A.Point = {});
A.Point = Point, A.Point || (A.Point = {});
}(A || (A = {}));
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//// [assignAnyToEveryType.ts]
var x, E, M;
var E, M;
import "@swc/helpers/_/_class_call_check";
!function(E) {
E[E.A = 0] = "A";
}(E || (E = {})), E.A, (M || (M = {})).foo = 1, M = x;
}(E || (E = {})), E.A, (M || (M = {})).foo = 1;
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
//// [assignFromBooleanInterface2.ts]
var a, b, x = !0;
a = !0, b = a = b, b = x, x = a, x = b;
a = !0, b = a = b, b = x, x = a;
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
//// [assignFromNumberInterface2.ts]
var a, b, x = 1;
a = 1, b = a = b, b = x, x = a, x = b;
a = 1, b = a = b, b = x, x = a;
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
//// [assignFromStringInterface2.ts]
var a, b, x = "";
a = x, b = a = b, b = x, x = a, x = b;
a = x, b = a = b, b = x, x = a;
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ var x, b, b2, b3, b4, b5, b11, b16;
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";
x.a = b, b = x.a, x.a2 = b2, b2 = x.a2, x.a3 = b3, b3 = x.a3, x.a4 = b4, b4 = x.a4, x.a5 = b5, b5 = x.a5, x.a11 = b11, b11 = x.a11, x.a16 = b16, b16 = x.a16;
x.a = b, b = x.a, x.a2 = b2, b2 = x.a2, x.a3 = b3, b3 = x.a3, x.a4 = b4, b4 = x.a4, x.a5 = b5, b5 = x.a5, x.a11 = b11, b11 = x.a11, x.a16 = b16, x.a16;
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ var x, b, b2, b3, b4, b5, b11, b16;
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";
x.a = b, b = x.a, x.a2 = b2, b2 = x.a2, x.a3 = b3, b3 = x.a3, x.a4 = b4, b4 = x.a4, x.a5 = b5, b5 = x.a5, x.a11 = b11, b11 = x.a11, x.a16 = b16, b16 = x.a16;
x.a = b, b = x.a, x.a2 = b2, b2 = x.a2, x.a3 = b3, b3 = x.a3, x.a4 = b4, b4 = x.a4, x.a5 = b5, b5 = x.a5, x.a11 = b11, b11 = x.a11, x.a16 = b16, x.a16;
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
//// [assignmentCompatWithDiscriminatedUnion.ts]
var Example1, Example2, Example3, Example4, Example5, GH14865, GH30170, GH12052, GH18421, GH15907, GH20889, GH39357;
Example1 || (Example1 = {}), t = s, Example2 || (Example2 = {}), t = s, Example3 || (Example3 = {}), t = s, Example4 || (Example4 = {}), t = s, Example5 || (Example5 = {}), t = s, function(GH14865) {
var b1, a = {
type: "A",
data: "whatevs"
};
a.type, b1.type, b1 = a;
}(GH14865 || (GH14865 = {})), GH30170 || (GH30170 = {}), function(GH12052) {
Example1 || (Example1 = {}), t = s, Example2 || (Example2 = {}), t = s, Example3 || (Example3 = {}), t = s, Example4 || (Example4 = {}), t = s, Example5 || (Example5 = {}), t = s, GH14865 || (GH14865 = {}), (void 0).type, GH30170 || (GH30170 = {}), function(GH12052) {
var getAxisType = function() {
return "categorical";
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
var SimpleTypes, ObjectTypes;
import "@swc/helpers/_/_class_call_check";
SimpleTypes || (SimpleTypes = {}), function(ObjectTypes) {
var t, t2, b, a2 = {
var t2, b, a2 = {
foo: a2
}, b2 = {
foo: b2
};
b2 = a2 = b2, a2 = b, a2 = t2, a2 = t;
b2 = a2 = b2, a2 = b, a2 = t2;
}(ObjectTypes || (ObjectTypes = {}));
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,4 @@ var TargetHasOptional, SourceHasOptional, Base = function Base() {
"use strict";
_class_call_check(this, Base);
};
!function(TargetHasOptional) {
var c, a, d, e, f;
new Base(), c = d, c = e, c = f, c = a, a = d, a = e, a = f, a = c;
}(TargetHasOptional || (TargetHasOptional = {})), function(SourceHasOptional) {
var c, a, d, e, f;
new Base(), c = d, c = e, c = f, c = a, a = d, a = e, a = f, a = c;
}(SourceHasOptional || (SourceHasOptional = {}));
TargetHasOptional || (TargetHasOptional = {}), new Base(), SourceHasOptional || (SourceHasOptional = {}), new Base();
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,4 @@ var TargetHasOptional, SourceHasOptional, Base = function Base() {
"use strict";
_class_call_check(this, Base);
};
TargetHasOptional || (TargetHasOptional = {}), new Base(), function(SourceHasOptional) {
var c, a, d, e, f;
new Base(), c = d, c = e, c = f, c = a, a = d, a = e, a = f, a = c;
}(SourceHasOptional || (SourceHasOptional = {}));
TargetHasOptional || (TargetHasOptional = {}), new Base(), SourceHasOptional || (SourceHasOptional = {}), new Base();
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ import "@swc/helpers/_/_class_call_check";
x: ""
}, function(E) {
E[E.A = 0] = "A";
}(E || (E = {})), E = void 0, E = void 0;
}(E || (E = {}));
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
//// [emitCompoundExponentiationOperator1.ts]
var comp;
comp = Math.pow(comp = Math.pow(comp = Math.pow(comp = Math.pow(comp = Math.pow(comp = Math.pow(comp = Math.pow(comp = Math.pow(comp = Math.pow(comp = Math.pow(comp = Math.pow(comp = Math.pow(comp = Math.pow(comp = Math.pow(comp = Math.pow(comp = Math.pow(comp = Math.pow(comp = Math.pow(comp, 1), Math.pow(comp, comp)), Math.pow(comp, Math.pow(comp, 2))), Math.pow(comp, comp) + 2), Math.pow(comp, comp) - 2), 2 * Math.pow(comp, comp)), Math.pow(comp, comp) / 2), Math.pow(comp, comp) % 2), Math.pow(comp - 2, 5)), Math.pow(comp + 2, 5)), Math.pow(2 * comp, 5)), Math.pow(comp / 2, 5)), Math.pow(comp % 2, 5)), Math.pow(comp, 7)), Math.pow(comp, 3)), Math.pow(comp, 10)), Math.pow(comp, 2.5)), Math.pow(comp, 1));
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
//// [emitCompoundExponentiationOperator2.ts]
var comp;
comp = Math.pow(comp = Math.pow(comp = Math.pow(comp = Math.pow(comp = Math.pow(comp = Math.pow(comp = Math.pow(comp = Math.pow(comp = Math.pow(comp = Math.pow(comp = Math.pow(comp = Math.pow(comp = Math.pow(comp = Math.pow(comp = Math.pow(comp = Math.pow(comp = Math.pow(comp = Math.pow(comp, 1), comp = Math.pow(comp, 1)), comp = Math.pow(comp, 3)), comp = Math.pow(comp, -1)), comp = Math.pow(comp, 2)), comp = Math.pow(comp, 0.5)), comp = Math.pow(comp, 3)), comp = Math.pow(comp, -1)), comp = Math.pow(comp, 2)), comp = Math.pow(comp, 0.5)), comp = Math.pow(comp, 9)), comp = Math.pow(comp, -15)), comp = Math.pow(comp, 32)), comp = Math.pow(comp, 0.015625)), comp = Math.pow(comp, 27)), comp = Math.pow(comp, 1)), comp = Math.pow(comp, 32)), comp = Math.pow(comp, 0.015625));
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
//// [intersectionAndUnionTypes.ts]
var a, b, anb, aob, cnd, cod, x, y;
a = anb, b = anb, anb = a, x = anb = b, x = aob, x = cnd, anb = x = cod, aob = x, cnd = x, cod = x, y = anb, y = aob, y = cnd, anb = y = cod, aob = y, cnd = y, cod = y;
a = anb, b = anb, anb = a, x = anb = b, x = aob, x = cnd, anb = x = cod, aob = x, cnd = x, cod = x, y = anb, y = aob, y = cnd, anb = y = cod, aob = y, cnd = y;
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
//// [intersectionTypeAssignment.ts]
var a, b, x, y;
a = x, x = a = y, y = a, b = x, x = b = y, y = x = y = b;
var a, x, y;
a = x, x = a = y, x = y = a;
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ var E, M;
import "@swc/helpers/_/_class_call_check";
!function(E) {
E[E.A = 0] = "A";
}(E || (E = {})), (M || (M = {})).a = 1, M = !0;
}(E || (E = {})), (M || (M = {})).a = 1;
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//// [invalidNumberAssignments.ts]
var M;
import "@swc/helpers/_/_class_call_check";
(M || (M = {})).x = 1, M = 1;
(M || (M = {})).x = 1;
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ var x, E, M;
import "@swc/helpers/_/_class_call_check";
!function(E) {
E[E.A = 0] = "A";
}(E || (E = {})), (E = x).A = x, I = x, (M || (M = {})).x = 1, M = x;
}(E || (E = {})), (E = x).A = x, I = x, (M || (M = {})).x = 1;
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,4 @@ var x, M, E;
import "@swc/helpers/_/_class_call_check";
(M || (M = {})).x = 1, M = x, function(E) {
E[E.A = 0] = "A";
}(E || (E = {})), x = E, x = E.A, x = {
f: function() {}
};
}(E || (E = {})), x = E, x = E.A;
11 changes: 4 additions & 7 deletions crates/swc/tests/tsc-references/literalTypes2.2.minified.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,8 @@ g1(1), g2(1, 1), g2(1, 2), g3(1, "two"), g4(1), g5(1, 2), g6([
return x;
}), g8(1, function(x) {
return x + 1;
});
var aa = [
}), function(a, x) {
a.slice().push(1);
}([
0
];
aa = function(a, x) {
var result = a.slice();
return result.push(1), result;
}(aa, 0);
], 0);
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ var a, x = {}, y = {
};
x = a, y = a, a = x, a = y;
var s = "fooo";
a = 123, a = !0, s = a = s, a = 123, a = !0, a = "string";
a = 123, a = !0, s = a = s;
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
//// [recursiveIntersectionTypes.ts]
var entityList, productList;
entityList.name, entityList.next.name, entityList.next.next.name, entityList.next.next.next.name, productList = entityList = productList;
var entityList;
entityList.name, entityList.next.name, entityList.next.next.name, entityList.next.next.next.name;
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
//// [strictTupleLength.ts]
t0.length, t1.length, t2.length, arr.length;
var t0, t2, arr, t1 = t2, t2 = t1;
arr = t1 = arr;
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
//// [stringLiteralTypeAssertion01.ts]
var s, t, str;
s = t, s = t, s = str, t = s = str, t = s, t = str, t = str, str = s, str = s, str = t, str = t;
s = t, s = t, s = str, t = s = str, t = s, t = str, t = str, str = s, str = s, str = t;
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ function fun3() {
}
!function(n1) {
var a = n1.a = fun1("Hello", "World"), b = n1.b = fun1("Hello", "Hello"), c = n1.c = fun2("Hello", "World"), d = n1.d = fun2("Hello", "Hello"), e = n1.e = fun3("Hello", "Hello", "World", "Foo");
a = takeReturnString(a), b = takeReturnString(b), c = takeReturnString(c), d = takeReturnString(d), e = takeReturnString(e), a = takeReturnHello(a), b = takeReturnHello(b), c = takeReturnHello(c), d = takeReturnHello(d), e = takeReturnHello(e), a = takeReturnHelloWorld(a), b = takeReturnHelloWorld(b), c = takeReturnHelloWorld(c), d = takeReturnHelloWorld(d), e = takeReturnHelloWorld(e);
a = takeReturnString(a), b = takeReturnString(b), c = takeReturnString(c), d = takeReturnString(d), e = takeReturnString(e), a = takeReturnHello(a), b = takeReturnHello(b), c = takeReturnHello(c), d = takeReturnHello(d), e = takeReturnHello(e), a = takeReturnHelloWorld(a), b = takeReturnHelloWorld(b), c = takeReturnHelloWorld(c), d = takeReturnHelloWorld(d), takeReturnHelloWorld(e);
}(n1 || (n1 = {})), function(n2) {
var a = n2.a = fun1("Hello", "Hello"), b = n2.b = fun1("Hello", "World"), c = n2.c = fun2("Hello", "Hello"), d = n2.d = fun2("Hello", "World"), e = n2.e = fun3("Hello", "World");
a = takeReturnString(a), b = takeReturnString(b), c = takeReturnString(c), d = takeReturnString(d), e = takeReturnString(e), a = takeReturnHello(a), b = takeReturnHello(b), c = takeReturnHello(c), d = takeReturnHello(d), e = takeReturnHello(e), a = takeReturnHelloWorld(a), b = takeReturnHelloWorld(b), c = takeReturnHelloWorld(c), d = takeReturnHelloWorld(d), e = takeReturnHelloWorld(e);
a = takeReturnString(a), b = takeReturnString(b), c = takeReturnString(c), d = takeReturnString(d), e = takeReturnString(e), a = takeReturnHello(a), b = takeReturnHello(b), c = takeReturnHello(c), d = takeReturnHello(d), e = takeReturnHello(e), a = takeReturnHelloWorld(a), b = takeReturnHelloWorld(b), c = takeReturnHelloWorld(c), d = takeReturnHelloWorld(d), takeReturnHelloWorld(e);
}(n2 || (n2 = {})), function(n3) {
var a = n3.a = fun2("Hello", "World"), b = n3.b = fun2("World", "Hello"), c = n3.c = fun2("Hello", "Hello"), d = n3.d = fun2("World", "World"), e = n3.e = fun3("Hello", "World");
a = takeReturnString(a), b = takeReturnString(b), c = takeReturnString(c), d = takeReturnString(d), e = takeReturnString(e), a = takeReturnHello(a), b = takeReturnHello(b), c = takeReturnHello(c), d = takeReturnHello(d), e = takeReturnHello(e), a = takeReturnHelloWorld(a), b = takeReturnHelloWorld(b), c = takeReturnHelloWorld(c), d = takeReturnHelloWorld(d), e = takeReturnHelloWorld(e);
a = takeReturnString(a), b = takeReturnString(b), c = takeReturnString(c), d = takeReturnString(d), e = takeReturnString(e), a = takeReturnHello(a), b = takeReturnHello(b), c = takeReturnHello(c), d = takeReturnHello(d), e = takeReturnHello(e), a = takeReturnHelloWorld(a), b = takeReturnHelloWorld(b), c = takeReturnHelloWorld(c), d = takeReturnHelloWorld(d), takeReturnHelloWorld(e);
}(n3 || (n3 = {}));
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
//// [typeGuardsInFunction.ts]
var strOrNum;
strOrNum = "string" == typeof strOrNum && strOrNum;
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//// [unionTypesAssignability.ts]
var unionNumberString, unionDE, num, str, c, d, e, anyVar;
var unionNumberString, unionDE, num, str, c, d, e;
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";
c = d, c = e, c = unionDE, d = e, e = d = unionDE, e = unionDE, num = str, str = num = unionNumberString, str = unionNumberString, d = c, e = c, unionDE = c, e = d, unionDE = d, d = e, unionDE = e, str = num, unionNumberString = num, num = str, unionNumberString = str, anyVar = unionDE, unionDE = anyVar = unionNumberString, unionNumberString = anyVar, unionDE = null, unionNumberString = null, unionDE = void 0, unionNumberString = void 0;
c = d, c = e, c = unionDE, d = e, e = d = unionDE, e = unionDE, num = str, str = num = unionNumberString, str = unionNumberString, d = c, e = c, unionDE = c, e = d, unionDE = d, d = e, unionDE = e, str = num, unionNumberString = num, num = str;
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//// [validEnumAssignments.ts]
var E, n, e;
var E;
!function(E) {
E[E.A = 0] = "A", E[E.B = 1] = "B";
}(E || (E = {})), n = e, n = E.A, E.A, e = E.A, e = E.B, e = n, e = null, e = void 0, e = 1, e = 1., e = 1.0, e = -1;
}(E || (E = {})), E.A, E.A, E.A, E.B;
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ var E, M;
import "@swc/helpers/_/_class_call_check";
!function(E) {
E[E.A = 0] = "A";
}(E || (E = {})), E.A = null, I = null, (M || (M = {})).x = 1, M = null;
}(E || (E = {})), E.A = null, I = null, (M || (M = {})).x = 1;
20 changes: 13 additions & 7 deletions crates/swc_ecma_minifier/src/compress/optimize/dead_code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,21 @@ where
/// throw x();
/// }
/// ```
pub(super) fn optimize_in_fn_termination(&mut self, e: &mut Expr) {
/// # Returns
///
/// returns true if `e` is changed.
pub(super) fn optimize_last_expr_before_termination(&mut self, e: &mut Expr) -> bool {
if !self.options.dead_code {
return;
return false;
}

// A return statement in a try block may not terminate function.
if self.ctx.in_try_block {
return;
return false;
}

if let Expr::Assign(assign @ AssignExpr { op: op!("="), .. }) = e {
self.optimize_in_fn_termination(&mut assign.right);
self.optimize_last_expr_before_termination(&mut assign.right);

// We only handle identifiers on lhs for now.
if let Some(lhs) = assign.left.as_ident() {
Expand All @@ -53,7 +56,7 @@ where
.data
.vars
.get(&lhs.to_id())
.map(|var| var.is_fn_local && !var.declared_as_fn_param)
.map(|var| var.declared && var.is_fn_local && !var.declared_as_fn_param)
.unwrap_or(false)
{
report_change!(
Expand All @@ -62,7 +65,7 @@ where
);
self.changed = true;
*e = *assign.right.take();
return;
return true;
}
}
}
Expand All @@ -76,7 +79,7 @@ where
.data
.vars
.get(&lhs.to_id())
.map(|var| var.is_fn_local)
.map(|var| var.declared && var.is_fn_local)
.unwrap_or(false)
{
report_change!(
Expand All @@ -91,9 +94,12 @@ where
left: Box::new(Expr::Ident(lhs.clone())),
right: assign.right.take(),
});
return true;
}
}
}
}

false
}
}