fix corner case in `comments` (#3500)
authorAlex Lam S.L <alexlamsl@gmail.com>
Sat, 19 Oct 2019 19:21:30 +0000 (03:21 +0800)
committerGitHub <noreply@github.com>
Sat, 19 Oct 2019 19:21:30 +0000 (03:21 +0800)
lib/output.js
test/mocha/comments.js

index 827fa43..b9d8a13 100644 (file)
@@ -452,7 +452,7 @@ function OutputStream(options) {
         var self = this;
         var scan = node instanceof AST_Exit && node.value;
         var comments = dump(node);
-        if (!comments) return;
+        if (!comments) comments = [];
 
         if (scan) {
             var tw = new TreeWalker(function(node) {
index 04dd171..764f6db 100644 (file)
@@ -48,53 +48,84 @@ describe("comments", function() {
         }
     });
 
-    it("Should handle comment within return correctly", function() {
-        var result = UglifyJS.minify([
-            "function unequal(x, y) {",
-            "    return (",
-            "        // Either one",
-            "        x < y",
-            "        ||",
-            "        y < x",
-            "    );",
-            "}",
-        ].join("\n"), {
-            compress: false,
-            mangle: false,
-            output: {
-                beautify: true,
-                comments: "all",
-            },
+    describe("comment within return", function() {
+        it("Should handle leading return", function() {
+            var result = UglifyJS.minify([
+                "function unequal(x, y) {",
+                "    return (",
+                "        // Either one",
+                "        x < y",
+                "        ||",
+                "        y < x",
+                "    );",
+                "}",
+            ].join("\n"), {
+                compress: false,
+                mangle: false,
+                output: {
+                    beautify: true,
+                    comments: "all",
+                },
+            });
+            if (result.error) throw result.error;
+            assert.strictEqual(result.code, [
+                "function unequal(x, y) {",
+                "    // Either one",
+                "    return x < y || y < x;",
+                "}",
+            ].join("\n"));
         });
-        if (result.error) throw result.error;
-        assert.strictEqual(result.code, [
-            "function unequal(x, y) {",
-            "    // Either one",
-            "    return x < y || y < x;",
-            "}",
-        ].join("\n"));
-    });
 
-    it("Should handle comment folded into return correctly", function() {
-        var result = UglifyJS.minify([
-            "function f() {",
-            "    /* boo */ x();",
-            "    return y();",
-            "}",
-        ].join("\n"), {
-            mangle: false,
-            output: {
-                beautify: true,
-                comments: "all",
-            },
+        it("Should handle trailing return", function() {
+            var result = UglifyJS.minify([
+                "function unequal(x) {",
+                "    var y;",
+                "    return (",
+                "        // Either one",
+                "        x < y",
+                "        ||",
+                "        y < x",
+                "    );",
+                "}",
+            ].join("\n"), {
+                compress: false,
+                mangle: false,
+                output: {
+                    beautify: true,
+                    comments: "all",
+                },
+            });
+            if (result.error) throw result.error;
+            assert.strictEqual(result.code, [
+                "function unequal(x) {",
+                "    var y;",
+                "    // Either one",
+                "    return x < y || y < x;",
+                "}",
+            ].join("\n"));
+        });
+
+        it("Should handle comment folded into return", function() {
+            var result = UglifyJS.minify([
+                "function f() {",
+                "    /* boo */ x();",
+                "    return y();",
+                "}",
+            ].join("\n"), {
+                mangle: false,
+                output: {
+                    beautify: true,
+                    comments: "all",
+                },
+            });
+            if (result.error) throw result.error;
+            assert.strictEqual(result.code, [
+                "function f() {",
+                "    /* boo */",
+                "    return x(), y();",
+                "}",
+            ].join("\n"));
         });
-        if (result.error) throw result.error;
-        assert.strictEqual(result.code, [
-            "function f() {",
-            "    /* boo */",
-            "    return x(), y();",
-            "}",
-        ].join("\n"));
     });
 
     it("Should not drop comments after first OutputStream", function() {