add `--ast-help`
authorMihai Bazon <mihai@bazon.net>
Tue, 9 Oct 2012 10:21:21 +0000 (13:21 +0300)
committerMihai Bazon <mihai@bazon.net>
Tue, 9 Oct 2012 10:21:21 +0000 (13:21 +0300)
displays a rather cruel description of the AST classes, derived
directly from the node definitions.

bin/uglifyjs2
lib/ast.js
tools/node.js

index b3b00a7..8515586 100755 (executable)
@@ -78,6 +78,14 @@ You need to pass an argument to this option to specify the name that your module
     .argv
 ;
 
+normalize(ARGS);
+
+if (ARGS.ast_help) {
+    var desc = UglifyJS.describe_ast();
+    sys.puts(typeof desc == "string" ? desc : JSON.stringify(desc, null, 2));
+    process.exit(0);
+}
+
 if (ARGS.h || ARGS.help) {
     sys.puts(optimist.help());
     process.exit(0);
@@ -87,8 +95,6 @@ if (ARGS.acorn) {
     acorn = require("acorn");
 }
 
-normalize(ARGS);
-
 var COMPRESS = getOptions("c");
 var MANGLE = getOptions("m");
 var BEAUTIFY = getOptions("b");
index f44fa3a..fde1ca1 100644 (file)
@@ -43,8 +43,6 @@
 
 "use strict";
 
-var NODE_HIERARCHY = {};
-
 function DEFNODE(type, props, methods, base) {
     if (arguments.length < 4) base = AST_Node;
     if (!props) props = [];
@@ -67,9 +65,11 @@ function DEFNODE(type, props, methods, base) {
         ctor.prototype = proto;
         ctor.BASE = base;
     }
+    if (base) base.SUBCLASSES.push(ctor);
     ctor.prototype.CTOR = ctor;
     ctor.PROPS = props || null;
     ctor.SELF_PROPS = self_props;
+    ctor.SUBCLASSES = [];
     if (type) {
         ctor.prototype.TYPE = ctor.TYPE = type;
     }
@@ -83,10 +83,6 @@ function DEFNODE(type, props, methods, base) {
     ctor.DEFMETHOD = function(name, method) {
         this.prototype[name] = method;
     };
-    NODE_HIERARCHY[type] = {
-        def: ctor,
-        base: base
-    };
     return ctor;
 };
 
index fccdd83..a35dcd2 100644 (file)
@@ -107,3 +107,52 @@ exports.minify = function(files, options) {
         map  : map + ""
     };
 };
+
+// exports.describe_ast = function() {
+//     function doitem(ctor) {
+//         var sub = {};
+//         ctor.SUBCLASSES.forEach(function(ctor){
+//             sub[ctor.TYPE] = doitem(ctor);
+//         });
+//         var ret = {};
+//         if (ctor.SELF_PROPS.length > 0) ret.props = ctor.SELF_PROPS;
+//         if (ctor.SUBCLASSES.length > 0) ret.sub = sub;
+//         return ret;
+//     }
+//     return doitem(UglifyJS.AST_Node).sub;
+// }
+
+exports.describe_ast = function() {
+    var out = UglifyJS.OutputStream({ beautify: true });
+    function doitem(ctor) {
+        out.print("AST_" + ctor.TYPE);
+        var props = ctor.SELF_PROPS.filter(function(prop){
+            return !/^\$/.test(prop);
+        });
+        if (props.length > 0) {
+            out.space();
+            out.with_parens(function(){
+                props.forEach(function(prop, i){
+                    if (i) out.space();
+                    out.print(prop);
+                });
+            });
+        }
+        if (ctor.documentation) {
+            out.space();
+            out.print_string(ctor.documentation);
+        }
+        if (ctor.SUBCLASSES.length > 0) {
+            out.space();
+            out.with_block(function(){
+                ctor.SUBCLASSES.forEach(function(ctor, i){
+                    out.indent();
+                    doitem(ctor);
+                    out.newline();
+                });
+            });
+        }
+    };
+    doitem(UglifyJS.AST_Node);
+    return out + "";
+};