[Pkg-javascript-commits] [uglifyjs] 35/228: clean up `max_line_len` - never exceed specified limit - otherwise warning is shown - enabled only for final output

Jonas Smedegaard dr at jones.dk
Sat Apr 15 14:25:14 UTC 2017


This is an automated email from the git hooks/post-receive script.

js pushed a commit to branch master
in repository uglifyjs.

commit 8898b8a0fe87f71c0ea2d35face6dfbf11db27ec
Author: alexlamsl <alexlamsl at gmail.com>
Date:   Sat Feb 18 22:44:53 2017 +0800

    clean up `max_line_len`
    - never exceed specified limit
    - otherwise warning is shown
    - enabled only for final output
    
    closes #1496
---
 bin/uglifyjs                  | 11 ++++++-----
 lib/output.js                 | 43 ++++++++++++++++++++++++++++++++-----------
 test/compress/max_line_len.js | 28 ++++++++++++++++++++++++++++
 tools/node.js                 |  2 +-
 4 files changed, 67 insertions(+), 17 deletions(-)

diff --git a/bin/uglifyjs b/bin/uglifyjs
index 8cb2f0d..27717fb 100755
--- a/bin/uglifyjs
+++ b/bin/uglifyjs
@@ -228,9 +228,10 @@ if (ARGS.mangle_props === true) {
 }
 
 var OUTPUT_OPTIONS = {
-    beautify    : BEAUTIFY ? true : false,
-    preamble    : ARGS.preamble || null,
-    quote_style : ARGS.quotes != null ? ARGS.quotes : 0
+    beautify     : BEAUTIFY ? true : false,
+    max_line_len : 32000,
+    preamble     : ARGS.preamble || null,
+    quote_style  : ARGS.quotes != null ? ARGS.quotes : 0,
 };
 
 if (ARGS.mangle_props == 2) {
@@ -540,7 +541,7 @@ function getOptions(flag, constants) {
         ast.walk(new UglifyJS.TreeWalker(function(node){
             if (node instanceof UglifyJS.AST_Seq) return; // descend
             if (node instanceof UglifyJS.AST_Assign) {
-                var name = node.left.print_to_string({ beautify: false }).replace(/-/g, "_");
+                var name = node.left.print_to_string().replace(/-/g, "_");
                 var value = node.right;
                 if (constants)
                     value = new Function("return (" + value.print_to_string() + ")")();
@@ -548,7 +549,7 @@ function getOptions(flag, constants) {
                 return true;    // no descend
             }
             if (node instanceof UglifyJS.AST_Symbol || node instanceof UglifyJS.AST_Binary) {
-                var name = node.print_to_string({ beautify: false }).replace(/-/g, "_");
+                var name = node.print_to_string().replace(/-/g, "_");
                 ret[name] = true;
                 return true;    // no descend
             }
diff --git a/lib/output.js b/lib/output.js
index 2802c30..4a0a1e0 100644
--- a/lib/output.js
+++ b/lib/output.js
@@ -70,7 +70,7 @@ function OutputStream(options) {
         unescape_regexps : false,
         inline_script    : false,
         width            : 80,
-        max_line_len     : 32000,
+        max_line_len     : false,
         beautify         : false,
         source_map       : null,
         bracketize       : false,
@@ -198,16 +198,29 @@ function OutputStream(options) {
 
     var might_need_space = false;
     var might_need_semicolon = false;
+    var might_add_newline = 0;
     var last = null;
 
     function last_char() {
         return last.charAt(last.length - 1);
     };
 
-    function maybe_newline() {
-        if (options.max_line_len && current_col > options.max_line_len)
-            print("\n");
-    };
+    var ensure_line_len = options.max_line_len ? function() {
+        if (current_col > options.max_line_len) {
+            if (might_add_newline) {
+                var left = OUTPUT.slice(0, might_add_newline);
+                var right = OUTPUT.slice(might_add_newline);
+                OUTPUT = left + "\n" + right;
+                current_line++;
+                current_pos++;
+                current_col = right.length;
+            }
+            if (current_col > options.max_line_len) {
+                AST_Node.warn("Output exceeds {max_line_len} characters", options);
+            }
+        }
+        might_add_newline = 0;
+    } : noop;
 
     var requireSemicolonChars = makePredicate("( [ + * / - , .");
 
@@ -223,6 +236,7 @@ function OutputStream(options) {
                     current_col++;
                     current_pos++;
                 } else {
+                    ensure_line_len();
                     OUTPUT += "\n";
                     current_pos++;
                     current_line++;
@@ -243,6 +257,7 @@ function OutputStream(options) {
         if (!options.beautify && options.preserve_line && stack[stack.length - 1]) {
             var target_line = stack[stack.length - 1].start.line;
             while (current_line < target_line) {
+                ensure_line_len();
                 OUTPUT += "\n";
                 current_pos++;
                 current_line++;
@@ -264,16 +279,16 @@ function OutputStream(options) {
             }
             might_need_space = false;
         }
+        OUTPUT += str;
+        current_pos += str.length;
         var a = str.split(/\r?\n/), n = a.length - 1;
         current_line += n;
-        if (n == 0) {
-            current_col += a[n].length;
-        } else {
+        current_col += a[0].length;
+        if (n > 0) {
+            ensure_line_len();
             current_col = a[n].length;
         }
-        current_pos += str.length;
         last = str;
-        OUTPUT += str;
     };
 
     var space = options.beautify ? function() {
@@ -299,7 +314,10 @@ function OutputStream(options) {
 
     var newline = options.beautify ? function() {
         print("\n");
-    } : maybe_newline;
+    } : options.max_line_len ? function() {
+        ensure_line_len();
+        might_add_newline = OUTPUT.length;
+    } : noop;
 
     var semicolon = options.beautify ? function() {
         print(";");
@@ -376,6 +394,9 @@ function OutputStream(options) {
     } : noop;
 
     function get() {
+        if (might_add_newline) {
+            ensure_line_len();
+        }
         return OUTPUT;
     };
 
diff --git a/test/compress/max_line_len.js b/test/compress/max_line_len.js
new file mode 100644
index 0000000..b9e0917
--- /dev/null
+++ b/test/compress/max_line_len.js
@@ -0,0 +1,28 @@
+too_short: {
+    beautify = {
+        max_line_len: 10,
+    }
+    input: {
+        function f(a) {
+            return { c: 42, d: a(), e: "foo"};
+        }
+    }
+    expect_exact: 'function f(a){\nreturn{\nc:42,\nd:a(),\ne:"foo"}}'
+    expect_warnings: [
+        "WARN: Output exceeds 10 characters"
+    ]
+}
+
+just_enough: {
+    beautify = {
+        max_line_len: 14,
+    }
+    input: {
+        function f(a) {
+            return { c: 42, d: a(), e: "foo"};
+        }
+    }
+    expect_exact: 'function f(a){\nreturn{c:42,\nd:a(),e:"foo"}\n}'
+    expect_warnings: [
+    ]
+}
diff --git a/tools/node.js b/tools/node.js
index c68faaa..108803e 100644
--- a/tools/node.js
+++ b/tools/node.js
@@ -115,7 +115,7 @@ exports.minify = function(files, options) {
 
     // 5. output
     var inMap = options.inSourceMap;
-    var output = {};
+    var output = { max_line_len: 32000 };
     if (typeof options.inSourceMap == "string") {
         inMap = JSON.parse(fs.readFileSync(options.inSourceMap, "utf8"));
     }

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/uglifyjs.git



More information about the Pkg-javascript-commits mailing list