mirror of
https://github.com/swc-project/swc.git
synced 2024-10-05 12:49:21 +03:00
feat(es/minifier): Improve ignore_return_value
(#4673)
This commit is contained in:
parent
d420d2d275
commit
e57123e61c
@ -1,4 +1,2 @@
|
||||
var Symbol;
|
||||
({
|
||||
[Symbol.foo]: 0
|
||||
})[Symbol.foo];
|
||||
Symbol.foo, Symbol.foo;
|
||||
|
@ -1 +1 @@
|
||||
({})[0], (()=>{})[0];
|
||||
(()=>{})[0];
|
||||
|
@ -1 +1 @@
|
||||
({})[0], (function() {})[0];
|
||||
(function() {})[0];
|
||||
|
@ -1,3 +1 @@
|
||||
({
|
||||
[this.bar()]: 1
|
||||
})[0];
|
||||
this.bar();
|
||||
|
@ -1,3 +1 @@
|
||||
({
|
||||
[this.bar()]: 1
|
||||
})[0];
|
||||
this.bar();
|
||||
|
@ -1,3 +1 @@
|
||||
({
|
||||
[super.bar()]: 1
|
||||
})[0];
|
||||
super.bar();
|
||||
|
@ -1,3 +1 @@
|
||||
({
|
||||
[super.bar()]: 1
|
||||
})[0];
|
||||
super.bar();
|
||||
|
@ -1,6 +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 = {})), ({
|
||||
1: !0
|
||||
})[1];
|
||||
}(G || (G = {}));
|
||||
|
@ -1,9 +1,7 @@
|
||||
import * as swcHelpers from "@swc/helpers";
|
||||
!function(G) {
|
||||
G[G.A = 1] = "A", G[G.B = 2] = "B", G[G.C = 3] = "C", G[G.D = 2] = "D";
|
||||
}(G || (G = {})), ({
|
||||
1: !0
|
||||
})[1];
|
||||
}(G || (G = {}));
|
||||
var G, C = function() {
|
||||
"use strict";
|
||||
function C() {
|
||||
|
@ -9,10 +9,4 @@ function foo1() {
|
||||
}
|
||||
(void 0 === tmp ? {
|
||||
b21: "string"
|
||||
} : tmp).b21, ({
|
||||
1: "string"
|
||||
})[1], ({
|
||||
1: !0
|
||||
})[1], ({
|
||||
2: !0
|
||||
})[1], foo1().prop1, foo1().prop2;
|
||||
} : tmp).b21, foo1().prop1, foo1().prop2;
|
||||
|
@ -9,10 +9,4 @@ function foo1() {
|
||||
}
|
||||
(void 0 === tmp ? {
|
||||
b21: "string"
|
||||
} : tmp).b21, ({
|
||||
1: "string"
|
||||
})[1], ({
|
||||
1: !0
|
||||
})[1], ({
|
||||
2: !0
|
||||
})[1], foo1().prop1, foo1().prop2;
|
||||
} : tmp).b21, foo1().prop1, foo1().prop2;
|
||||
|
@ -1,2 +1,2 @@
|
||||
var c, i, o2;
|
||||
({})[''], c[''], i[''], o2[''];
|
||||
var i, o2;
|
||||
(void 0)[''], i[''], o2[''];
|
||||
|
@ -1,5 +1,4 @@
|
||||
import * as swcHelpers from "@swc/helpers";
|
||||
({})[""];
|
||||
var i, o2, C = function() {
|
||||
"use strict";
|
||||
swcHelpers.classCallCheck(this, C);
|
||||
|
@ -1,5 +1,3 @@
|
||||
var i, a;
|
||||
(void 0)["a b"], (class {
|
||||
})['c d'], i["a b"], a["a b"], ({
|
||||
"a b": 1
|
||||
})["a b"];
|
||||
})['c d'], i["a b"], a["a b"];
|
||||
|
@ -3,6 +3,4 @@ var i, a, C = function() {
|
||||
"use strict";
|
||||
swcHelpers.classCallCheck(this, C);
|
||||
};
|
||||
(void 0)["a b"], C["c d"], i["a b"], a["a b"], ({
|
||||
"a b": 1
|
||||
})["a b"];
|
||||
(void 0)["a b"], C["c d"], i["a b"], a["a b"];
|
||||
|
@ -1 +0,0 @@
|
||||
({})[Symbol.nonsense];
|
@ -1,2 +1,2 @@
|
||||
import * as swcHelpers from "@swc/helpers";
|
||||
swcHelpers.defineProperty({}, Symbol.nonsense, 0), ({})[Symbol.nonsense];
|
||||
swcHelpers.defineProperty({}, Symbol.nonsense, 0);
|
||||
|
@ -1,3 +0,0 @@
|
||||
({
|
||||
[Symbol.for]: 0
|
||||
})[Symbol.for];
|
@ -1,4 +1,2 @@
|
||||
var M;
|
||||
M || (M = {}), ({
|
||||
[Symbol.iterator]: 0
|
||||
})[(void 0).iterator];
|
||||
M || (M = {}), (void 0).iterator;
|
||||
|
@ -1,4 +1,2 @@
|
||||
var M;
|
||||
M || (M = {}), ({
|
||||
[Symbol.iterator]: 0
|
||||
})[(void 0).iterator];
|
||||
M || (M = {}), (void 0).iterator;
|
||||
|
@ -1,3 +0,0 @@
|
||||
({
|
||||
[Symbol.iterator]: 0
|
||||
})[Symbol.nonsense];
|
@ -779,6 +779,77 @@ impl Pure<'_> {
|
||||
}
|
||||
}
|
||||
|
||||
Expr::Object(obj) => {
|
||||
if obj.props.iter().all(|p| match p {
|
||||
PropOrSpread::Spread(_) => false,
|
||||
PropOrSpread::Prop(p) => matches!(
|
||||
&**p,
|
||||
Prop::Shorthand(_) | Prop::KeyValue(_) | Prop::Method(..)
|
||||
),
|
||||
}) {
|
||||
let mut exprs = vec![];
|
||||
|
||||
for prop in obj.props.take() {
|
||||
if let PropOrSpread::Prop(p) = prop {
|
||||
match *p {
|
||||
Prop::Shorthand(p) => {
|
||||
exprs.push(Box::new(Expr::Ident(p)));
|
||||
}
|
||||
Prop::KeyValue(p) => {
|
||||
if let PropName::Computed(e) = p.key {
|
||||
exprs.push(e.expr);
|
||||
}
|
||||
|
||||
exprs.push(p.value);
|
||||
}
|
||||
Prop::Method(p) => {
|
||||
if let PropName::Computed(e) = p.key {
|
||||
exprs.push(e.expr);
|
||||
}
|
||||
}
|
||||
|
||||
_ => unreachable!(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
*e = self
|
||||
.make_ignored_expr(exprs.into_iter())
|
||||
.unwrap_or(Expr::Invalid(Invalid { span: DUMMY_SP }));
|
||||
report_change!("Ignored an object literal");
|
||||
self.changed = true;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
Expr::Array(arr) => {
|
||||
let mut exprs = vec![];
|
||||
|
||||
//
|
||||
|
||||
for ExprOrSpread { mut expr, .. } in arr.elems.take().into_iter().flatten() {
|
||||
self.ignore_return_value(
|
||||
&mut expr,
|
||||
DropOpts {
|
||||
drop_str_lit: true,
|
||||
drop_zero: true,
|
||||
drop_global_refs_if_unused: true,
|
||||
..opts
|
||||
},
|
||||
);
|
||||
if !expr.is_invalid() {
|
||||
exprs.push(expr);
|
||||
}
|
||||
}
|
||||
|
||||
*e = self
|
||||
.make_ignored_expr(exprs.into_iter())
|
||||
.unwrap_or(Expr::Invalid(Invalid { span: DUMMY_SP }));
|
||||
report_change!("Ignored an array literal");
|
||||
self.changed = true;
|
||||
return;
|
||||
}
|
||||
|
||||
// terser compiles
|
||||
//
|
||||
// [1,foo(),...bar()][{foo}]
|
||||
@ -790,38 +861,24 @@ impl Pure<'_> {
|
||||
obj,
|
||||
prop: MemberProp::Computed(prop),
|
||||
..
|
||||
}) => {
|
||||
let mut exprs = vec![];
|
||||
}) => match &**obj {
|
||||
Expr::Object(..) | Expr::Array(..) => {
|
||||
self.ignore_return_value(obj, opts);
|
||||
|
||||
if let Expr::Array(obj_arr) = &mut **obj {
|
||||
//
|
||||
|
||||
for ExprOrSpread { mut expr, .. } in
|
||||
obj_arr.elems.take().into_iter().flatten()
|
||||
{
|
||||
self.ignore_return_value(
|
||||
&mut expr,
|
||||
DropOpts {
|
||||
drop_str_lit: true,
|
||||
drop_zero: true,
|
||||
drop_global_refs_if_unused: true,
|
||||
..opts
|
||||
},
|
||||
);
|
||||
if !expr.is_invalid() {
|
||||
exprs.push(expr);
|
||||
match &**obj {
|
||||
Expr::Object(..) => {}
|
||||
_ => {
|
||||
*e = self
|
||||
.make_ignored_expr(
|
||||
vec![obj.take(), prop.expr.take()].into_iter(),
|
||||
)
|
||||
.unwrap_or(Expr::Invalid(Invalid { span: DUMMY_SP }));
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
exprs.push(prop.expr.take());
|
||||
|
||||
*e = self
|
||||
.make_ignored_expr(exprs.into_iter())
|
||||
.unwrap_or(Expr::Invalid(Invalid { span: DUMMY_SP }));
|
||||
self.changed = true;
|
||||
return;
|
||||
};
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
},
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user