give sensible error against invalid input source map (#3044)
authorAlex Lam S.L <alexlamsl@gmail.com>
Sat, 31 Mar 2018 09:48:20 +0000 (18:48 +0900)
committerGitHub <noreply@github.com>
Sat, 31 Mar 2018 09:48:20 +0000 (18:48 +0900)
lib/minify.js
test/mocha/cli.js

index 8e4aded..8dc275c 100644 (file)
@@ -165,7 +165,11 @@ function minify(files, options) {
         if (!HOP(options.output, "code") || options.output.code) {
             if (options.sourceMap) {
                 if (typeof options.sourceMap.content == "string") {
-                    options.sourceMap.content = JSON.parse(options.sourceMap.content);
+                    try {
+                        options.sourceMap.content = JSON.parse(options.sourceMap.content);
+                    } catch (ex) {
+                        throw new Error("invalid input source map: " + options.sourceMap.content);
+                    }
                 }
                 options.output.source_map = SourceMap({
                     file: options.sourceMap.filename,
index 10f0465..7f0bd79 100644 (file)
@@ -56,6 +56,18 @@ describe("bin/uglifyjs", function () {
             done();
         });
     });
+    it("Should give sensible error against invalid input source map", function(done) {
+        var command = uglifyjscmd + " test/mocha.js --source-map content=blah,url=inline";
+
+        exec(command, function (err, stdout, stderr) {
+            assert.ok(err);
+            assert.deepEqual(stderr.split(/\n/).slice(0, 2), [
+                "INFO: Using input source map: blah",
+                "ERROR: invalid input source map: blah",
+            ]);
+            done();
+        });
+    });
     it("Should append source map to output when using --source-map url=inline", function (done) {
         var command = uglifyjscmd + " test/input/issue-1323/sample.js --source-map url=inline";