Improve optimizing function() { if(c){return foo} bar();}

closes #1437
This commit is contained in:
Anthony Van de Gejuchte
2017-02-18 18:56:18 +08:00
committed by alexlamsl
parent 7f8d72d9d3
commit dd31d12a91
4 changed files with 107 additions and 8 deletions

View File

@@ -546,7 +546,7 @@ merge(Compressor.prototype, {
var self = compressor.self();
var multiple_if_returns = has_multiple_if_returns(statements);
var in_lambda = self instanceof AST_Lambda;
var ret = [];
var ret = []; // Optimized statements, build from tail to front
loop: for (var i = statements.length; --i >= 0;) {
var stat = statements[i];
switch (true) {
@@ -607,19 +607,21 @@ merge(Compressor.prototype, {
ret = funs.concat([ stat.transform(compressor) ]);
continue loop;
}
//---
// XXX: what was the intention of this case?
// if (a) return b; if (c) return d; e; ==> return a ? b : c ? d : void e;
//
// if sequences is not enabled, this can lead to an endless loop (issue #866).
// however, with sequences on this helps producing slightly better output for
// the example code.
if (compressor.option("sequences")
&& i > 0 && statements[i - 1] instanceof AST_If && statements[i - 1].body instanceof AST_Return
&& ret.length == 1 && in_lambda && ret[0] instanceof AST_SimpleStatement
&& (!stat.alternative || stat.alternative instanceof AST_SimpleStatement)) {
&& !stat.alternative) {
CHANGED = true;
ret.push(make_node(AST_Return, ret[0], {
value: make_node(AST_Undefined, ret[0])
}).transform(compressor));
ret = as_statement_array(stat.alternative).concat(ret);
ret.unshift(stat);
continue loop;
}