enhance `join_vars` (#4089)
authorAlex Lam S.L <alexlamsl@gmail.com>
Wed, 2 Sep 2020 17:41:10 +0000 (18:41 +0100)
committerGitHub <noreply@github.com>
Wed, 2 Sep 2020 17:41:10 +0000 (01:41 +0800)
lib/compress.js
test/compress/join_vars.js
test/input/reduce/label.js
test/input/reduce/label.reduced.js
test/mocha/reduce.js

index 6761c24..9aa70ef 100644 (file)
@@ -2644,6 +2644,14 @@ merge(Compressor.prototype, {
                         defs = stat.init;
                     }
                 } else if (stat instanceof AST_ForIn) {
+                    if (defs && defs.TYPE == stat.init.TYPE) {
+                        defs.definitions = defs.definitions.concat(stat.init.definitions);
+                        var name = stat.init.definitions[0].name;
+                        var ref = make_node(AST_SymbolRef, name, name);
+                        name.definition().references.push(ref);
+                        stat.init = ref;
+                        CHANGED = true;
+                    }
                     stat.object = join_assigns_expr(stat.object);
                 } else if (stat instanceof AST_If) {
                     stat.condition = join_assigns_expr(stat.condition);
index 9f0b35b..c2c8e1e 100644 (file)
@@ -277,8 +277,8 @@ join_object_assignments_forin: {
     }
     expect: {
         console.log(function() {
-            var o = { a: "PASS" };
-            for (var a in o)
+            var o = { a: "PASS" }, a;
+            for (a in o)
                 return o[a];
         }());
     }
index aee57a4..5b0f059 100644 (file)
@@ -1,9 +1,3 @@
-var o = this;
-
-for (var k in o) L17060: {
-    a++;
+UNUSED: {
+    console.log(0 - .1 - .1 - .1);
 }
-
-var a;
-
-console.log(k);
index a1e7950..ba4b56c 100644 (file)
@@ -1,15 +1,12 @@
 // (beautified)
-var o = this;
-
-for (var k in o) {}
-
-var a;
-
-console.log(k);
-// output: a
+console.log(0 - 1 - .1 - .1);
+// output: -1.2000000000000002
 // 
-// minify: k
+// minify: -1.2
 // 
 // options: {
+//   "compress": {
+//     "unsafe_math": true
+//   },
 //   "mangle": false
 // }
\ No newline at end of file
index c186473..7e2d460 100644 (file)
@@ -24,6 +24,9 @@ describe("test/reduce.js", function() {
     });
     it("Should eliminate unreferenced labels", function() {
         var result = reduce_test(read("test/input/reduce/label.js"), {
+            compress: {
+                unsafe_math: true,
+            },
             mangle: false,
         }, {
             verbose: false,