fix assignment extraction from conditional (#1651)
fixes #1645 fixes #1646
This commit is contained in:
@@ -3550,19 +3550,17 @@ merge(Compressor.prototype, {
|
|||||||
}
|
}
|
||||||
var consequent = self.consequent;
|
var consequent = self.consequent;
|
||||||
var alternative = self.alternative;
|
var alternative = self.alternative;
|
||||||
|
// if (foo) exp = something; else exp = something_else;
|
||||||
|
// |
|
||||||
|
// v
|
||||||
|
// exp = foo ? something : something_else;
|
||||||
if (consequent instanceof AST_Assign
|
if (consequent instanceof AST_Assign
|
||||||
&& alternative instanceof AST_Assign
|
&& alternative instanceof AST_Assign
|
||||||
&& consequent.operator == alternative.operator
|
&& consequent.operator == alternative.operator
|
||||||
&& consequent.left.equivalent_to(alternative.left)
|
&& consequent.left.equivalent_to(alternative.left)
|
||||||
&& (!consequent.left.has_side_effects(compressor)
|
&& (!self.condition.has_side_effects(compressor)
|
||||||
|| !self.condition.has_side_effects(compressor))
|
|| consequent.operator == "="
|
||||||
) {
|
&& !consequent.left.has_side_effects(compressor))) {
|
||||||
/*
|
|
||||||
* Stuff like this:
|
|
||||||
* if (foo) exp = something; else exp = something_else;
|
|
||||||
* ==>
|
|
||||||
* exp = foo ? something : something_else;
|
|
||||||
*/
|
|
||||||
return make_node(AST_Assign, self, {
|
return make_node(AST_Assign, self, {
|
||||||
operator: consequent.operator,
|
operator: consequent.operator,
|
||||||
left: consequent.left,
|
left: consequent.left,
|
||||||
|
|||||||
@@ -893,3 +893,43 @@ equality_conditionals_true: {
|
|||||||
}
|
}
|
||||||
expect_stdout: true
|
expect_stdout: true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
issue_1645_1: {
|
||||||
|
options = {
|
||||||
|
conditionals: true,
|
||||||
|
}
|
||||||
|
input: {
|
||||||
|
var a = 100, b = 10;
|
||||||
|
(b = a) ? a++ + (b += a) ? b += a : b += a : b ^= a;
|
||||||
|
console.log(a, b);
|
||||||
|
}
|
||||||
|
expect: {
|
||||||
|
var a = 100, b = 10;
|
||||||
|
(b = a) ? (a++ + (b += a), b += a) : b ^= a;
|
||||||
|
console.log(a,b);
|
||||||
|
}
|
||||||
|
expect_stdout: true
|
||||||
|
}
|
||||||
|
|
||||||
|
issue_1645_2: {
|
||||||
|
options = {
|
||||||
|
conditionals: true,
|
||||||
|
}
|
||||||
|
input: {
|
||||||
|
var a = 0;
|
||||||
|
function f() {
|
||||||
|
return a++;
|
||||||
|
}
|
||||||
|
f() ? a += 2 : a += 4;
|
||||||
|
console.log(a);
|
||||||
|
}
|
||||||
|
expect: {
|
||||||
|
var a = 0;
|
||||||
|
function f(){
|
||||||
|
return a++;
|
||||||
|
}
|
||||||
|
f() ? a += 2 : a += 4;
|
||||||
|
console.log(a);
|
||||||
|
}
|
||||||
|
expect_stdout: true
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user