[Pkg-javascript-commits] [uglifyjs] 40/491: return `Error` from `minify()` (#1880)

Jonas Smedegaard dr at jones.dk
Wed Feb 14 19:51:20 UTC 2018


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

js pushed a commit to annotated tag debian/3.3.10-1
in repository uglifyjs.

commit a3b2eb75bd57e78305c418242c538c41acf010e7
Author: Alex Lam S.L <alexlamsl at gmail.com>
Date:   Mon May 8 07:05:19 2017 +0800

    return `Error` from `minify()` (#1880)
    
    Have `minify()` return `Error` in `result.error` rather than throwing it.
---
 bin/uglifyjs         | 11 +++++++----
 lib/minify.js        |  2 ++
 test/mocha/minify.js | 40 +++++++++++++++++++---------------------
 3 files changed, 28 insertions(+), 25 deletions(-)

diff --git a/bin/uglifyjs b/bin/uglifyjs
index 5fa81f3..8bb8e70 100755
--- a/bin/uglifyjs
+++ b/bin/uglifyjs
@@ -172,7 +172,7 @@ function run() {
     UglifyJS.AST_Node.warn_function = function(msg) {
         console.error("WARN:", msg);
     };
-    if (program.stats)  program.stats = Date.now();
+    if (program.stats) program.stats = Date.now();
     try {
         if (program.parse) {
             if (program.parse.acorn) {
@@ -192,8 +192,12 @@ function run() {
                 });
             }
         }
-        var result = UglifyJS.minify(files, options);
     } catch (ex) {
+        fatal("ERROR: " + ex.message);
+    }
+    var result = UglifyJS.minify(files, options);
+    if (result.error) {
+        var ex = result.error;
         if (ex.name == "SyntaxError") {
             console.error("Parse error at " + ex.filename + ":" + ex.line + "," + ex.col);
             var col = ex.col;
@@ -217,8 +221,7 @@ function run() {
             console.error(ex.defs);
         }
         fatal("ERROR: " + ex.message);
-    }
-    if (program.output == "ast") {
+    } else if (program.output == "ast") {
         console.log(JSON.stringify(result.ast, function(key, value) {
             if (skip_key(key)) return;
             if (value instanceof UglifyJS.AST_Token) return;
diff --git a/lib/minify.js b/lib/minify.js
index 27cc911..a827930 100644
--- a/lib/minify.js
+++ b/lib/minify.js
@@ -146,6 +146,8 @@ function minify(files, options) {
             result.warnings = warnings;
         }
         return result;
+    } catch (ex) {
+        return { error: ex };
     } finally {
         AST_Node.warn_function = warn_function;
     }
diff --git a/test/mocha/minify.js b/test/mocha/minify.js
index d69ef59..7812fe6 100644
--- a/test/mocha/minify.js
+++ b/test/mocha/minify.js
@@ -114,17 +114,18 @@ describe("minify", function() {
             }
         });
         it("Should fail with multiple input and inline source map", function() {
-            assert.throws(function() {
-                Uglify.minify([
-                    read("./test/input/issue-520/input.js"),
-                    read("./test/input/issue-520/output.js")
-                ], {
-                    sourceMap: {
-                        content: "inline",
-                        url: "inline"
-                    }
-                });
+            var result = Uglify.minify([
+                read("./test/input/issue-520/input.js"),
+                read("./test/input/issue-520/output.js")
+            ], {
+                sourceMap: {
+                    content: "inline",
+                    url: "inline"
+                }
             });
+            var err = result.error;
+            assert.ok(err instanceof Error);
+            assert.strictEqual(err.stack.split(/\n/)[0], "Error: inline source map only works with singular input");
         });
     });
 
@@ -170,17 +171,14 @@ describe("minify", function() {
     });
 
     describe("JS_Parse_Error", function() {
-        it("should throw syntax error", function() {
-            assert.throws(function() {
-                Uglify.minify("function f(a{}");
-            }, function(err) {
-                assert.ok(err instanceof Error);
-                assert.strictEqual(err.stack.split(/\n/)[0], "SyntaxError: Unexpected token punc «{», expected punc «,»");
-                assert.strictEqual(err.filename, "0");
-                assert.strictEqual(err.line, 1);
-                assert.strictEqual(err.col, 12);
-                return true;
-            });
+        it("should return syntax error", function() {
+            var result = Uglify.minify("function f(a{}");
+            var err = result.error;
+            assert.ok(err instanceof Error);
+            assert.strictEqual(err.stack.split(/\n/)[0], "SyntaxError: Unexpected token punc «{», expected punc «,»");
+            assert.strictEqual(err.filename, "0");
+            assert.strictEqual(err.line, 1);
+            assert.strictEqual(err.col, 12);
         });
     });
 });

-- 
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