reject malformed CLI parameters (#2239)
authorAlex Lam S.L <alexlamsl@gmail.com>
Sat, 15 Jul 2017 15:50:27 +0000 (23:50 +0800)
committerGitHub <noreply@github.com>
Sat, 15 Jul 2017 15:50:27 +0000 (23:50 +0800)
fixes #2237

bin/uglifyjs
lib/propmangle.js
test/mocha/cli.js

index 68d67c2..8cbb3ca 100755 (executable)
@@ -35,11 +35,11 @@ else if (process.argv.indexOf("options") >= 0) program.helpInformation = functio
     }
     return text.join("\n");
 };
-program.option("-p, --parse <options>", "Specify parser options.", parse_js("parse", true));
-program.option("-c, --compress [options]", "Enable compressor/specify compressor options.", parse_js("compress", true));
-program.option("-m, --mangle [options]", "Mangle names/specify mangler options.", parse_js("mangle", true));
-program.option("--mangle-props [options]", "Mangle properties/specify mangler options.", parse_js("mangle-props", true));
-program.option("-b, --beautify [options]", "Beautify output/specify output options.", parse_js("beautify", true));
+program.option("-p, --parse <options>", "Specify parser options.", parse_js());
+program.option("-c, --compress [options]", "Enable compressor/specify compressor options.", parse_js());
+program.option("-m, --mangle [options]", "Mangle names/specify mangler options.", parse_js());
+program.option("--mangle-props [options]", "Mangle properties/specify mangler options.", parse_js());
+program.option("-b, --beautify [options]", "Beautify output/specify output options.", parse_js());
 program.option("-o, --output <file>", "Output file (default STDOUT).");
 program.option("--comments [filter]", "Preserve copyright comments in the output.");
 program.option("--config-file <file>", "Read minify() options from JSON file.");
@@ -310,7 +310,7 @@ function read_file(path, default_value) {
     }
 }
 
-function parse_js(flag, constants) {
+function parse_js(flag) {
     return function(value, options) {
         options = options || {};
         try {
@@ -328,7 +328,7 @@ function parse_js(flag, constants) {
                 if (node instanceof UglifyJS.AST_Assign) {
                     var name = node.left.print_to_string();
                     var value = node.right;
-                    if (!constants) {
+                    if (flag) {
                         options[name] = value;
                     } else if (value instanceof UglifyJS.AST_Array) {
                         options[name] = value.elements.map(to_string);
@@ -351,14 +351,18 @@ function parse_js(flag, constants) {
                 }
             }));
         } catch(ex) {
-            options[value] = null;
+            if (flag) {
+                fatal("Error parsing arguments for '" + flag + "': " + value);
+            } else {
+                options[value] = null;
+            }
         }
         return options;
     }
 }
 
 function parse_source_map() {
-    var parse = parse_js("sourceMap", true);
+    var parse = parse_js();
     return function(value, options) {
         var hasContent = options && "content" in options;
         var settings = parse(value, options);
index f17909d..4949075 100644 (file)
@@ -76,7 +76,7 @@ function mangle_properties(ast, options) {
         only_cache: false,
         regex: null,
         reserved: null,
-    });
+    }, true);
 
     var reserved = options.reserved;
     if (!Array.isArray(reserved)) reserved = [];
index 8bfdc85..1d847dc 100644 (file)
@@ -593,8 +593,8 @@ describe("bin/uglifyjs", function () {
             done();
         });
     });
-    it("Should work with --mangle reserved=[]", function (done) {
-        var command = uglifyjscmd + ' test/input/issue-505/input.js -m reserved=[callback]';
+    it("Should work with --mangle reserved=[]", function(done) {
+        var command = uglifyjscmd + " test/input/issue-505/input.js -m reserved=[callback]";
 
         exec(command, function (err, stdout) {
             if (err) throw err;
@@ -603,8 +603,8 @@ describe("bin/uglifyjs", function () {
             done();
         });
     });
-    it("Should work with --mangle reserved=false", function (done) {
-        var command = uglifyjscmd + ' test/input/issue-505/input.js -m reserved=false';
+    it("Should work with --mangle reserved=false", function(done) {
+        var command = uglifyjscmd + " test/input/issue-505/input.js -m reserved=false";
 
         exec(command, function (err, stdout) {
             if (err) throw err;
@@ -613,4 +613,22 @@ describe("bin/uglifyjs", function () {
             done();
         });
     });
+    it("Should fail with --mangle-props reserved=[in]", function(done) {
+        var command = uglifyjscmd + " test/input/issue-505/input.js --mangle-props reserved=[in]";
+        exec(command, function (err, stdout, stderr) {
+            assert.ok(err);
+            assert.strictEqual(stdout, "");
+            assert.ok(/^Supported options:\n[\s\S]*?\nERROR: `reserved=\[in]` is not a supported option/.test(stderr), stderr);
+            done();
+        });
+    });
+    it("Should fail with --define a-b", function(done) {
+        var command = uglifyjscmd + " test/input/issue-505/input.js --define a-b";
+        exec(command, function (err, stdout, stderr) {
+            assert.ok(err);
+            assert.strictEqual(stdout, "");
+            assert.strictEqual(stderr, "Error parsing arguments for 'define': a-b\n");
+            done();
+        });
+    });
 });