fix corner case in `comments` (#3550)
authorAlex Lam S.L <alexlamsl@gmail.com>
Tue, 29 Oct 2019 19:49:39 +0000 (03:49 +0800)
committerGitHub <noreply@github.com>
Tue, 29 Oct 2019 19:49:39 +0000 (03:49 +0800)
lib/parse.js
test/mocha/comments.js

index 7321878..16d8dad 100644 (file)
@@ -1253,6 +1253,7 @@ function parse($TEXT, options) {
                 var ex = expression(true);
                 var len = start.comments_before.length;
                 [].unshift.apply(ex.start.comments_before, start.comments_before);
+                start.comments_before.length = 0;
                 start.comments_before = ex.start.comments_before;
                 start.comments_before_length = len;
                 if (len == 0 && start.comments_before.length > 0) {
@@ -1268,6 +1269,7 @@ function parse($TEXT, options) {
                 var end = prev();
                 end.comments_before = ex.end.comments_before;
                 [].push.apply(ex.end.comments_after, end.comments_after);
+                end.comments_after.length = 0;
                 end.comments_after = ex.end.comments_after;
                 ex.end = end;
                 if (ex instanceof AST_Call) mark_pure(ex);
index 764f6db..9a4b3be 100644 (file)
@@ -259,6 +259,30 @@ describe("comments", function() {
         assert.strictEqual(result.code, code);
     });
 
+    it("Should handle comments around parenthesis correctly", function() {
+        var code = [
+            "a();",
+            "/* foo */",
+            "(b())",
+            "/* bar */",
+            "c();",
+        ].join("\n");
+        var result = UglifyJS.minify(code, {
+            compress: false,
+            mangle: false,
+            output: {
+                comments: "all",
+            },
+        });
+        if (result.error) throw result.error;
+        assert.strictEqual(result.code, [
+            "a();",
+            "/* foo */",
+            "b()",
+            "/* bar */;c();",
+        ].join("\n"));
+    });
+
     it("Should preserve comments around IIFE", function() {
         var result = UglifyJS.minify("/*a*/(/*b*/function(){/*c*/}/*d*/)/*e*/();", {
             compress: false,