enhance side_effects (#4727)

This commit is contained in:
Alex Lam S.L
2021-03-03 10:47:17 +00:00
committed by GitHub
parent 04ed818f0a
commit 2a49760032
6 changed files with 64 additions and 43 deletions

View File

@@ -4797,9 +4797,15 @@ merge(Compressor.prototype, {
|| this.right.has_side_effects(compressor); || this.right.has_side_effects(compressor);
}); });
def(AST_Binary, function(compressor) { def(AST_Binary, function(compressor) {
return this.left.has_side_effects(compressor) var lhs = this.left;
|| this.right.has_side_effects(compressor) if (lhs.has_side_effects(compressor)) return true;
|| this.operator == "in" && !is_object(this.right); var rhs = this.right;
var op = this.operator;
if (!rhs.has_side_effects(compressor)) return op == "in" && !is_object(rhs);
if (op == "&&" && rhs instanceof AST_PropAccess && lhs.equivalent_to(rhs.expression)) {
return rhs instanceof AST_Sub && rhs.property.has_side_effects(compressor);
}
return true;
}); });
def(AST_Block, function(compressor) { def(AST_Block, function(compressor) {
return any(this.body, compressor); return any(this.body, compressor);
@@ -7208,33 +7214,42 @@ merge(Compressor.prototype, {
return node; return node;
}); });
def(AST_Binary, function(compressor, first_in_statement) { def(AST_Binary, function(compressor, first_in_statement) {
if (this.operator == "in" && !is_object(this.right)) { var left = this.left;
var left = this.left.drop_side_effect_free(compressor, first_in_statement); var right = this.right;
if (left === this.left) return this; var op = this.operator;
if (op == "in" && !is_object(right)) {
var lhs = left.drop_side_effect_free(compressor, first_in_statement);
if (lhs === left) return this;
var node = this.clone(); var node = this.clone();
node.left = left || make_node(AST_Number, this.left, { node.left = lhs || make_node(AST_Number, left, { value: 0 });
value: 0
});
return node; return node;
} }
var right = this.right.drop_side_effect_free(compressor, first_in_statement); var rhs = right.drop_side_effect_free(compressor, first_in_statement);
if (!right) return this.left.drop_side_effect_free(compressor, first_in_statement); if (!rhs) return left.drop_side_effect_free(compressor, first_in_statement);
if (lazy_op[this.operator] && !(right instanceof AST_Function)) { if (lazy_op[op] && !(rhs instanceof AST_Function)) {
var node = this; var node = this;
if (right !== node.right) { if (op == "&&"
node = this.clone(); && rhs instanceof AST_PropAccess
node.right = right.drop_side_effect_free(compressor); && left.equivalent_to(rhs.expression)
&& !left.has_side_effects(compressor)) {
var prop = rhs instanceof AST_Sub && rhs.property.drop_side_effect_free(compressor);
if (!prop) return left.drop_side_effect_free(compressor, first_in_statement);
node = node.clone();
node.right = prop;
} else if (rhs !== right) {
node = node.clone();
node.right = rhs.drop_side_effect_free(compressor);
} }
if (this.operator == "??") return node; if (op == "??") return node;
return (first_in_statement ? best_of_statement : best_of_expression)(node, make_node(AST_Binary, this, { return (first_in_statement ? best_of_statement : best_of_expression)(node, make_node(AST_Binary, this, {
operator: node.operator == "&&" ? "||" : "&&", operator: op == "&&" ? "||" : "&&",
left: node.left.negate(compressor, first_in_statement), left: left.negate(compressor, first_in_statement),
right: node.right right: node.right,
})); }));
} else { } else {
var left = this.left.drop_side_effect_free(compressor, first_in_statement); var lhs = left.drop_side_effect_free(compressor, first_in_statement);
if (!left) return right; if (!lhs) return rhs;
return make_sequence(this, [ left, right.drop_side_effect_free(compressor) ]); return make_sequence(this, [ lhs, rhs.drop_side_effect_free(compressor) ]);
} }
}); });
def(AST_Call, function(compressor, first_in_statement) { def(AST_Call, function(compressor, first_in_statement) {

View File

@@ -2863,7 +2863,7 @@ lvalues_def: {
expect: { expect: {
var a = 0, b = 1; var a = 0, b = 1;
a = b++, b = +void 0; a = b++, b = +void 0;
a && a[a++]; a && a++;
console.log(a, b); console.log(a, b);
} }
expect_stdout: true expect_stdout: true
@@ -5098,7 +5098,7 @@ issue_2878: {
} }
b = f2(); b = f2();
a = 1; a = 1;
b && b.b; b && b[console];
f2(); f2();
})(); })();
console.log(c); console.log(c);
@@ -5111,7 +5111,7 @@ issue_2878: {
} }
b = f2(), b = f2(),
a = 1, a = 1,
b && b.b, b && console,
f2(); f2();
})(), })(),
console.log(c); console.log(c);
@@ -6546,7 +6546,7 @@ issue_3520: {
(function f() { (function f() {
c = 0; c = 0;
var i = void 0; var i = void 0;
var f = f && f[i]; var f = f && f[console && i];
})(); })();
a += b; a += b;
c && b++; c && b++;
@@ -6560,7 +6560,7 @@ issue_3520: {
for (var i = 2; --i >= 0;) { for (var i = 2; --i >= 0;) {
(function() { (function() {
c = 0; c = 0;
var f = f && f[void 0]; var f = f && f[console && void 0];
})(); })();
a += b; a += b;
c && b++; c && b++;

View File

@@ -463,15 +463,19 @@ drop_fargs: {
var a = 1; var a = 1;
!function(a_1) { !function(a_1) {
a++; a++;
}(a++ + (a && a.var)); }(a++ + (a && console.log(a)));
console.log(a); console.log(a);
} }
expect: { expect: {
var a = 1; var a = 1;
++a && a.var, a++; ++a && console.log(a),
a++;
console.log(a); console.log(a);
} }
expect_stdout: "3" expect_stdout: [
"2",
"3",
]
} }
keep_fargs: { keep_fargs: {
@@ -486,13 +490,17 @@ keep_fargs: {
var a = 1; var a = 1;
!function(a_1) { !function(a_1) {
a++; a++;
}(a++ + (a && a.var)); }(a++ + (a && console.log(a)));
console.log(a); console.log(a);
} }
expect: { expect: {
var a = 1; var a = 1;
++a && a.var, a++; ++a && console.log(a),
a++;
console.log(a); console.log(a);
} }
expect_stdout: "3" expect_stdout: [
"2",
"3",
]
} }

View File

@@ -2723,7 +2723,7 @@ issue_4135: {
var c = function() { var c = function() {
var d = 0; var d = 0;
function f() { function f() {
d && d.p; d && d[console];
} }
f(); f();
this; this;
@@ -2735,7 +2735,7 @@ issue_4135: {
0; 0;
a++; a++;
if (!a) if (!a)
c = (a++, c = 0, void (c && c.p)); c = (a++, c = 0, void (c && console));
var c; var c;
console.log(a, -1, c); console.log(a, -1, c);
} }

View File

@@ -2554,7 +2554,7 @@ side_effects_assign: {
console.log(a); console.log(a);
} }
expect: { expect: {
var a = typeof void (a && a.in); var a = "undefined";
console.log(a); console.log(a);
} }
expect_stdout: "undefined" expect_stdout: "undefined"
@@ -2595,9 +2595,7 @@ pure_getters_2: {
var a; var a;
var a = a && a.b; var a = a && a.b;
} }
expect: { expect: {}
var a = a && a.b;
}
} }
pure_getters_3: { pure_getters_3: {

View File

@@ -745,12 +745,12 @@ issue_2062: {
} }
input: { input: {
var a = 1; var a = 1;
if ([ a || a++ + a--, a++ + a--, a && a.var ]); if ([ a || a++ + a--, a++ + a--, a && a[console] ]);
console.log(a); console.log(a);
} }
expect: { expect: {
var a = 1; var a = 1;
a || (a++, a--), a++, --a && a.var; a || (a++, a--), a++, --a && console;
console.log(a); console.log(a);
} }
expect_stdout: "1" expect_stdout: "1"
@@ -1088,7 +1088,7 @@ issue_3490_1: {
if ({ if ({
3: function() { 3: function() {
var a; var a;
return (a && a.p) < this; return (a && a[console]) < this;
}(), }(),
}) c = "PASS"; }) c = "PASS";
if (b) while ("" == typeof d); if (b) while ("" == typeof d);
@@ -1098,7 +1098,7 @@ issue_3490_1: {
var b = 42, c = "FAIL"; var b = 42, c = "FAIL";
if (function() { if (function() {
var a; var a;
a && a.p; a && console;
}(), c = "PASS", b) while ("" == typeof d); }(), c = "PASS", b) while ("" == typeof d);
console.log(c, b); console.log(c, b);
} }