fix for `if (...) return; else return ...;`
authorMihai Bazon <mihai@bazon.net>
Wed, 24 Oct 2012 06:33:32 +0000 (09:33 +0300)
committerMihai Bazon <mihai@bazon.net>
Wed, 24 Oct 2012 06:33:32 +0000 (09:33 +0300)
(it was assumed that the first `return` always contains a value)

close #22

lib/compress.js
test/compress/issue-22.js [new file with mode: 0644]

index 22fb330..2ea91d0 100644 (file)
@@ -1220,8 +1220,8 @@ merge(Compressor.prototype, {
             return make_node(self.body.CTOR, self, {
                 value: make_node(AST_Conditional, self, {
                     condition   : self.condition,
-                    consequent  : self.body.value,
-                    alternative : self.alternative.value || make_node(AST_Undefined, self).optimize(compressor)
+                    consequent  : self.body.value || make_node(AST_Undefined, self.body).optimize(compressor),
+                    alternative : self.alternative.value || make_node(AST_Undefined, self.alternative).optimize(compressor)
                 })
             }).transform(compressor);
         }
diff --git a/test/compress/issue-22.js b/test/compress/issue-22.js
new file mode 100644 (file)
index 0000000..a8b7bc6
--- /dev/null
@@ -0,0 +1,17 @@
+return_with_no_value_in_if_body: {
+    options = { conditionals: true };
+    input: {
+        function foo(bar) {
+            if (bar) {
+                return;
+            } else {
+                return 1;
+            }
+        }
+    }
+    expect: {
+        function foo (bar) {
+            return bar ? void 0 : 1;
+        }
+    }
+}