Skip to content

Commit

Permalink
enhance side_effects (#5578)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexlamsl authored Jul 27, 2022
1 parent da930af commit db6fd6d
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 13 deletions.
32 changes: 19 additions & 13 deletions lib/compress.js
Original file line number Diff line number Diff line change
Expand Up @@ -10497,6 +10497,14 @@ Compressor.prototype.compress = function(node) {
})).optimize(compressor);
}
break;
} else if (compressor.option("side_effects")
&& exp instanceof AST_Call
&& exp.args.length == 1
&& is_undeclared_ref(exp.expression)
&& exp.expression.name == "Object") {
var call = self.clone();
call.expression = maintain_this_binding(compressor, self, exp, exp.args[0]);
return call.optimize(compressor);
}
}
if (compressor.option("unsafe_Function")
Expand Down Expand Up @@ -11131,24 +11139,22 @@ Compressor.prototype.compress = function(node) {
});

OPT(AST_New, function(self, compressor) {
if (compressor.option("unsafe")) {
var exp = self.expression;
if (is_undeclared_ref(exp)) switch (exp.name) {
case "Array":
case "Error":
case "Function":
case "Object":
case "RegExp":
return make_node(AST_Call, self, self).transform(compressor);
}
}
if (compressor.option("sequences")) {
var seq = lift_sequence_in_expression(self, compressor);
if (seq !== self) return seq.optimize(compressor);
}
if (compressor.option("unused")) drop_unused_call_args(self, compressor);
if (compressor.option("unsafe")) {
var exp = self.expression;
if (is_undeclared_ref(exp)) {
switch (exp.name) {
case "Object":
case "RegExp":
case "Function":
case "Error":
case "Array":
return make_node(AST_Call, self, self).transform(compressor);
}
}
}
return self;
});

Expand Down
26 changes: 26 additions & 0 deletions test/compress/side_effects.js
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,32 @@ unsafe_string_replace: {
expect_stdout: "PASS"
}

unsafe_Object_call: {
options = {
side_effects: true,
unsafe: true,
}
input: {
var o = {
f: function(a) {
console.log(a ? this.p : "FAIL 1");
},
p: "FAIL 2",
}, p = "PASS";
Object(o.f)(42);
}
expect: {
var o = {
f: function(a) {
console.log(a ? this.p : "FAIL 1");
},
p: "FAIL 2",
}, p = "PASS";
(0, o.f)(42);
}
expect_stdout: "PASS"
}

drop_value: {
options = {
side_effects: true,
Expand Down

0 comments on commit db6fd6d

Please sign in to comment.