CHANGED = true;
stat = stat.clone();
stat.condition = stat.condition.negate(compressor);
+ var body = as_statement_array(stat.alternative).concat(ret);
+ var funs = extract_functions_from_statement_array(body);
stat.body = make_node(AST_BlockStatement, stat, {
- body: as_statement_array(stat.alternative).concat(ret)
+ body: body
});
stat.alternative = null;
- ret = [ stat.transform(compressor) ];
+ ret = funs.concat([ stat.transform(compressor) ]);
continue loop;
}
//---
};
+ function extract_functions_from_statement_array(statements) {
+ var funs = [];
+ for (var i = statements.length - 1; i >= 0; --i) {
+ var stat = statements[i];
+ if (stat instanceof AST_Defun) {
+ statements.splice(i, 1);
+ funs.unshift(stat);
+ }
+ }
+ return funs;
+ }
+
function extract_declarations_from_unreachable_code(compressor, stat, target) {
if (!(stat instanceof AST_Defun)) {
compressor.warn("Dropping unreachable code [{file}:{line},{col}]", stat.start);
--- /dev/null
+hoist_funs_when_handling_if_return_rerversal: {
+ options = { if_return: true, hoist_funs: false };
+ input: {
+ "use strict";
+
+ ( function() {
+ if ( !window ) {
+ return;
+ }
+
+ function f() {}
+ function g() {}
+ } )();
+ }
+ expect: {
+ "use strict";
+
+ ( function() {
+ function f() {}
+ function g() {}
+
+ // NOTE: other compression steps will reduce this
+ // down to just `window`.
+ if ( window );
+ } )();
+ }
+}