Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enhance conditionals #3243

Merged
merged 1 commit into from
Aug 29, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 33 additions & 24 deletions lib/compress.js
Original file line number Diff line number Diff line change
Expand Up @@ -6047,20 +6047,22 @@ merge(Compressor.prototype, {
// |
// v
// exp = foo ? something : something_else;
if (consequent instanceof AST_Assign
&& alternative instanceof AST_Assign
&& consequent.operator == alternative.operator
&& consequent.left.equivalent_to(alternative.left)
&& (!self.condition.has_side_effects(compressor)
|| consequent.operator == "="
&& !consequent.left.has_side_effects(compressor))) {
var seq_tail = consequent.tail_node();
var alt_tail = alternative.tail_node();
if (seq_tail instanceof AST_Assign
&& alt_tail instanceof AST_Assign
&& seq_tail.operator == alt_tail.operator
&& seq_tail.left.equivalent_to(alt_tail.left)
&& (!condition.has_side_effects(compressor)
|| seq_tail.operator == "="
&& !seq_tail.left.has_side_effects(compressor))) {
return make_node(AST_Assign, self, {
operator: consequent.operator,
left: consequent.left,
operator: seq_tail.operator,
left: seq_tail.left,
right: make_node(AST_Conditional, self, {
condition: self.condition,
consequent: consequent.right,
alternative: alternative.right
condition: condition,
consequent: pop_lhs(consequent),
alternative: pop_lhs(alternative)
})
});
}
Expand All @@ -6071,12 +6073,12 @@ merge(Compressor.prototype, {
&& consequent.args.length > 0
&& consequent.args.length == alternative.args.length
&& consequent.expression.equivalent_to(alternative.expression)
&& !self.condition.has_side_effects(compressor)
&& !condition.has_side_effects(compressor)
&& !consequent.expression.has_side_effects(compressor)
&& typeof (arg_index = single_arg_diff()) == "number") {
var node = consequent.clone();
node.args[arg_index] = make_node(AST_Conditional, self, {
condition: self.condition,
condition: condition,
consequent: consequent.args[arg_index],
alternative: alternative.args[arg_index]
});
Expand All @@ -6087,7 +6089,7 @@ merge(Compressor.prototype, {
&& consequent.alternative.equivalent_to(alternative)) {
return make_node(AST_Conditional, self, {
condition: make_node(AST_Binary, self, {
left: self.condition,
left: condition,
operator: "&&",
right: consequent.condition
}),
Expand All @@ -6098,7 +6100,7 @@ merge(Compressor.prototype, {
// x ? y : y --> x, y
if (consequent.equivalent_to(alternative)) {
return make_sequence(self, [
self.condition,
condition,
consequent
]).optimize(compressor);
}
Expand All @@ -6107,7 +6109,7 @@ merge(Compressor.prototype, {
&& consequent.tail_node().equivalent_to(alternative.tail_node())) {
return make_sequence(self, [
make_node(AST_Conditional, self, {
condition: self.condition,
condition: condition,
consequent: pop_seq(consequent),
alternative: pop_seq(alternative)
}),
Expand All @@ -6122,7 +6124,7 @@ merge(Compressor.prototype, {
operator: "||",
left: make_node(AST_Binary, self, {
operator: "&&",
left: self.condition,
left: condition,
right: consequent.left
}),
right: alternative
Expand All @@ -6132,40 +6134,40 @@ merge(Compressor.prototype, {
if (is_true(self.consequent)) {
if (is_false(self.alternative)) {
// c ? true : false ---> !!c
return booleanize(self.condition);
return booleanize(condition);
}
// c ? true : x ---> !!c || x
return make_node(AST_Binary, self, {
operator: "||",
left: booleanize(self.condition),
left: booleanize(condition),
right: self.alternative
});
}
if (is_false(self.consequent)) {
if (is_true(self.alternative)) {
// c ? false : true ---> !c
return booleanize(self.condition.negate(compressor));
return booleanize(condition.negate(compressor));
}
// c ? false : x ---> !c && x
return make_node(AST_Binary, self, {
operator: "&&",
left: booleanize(self.condition.negate(compressor)),
left: booleanize(condition.negate(compressor)),
right: self.alternative
});
}
if (is_true(self.alternative)) {
// c ? x : true ---> !c || x
return make_node(AST_Binary, self, {
operator: "||",
left: booleanize(self.condition.negate(compressor)),
left: booleanize(condition.negate(compressor)),
right: self.consequent
});
}
if (is_false(self.alternative)) {
// c ? x : false ---> !!c && x
return make_node(AST_Binary, self, {
operator: "&&",
left: booleanize(self.condition),
left: booleanize(condition),
right: self.consequent
});
}
Expand Down Expand Up @@ -6217,6 +6219,13 @@ merge(Compressor.prototype, {
}
}

function pop_lhs(node) {
if (!(node instanceof AST_Sequence)) return node.right;
var exprs = node.expressions.slice();
exprs.push(exprs.pop().right);
return make_sequence(node, exprs);
}

function pop_seq(node) {
if (!(node instanceof AST_Sequence)) return make_node(AST_Number, node, {
value: 0
Expand Down
72 changes: 72 additions & 0 deletions test/compress/conditionals.js
Original file line number Diff line number Diff line change
Expand Up @@ -1292,3 +1292,75 @@ to_and_or: {
}
expect_stdout: true
}

cond_seq_assign_1: {
options = {
conditionals: true,
sequences: true,
}
input: {
function f(a) {
var t;
if (a) {
t = "foo";
t = "bar";
} else {
console.log(t);
t = 42;
}
console.log(t);
}
f(f);
f();
}
expect: {
function f(a) {
var t;
t = a ? (t = "foo", "bar") : (console.log(t), 42),
console.log(t);
}
f(f),
f();
}
expect_stdout: [
"bar",
"undefined",
"42",
]
}

cond_seq_assign_2: {
options = {
conditionals: true,
sequences: true,
}
input: {
function f(a) {
var t;
if (a) {
t = "foo";
a = "bar";
} else {
console.log(t);
t = 42;
}
console.log(t);
}
f(f);
f();
}
expect: {
function f(a) {
var t;
a ? (t = "foo", a = "bar") : (console.log(t), t = 42),
console.log(t);
}
f(f),
f();
}
expect_stdout: [
"foo",
"undefined",
"42",
]
}