diff --git a/lib/compress.js b/lib/compress.js index 7de3e535..ac5cd235 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -4005,7 +4005,7 @@ merge(Compressor.prototype, { } if (defs) defs.push(arg.definition()); } - return !defs || defs.length == 0 || !fn.body[0].is_reachable(defs); + return !defs || defs.length == 0 || !is_reachable(fn.body[0], defs); } function flatten_args(fn) { @@ -4845,7 +4845,7 @@ merge(Compressor.prototype, { return self; }); - AST_Node.DEFMETHOD("is_reachable", function(defs) { + function is_reachable(node, defs) { var reachable = false; var find_ref = new TreeWalker(function(node) { if (reachable) return true; @@ -4853,15 +4853,19 @@ merge(Compressor.prototype, { return reachable = true; } }); - this.walk(new TreeWalker(function(node) { + var scan_scope = new TreeWalker(function(node) { if (reachable) return true; if (node instanceof AST_Scope) { - node.walk(find_ref); + var parent = scan_scope.parent(); + if (!(parent instanceof AST_Call && parent.expression === node)) { + node.walk(find_ref); + } return true; } - })); + }); + node.walk(scan_scope); return reachable; - }); + } var ASSIGN_OPS = [ '+', '-', '/', '*', '%', '>>', '<<', '>>>', '|', '^', '&' ]; var ASSIGN_OPS_COMMUTATIVE = [ '*', '|', '^', '&' ]; @@ -4876,7 +4880,7 @@ merge(Compressor.prototype, { parent = compressor.parent(level++); if (parent instanceof AST_Exit) { if (in_try(level, parent instanceof AST_Throw)) break; - if (self.is_reachable([ def ])) break; + if (is_reachable(self, [ def ])) break; if (self.operator == "=") return self.right; return make_node(AST_Binary, self, { operator: self.operator.slice(0, -1), diff --git a/test/compress/functions.js b/test/compress/functions.js index 6d446432..888c6e3c 100644 --- a/test/compress/functions.js +++ b/test/compress/functions.js @@ -1509,7 +1509,7 @@ issue_2663_1: { function createFn(j) { return function() { console.log(j); - } + }; } for (i in { a: 1, b: 2, c: 3 }) o[i] = createFn(i); @@ -1523,7 +1523,7 @@ issue_2663_1: { function createFn(j) { return function() { console.log(j); - } + }; } for (i in { a: 1, b: 2, c: 3 }) o[i] = createFn(i); @@ -1539,6 +1539,40 @@ issue_2663_1: { } issue_2663_2: { + options = { + inline: true, + reduce_vars: true, + side_effects: true, + unused: true, + } + input: { + (function() { + var i; + function fn(j) { + return function() { + console.log(j); + }(); + } + for (i in { a: 1, b: 2, c: 3 }) + fn(i); + })(); + } + expect: { + (function() { + var i; + for (i in { a: 1, b: 2, c: 3 }) + j = i, console.log(j); + var j; + })(); + } + expect_stdout: [ + "a", + "b", + "c", + ] +} + +issue_2663_3: { options = { inline: true, reduce_vars: true,