diff options
author | Mihai Bazon <mihai@bazon.net> | 2012-10-17 15:56:45 +0300 |
---|---|---|
committer | Mihai Bazon <mihai@bazon.net> | 2012-10-17 15:56:45 +0300 |
commit | a21f3c6cdd27696770b5cc605b5f02d81f0a32af (patch) | |
tree | c9423d2fea130852d11574153aae8b704ea5cf67 /bin | |
parent | 8f664585983e4174bb850e3975659127eb99df36 (diff) | |
download | tracifyjs-a21f3c6cdd27696770b5cc605b5f02d81f0a32af.tar.gz tracifyjs-a21f3c6cdd27696770b5cc605b5f02d81f0a32af.zip |
employ a better parser for command-line arguments
to support passing commas in strings in for example:
uglifyjs2 -cd TEST="'a,b'" <<EOF
console.log(TEST);
EOF
→ console.log("a,b")
close #14
Diffstat (limited to 'bin')
-rwxr-xr-x | bin/uglifyjs2 | 38 |
1 files changed, 29 insertions, 9 deletions
diff --git a/bin/uglifyjs2 b/bin/uglifyjs2 index 62f59105..acca760d 100755 --- a/bin/uglifyjs2 +++ b/bin/uglifyjs2 @@ -97,9 +97,9 @@ if (ARGS.acorn) { acorn = require("acorn"); } -var COMPRESS = getOptions("c"); -var MANGLE = getOptions("m"); -var BEAUTIFY = getOptions("b"); +var COMPRESS = getOptions("c", true); +var MANGLE = getOptions("m", true); +var BEAUTIFY = getOptions("b", true); if (COMPRESS && ARGS.d) { COMPRESS.global_defs = getOptions("d"); @@ -299,16 +299,36 @@ function normalize(o) { } } -function getOptions(x) { +function getOptions(x, constants) { x = ARGS[x]; if (!x) return null; var ret = {}; if (x !== true) { - x.replace(/^\s+|\s+$/g).split(/\s*,+\s*/).forEach(function(opt){ - var a = opt.split(/\s*[=:]\s*/); - ret[a[0]] = a.length > 1 ? new Function("return(" + a[1] + ")")() : true; - }); - normalize(ret); + var ast; + try { + ast = UglifyJS.parse(x); + } catch(ex) { + if (ex instanceof UglifyJS.JS_Parse_Error) { + sys.error("Error parsing arguments in: " + x); + process.exit(1); + } + } + ast.walk(new UglifyJS.TreeWalker(function(node){ + if (node instanceof UglifyJS.AST_Toplevel) return; // descend + if (node instanceof UglifyJS.AST_SimpleStatement) return; // descend + 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 value = node.right; + if (constants) + value = new Function("return (" + value.print_to_string() + ")")(); + ret[name] = value; + return true; // no descend + } + sys.error(node.TYPE) + sys.error("Error parsing arguments in: " + x); + process.exit(1); + })); } return ret; } |