Retain comments before AST_Constants during mangle.
authorkzc <zaxxon2011@gmail.com>
Tue, 3 May 2016 19:08:40 +0000 (15:08 -0400)
committerRichard van Velzen <rvanvelzen@experty.com>
Wed, 4 May 2016 18:11:45 +0000 (20:11 +0200)
lib/output.js
test/mocha/comment_before_constant.js [new file with mode: 0644]

index a59066f..f878758 100644 (file)
@@ -409,6 +409,7 @@ function OutputStream(options) {
 
     AST_Node.DEFMETHOD("print_to_string", function(options){
         var s = OutputStream(options);
+        if (!options) s._readonly = true;
         this.print(s);
         return s.get();
     });
@@ -416,6 +417,7 @@ function OutputStream(options) {
     /* -----[ comments ]----- */
 
     AST_Node.DEFMETHOD("add_comments", function(output){
+        if (output._readonly) return;
         var c = output.option("comments"), self = this;
         var start = self.start;
         if (start && !start._comments_dumped) {
diff --git a/test/mocha/comment_before_constant.js b/test/mocha/comment_before_constant.js
new file mode 100644 (file)
index 0000000..cfdb6da
--- /dev/null
@@ -0,0 +1,27 @@
+var Uglify = require('../../');
+var assert = require("assert");
+
+describe("comment before constant", function() {
+    var js = 'function f() { /*c1*/ var /*c2*/ foo = /*c3*/ false; return foo; }';
+
+    it("Should test comment before constant is retained and output after mangle.", function() {
+        var result = Uglify.minify(js, {
+            fromString: true,
+            compress: { collapse_vars: false },
+            mangle: {},
+            output: { comments: true },
+        });
+        assert.strictEqual(result.code, 'function f(){/*c1*/var/*c2*/n=/*c3*/!1;return n}');
+    });
+
+    it("Should test code works when comments disabled.", function() {
+        var result = Uglify.minify(js, {
+            fromString: true,
+            compress: { collapse_vars: false },
+            mangle: {},
+            output: {},
+        });
+        assert.strictEqual(result.code, 'function f(){var n=!1;return n}');
+    });
+});
+