Skip to content

Commit

Permalink
fix(es/minifier): Fix the order of match arms to inline correctly (#1…
Browse files Browse the repository at this point in the history
…0053)

**Description:**

Well, this's kind of awkward

**Related issue:**

 - Closes #10041
  • Loading branch information
Austaras authored Feb 20, 2025
1 parent d3fbd21 commit f0f842d
Show file tree
Hide file tree
Showing 39 changed files with 2,618 additions and 2,683 deletions.
6 changes: 6 additions & 0 deletions .changeset/rotten-bananas-decide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
swc_core: patch
swc_ecma_minifier: patch
---

fix(es/minifier): Try inline value ignored assign
4 changes: 2 additions & 2 deletions crates/swc/tests/fixture/issues-8xxx/8880/output/1.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ function a(module, exports, farmRequire, farmDynamicRequire) {
farmRequire("15d5169f");
(0, _dep734fea04.c)(function(module, exports) {
var e__1, _;
e__1 = _dep734fea04.a, _ = function(e__2) {
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
Expand Down Expand Up @@ -59,6 +59,6 @@ function a(module, exports, farmRequire, farmDynamicRequire) {
}
};
return t["default"].locale(d, null, !0), d;
}, module.exports = _(_dayjs.default);
}(_dayjs.default);
});
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//// [computedPropertyNames11_ES5.ts]
import { _ as _define_enumerable_properties } from "@swc/helpers/_/_define_enumerable_properties";
var s, n, a, _obj, _mutatorMap = {};
_obj = {}, _mutatorMap[s] = _mutatorMap[s] || {}, _mutatorMap[s].get = function() {
var s, n, a, _mutatorMap = {};
_mutatorMap[s] = _mutatorMap[s] || {}, _mutatorMap[s].get = function() {
return 0;
}, _mutatorMap[n] = _mutatorMap[n] || {}, _mutatorMap[n].set = function(v) {}, _mutatorMap[s + s] = _mutatorMap[s + s] || {}, _mutatorMap[s + s].get = function() {
return 0;
Expand All @@ -13,4 +13,4 @@ _obj = {}, _mutatorMap[s] = _mutatorMap[s] || {}, _mutatorMap[s].get = function(
return 0;
}, _mutatorMap["hello bye"] = _mutatorMap["hello bye"] || {}, _mutatorMap["hello bye"].set = function(v) {}, _mutatorMap["hello ".concat(a, " bye")] = _mutatorMap["hello ".concat(a, " bye")] || {}, _mutatorMap["hello ".concat(a, " bye")].get = function() {
return 0;
}, _define_enumerable_properties(_obj, _mutatorMap);
}, _define_enumerable_properties({}, _mutatorMap);
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//// [computedPropertyNames1_ES5.ts]
import { _ as _define_enumerable_properties } from "@swc/helpers/_/_define_enumerable_properties";
var _obj, _mutatorMap = {};
_obj = {}, _mutatorMap[1] = _mutatorMap[1] || {}, _mutatorMap[1].get = function() {
var _mutatorMap = {};
_mutatorMap[1] = _mutatorMap[1] || {}, _mutatorMap[1].get = function() {
return 0;
}, _mutatorMap[1] = _mutatorMap[1] || {}, _mutatorMap[1].set = function(v) {}, _define_enumerable_properties(_obj, _mutatorMap);
}, _mutatorMap[1] = _mutatorMap[1] || {}, _mutatorMap[1].set = function(v) {}, _define_enumerable_properties({}, _mutatorMap);
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
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";
var y, y1, Base = function Base() {
var Base = function Base() {
_class_call_check(this, Base);
}, Derived = /*#__PURE__*/ function(Base) {
function Derived() {
Expand All @@ -15,8 +15,4 @@ var y, y1, Base = function Base() {
}
return _inherits(Derived2, Base), Derived2;
}(Base);
new Derived(), new Derived2(), new Derived(), new Derived2(), new Derived(), new Derived2(), y = function(x) {
return x;
}, new Base(), y(null), y1 = function(x) {
return x;
}, new Derived(), y1(null);
new Derived(), new Derived2(), new Derived(), new Derived2(), new Derived(), new Derived2(), new Base(), new Derived();
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//// [parserES5ComputedPropertyName4.ts]
import { _ as _define_enumerable_properties } from "@swc/helpers/_/_define_enumerable_properties";
var _obj, _mutatorMap = {};
_obj = {}, _mutatorMap[e] = _mutatorMap[e] || {}, _mutatorMap[e].get = function() {}, _define_enumerable_properties(_obj, _mutatorMap);
var _mutatorMap = {};
_mutatorMap[e] = _mutatorMap[e] || {}, _mutatorMap[e].get = function() {}, _define_enumerable_properties({}, _mutatorMap);
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
//// [privateNameComputedPropertyName2.ts]
let getX;
import { _ as _class_private_field_get } from "@swc/helpers/_/_class_private_field_get";
import { _ as _class_private_field_init } from "@swc/helpers/_/_class_private_field_init";
import { _ as _class_private_field_set } from "@swc/helpers/_/_class_private_field_set";
var _x = /*#__PURE__*/ new WeakMap();
let _tmp = (getX = (a)=>_class_private_field_get(a, _x), "_");
console.log(getX(new class {
[_tmp]() {}
console.log(_class_private_field_get(new class {
_() {}
constructor(){
_class_private_field_init(this, _x, {
writable: !0,
value: void 0
}), _class_private_field_set(this, _x, 100);
}
}));
}, _x));
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ function set_valueOne(v) {}
function set_valueCompound(v) {}
new class {
m() {
var _tmp, _tmp1;
let foo = {
bar: 1
};
Expand All @@ -24,13 +23,13 @@ new class {
o: {
foo
}
}), _tmp = {
}), _class_private_field_destructure(this, _value).value = _extends({}, _object_destructuring_empty({
foo
}, _class_private_field_destructure(this, _value).value = _extends({}, _object_destructuring_empty(_tmp)), ({ foo: _write_only_error("#value").foo } = {
})), ({ foo: _write_only_error("#value").foo } = {
foo
}), _tmp1 = {
}), _write_only_error("#value").foo = _extends({}, _object_destructuring_empty({
foo
}, _write_only_error("#value").foo = _extends({}, _object_destructuring_empty(_tmp1.foo)), _write_only_error("#value"), [_class_private_field_destructure(this, _valueOne).value, ..._class_private_field_destructure(this, _valueRest).value] = [
}.foo)), _write_only_error("#value"), [_class_private_field_destructure(this, _valueOne).value, ..._class_private_field_destructure(this, _valueRest).value] = [
1,
2,
3
Expand Down
24 changes: 12 additions & 12 deletions crates/swc/tests/tsc-references/variadicTuples1.2.minified.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@ function curry(f) {
return f.apply(void 0, _to_consumable_array(_$a).concat(_to_consumable_array(b)));
};
}
u = [
10,
!0
], [
[
1
].concat(_to_consumable_array([
'hello'
]), [
2
], _to_consumable_array(u), [
], _to_consumable_array([
10,
!0
]), [
3
]), concat([], []), concat([
'hello'
Expand All @@ -34,15 +34,15 @@ u = [
1,
2,
3
]), u1 = [
4,
5,
6
], _to_consumable_array([
]), _to_consumable_array([
1,
2,
3
]).concat(_to_consumable_array(u1)), ft1([
]).concat(_to_consumable_array([
4,
5,
6
])), ft1([
'hello',
42
]), ft2([
Expand All @@ -68,7 +68,7 @@ u = [
'def'
]
]);
var u, u1, fn1 = function(a1, b, c, d) {
var fn1 = function(a1, b, c, d) {
return 0;
};
curry(fn1), curry(fn1, 1), curry(fn1, 1, 'abc'), curry(fn1, 1, 'abc', !0), curry(fn1, 1, 'abc', !0, [
Expand Down
45 changes: 24 additions & 21 deletions crates/swc_ecma_minifier/src/compress/optimize/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -959,8 +959,30 @@ impl Optimizer<'_> {
}

Expr::Assign(AssignExpr {
op, left, right, ..
}) if left.is_simple() && !op.may_short_circuit() => {
op: op!("="),
left: AssignTarget::Simple(SimpleAssignTarget::Ident(i)),
right,
..
}) => {
let old = i.id.to_id();
self.store_var_for_inlining(&mut i.id, right, true);

if i.is_dummy() && self.options.unused {
report_change!("inline: Removed variable ({}{:?})", old.0, old.1);
self.vars.removed.insert(old);
}

if right.is_invalid() {
return None;
}
}

Expr::Assign(AssignExpr {
op,
left: left @ AssignTarget::Simple(_),
right,
..
}) if !op.may_short_circuit() => {
if let AssignTarget::Simple(expr) = left {
if let SimpleAssignTarget::Member(m) = expr {
if !m.obj.may_have_side_effects(self.ctx.expr_ctx)
Expand Down Expand Up @@ -990,25 +1012,6 @@ impl Optimizer<'_> {
return Some(e.take());
}

Expr::Assign(AssignExpr {
op: op!("="),
left: AssignTarget::Simple(SimpleAssignTarget::Ident(i)),
right,
..
}) => {
let old = i.id.to_id();
self.store_var_for_inlining(&mut i.id, right, true);

if i.is_dummy() && self.options.unused {
report_change!("inline: Removed variable ({}{:?})", old.0, old.1);
self.vars.removed.insert(old);
}

if right.is_invalid() {
return None;
}
}

// We drop `f.g` in
//
// function f() {
Expand Down
Loading

0 comments on commit f0f842d

Please sign in to comment.