optimize conditional when condition symbol matches consequent (#1684)
This commit is contained in:
@@ -3565,8 +3565,19 @@ merge(Compressor.prototype, {
|
|||||||
alternative: self.consequent
|
alternative: self.consequent
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
var condition = self.condition;
|
||||||
var consequent = self.consequent;
|
var consequent = self.consequent;
|
||||||
var alternative = self.alternative;
|
var alternative = self.alternative;
|
||||||
|
// x?x:y --> x||y
|
||||||
|
if (condition instanceof AST_SymbolRef
|
||||||
|
&& consequent instanceof AST_SymbolRef
|
||||||
|
&& condition.definition() === consequent.definition()) {
|
||||||
|
return make_node(AST_Binary, self, {
|
||||||
|
operator: "||",
|
||||||
|
left: condition,
|
||||||
|
right: alternative
|
||||||
|
});
|
||||||
|
}
|
||||||
// if (foo) exp = something; else exp = something_else;
|
// if (foo) exp = something; else exp = something_else;
|
||||||
// |
|
// |
|
||||||
// v
|
// v
|
||||||
|
|||||||
@@ -933,3 +933,32 @@ issue_1645_2: {
|
|||||||
}
|
}
|
||||||
expect_stdout: true
|
expect_stdout: true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
condition_symbol_matches_consequent: {
|
||||||
|
options = {
|
||||||
|
conditionals: true,
|
||||||
|
}
|
||||||
|
input: {
|
||||||
|
function foo(x, y) {
|
||||||
|
return x ? x : y;
|
||||||
|
}
|
||||||
|
function bar() {
|
||||||
|
return g ? g : h;
|
||||||
|
}
|
||||||
|
var g = 4;
|
||||||
|
var h = 5;
|
||||||
|
console.log(foo(3, null), foo(0, 7), foo(true, false), bar());
|
||||||
|
}
|
||||||
|
expect: {
|
||||||
|
function foo(x, y) {
|
||||||
|
return x || y;
|
||||||
|
}
|
||||||
|
function bar() {
|
||||||
|
return g || h;
|
||||||
|
}
|
||||||
|
var g = 4;
|
||||||
|
var h = 5;
|
||||||
|
console.log(foo(3, null), foo(0, 7), foo(true, false), bar());
|
||||||
|
}
|
||||||
|
expect_stdout: "3 7 true 4"
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user