@@ -71,9 +71,14 @@ interface IYamlReferences {
7171}
7272
7373const enum FlattenMode {
74+ /** Include entries for nested namespaces and non-namespace children. */
7475 NestedNamespacesAndChildren ,
76+ /** Include entries for nested namespaces only. */
7577 NestedNamespacesOnly ,
76- NoNamespaces
78+ /** Include entries for non-namespace immediate children. */
79+ ImmediateChildren ,
80+ /** Include entries for nested non-namespace children. */
81+ NestedChildren
7782}
7883
7984interface INameOptions {
@@ -85,15 +90,17 @@ interface INameOptions {
8590 * Writes documentation in the Universal Reference YAML file format, as defined by typescript.schema.json.
8691 */
8792export class YamlDocumenter {
93+ protected readonly documentNamespaces : boolean ;
8894 private readonly _apiModel : ApiModel ;
8995 private readonly _markdownEmitter : CustomMarkdownEmitter ;
9096
9197 private _apiItemsByCanonicalReference : Map < string , ApiItem > ;
9298 private _yamlReferences : IYamlReferences | undefined ;
9399 private _outputFolder : string ;
94100
95- public constructor ( apiModel : ApiModel ) {
101+ public constructor ( apiModel : ApiModel , documentNamespaces : boolean = false ) {
96102 this . _apiModel = apiModel ;
103+ this . documentNamespaces = documentNamespaces ;
97104 this . _markdownEmitter = new CustomMarkdownEmitter ( this . _apiModel ) ;
98105 this . _apiItemsByCanonicalReference = new Map < string , ApiItem > ( ) ;
99106
@@ -186,7 +193,7 @@ export class YamlDocumenter {
186193 this . _recordYamlReference (
187194 this . _ensureYamlReferences ( ) ,
188195 this . _getUid ( apiItem ) ,
189- this . _getYamlItemName ( apiItem , { includeSignature : true } ) ,
196+ this . _getYamlItemName ( apiItem , { includeNamespace : ! this . documentNamespaces , includeSignature : true } ) ,
190197 this . _getYamlItemName ( apiItem , { includeNamespace : true , includeSignature : true } ) ) ;
191198 }
192199 }
@@ -198,9 +205,11 @@ export class YamlDocumenter {
198205 const children : ApiItem [ ] = [ ] ;
199206 if ( apiItem . kind === ApiItemKind . Package ) {
200207 // Skip over the entry point, since it's not part of the documentation hierarchy
201- this . _flattenNamespaces ( apiItem . members [ 0 ] . members , children , FlattenMode . NestedNamespacesAndChildren ) ;
208+ this . _flattenNamespaces ( apiItem . members [ 0 ] . members , children ,
209+ this . documentNamespaces ? FlattenMode . NestedNamespacesAndChildren : FlattenMode . NestedChildren ) ;
202210 } else {
203- this . _flattenNamespaces ( apiItem . members , children , FlattenMode . NoNamespaces ) ;
211+ this . _flattenNamespaces ( apiItem . members , children ,
212+ this . documentNamespaces ? FlattenMode . ImmediateChildren : FlattenMode . NestedChildren ) ;
204213 }
205214 return children ;
206215 }
@@ -215,23 +224,34 @@ export class YamlDocumenter {
215224 let hasNonNamespaceChildren : boolean = false ;
216225 for ( const item of items ) {
217226 if ( item . kind === ApiItemKind . Namespace ) {
218- if ( mode !== FlattenMode . NoNamespaces ) {
219- // At any level, always include a nested namespace if it has non-namespace children, but do not include its
220- // non-namespace children in the result.
221-
222- // Record the offset at which the namespace is added in case we need to remove it later.
223- const index : number = childrenOut . length ;
224- childrenOut . push ( item ) ;
225-
226- if ( ! this . _flattenNamespaces ( item . members , childrenOut , FlattenMode . NestedNamespacesOnly ) ) {
227- // This namespace had no non-namespace children, remove it.
228- childrenOut . splice ( index , 1 ) ;
229- }
227+ switch ( mode ) {
228+ case FlattenMode . NestedChildren :
229+ // Include children of namespaces, but not the namespaces themselves. This matches existing legacy behavior.
230+ this . _flattenNamespaces ( item . members , childrenOut , FlattenMode . NestedChildren ) ;
231+ break ;
232+ case FlattenMode . NestedNamespacesOnly :
233+ case FlattenMode . NestedNamespacesAndChildren :
234+ // At any level, always include a nested namespace if it has non-namespace children, but do not include its
235+ // non-namespace children in the result.
236+
237+ // Record the offset at which the namespace is added in case we need to remove it later.
238+ const index : number = childrenOut . length ;
239+ childrenOut . push ( item ) ;
240+
241+ if ( ! this . _flattenNamespaces ( item . members , childrenOut , FlattenMode . NestedNamespacesOnly ) ) {
242+ // This namespace had no non-namespace children, remove it.
243+ childrenOut . splice ( index , 1 ) ;
244+ }
245+ break ;
230246 }
231247 } else if ( this . _shouldInclude ( item . kind ) ) {
232- if ( mode !== FlattenMode . NestedNamespacesOnly ) {
233- // At the top level, include non-namespace children as well.
234- childrenOut . push ( item ) ;
248+ switch ( mode ) {
249+ case FlattenMode . NestedChildren :
250+ case FlattenMode . NestedNamespacesAndChildren :
251+ case FlattenMode . ImmediateChildren :
252+ // At the top level, include non-namespace children as well.
253+ childrenOut . push ( item ) ;
254+ break ;
235255 }
236256 hasNonNamespaceChildren = true ;
237257 }
@@ -266,15 +286,23 @@ export class YamlDocumenter {
266286 private _buildTocItems ( apiItems : ReadonlyArray < ApiItem > ) : IYamlTocItem [ ] {
267287 const tocItems : IYamlTocItem [ ] = [ ] ;
268288 for ( const apiItem of apiItems ) {
269- if ( this . _shouldEmbed ( apiItem . kind ) ) {
270- // Don't generate table of contents items for embedded definitions
271- continue ;
289+ let tocItem : IYamlTocItem ;
290+ if ( apiItem . kind === ApiItemKind . Namespace && ! this . documentNamespaces ) {
291+ tocItem = {
292+ name : this . _getTocItemName ( apiItem )
293+ } ;
294+ } else {
295+ if ( this . _shouldEmbed ( apiItem . kind ) ) {
296+ // Don't generate table of contents items for embedded definitions
297+ continue ;
298+ }
299+
300+ tocItem = {
301+ name : this . _getTocItemName ( apiItem ) ,
302+ uid : this . _getUid ( apiItem )
303+ } ;
272304 }
273305
274- const tocItem : IYamlTocItem = {
275- name : this . _getTocItemName ( apiItem ) ,
276- uid : this . _getUid ( apiItem )
277- } ;
278306 tocItems . push ( tocItem ) ;
279307
280308 const children : ApiItem [ ] = this . _getLogicalChildren ( apiItem ) ;
@@ -308,8 +336,9 @@ export class YamlDocumenter {
308336 case ApiItemKind . Package :
309337 case ApiItemKind . Interface :
310338 case ApiItemKind . Enum :
311- case ApiItemKind . Namespace :
312339 return false ;
340+ case ApiItemKind . Namespace :
341+ return ! this . documentNamespaces ;
313342 }
314343 return true ;
315344 }
@@ -366,13 +395,15 @@ export class YamlDocumenter {
366395 }
367396 }
368397
369- yamlItem . name = this . _getYamlItemName ( apiItem , { includeSignature : true } ) ;
398+ yamlItem . name = this . _getYamlItemName ( apiItem , { includeSignature : true ,
399+ includeNamespace : ! this . documentNamespaces } ) ;
370400 yamlItem . fullName = this . _getYamlItemName ( apiItem , { includeSignature : true , includeNamespace : true } ) ;
371401 yamlItem . langs = [ 'typeScript' ] ;
372402
373403 // Add the namespace of the item if it is contained in one.
374404 // Do not add the namespace parent of a namespace as they are flattened in the documentation.
375- if ( apiItem . kind !== ApiItemKind . Namespace && apiItem . parent && apiItem . parent . kind === ApiItemKind . Namespace ) {
405+ if ( apiItem . kind !== ApiItemKind . Namespace && apiItem . parent && apiItem . parent . kind === ApiItemKind . Namespace &&
406+ this . documentNamespaces ) {
376407 yamlItem . namespace = apiItem . parent . canonicalReference . toString ( ) ;
377408 }
378409
0 commit comments