sync fixes
[app.jsdoc] / JSDOC / ScopeNamer.js
index e6f7253..97d1118 100644 (file)
@@ -7,7 +7,8 @@ Symbol = imports.Symbol.Symbol;
 
 /**
  * @class ScopeNamer
- * @namespace JSDOC
+ * @extends JSDOC.Collapse
+ * @scope JSDOC
  * The point of this class is to iterate through the Collapsed tree
  * and add the property 'scopedName' to the tokens.
  *
@@ -87,18 +88,26 @@ ScopeNamer = XObject.define(
         this.args = this.args ? this.args.slice(0)  : [];
         Symbol.srcFile = this.filename;
         this.createJSDOC = true;
-        
+        this.global = '_global_';
+        this.locals = this.locals || [];
        // console.dump(ar);
         
     }, 
     imports.JSDOC.Collapse.Collapse, 
     {
+        
+        global : false,
         /**
          * Call debugging
          * 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(/Date\.js/)) return print(str);
+            return;
+        }, 
         
         collapseTop : true,
         
@@ -108,13 +117,13 @@ ScopeNamer = XObject.define(
                 this.statements = this.collapse(this.tokens);
                 //print(JSON.stringify(this.s, null,4));
             }
-            
+            //if (this.filename.match(/Roo\.js/)) print(JSON.stringify(this.statements, null,4));
             //this.globalScope(this.statements);
             this.debugCall("build Symbols");
             //print (this.statements);
             //print (JSON.stringify(this.statements, null,2));
            
-            this.walkStatements('_global_', this.statements)
+            this.walkStatements(this.global, this.statements)
             
                 
                 
@@ -128,12 +137,32 @@ ScopeNamer = XObject.define(
         
         walkStatements: function(scope, statements)
         {
-            this.debugCall("walkStatements :" + scope ) ;            
+            this.debugCall("walkStatements :" + scope + '@' + statements[0][0].line) ;            
             var _this = this;
             var res = false;
+            var isGlobal = scope == this.global;
+           
             
-            
-            statements.forEach(function(st ) {
+           
+            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');
+                    // might be an array..
+                    if (typeof(scope) != 'String') {
+                        scope = scope[0].desc;
+                        
+                    }
+                    print("SCOPE CHANGE TO:" + scope);
+                    _this.global = scope;
+                }
+                
+                
                 res = _this.walkStatement(scope, st);
                 if (res === true) {
                     return true;
@@ -146,29 +175,76 @@ ScopeNamer = XObject.define(
         {
             this.tokens = stmt;
             this.rewind();
-            this.debugCall("walkStatement :" + scope + '@' + this.tokens[0].line );
+            this.debugCall("walkStatement :" + this.global +'/' +scope + '@' + this.tokens[0].line );
              
             var name;
             var sn;
+            var token = false; 
+            
+            var isGlobal = scope ==   this.global;
             
-            while (null != (token = this.next())) {
             
+            
+            //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())) {
+                
+                
+                // var xx =
+                 
+                
+                
                 //'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);
+                        this.next();
+                        this.next();
+                        this.next();
                         continue;
                     }
                      //a = function() { } << scope might be a  $this$={a}
                     if (this.lookTok(-1).data == '=' &&
                         this.lookTok(-2).is('NAME')
                     ) {
+                        
                         name = this.lookTok(-2).data;
+                        
+                        if (!isGlobal) {
+                            // only this.*
+                            if (this.lookTok(-3).name == 'VAR') { 
+                                name = '';
+                            }
+                            if (name.match(/^this\./) && scope.match(/\.prototype$/)) {
+                                name = name.substring(5);
+                            }
+                            if (name.split('.').length > 1) {
+                                name = '';
+                            }
+                            
+                        }
+                        
+                        
                         this.walkFunctionDef(scope, name, this.lookTok(1).args, this.lookTok(2).items, this.lookTok(-2));
+                        this.next();
+                        this.next();
                         continue;
                     }
                     
@@ -194,6 +270,8 @@ ScopeNamer = XObject.define(
                         jsdoc = jsdoc || this.lookTok(-2).jsdoc;
                     }
                     this.walkCall(scope, assign, token.data, this.lookTok(1).items, jsdoc);
+                    this.next();
+                        
                     continue;
                 }
                 
@@ -202,17 +280,169 @@ 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;
+                    
+                    // some a = {} can be hidden
+                    // using @type {string} for example.
+                    // FIXME - they should be registered as a property...
+                    if (jd && jd.jsdoc && jd.jsdoc.getTag('type').length) {
+                         
+                        //name = false;
+                        //continue;
+                        // wipe out the children..
+                        token.props = false;
+                    }
+                    /*
+                    print(JSON.stringify({
+                        name : name,
+                        isGlobal : isGlobal,
+                        isVar : isVar
+                    }, null,4));
+                    */  
+                        
+                    if (!isGlobal) {
+                        // not global.
+                        if (isVar) {
+                            if (name && this.locals.indexOf(name) < 0) {
+                                this.locals.push(name);
+                            }
+                            name = false;
+                            continue;
+                        }
+                        if (this.locals.indexOf(name) > -1 ) {
+                            continue;
+                        }
+                        
+                        
+                        if (name && !name.match(/^this\./)) {
+                            name = false;
+                        }
+                        if (name && name.match(/^this\./) ) { 
+                            // see if scope ends in prototype..
+                            //print("q" + JSON.stringify(scope));
+                            if (
+                                (scope.split('.').pop() == 'prototype') &&
+                                (name.split('.').length == 2)
+                            ){
+                                name = name.split('.').pop();
+                            } else {
+                                name = false;
+                            }
+                            
+                        }   
+                        
+                    }
+                    
+                     
+                    //print(JSON.stringify(token,null,4));
+                    this.walkObject(scope, name, token.props, jd);
                     continue;
                 }
                  
-                // this.xxxx = (with jsdoc...)
                 
+                 //  'assignment..'
+                 // it's done on the third element to enable object assignment to work above..
+                //   a = .....
+                if (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);
+                    
+                    var isVar = this.lookTok(-3).name == 'VAR';
+                    
+                    // only register names of objects if 
+                    var name = this.lookTok(-2).data;
+                    
+                    // some a = {} can be hidden
+                    // using @type {string} for example.
+                    // FIXME - they should be registered as a property...
+                    if (jd && jd.jsdoc && jd.jsdoc.getTag('type').length) {
+                         
+                        //name = false;
+                        //continue;
+                        // wipe out the children..
+                        token.props = false;
+                    }
+                    /*
+                    print(JSON.stringify({
+                        name : name,
+                        isGlobal : isGlobal,
+                        isVar : isVar
+                    }, null,4));
+                    */
+                    
+                    // skip imports.
+                    if (token.data.match(/imports\./)) {
+                        name = false;
+                    }
+                    
+                        
+                    if (!isGlobal) {
+                        // not global.
+                        if (isVar) {
+                            if (name && this.locals.indexOf(name) < 0) {
+                                this.locals.push(name);
+                            }
+                            name = false;
+                            continue;
+                        }
+                        if (this.locals.indexOf(name) > -1 ) {
+                            continue;
+                        }
+                        
+                        if (name && !name.match(/^this\./)) {
+                            name = false; 
+                        }
+                        
+                        
+                        if (name && name.match(/^this\./) ) { 
+                            // see if scope ends in prototype..
+                            //print("SCOPE:" + JSON.stringify(scope));
+                            if (
+                                (scope.split('.').pop() == 'prototype') &&
+                                (name.split('.').length == 2)
+                            ){
+                                name = name.split('.').pop();
+                            } else {
+                                name = false;
+                            }
+                            
+                        }   
+                        
+                    } else {
+                        // should not happen...
+                        if (name && name.match(/^this\./)) {
+                            name = false; 
+                        }
+                    }
+                    
+
+                    if (name) {
+                        var symbol = new Symbol( scope +'.'+ name , false , "PROPERTY" ,  jd.jsdoc);
+                        symbol._token = this.lookTok(-2);
+                           
+                        this.addSymbol(symbol, jd.jsdoc);
+                        continue;
+                    }
+                    
+                    
+                     
+                }
                 
+                // TODO:
+                // this.xxxx = (with jsdoc...)
+
+                      
                 
                  
-                // standard flow....
+                // standard flow... - ignore opening {.
                 if (token.data == '{') { 
                     sn = new ScopeNamer(this);
                     //print("GOT { - walkings statuements;}");
@@ -220,31 +450,52 @@ ScopeNamer = XObject.define(
                         continue; // object..!?!?!? = ignore ???
                         print(JSON.stringify(token,null,4));
                     }
+                     
+                    
+                    //print ("GOT { = running walk statemengs on children?} @" + this.cursor);
                     sn.walkStatements(scope, token.items);
                     continue;
                 }
+                  if (this.filename.match(/Roo\.js/)   && token.data == '(')  {
+                    //print(JSON.stringify(token,null,4));
+                      
+                  }
+                // ( function() { 
+                if ((token.data == '(') && (token.items[0][0].data == 'function')) {
+                    sn = new ScopeNamer(this);
+                    sn.walkStatements('_private_', token.items[0][2].items);
+                    continue;
+                    
+                }
+                
+                
+                
             }
+            
+             this.debugCall("walkStatement (END):" + this.global +'/' +scope + '@' + this.tokens[0].line );
+           
         },
         
         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);
+            this.debugCall("wallkFuncDef: " + inscope + '#' + name + '@' + this.look(0).line );
+            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);
             
+            sn.walkStatements(scope, stmts);
+            this.debugCall("wallkFuncDef:  (END)" + inscope + '#' + name + '@' + this.look(0).line );
         },            
         
         
         
         walkCall : function (inscope, assign, callname, items, jsdocTok)
         {
-            this.debugCall("wallkCall : " + inscope  +':' + assign + '@' + this.look(0).line + ' ' + callname );
+            this.debugCall("walkCall : " + inscope  +':' + assign + '@' + this.look(0).line + ' ' + callname );
             var scope = inscope + ( assign ? ('.' + assign) : '' );
             //scope = scope.replace(/\^_global\$\./, '');
             
@@ -258,17 +509,31 @@ ScopeNamer = XObject.define(
                 // or... x = somecall(BASE, { ... object..})
                 case 'XObject.extend':
                 case 'Roo.apply':
+                case 'Roo.applyIf':
                     //print(JSON.stringify(items,null,4));
-                    scope = items[0][0].data;
+                    // SHOULD WE ADD SCOPE HERE???
+                    
+                    var arg0 = items[0][0].data;
+                    
+                    var topscope = scope.split('.').pop();
+                    
+                    scope = (topscope == arg0) ? arg0 : scope + '.' + arg0;
+                    
+                    // see if arg0 is defined has a symbol..
+                    if (this.haveSymbol(arg0)) {
+                        scope = arg0;
+                    }
+                    
+                    
                     // 2nd arg is a object def
                     if (items[1][0].data != '{') {
                         return;
                     }
                     
                     if (assign != false   && !scope.match(/\.prototype$/)) { 
-                            var symbol = new Symbol( scope , false , "OBJECT" ,  jsdocTok);
-                            symbol._token = jsdocTok;
-                        }
+                        var symbol = new Symbol( scope , false , "OBJECT" ,  jsdocTok);
+                        symbol._token = jsdocTok;
+                        this.addSymbol(symbol, jsdocTok.jsdoc);
                         
                     }
                     
@@ -276,8 +541,9 @@ ScopeNamer = XObject.define(
                     var sn = new ScopeNamer(this);
                     
                     //print(JSON.stringify(items,null,4));
-                    sn.walkObject(scope + '.prototype', false, items[1][0].props, jsdocTok );
-                    
+                    sn.walkObject(scope , false, items[1][0].props, jsdocTok );
+                    this.debugCall("walkCall(END): " + inscope  +':' + assign + '@' + this.look(0).line + ' ' + callname );
+           
                     return;
                 
                 
@@ -292,17 +558,22 @@ ScopeNamer = XObject.define(
                     
                     this.addSymbol(symbol, jsdocTok.jsdoc);
                     var sn = new ScopeNamer(this);
+                    //print(JSON.stringify(items, null,4));
+                    // ctr statements.
+                    sn.walkStatements(scope + '.prototype', items[0][2].items);
                     
                     
-                    sn.walkStatements(scope, items[0][2].items);
-                    sn.walkObject(scope + '.prototype', false, items[2].props );
+                    sn.walkObject(scope + '.prototype', false, items[2][0].props );
+                    this.debugCall("walkCall(END): " + inscope  +':' + assign + '@' + this.look(0).line + ' ' + callname );
+           
                     return;
                 
                 
                 
             }
             
-            
+            this.debugCall("walkCall(END): " + inscope  +':' + assign + '@' + this.look(0).line + ' ' + callname );
+           
              
         },
                             
@@ -317,8 +588,18 @@ ScopeNamer = XObject.define(
                 this.addSymbol(symbol, jsdocTok.jsdoc);
                  
             }
-            print(typeof(items));
-            this.debugCall("wallkObject : " + scope + '@' + items[Object.keys(items)[0]].key.line);
+            // items can be false in this scenaro: a = {};
+            if (!items) {
+                return;
+            }
+            // our other scenario is where something is flagged as private, we will not investigate further
+            // we do add it however..
+            if (jsdocTok && jsdocTok.jsdoc && jsdocTok.jsdoc.getTag('private').length) {
+                return;
+            }
+            
+            //print(typeof(items));
+            this.debugCall("wallkObject : " + scope + '#' + name + '@' + items[Object.keys(items)[0]].key.line);
             for( var k in items) {
                 var key = items[k].key;
                 var val = items[k].val;
@@ -361,8 +642,22 @@ ScopeNamer = XObject.define(
         {
               //print("ADD symbol: " + JSON.stringify( symbol, null, 4));
               //print("PARSER addSYMBOL : " + symbol.alias);
-             this.debugCall("addSymbol : " + symbol.alias );       
+            
             // if a symbol alias is documented more than once the last one with the user docs wins
+            
+            // dumpe some invalid symbols..
+            var ptype_ar = symbol.alias.split(/#/);
+            if (ptype_ar.length > 2) {
+                // multiple #
+                return;
+            }
+            if (ptype_ar.length > 1 && ptype_ar[1].split(/\./).length > 1) {
+                // multiple . after #
+                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.
@@ -387,9 +682,11 @@ ScopeNamer = XObject.define(
             //if (this.conf.treatUnderscoredAsPrivate && symbol.name.match(/[.#-]_[^.#-]+$/)) {
             //    symbol.isPrivate = true;
             //}
-            
+            if (ScopeNamer.builder.treatUnderscoredAsPrivate && symbol.name.match(/[.#-]_[^.#-]+$/)) {
+                symbol.isPrivate = true;
+            }
             // -p flag is required to document private things
-            if ((symbol.isInner || symbol.isPrivate) && !this.docPrivate) return;
+            //if ((symbol.isInner || symbol.isPrivate) && !this.docPrivate) return;
             
             // ignored things are not documented, this doesn't cascade
             if (symbol.isIgnored) return;
@@ -405,6 +702,13 @@ ScopeNamer = XObject.define(
             
             ScopeNamer.symbols.addSymbol(symbol);
         },
+        
+        haveSymbol: function(name)
+        {
+            return ScopeNamer.symbols.hasSymbol(name);
+             
+        },
+        
         addBuiltin : function(name) {
 
             var builtin = new Symbol(name, [], "CONSTRUCTOR", new imports.DocComment.DocComment(""));