JSDOC/ScopeNamer.js
[app.jsdoc] / JSDOC / ScopeNamer.js
index 8cd7934..fea331e 100644 (file)
@@ -7,6 +7,7 @@ Symbol = imports.Symbol.Symbol;
 
 /**
  * @class ScopeNamer
+ * @extends Collapse
  * @namespace JSDOC
  * The point of this class is to iterate through the Collapsed tree
  * and add the property 'scopedName' to the tokens.
@@ -98,7 +99,10 @@ ScopeNamer = XObject.define(
          * add/remove ';' after return to configure at present..
          * @param {String} str String to output
          */
-        debugCall : function(str)  { return; print(str); }, 
+        debugCall : function(str)  {
+            if (!this.filename.match(/BuildDocs\.js/)) return;
+            return   print(str);
+        }, 
         
         collapseTop : true,
         
@@ -114,7 +118,7 @@ ScopeNamer = XObject.define(
             //print (this.statements);
             //print (JSON.stringify(this.statements, null,2));
            
-            this.walkStatements('$global$', this.statements)
+            this.walkStatements('_global_', this.statements)
             
                 
                 
@@ -131,9 +135,18 @@ ScopeNamer = XObject.define(
             this.debugCall("walkStatements :" + scope ) ;            
             var _this = this;
             var res = false;
-            
-            
-            statements.forEach(function(st ) {
+            var isGlobal = scope == '_global_';
+           
+            statements.some(function(st ) {
+                // handle change of scope..
+                if (isGlobal &&
+                        st[0].jsdoc &&
+                        st[0].jsdoc.getTag('scope').length
+                   ) {
+                    scope = st[0].jsdoc.getTag('scope');
+                }
+                
+                
                 res = _this.walkStatement(scope, st);
                 if (res === true) {
                     return true;
@@ -151,15 +164,34 @@ ScopeNamer = XObject.define(
             var name;
             var sn;
             
-            while (null != (token = this.next())) {
+            var isGlobal = scope == '_global_';
+            
             
+            
+            //if (this.filename.match(/pack\.js/) && isGlobal) {
+            //    print(JSON.stringify(stmt , null,4));
+            //}
+            // @ignore stops the parser..
+            if (isGlobal &&
+                    stmt[0].jsdoc &&
+                    stmt[0].jsdoc.getTag('ignore').length
+               ) {
+                this.debugCall("walkStatement : ignore found - " + scope + '@' + this.tokens[0].line );
+                print("GOT ignore?");
+                return true;
+            }
+           
+            
+            while (null != (token = this.next())) {
+                
                 //'function' 
                 //walkFunction(scope, name , args,  stmts  )
                 //
                 if (token.name == "FUNCTION") {
                     // function a() { .... } << scope is a  $this$={a}
                     if (this.lookTok(1).is('NAME')) {
-                        name = this.lookTok(2).data;
+                        name = isGlobal ? this.lookTok(1).data : '';
+                        
                         this.walkFunctionDef(scope, name, this.lookTok(2).args, this.lookTok(3).items, token);
                         continue;
                     }
@@ -202,8 +234,16 @@ ScopeNamer = XObject.define(
                 if (token.data == '{' && this.lookTok(-1).data == '=' && this.lookTok(-2).is('NAME')) {
                     
                     // could be var x = ..
-                    var jd = this.lookTok(-2).jsdoc ? this.lookTok(-2) : this.lookTok(-3); 
-                    this.walkObject(scope, this.lookTok(-2).data, token.props, jd);
+                    var jd = this.lookTok(-2).jsdoc ? this.lookTok(-2) : this.lookTok(-3);
+                    
+                    var isVar = this.lookTok(-3).name == 'VAR';
+                    
+                    // only register names of objects if 
+                    var name = this.lookTok(-2).data;
+                    name = !isGlobal && isVar ? false : name;
+                    name = !isGlobal && name && !name.match(/^this\./) ? false : name;
+                    //print(JSON.stringify(token,null,4));
+                    this.walkObject(scope, name, token.props, jd);
                     continue;
                 }
                  
@@ -215,7 +255,7 @@ ScopeNamer = XObject.define(
                 // standard flow....
                 if (token.data == '{') { 
                     sn = new ScopeNamer(this);
-                    print("GOT { - walkings statuements;}");
+                    //print("GOT { - walkings statuements;}");
                     if (!token.items) {
                         continue; // object..!?!?!? = ignore ???
                         print(JSON.stringify(token,null,4));
@@ -229,12 +269,12 @@ ScopeNamer = XObject.define(
         walkFunctionDef : function (inscope, name, args, stmts, jsdocTok)
         {
             this.debugCall("wallkFuncDef: " + inscope + '@' + this.look(0).line );
-            var scope = inscope + '.' + name;
-            
-            
-            var symbol = new Symbol( scope , args || [] , "FUNCTION" ,  jsdocTok.jsdoc);
-            symbol._token = jsdocTok;
-            this.addSymbol(symbol, jsdocTok.jsdoc);
+            var scope = name.length ? (inscope + '.' + name) : inscope;
+            if (name.length) { 
+                var symbol = new Symbol( scope , args || [] , "FUNCTION" ,  jsdocTok.jsdoc);
+                symbol._token = jsdocTok;
+                this.addSymbol(symbol, jsdocTok.jsdoc);
+            }
             var sn = new ScopeNamer(this);
             sn.walkStatements(scope, stmts);
             
@@ -244,9 +284,9 @@ ScopeNamer = XObject.define(
         
         walkCall : function (inscope, assign, callname, items, jsdocTok)
         {
-            this.debugCall("wallkCall : " + inscope + '@' + this.look(0).line );
+            this.debugCall("wallkCall : " + inscope  +':' + assign + '@' + this.look(0).line + ' ' + callname );
             var scope = inscope + ( assign ? ('.' + assign) : '' );
-            scope = scope.replace(/\^$global\$\./, '');
+            //scope = scope.replace(/\^_global\$\./, '');
             
             
             
@@ -264,8 +304,19 @@ ScopeNamer = XObject.define(
                     if (items[1][0].data != '{') {
                         return;
                     }
+                    
+                    if (assign != false   && !scope.match(/\.prototype$/)) { 
+                        var symbol = new Symbol( scope , false , "OBJECT" ,  jsdocTok);
+                        symbol._token = jsdocTok;
+                        this.addSymbol(symbol, jsdocTok.jsdoc);
+                        
+                    }
+                    
+                    
                     var sn = new ScopeNamer(this);
-                    sn.walkObject(scope + '.prototype', false, items[1][0].props );
+                    
+                    //print(JSON.stringify(items,null,4));
+                    sn.walkObject(scope , false, items[1][0].props, jsdocTok );
                     
                     return;
                 
@@ -281,10 +332,10 @@ ScopeNamer = XObject.define(
                     
                     this.addSymbol(symbol, jsdocTok.jsdoc);
                     var sn = new ScopeNamer(this);
-                    
+                    print(JSON.stringify(items, null,4));
                     
                     sn.walkStatements(scope, items[0][2].items);
-                    sn.walkObject(scope + '.prototype', false, items[2].props );
+                    sn.walkObject(scope + '.prototype', false, items[2][0].props );
                     return;
                 
                 
@@ -306,8 +357,12 @@ ScopeNamer = XObject.define(
                 this.addSymbol(symbol, jsdocTok.jsdoc);
                  
             }
-            
-            this.debugCall("wallkObject : " + scope);
+            // items can be false in this scenaro: a = {};
+            if (!items) {
+                return;
+            }
+            print(typeof(items));
+            this.debugCall("wallkObject : " + scope + '@' + items[Object.keys(items)[0]].key.line);
             for( var k in items) {
                 var key = items[k].key;
                 var val = items[k].val;
@@ -316,7 +371,7 @@ ScopeNamer = XObject.define(
                 // x : function(....)
                 if (val[0].name == 'FUNCTION' ) {
                     
-                  
+                    
                     this.walkFunctionDef (scope, k, val[1].args, val[2].items, key)
 
                     
@@ -332,7 +387,7 @@ ScopeNamer = XObject.define(
                 var symbol = new Symbol( scope +'.'+ k , val[1].args || [] , "PROPERTY" ,  key.jsdoc);
                 symbol._token = key;
                    
-                this.addSymbol(key, key.jsdoc);
+                this.addSymbol(symbol, key.jsdoc);
                 continue;
                  
                 
@@ -348,9 +403,17 @@ ScopeNamer = XObject.define(
         
         addSymbol: function(symbol, comment)
         {
-                     //print("PARSER addSYMBOL : " + symbol.alias);
-                    
+              //print("ADD symbol: " + JSON.stringify( symbol, null, 4));
+              //print("PARSER addSYMBOL : " + symbol.alias);
+            
             // if a symbol alias is documented more than once the last one with the user docs wins
+            
+            // dumpe some invalid symbols..
+            if (symbol.alias.split(/[#.]/).length > 2) {
+                return;
+            }
+            ScopeNamer.prototype.debugCall("addSymbol : " + symbol.alias );       
+            
             if (ScopeNamer.symbols.hasSymbol(symbol.alias)) {
                 
                 if (!comment) { // we do not have a comment, and it's registered.
@@ -384,7 +447,7 @@ ScopeNamer = XObject.define(
             
             
             //print("ADD symbol: " + symbol.isa + ' => ' + symbol.alias );
-            print("ADD symbol: " + JSON.stringify( symbol, null, 4));
+            //print("ADD symbol: " + JSON.stringify( symbol, null, 4));
             
             // add it to the file's list... (for dumping later..)
             if (this.srcFile) {
@@ -401,7 +464,7 @@ ScopeNamer = XObject.define(
             builtin.isPrivate = false;
             this.addSymbol(builtin);
             return builtin;
-        };
+        }