Skip to content

Commit

Permalink
feat(es/minifier): Inline across side-effect-free member exprs, (#10056)
Browse files Browse the repository at this point in the history
**Description:**

This is safe even in the presence of an impure setter because JS eval rhs **before** setter

```js
({ set a(v) { console.log(v) }}).a = (console.log(1), 2)
```

would output `1 2`, and gcc has this optimization too.
  • Loading branch information
Austaras authored Feb 20, 2025
1 parent e78b9d1 commit 19d01d7
Show file tree
Hide file tree
Showing 84 changed files with 1,862 additions and 2,541 deletions.
6 changes: 6 additions & 0 deletions .changeset/long-monkeys-kiss.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
swc_core: minor
swc_ecma_minifier: minor
---

feat(es/minifier): Seq inline across side effect free member
98 changes: 47 additions & 51 deletions crates/swc/tests/fixture/issues-8xxx/8880/output/1.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,56 +9,52 @@ function a(module, exports, farmRequire, farmDynamicRequire) {
const _dayjs = _interop_require_default._(farmRequire("d0dc4dad"));
farmRequire("15d5169f");
(0, _dep734fea04.c)(function(module, exports) {
var e__1, _;
e__1 = _dep734fea04.a, module.exports = function(e__2) {
var e__3;
var t = e__2 && "object" == (0, _typeof.default)(e__2) && "default" in e__2 ? e__2 : {
default: e__2
}, d = {
name: "zh-cn",
weekdays: "星期日_星期一_星期二_星期三_星期四_星期五_星期六".split("_"),
weekdaysShort: "周日_周一_周二_周三_周四_周五_周六".split("_"),
weekdaysMin: "日_一_二_三_四_五_六".split("_"),
months: "一月_二月_三月_四月_五月_六月_七月_八月_九月_十月_十一月_十二月".split("_"),
monthsShort: "1月_2月_3月_4月_5月_6月_7月_8月_9月_10月_11月_12月".split("_"),
ordinal: function ordinal(e, _) {
return "W" === _ ? e + "周" : e + "日";
},
weekStart: 1,
yearStart: 4,
formats: {
LT: "HH:mm",
LTS: "HH:mm:ss",
L: "YYYY/MM/DD",
LL: "YYYY年M月D日",
LLL: "YYYY年M月D日Ah点mm分",
LLLL: "YYYY年M月D日ddddAh点mm分",
l: "YYYY/M/D",
ll: "YYYY年M月D日",
lll: "YYYY年M月D日 HH:mm",
llll: "YYYY年M月D日dddd HH:mm"
},
relativeTime: {
future: "%s内",
past: "%s前",
s: "几秒",
m: "1 分钟",
mm: "%d 分钟",
h: "1 小时",
hh: "%d 小时",
d: "1 天",
dd: "%d 天",
M: "1 个月",
MM: "%d 个月",
y: "1 年",
yy: "%d 年"
},
meridiem: function meridiem(e, _) {
var t = 100 * e + _;
return t < 600 ? "凌晨" : t < 900 ? "早上" : t < 1100 ? "上午" : t < 1300 ? "中午" : t < 1800 ? "下午" : "晚上";
}
};
return t["default"].locale(d, null, !0), d;
}(_dayjs.default);
var e__1, e__3, t, d;
e__1 = _dep734fea04.a, module.exports = (t = (e__3 = _dayjs.default) && "object" == (0, _typeof.default)(e__3) && "default" in e__3 ? e__3 : {
default: e__3
}, d = {
name: "zh-cn",
weekdays: "星期日_星期一_星期二_星期三_星期四_星期五_星期六".split("_"),
weekdaysShort: "周日_周一_周二_周三_周四_周五_周六".split("_"),
weekdaysMin: "日_一_二_三_四_五_六".split("_"),
months: "一月_二月_三月_四月_五月_六月_七月_八月_九月_十月_十一月_十二月".split("_"),
monthsShort: "1月_2月_3月_4月_5月_6月_7月_8月_9月_10月_11月_12月".split("_"),
ordinal: function ordinal(e, _) {
return "W" === _ ? e + "周" : e + "日";
},
weekStart: 1,
yearStart: 4,
formats: {
LT: "HH:mm",
LTS: "HH:mm:ss",
L: "YYYY/MM/DD",
LL: "YYYY年M月D日",
LLL: "YYYY年M月D日Ah点mm分",
LLLL: "YYYY年M月D日ddddAh点mm分",
l: "YYYY/M/D",
ll: "YYYY年M月D日",
lll: "YYYY年M月D日 HH:mm",
llll: "YYYY年M月D日dddd HH:mm"
},
relativeTime: {
future: "%s内",
past: "%s前",
s: "几秒",
m: "1 分钟",
mm: "%d 分钟",
h: "1 小时",
hh: "%d 小时",
d: "1 天",
dd: "%d 天",
M: "1 个月",
MM: "%d 个月",
y: "1 年",
yy: "%d 年"
},
meridiem: function meridiem(e, _) {
var t = 100 * e + _;
return t < 600 ? "凌晨" : t < 900 ? "早上" : t < 1100 ? "上午" : t < 1300 ? "中午" : t < 1800 ? "下午" : "晚上";
}
}, t["default"].locale(d, null, !0), d);
});
}
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 = /*#__PURE__*/ function() {
var A, A1, Point = /*#__PURE__*/ function() {
function Point(x, y) {
_class_call_check(this, Point), this.x = x, this.y = y;
}
Expand All @@ -11,9 +11,9 @@ var A, Point, A1, Point1 = /*#__PURE__*/ function() {
};
}, Point;
}();
(Point1 || (Point1 = {})).Origin = function() {
(Point || (Point = {})).Origin = function() {
return null;
}, A = A1 || (A1 = {}), Point = /*#__PURE__*/ function() {
}, (A = A1 || (A1 = {})).Point = /*#__PURE__*/ function() {
function Point(x, y) {
_class_call_check(this, Point), this.x = x, this.y = y;
}
Expand All @@ -23,6 +23,6 @@ var A, Point, A1, Point1 = /*#__PURE__*/ function() {
y: 0
};
}, Point;
}(), A.Point = Point, (A.Point || (A.Point = {})).Origin = function() {
}(), (A.Point || (A.Point = {})).Origin = function() {
return "";
};
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 = /*#__PURE__*/ function() {
var A, A1, Point = /*#__PURE__*/ 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 = /*#__PURE__*/ function() {
};
}, Point;
}();
Point1 || (Point1 = {}), A = A1 || (A1 = {}), Point = /*#__PURE__*/ function() {
Point || (Point = {}), (A = A1 || (A1 = {})).Point = /*#__PURE__*/ function() {
function Point(x, y) {
_class_call_check(this, Point), this.x = x, this.y = y;
}
Expand All @@ -21,4 +21,4 @@ Point1 || (Point1 = {}), A = A1 || (A1 = {}), Point = /*#__PURE__*/ function() {
y: 0
};
}, Point;
}(), A.Point = Point, A.Point || (A.Point = {});
}(), A.Point || (A.Point = {});
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
//// [ES5SymbolProperty2.ts]
var M, M1, _$Symbol, C;
var M, _$Symbol, C;
import { _ as _class_call_check } from "@swc/helpers/_/_class_call_check";
M1 = M || (M = {}), C = /*#__PURE__*/ function() {
(M || (M = {})).C = C = /*#__PURE__*/ function() {
function C() {
_class_call_check(this, C);
}
return C.prototype[_$Symbol.iterator] = function() {}, C;
}(), M1.C = C, (new C)[_$Symbol.iterator], (new M.C)[Symbol.iterator];
}(), (new C)[_$Symbol.iterator], (new M.C)[Symbol.iterator];
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
//// [ExportClassWhichExtendsInterfaceWithInaccessibleType.ts]
var A, A1, Point2d;
var A;
import { _ as _class_call_check } from "@swc/helpers/_/_class_call_check";
A1 = A || (A = {}), Point2d = /*#__PURE__*/ function() {
(A || (A = {})).Point2d = /*#__PURE__*/ 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;
}();
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
//// [ExportClassWithAccessibleTypesInTypeParameterConstraintsClassHeritageListMemberTypeAnnotations.ts]
var A, A1, Point, Point3d;
var A, A1, Point;
import { _ as _call_super } from "@swc/helpers/_/_call_super";
import { _ as _class_call_check } from "@swc/helpers/_/_class_call_check";
import { _ as _inherits } from "@swc/helpers/_/_inherits";
A1 = A || (A = {}), Point = function Point() {
(A1 = A || (A = {})).Point = Point = function Point() {
_class_call_check(this, Point);
}, A1.Point = Point, A1.Origin = {
}, A1.Origin = {
x: 0,
y: 0
}, Point3d = /*#__PURE__*/ function(Point) {
}, A1.Point3d = /*#__PURE__*/ function(Point) {
function Point3d() {
return _class_call_check(this, Point3d), _call_super(this, Point3d, arguments);
}
return _inherits(Point3d, Point), Point3d;
}(Point), A1.Point3d = Point3d, A1.Origin3d = {
}(Point), A1.Origin3d = {
x: 0,
y: 0,
z: 0
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
//// [ExportClassWithInaccessibleTypeInTypeParameterConstraint.ts]
var A, A1, Point3d, Line;
var A, A1;
import { _ as _call_super } from "@swc/helpers/_/_call_super";
import { _ as _class_call_check } from "@swc/helpers/_/_class_call_check";
import { _ as _inherits } from "@swc/helpers/_/_inherits";
(A1 = A || (A = {})).Origin = {
x: 0,
y: 0
}, Point3d = /*#__PURE__*/ function(Point) {
}, A1.Point3d = /*#__PURE__*/ function(Point) {
function Point3d() {
return _class_call_check(this, Point3d), _call_super(this, Point3d, arguments);
}
return _inherits(Point3d, Point), Point3d;
}(function Point() {
_class_call_check(this, Point);
}), A1.Point3d = Point3d, A1.Origin3d = {
}), A1.Origin3d = {
x: 0,
y: 0,
z: 0
}, Line = /*#__PURE__*/ function() {
}, A1.Line = /*#__PURE__*/ function() {
function Line(start, end) {
_class_call_check(this, Line), this.start = start, this.end = end;
}
return Line.fromorigin2d = function(p) {
return null;
}, Line;
}(), A1.Line = Line;
}();
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ var A, A1, Line;
import { _ as _class_call_check } from "@swc/helpers/_/_class_call_check";
(A1 = A || (A = {})).Point = function Point() {
_class_call_check(this, Point);
}, Line = function Line(start, end) {
}, A1.Line = Line = function Line(start, end) {
_class_call_check(this, Line), this.start = start, this.end = end;
}, A1.Line = Line, A1.fromOrigin = function(p) {
}, A1.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;
import { _ as _class_call_check } from "@swc/helpers/_/_class_call_check";
A1 = A || (A = {}), Line = function Line(start, end) {
(A1 = A || (A = {})).Line = Line = function Line(start, end) {
_class_call_check(this, Line), this.start = start, this.end = end;
}, A1.Line = Line, A1.fromOrigin = function(p) {
}, A1.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 @@
//// [ExportModuleWithAccessibleTypesOnItsExportedMembers.ts]
var A, A1, Point, B, Line;
var A, A1, Point, B;
import { _ as _class_call_check } from "@swc/helpers/_/_class_call_check";
A1 = A || (A = {}), Point = function Point(x, y) {
(A1 = A || (A = {})).Point = 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 = /*#__PURE__*/ function() {
}, (B = A1.B || (A1.B = {})).Origin = new Point(0, 0), B.Line = /*#__PURE__*/ function() {
function Line(start, end) {
_class_call_check(this, Line);
}
Expand All @@ -13,4 +13,4 @@ A1 = A || (A = {}), Point = function Point(x, y) {
y: 0
}, p);
}, Line;
}(), B.Line = Line;
}();
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
//// [asyncAwaitIsolatedModules_es2017.ts]
var M;
!function(M) {
async function f1() {}
M.f1 = f1;
}(M || (M = {}));
(M || (M = {})).f1 = async function() {};
export { };
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
var _B, A = function A() {
_class_call_check(this, A);
};
A.B = ((_B = /*#__PURE__*/ function() {
(_B = /*#__PURE__*/ function() {
function B() {
_class_call_check(this, B);
}
Expand Down Expand Up @@ -35,4 +35,4 @@ A.B = ((_B = /*#__PURE__*/ function() {
});
})();
}, C;
}(), _B), A.B.C.func();
}(), A.B = _B, A.B.C.func();
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
//// [asyncAwait_es2017.ts]
var M;
!function(M) {
async function f1() {}
M.f1 = f1;
}(M || (M = {}));
(M || (M = {})).f1 = async function() {};
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
//// [circularImportAlias.ts]
var B, A, B1, D, A1;
var B, A, B1, A1;
import { _ as _call_super } from "@swc/helpers/_/_call_super";
import { _ as _class_call_check } from "@swc/helpers/_/_class_call_check";
import { _ as _inherits } from "@swc/helpers/_/_inherits";
(B1 = B || (B = {})).a = A, D = /*#__PURE__*/ function(_B_a_C) {
(B1 = B || (B = {})).a = A, B1.D = /*#__PURE__*/ function(_B_a_C) {
function D() {
return _class_call_check(this, D), _call_super(this, D, arguments);
}
return _inherits(D, _B_a_C), D;
}(B1.a.C), B1.D = D, (A1 = A || (A = {})).C = function C() {
}(B1.a.C), (A1 = A || (A = {})).C = function C() {
_class_call_check(this, C);
}, A1.b = B, new B.a.C();
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//// [classAbstractImportInstantiation.ts]
var M, A, M1;
var A, M;
import { _ as _class_call_check } from "@swc/helpers/_/_class_call_check";
M = M1 || (M1 = {}), A = function A() {
(M || (M = {})).A = A = function A() {
_class_call_check(this, A);
}, M.A = A, new A, new M1.A;
}, new A, new M.A;
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
//// [classAbstractInAModule.ts]
var M, M1, A, B;
var M, M1, A;
import { _ as _call_super } from "@swc/helpers/_/_call_super";
import { _ as _class_call_check } from "@swc/helpers/_/_class_call_check";
import { _ as _inherits } from "@swc/helpers/_/_inherits";
M1 = M || (M = {}), A = function A() {
(M1 = M || (M = {})).A = A = function A() {
_class_call_check(this, A);
}, M1.A = A, B = /*#__PURE__*/ function(A) {
}, M1.B = /*#__PURE__*/ function(A) {
function B() {
return _class_call_check(this, B), _call_super(this, B, arguments);
}
return _inherits(B, A), B;
}(A), M1.B = B, new M.A, new M.B;
}(A), new M.A, new M.B;
Loading

0 comments on commit 19d01d7

Please sign in to comment.