|| node instanceof AST_Undefined;
}
+ function declarations_only(node) {
+ return all(node.definitions, function(var_def) {
+ return !var_def.value;
+ });
+ }
+
+ function is_declaration(stat) {
+ return stat instanceof AST_Defun || stat instanceof AST_Var && declarations_only(stat);
+ }
+
function tighten_body(statements, compressor) {
var in_loop, in_try, scope;
find_loop_scope_try();
});
}
- function declarations_only(node) {
- return all(node.definitions, function(var_def) {
- return !var_def.value;
- });
- }
-
- function is_declaration(stat) {
- return stat instanceof AST_Defun || stat instanceof AST_Var && declarations_only(stat);
- }
-
function sequencesize(statements, compressor) {
if (statements.length < 2) return;
var seq = [], n = 0;
return self;
});
+ function trim_block(stat) {
+ switch (stat.body.length) {
+ case 1: return stat.body[0];
+ case 0: return make_node(AST_EmptyStatement, stat);
+ }
+ return stat;
+ }
+
OPT(AST_BlockStatement, function(self, compressor) {
self.body = tighten_body(self.body, compressor);
- switch (self.body.length) {
- case 1: return self.body[0];
- case 0: return make_node(AST_EmptyStatement, self);
- }
- return self;
+ return trim_block(self);
});
OPT(AST_Function, function(self, compressor) {
}).optimize(compressor);
}
}
+ if (self.body instanceof AST_BlockStatement) {
+ var body = self.body.body;
+ for (var i = body.length; --i >= 0;) {
+ var stat = body[i];
+ if (stat instanceof AST_If
+ && !stat.alternative
+ && stat.body instanceof AST_Break
+ && compressor.loopcontrol_target(stat.body) === compressor.self()) {
+ self.condition = make_node(AST_Binary, self, {
+ operator: "&&",
+ left: stat.condition.negate(compressor),
+ right: self.condition,
+ });
+ body.splice(i, 1);
+ } else if (stat instanceof AST_SimpleStatement) {
+ self.condition = make_sequence(self, [
+ stat.body,
+ self.condition,
+ ]);
+ body.splice(i, 1);
+ } else if (!is_declaration(stat)) {
+ break;
+ }
+ }
+ self.body = trim_block(self.body);
+ }
+ if (self.body instanceof AST_EmptyStatement) return make_node(AST_For, self, self).optimize(compressor);
if (self.body instanceof AST_SimpleStatement) return make_node(AST_For, self, {
condition: make_sequence(self.condition, [
self.body.body,