@@ -55,39 +55,73 @@ module.exports = function(comments) {
55
55
members : getMembers ( )
56
56
} ;
57
57
58
+ const namesToUnroot = [ ] ;
59
+
58
60
comments . forEach ( comment => {
59
- var path = [ ] ;
61
+ let path = comment . path ;
62
+ if ( ! path ) {
63
+ path = [ ] ;
64
+
65
+ if ( comment . memberof ) {
66
+ // TODO: full namepath parsing
67
+ path = comment . memberof
68
+ . split ( '.' )
69
+ . map ( segment => ( { scope : 'static' , name : segment } ) ) ;
70
+ }
60
71
61
- if ( comment . memberof ) {
62
- // TODO: full namepath parsing
63
- path = comment . memberof . split ( '.' ) . map ( segment => [ 'static' , segment ] ) ;
64
- }
72
+ if ( ! comment . name ) {
73
+ comment . errors . push ( {
74
+ message : 'could not determine @name for hierarchy'
75
+ } ) ;
76
+ }
65
77
66
- if ( ! comment . name ) {
67
- comment . errors . push ( {
68
- message : 'could not determine @ name for hierarchy'
78
+ path . push ( {
79
+ scope : comment . scope || 'static' ,
80
+ name : comment . name || 'unknown_' + id ++
69
81
} ) ;
70
82
}
71
83
72
- path . push ( [ comment . scope || 'static' , comment . name || 'unknown_' + id ++ ] ) ;
73
-
74
84
var node = root ;
75
85
76
86
while ( path . length ) {
77
- var segment = path . shift ( ) , scope = segment [ 0 ] , name = segment [ 1 ] ;
87
+ var segment = path . shift ( ) ,
88
+ scope = segment . scope ,
89
+ name = segment . name ;
78
90
79
91
if ( ! hasOwnProperty . call ( node . members [ scope ] , name ) ) {
80
- node . members [ scope ] [ name ] = {
81
- comments : [ ] ,
82
- members : getMembers ( )
83
- } ;
92
+ // If segment.toc is true, everything up to this point in the path
93
+ // represents how the documentation should be nested, but not how the
94
+ // actual code is nested. To ensure that child members end up in the
95
+ // right places in the tree, we temporarily push the same node a second
96
+ // time to the root of the tree, and unroot it after all the comments
97
+ // have found their homes.
98
+ if (
99
+ segment . toc &&
100
+ node !== root &&
101
+ hasOwnProperty . call ( root . members [ scope ] , name )
102
+ ) {
103
+ node . members [ scope ] [ name ] = root . members [ scope ] [ name ] ;
104
+ namesToUnroot . push ( name ) ;
105
+ } else {
106
+ const newNode = ( node . members [ scope ] [ name ] = {
107
+ comments : [ ] ,
108
+ members : getMembers ( )
109
+ } ) ;
110
+ if ( segment . toc && node !== root ) {
111
+ root . members [ scope ] [ name ] = newNode ;
112
+ namesToUnroot . push ( name ) ;
113
+ }
114
+ }
84
115
}
85
116
86
117
node = node . members [ scope ] [ name ] ;
87
118
}
88
119
89
120
node . comments . push ( comment ) ;
90
121
} ) ;
122
+ namesToUnroot . forEach ( function ( name ) {
123
+ delete root . members . static [ name ] ;
124
+ } ) ;
91
125
92
126
/*
93
127
* Massage the hierarchy into a format more suitable for downstream consumers:
@@ -107,7 +141,8 @@ module.exports = function(comments) {
107
141
* Person~say // the inner method named "say."
108
142
*/
109
143
function toComments ( nodes , root , hasUndefinedParent , path ) {
110
- var result = [ ] , scope ;
144
+ var result = [ ] ,
145
+ scope ;
111
146
112
147
path = path || [ ] ;
113
148
@@ -119,7 +154,9 @@ module.exports = function(comments) {
119
154
node . members [ scope ] ,
120
155
root || result ,
121
156
! node . comments . length ,
122
- node . comments . length ? path . concat ( node . comments [ 0 ] ) : [ ]
157
+ node . comments . length && node . comments [ 0 ] . kind !== 'note'
158
+ ? path . concat ( node . comments [ 0 ] )
159
+ : [ ]
123
160
) ;
124
161
}
125
162
0 commit comments