in-place eliminate_spurious_blocks()

This commit is contained in:
alexlamsl 2017-06-16 15:37:37 +08:00
parent c2174d06b2
commit 2de466331a

View File

@ -668,7 +668,7 @@ merge(Compressor.prototype, {
var CHANGED, max_iter = 10; var CHANGED, max_iter = 10;
do { do {
CHANGED = false; CHANGED = false;
statements = eliminate_spurious_blocks(statements); eliminate_spurious_blocks(statements);
if (compressor.option("dead_code")) { if (compressor.option("dead_code")) {
eliminate_dead_code(statements, compressor); eliminate_dead_code(statements, compressor);
} }
@ -890,25 +890,27 @@ merge(Compressor.prototype, {
function eliminate_spurious_blocks(statements) { function eliminate_spurious_blocks(statements) {
var seen_dirs = []; var seen_dirs = [];
return statements.reduce(function(a, stat){ for (var i = 0; i < statements.length;) {
var stat = statements[i];
if (stat instanceof AST_BlockStatement) { if (stat instanceof AST_BlockStatement) {
CHANGED = true; CHANGED = true;
a.push.apply(a, eliminate_spurious_blocks(stat.body)); eliminate_spurious_blocks(stat.body);
[].splice.apply(statements, [i, 1].concat(stat.body));
i += stat.body.length;
} else if (stat instanceof AST_EmptyStatement) { } else if (stat instanceof AST_EmptyStatement) {
CHANGED = true; CHANGED = true;
statements.splice(i, 1);
} else if (stat instanceof AST_Directive) { } else if (stat instanceof AST_Directive) {
if (seen_dirs.indexOf(stat.value) < 0) { if (seen_dirs.indexOf(stat.value) < 0) {
a.push(stat); i++;
seen_dirs.push(stat.value); seen_dirs.push(stat.value);
} else { } else {
CHANGED = true; CHANGED = true;
statements.splice(i, 1);
}
} else i++;
} }
} else {
a.push(stat);
} }
return a;
}, []);
};
function handle_if_return(statements, compressor) { function handle_if_return(statements, compressor) {
var self = compressor.self(); var self = compressor.self();