From e9495bb82a4a26b3e483a811a480bfeea4a3ef2c Mon Sep 17 00:00:00 2001 From: alexlamsl Date: Tue, 18 Apr 2017 16:51:12 +0800 Subject: [PATCH] fix sub-optimal optimisation --- lib/compress.js | 5 ++--- test/compress/collapse_vars.js | 40 ++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 3 deletions(-) diff --git a/lib/compress.js b/lib/compress.js index 38c36dc2..a3641573 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -722,10 +722,9 @@ merge(Compressor.prototype, { var unwind = false; var tt = new TreeTransformer( function preorder(node) { - if (unwind) return node; + if (unwind || node instanceof AST_Scope && node !== scope) return node; var parent = tt.parent(); - if (node instanceof AST_Lambda - || node instanceof AST_Try + if (node instanceof AST_Try || node instanceof AST_With || node instanceof AST_Case || node instanceof AST_IterationStatement diff --git a/test/compress/collapse_vars.js b/test/compress/collapse_vars.js index 9faec6fc..0f82b743 100644 --- a/test/compress/collapse_vars.js +++ b/test/compress/collapse_vars.js @@ -1614,3 +1614,43 @@ reduce_vars_assign: { } expect_stdout: "0" } + +iife_1: { + options = { + collapse_vars: true, + reduce_vars: true, + toplevel: true, + unused: true, + } + input: { + var log = function(x) { + console.log(x); + }, foo = bar(); + log(foo); + } + expect: { + (function(x) { + console.log(x); + })(bar()); + } +} + +iife_2: { + options = { + collapse_vars: true, + reduce_vars: false, + toplevel: true, + unused: false, + } + input: { + var foo = bar(); + !function(x) { + console.log(x); + }(foo); + } + expect: { + !function(x) { + console.log(x); + }(bar()); + } +}