From a21f3c6cdd27696770b5cc605b5f02d81f0a32af Mon Sep 17 00:00:00 2001 From: Mihai Bazon Date: Wed, 17 Oct 2012 15:56:45 +0300 Subject: [PATCH] employ a better parser for command-line arguments MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit to support passing commas in strings in for example: uglifyjs2 -cd TEST="'a,b'" < 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; } diff --git a/lib/compress.js b/lib/compress.js index f216ed22..4a51a49f 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -134,6 +134,17 @@ merge(Compressor.prototype, { }; function make_node_from_constant(compressor, val, orig) { + // XXX: WIP. + // if (val instanceof AST_Node) return val.transform(new TreeTransformer(null, function(node){ + // if (node instanceof AST_SymbolRef) { + // var scope = compressor.find_parent(AST_Scope); + // var def = scope.find_variable(node); + // node.thedef = def; + // return node; + // } + // })).transform(compressor); + + if (val instanceof AST_Node) return val.transform(compressor); switch (typeof val) { case "string": return make_node(AST_String, orig, { -- 2.34.1