@@ -20,28 +20,130 @@ var comment = require('./comment');
2020 * @property {Date } version Last time when the file was parsed
2121 * @property {integer } size The total file size
2222 * @property {String } name The filename
23- * @property {namespace[] } namespaces {@link NAMESPACE.md|:link: } List of namespaces
24- * @property {declare[] } declares {@link DECLARE.md|:link: } List of declare nodes
25- * @property {reference[] } links {@link REFERENCE.md|:link: } List of references (constants, classes, interfaces...)
26- * @property {external[] } externals {@link EXTERNAL.md|:link: } List of external references
23+ * @property {node[] } nodes {@link NODE.md|:link: } List of nodes
2724 * @property {error } error Error node
2825 */
2926var file = block . extends ( function file ( repository , name , ast ) {
3027 this . repository = repository ;
3128 this . version = new Date ( ) ;
3229 this . size = 0 ;
3330 this . name = name ;
34- this . namespaces = [ ] ;
35- this . declares = [ ] ;
36- this . links = [ ] ;
37- this . externals = [ ] ;
31+ this . nodes = [ ] ;
3832 this . error = null ;
39- this . _scopes = [ ] ;
4033
4134 // super constructor
4235 block . apply ( this , [ this , ast ] ) ;
4336} ) ;
4437
38+ /**
39+ * Generic lookup by node type
40+ * @return {node[] }
41+ */
42+ file . prototype . getByType = function ( type ) {
43+ // build type index
44+ if ( ! this . _indexNodeType ) {
45+ this . _indexNodeType = { } ;
46+ var result = [ ] ;
47+ for ( var i = 0 ; i < this . nodes . length ; i ++ ) {
48+ var item = this . nodes [ i ] ;
49+ if ( ! this . _indexNodeType . hasOwnProperty ( item . type ) ) {
50+ this . _indexNodeType [ item . type ] = [ ] ;
51+ }
52+ this . _indexNodeType [ item . type ] . push ( item ) ;
53+ }
54+ }
55+ return this . _indexNodeType . hasOwnProperty ( type ) ?
56+ this . _indexNodeType [ type ] : [ ]
57+ ;
58+ } ;
59+
60+ /**
61+ * Generic lookup by node name
62+ * @return {node[] }
63+ */
64+ file . prototype . getByName = function ( type , name ) {
65+ if ( ! this . _indexNodeName ) {
66+ this . _indexNodeName = { } ;
67+ }
68+
69+ // build names index
70+ if ( ! this . _indexNodeName . hasOwnProperty ( type ) ) {
71+ var nodes = this . getByType ( type ) ;
72+ var cache = { } ;
73+ for ( var i = 0 ; i < nodes . length ; i ++ ) {
74+ var item = nodes [ i ] ;
75+ var index = null ;
76+ if ( item . hasOwnProperty ( 'fullName' ) ) {
77+ index = item . fullName ;
78+ } else if ( item . hasOwnProperty ( 'name' ) ) {
79+ index = item . name ;
80+ }
81+ if ( index ) {
82+ if ( ! cache . hasOwnProperty ( index ) ) {
83+ cache [ index ] = [ ] ;
84+ }
85+ cache [ index ] . push ( item ) ;
86+ }
87+ }
88+ this . _indexNodeName [ type ] = cache ;
89+ }
90+
91+ return this . _indexNodeType [ type ] . hasOwnProperty ( name ) ?
92+ this . _indexNodeType [ type ] [ name ] : [ ]
93+ ;
94+ } ;
95+
96+ /**
97+ * Generic lookup by node name
98+ * @return {node|null }
99+ */
100+ file . prototype . getFirstByName = function ( type , name ) {
101+ var result = this . getByName ( type , name ) ;
102+ return result . length > 0 ? result [ 0 ] : null ;
103+ } ;
104+
105+ /**
106+ * @return {namespace[] }
107+ */
108+ file . prototype . getNamespaces = function ( ) {
109+ return this . getByType ( 'namespace' ) ;
110+ } ;
111+
112+ /**
113+ * @return {class[] }
114+ */
115+ file . prototype . getClasses = function ( ) {
116+ return this . getByType ( 'class' ) ;
117+ } ;
118+
119+ /**
120+ * @return {interfaces[] }
121+ */
122+ file . prototype . getInterfaces = function ( ) {
123+ return this . getByType ( 'class' ) ;
124+ } ;
125+
126+ /**
127+ * @return {external[] }
128+ */
129+ file . prototype . getIncludes = function ( ) {
130+ return this . getByType ( 'external' ) ;
131+ } ;
132+
133+ /**
134+ * @return {class }
135+ */
136+ file . prototype . getClass = function ( name ) {
137+ return this . getFirstByName ( 'class' , name ) ;
138+ } ;
139+
140+ /**
141+ * @return {class }
142+ */
143+ file . prototype . getNamespace = function ( name ) {
144+ return this . getFirstByName ( 'namespace' , name ) ;
145+ } ;
146+
45147/**
46148 * @protected Consumes the current ast node
47149 */
@@ -63,7 +165,7 @@ file.prototype.consume = function(ast) {
63165 ast [ 1 ] . forEach ( function ( item ) {
64166 var type = block . getASTType ( item ) ;
65167
66- if ( type ) {
168+ if ( type ) { /*
67169 if (type === 'declare') {
68170 this.declares.push(
69171 new declare(this, item)
@@ -82,15 +184,15 @@ file.prototype.consume = function(ast) {
82184 root.push(doc);
83185 }
84186 root.push(item);
85- }
187+ }*/
86188 }
87189 } . bind ( this ) ) ;
88190
89191 // create an empty namespace
90192 if ( root . length > 0 ) {
91- new namespace ( this , [
193+ /* new namespace(this, [
92194 'namespace', [''], root
93- ] ) ;
195+ ]);*/
94196 }
95197} ;
96198
0 commit comments