Skip to content

Commit

Permalink
enhance evaluate & reduce_vars (#5060)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexlamsl committed Jul 7, 2021
1 parent 2340fef commit 6577d64
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 47 deletions.
65 changes: 27 additions & 38 deletions lib/compress.js
Original file line number Diff line number Diff line change
Expand Up @@ -734,7 +734,12 @@ merge(Compressor.prototype, {
var save = fixed;
if (save) fixed = function() {
var value = save();
return is_undefined(value) ? make_sequence(node, [ value, node.value ]) : node;
var ev;
if (is_undefined(value, compressor)
|| (ev = fuzzy_eval(compressor, value, true)) === undefined) {
return make_sequence(node, [ value, node.value ]);
}
return ev instanceof AST_Node ? node : value;
};
node.name.walk(scanner);
fixed = save;
Expand Down Expand Up @@ -7293,7 +7298,7 @@ merge(Compressor.prototype, {
node.in_bool = true;
var value = node.value;
if (value) {
var ev = value.is_truthy() || value.evaluate(compressor, true);
var ev = fuzzy_eval(compressor, value);
if (!ev) {
value = value.drop_side_effect_free(compressor);
node.value = value ? make_sequence(node.value, [
Expand Down Expand Up @@ -8006,7 +8011,7 @@ merge(Compressor.prototype, {

OPT(AST_Do, function(self, compressor) {
if (!compressor.option("loops")) return self;
var cond = self.condition.is_truthy() || self.condition.evaluate(compressor, true);
var cond = fuzzy_eval(compressor, self.condition);
if (!(cond instanceof AST_Node)) {
if (cond && !has_loop_control(self, compressor.parent(), AST_Continue)) return make_node(AST_For, self, {
body: make_node(AST_BlockStatement, self.body, {
Expand Down Expand Up @@ -8174,36 +8179,25 @@ merge(Compressor.prototype, {
if (self.step) self.step = self.step.drop_side_effect_free(compressor);
}
if (self.condition) {
var cond = self.condition.evaluate(compressor);
if (cond instanceof AST_Node) {
cond = self.condition.is_truthy() || self.condition.evaluate(compressor, true);
} else if (cond) {
self.condition = null;
}
var cond = fuzzy_eval(compressor, self.condition);
if (!cond) {
if (compressor.option("dead_code")) {
var body = [];
if (is_statement(self.init)) {
body.push(self.init);
} else if (self.init) {
body.push(make_node(AST_SimpleStatement, self.init, {
body: self.init
}));
body.push(make_node(AST_SimpleStatement, self.init, { body: self.init }));
}
body.push(make_node(AST_SimpleStatement, self.condition, {
body: self.condition
}));
body.push(make_node(AST_SimpleStatement, self.condition, { body: self.condition }));
extract_declarations_from_unreachable_code(compressor, self.body, body);
return make_node(AST_BlockStatement, self, { body: body }).optimize(compressor);
}
} else if (self.condition && !(cond instanceof AST_Node)) {
} else if (!(cond instanceof AST_Node)) {
self.body = make_node(AST_BlockStatement, self.body, {
body: [
make_node(AST_SimpleStatement, self.condition, {
body: self.condition
}),
self.body
]
make_node(AST_SimpleStatement, self.condition, { body: self.condition }),
self.body,
],
});
self.condition = null;
}
Expand Down Expand Up @@ -8894,7 +8888,7 @@ merge(Compressor.prototype, {
if (!compressor.option("optional_chains")) return;
if (!self.optional) return;
var expr = self.expression;
var ev = expr.evaluate(compressor, true);
var ev = fuzzy_eval(compressor, expr, true);
if (ev == null) return make_node(AST_UnaryPrefix, self, {
operator: "void",
expression: expr,
Expand Down Expand Up @@ -10492,7 +10486,7 @@ merge(Compressor.prototype, {
}
// (x || false) && y ---> x ? y : false
if (self.left.operator == "||") {
var lr = self.left.right.evaluate(compressor, true);
var lr = fuzzy_eval(compressor, self.left.right);
if (!lr) return make_node(AST_Conditional, self, {
condition: self.left.left,
consequent: self.right,
Expand Down Expand Up @@ -10545,7 +10539,7 @@ merge(Compressor.prototype, {
}
// x && true || y ---> x ? true : y
if (!nullish && self.left.operator == "&&") {
var lr = self.left.right.is_truthy() || self.left.right.evaluate(compressor, true);
var lr = fuzzy_eval(compressor, self.left.right);
if (lr && !(lr instanceof AST_Node)) return make_node(AST_Conditional, self, {
condition: self.left.left,
consequent: self.left.right,
Expand Down Expand Up @@ -11070,21 +11064,16 @@ merge(Compressor.prototype, {
}
var local = self.fixed !== def.fixed;
if (fixed && (local || def.should_replace !== false)) {
var init;
var ev, init;
if (fixed instanceof AST_This) {
if (!is_funarg(def) && same_scope(def)) {
init = fixed;
}
} else {
var ev = fixed.evaluate(compressor, true);
if (ev !== fixed
&& typeof ev != "function"
&& (typeof ev != "object"
|| ev instanceof RegExp
&& compressor.option("unsafe_regexp")
&& !def.cross_loop && same_scope(def))) {
init = make_node_from_constant(ev, fixed);
}
if (!is_funarg(def) && same_scope(def)) init = fixed;
} else if ((ev = fixed.evaluate(compressor, true)) !== fixed
&& typeof ev != "function"
&& (ev === null
|| typeof ev != "object"
|| compressor.option("unsafe_regexp")
&& ev instanceof RegExp && !def.cross_loop && same_scope(def))) {
init = make_node_from_constant(ev, fixed);
}
if (init) {
if (!local && def.should_replace === undefined) {
Expand Down
55 changes: 49 additions & 6 deletions test/compress/default-values.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ process_boolean_returns: {
}
expect: {
console.log(function(a = console.log("FAIL 1")) {
return a() ? "PASS" : "FAIL 2";
return 42 ? "PASS" : "FAIL 2";
}(function() {
return 1;
}));
Expand Down Expand Up @@ -245,21 +245,64 @@ maintain_if: {
node_version: ">=6"
}

reduce_value: {
reduce_funarg: {
options = {
evaluate: true,
keep_fargs: false,
reduce_vars: true,
unused: true,
}
input: {
console.log(function(a = "PASS") {
return a;
console.log(...function(a = "foo", b = "bar", c = "baz") {
return [ a, b, c ];
}(void 0, null));
}
expect: {
console.log(...function() {
return [ "foo", null, "baz" ];
}());
}
expect_stdout: "foo null baz"
node_version: ">=6"
}

reduce_array: {
options = {
evaluate: true,
reduce_vars: true,
toplevel: true,
unsafe: true,
unused: true,
}
input: {
var [ a = "foo", b = "bar", c = "baz" ] = [ void 0, null ];
console.log(a, b, c);
}
expect: {
console.log("PASS");
var [ , , c = "baz" ] = [ void 0, null ];
console.log("foo", null, c);
}
expect_stdout: "PASS"
expect_stdout: "foo null baz"
node_version: ">=6"
}

reduce_object: {
options = {
evaluate: true,
reduce_vars: true,
toplevel: true,
unsafe: true,
unused: true,
}
input: {
var { a = "foo", b = "bar", c = "baz" } = { a: void 0, b: null };
console.log(a, b, c);
}
expect: {
var { c = "baz" } = { a: void 0, b: null };
console.log("foo", null, c);
}
expect_stdout: "foo null baz"
node_version: ">=6"
}

Expand Down
4 changes: 1 addition & 3 deletions test/compress/hoist_vars.js
Original file line number Diff line number Diff line change
Expand Up @@ -323,9 +323,7 @@ issue_4893_1: {
expect: {
try{
(function f() {
var b;
b = null;
b.p += 42;
null.p += 42;
f;
})();
} catch (e) {
Expand Down
1 change: 1 addition & 0 deletions test/compress/reduce_vars.js
Original file line number Diff line number Diff line change
Expand Up @@ -1292,6 +1292,7 @@ toplevel_on_loops_3: {
loops: true,
reduce_funcs: true,
reduce_vars: true,
side_effects: true,
toplevel: true,
unused: true,
}
Expand Down

0 comments on commit 6577d64

Please sign in to comment.