fix corner case in `AST_For.init` (#1652)
authorAlex Lam S.L <alexlamsl@gmail.com>
Fri, 24 Mar 2017 11:31:17 +0000 (19:31 +0800)
committerGitHub <noreply@github.com>
Fri, 24 Mar 2017 11:31:17 +0000 (19:31 +0800)
Enforce `null` as value for empty initialisation blocks.

fixes #1648

lib/compress.js
lib/output.js
test/compress/loops.js

index cbcb7b8..3804a93 100644 (file)
@@ -2327,6 +2327,7 @@ merge(Compressor.prototype, {
     };
 
     OPT(AST_For, function(self, compressor){
+        if (is_empty(self.init)) self.init = null;
         if (!compressor.option("loops")) return self;
         if (self.condition) {
             var cond = self.condition.evaluate(compressor);
index 767abd4..c0f1052 100644 (file)
@@ -799,7 +799,7 @@ function OutputStream(options) {
         output.print("for");
         output.space();
         output.with_parens(function(){
-            if (self.init && !(self.init instanceof AST_EmptyStatement)) {
+            if (self.init) {
                 if (self.init instanceof AST_Definitions) {
                     self.init.print(output);
                 } else {
index b55c616..c8d7784 100644 (file)
@@ -440,3 +440,21 @@ issue_186_beautify_bracketize_ie8: {
         '}',
     ]
 }
+
+issue_1648: {
+    options = {
+        join_vars: true,
+        loops: true,
+        passes: 2,
+        sequences: true,
+        unused: true,
+    }
+    input: {
+        function f() {
+            x();
+            var b = 1;
+            while (1);
+        }
+    }
+    expect_exact: "function f(){for(x();1;);}"
+}