feat(es/minifier): Improve ignore_return_value (#4673)

This commit is contained in:
Donny/강동윤 2022-05-16 01:22:21 +09:00 committed by GitHub
parent d420d2d275
commit e57123e61c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
22 changed files with 104 additions and 89 deletions

View File

@ -1,4 +1,2 @@
var Symbol;
({
[Symbol.foo]: 0
})[Symbol.foo];
Symbol.foo, Symbol.foo;

View File

@ -1 +1 @@
({})[0], (function() {})[0];
(function() {})[0];

View File

@ -1,3 +1 @@
({
[this.bar()]: 1
})[0];
this.bar();

View File

@ -1,3 +1 @@
({
[this.bar()]: 1
})[0];
this.bar();

View File

@ -1,3 +1 @@
({
[super.bar()]: 1
})[0];
super.bar();

View File

@ -1,3 +1 @@
({
[super.bar()]: 1
})[0];
super.bar();

View File

@ -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 = {}));

View File

@ -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() {

View File

@ -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;

View File

@ -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;

View File

@ -1,2 +1,2 @@
var c, i, o2;
({})[''], c[''], i[''], o2[''];
var i, o2;
(void 0)[''], i[''], o2[''];

View File

@ -1,5 +1,4 @@
import * as swcHelpers from "@swc/helpers";
({})[""];
var i, o2, C = function() {
"use strict";
swcHelpers.classCallCheck(this, C);

View File

@ -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"];

View File

@ -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"];

View File

@ -1 +0,0 @@
({})[Symbol.nonsense];

View File

@ -1,2 +1,2 @@
import * as swcHelpers from "@swc/helpers";
swcHelpers.defineProperty({}, Symbol.nonsense, 0), ({})[Symbol.nonsense];
swcHelpers.defineProperty({}, Symbol.nonsense, 0);

View File

@ -1,3 +0,0 @@
({
[Symbol.for]: 0
})[Symbol.for];

View File

@ -1,4 +1,2 @@
var M;
M || (M = {}), ({
[Symbol.iterator]: 0
})[(void 0).iterator];
M || (M = {}), (void 0).iterator;

View File

@ -1,4 +1,2 @@
var M;
M || (M = {}), ({
[Symbol.iterator]: 0
})[(void 0).iterator];
M || (M = {}), (void 0).iterator;

View File

@ -1,3 +0,0 @@
({
[Symbol.iterator]: 0
})[Symbol.nonsense];

View File

@ -779,26 +779,55 @@ impl Pure<'_> {
}
}
// terser compiles
//
// [1,foo(),...bar()][{foo}]
//
// as
//
// foo(),basr(),foo;
Expr::Member(MemberExpr {
obj,
prop: MemberProp::Computed(prop),
..
}) => {
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![];
if let Expr::Array(obj_arr) = &mut **obj {
//
for ExprOrSpread { mut expr, .. } in
obj_arr.elems.take().into_iter().flatten()
{
for ExprOrSpread { mut expr, .. } in arr.elems.take().into_iter().flatten() {
self.ignore_return_value(
&mut expr,
DropOpts {
@ -813,15 +842,43 @@ impl Pure<'_> {
}
}
exprs.push(prop.expr.take());
*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}]
//
// as
//
// foo(),basr(),foo;
Expr::Member(MemberExpr {
obj,
prop: MemberProp::Computed(prop),
..
}) => match &**obj {
Expr::Object(..) | Expr::Array(..) => {
self.ignore_return_value(obj, opts);
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;
}
};
}
_ => {}
},
_ => {}
}
}