Add `--expr`, an option to parse a single expression (suitable for JSON)
authorMihai Bazon <mihai@bazon.net>
Wed, 15 May 2013 10:27:23 +0000 (13:27 +0300)
committerMihai Bazon <mihai@bazon.net>
Wed, 15 May 2013 10:27:23 +0000 (13:27 +0300)
bin/uglifyjs
lib/parse.js

index fab9c66..b30b759 100755 (executable)
@@ -23,6 +23,7 @@ mangling you need to use `-c` and `-m`.\
     .describe("source-map-url", "The path to the source map to be added in //@ sourceMappingURL.  Defaults to the value passed with --source-map.")
     .describe("in-source-map", "Input source map, useful if you're compressing JS that was generated from some other original code.")
     .describe("screw-ie8", "Pass this flag if you don't care about full compliance with Internet Explorer 6-8 quirks (by default UglifyJS will try to be IE-proof).")
+    .describe("expr", "Parse a single expression, rather than a program (for parsing JSON)")
     .describe("p", "Skip prefix for original filenames that appear in source maps. \
 For example -p 3 will drop 3 directories from file names and ensure they are relative paths.")
     .describe("o", "Output file (default STDOUT).")
@@ -76,6 +77,8 @@ You need to pass an argument to this option to specify the name that your module
     .string("e")
     .string("comments")
     .string("wrap")
+
+    .boolean("expr")
     .boolean("screw-ie8")
     .boolean("export-all")
     .boolean("self")
@@ -242,8 +245,9 @@ async.eachLimit(files, 1, function (file, cb) {
             }
             else {
                 TOPLEVEL = UglifyJS.parse(code, {
-                    filename: file,
-                    toplevel: TOPLEVEL
+                    filename   : file,
+                    toplevel   : TOPLEVEL,
+                    expression : ARGS.expr,
                 });
             };
         });
index a687495..e561ab6 100644 (file)
@@ -588,9 +588,10 @@ var ATOMIC_START_TOKEN = array_to_hash([ "atom", "num", "string", "regexp", "nam
 function parse($TEXT, options) {
 
     options = defaults(options, {
-        strict   : false,
-        filename : null,
-        toplevel : null
+        strict     : false,
+        filename   : null,
+        toplevel   : null,
+        expression : false
     });
 
     var S = {
@@ -1386,6 +1387,10 @@ function parse($TEXT, options) {
         return ret;
     };
 
+    if (options.expression) {
+        return expression(true);
+    }
+
     return (function(){
         var start = S.token;
         var body = [];