license import { Declaration } from './declarations' ; import { formatComment } from './formatting' ; import { Export , Node } from './index' ; export interface TsLintDisable { ruleName : string ; why : string ; } export class Document { readonly kind = 'document' ; path : string ; members : Declaration [ ] ; referencePaths : Set < string > ; header : string ; isEsModule : boolean ; tsLintDisables : TsLintDisable [ ] ; constructor ( data ) { this . path = data . path ; this . members = data . members || [ ] ; this . referencePaths = new Set ( Array . from ( data . referencePaths || [ ] ) ) ; this . header = data . header || '' ; this . isEsModule = data . isEsModule || false ; this . tsLintDisables = data . tsLintDisables ; } /**
     * Iterate over all nodes in the document, depth first. Includes all
     * recursive ancestors, and the document itself.
     */ * traverse ( ) { for ( const m of this . members ) { yield * m . traverse ( ) ; } yield this ; } /**
     * Clean up this AST.
     */ simplify ( ) { for ( const node of this . traverse ( ) ) { if ( node . kind === 'union' ) { node . simplify ( ) ; } } } serialize ( ) { let out = '' ; if ( this . header ) { out += formatComment ( this . header , 0 ) + '\n' ; } const disables = this . tsLintDisables . slice ( ) ; for ( const node of this . traverse ( ) ) { if ( node . kind === 'name' && node . name === 'any' ) { disables . push ( { ruleName : 'no-any' , why : 'describes the API as best we are able today' } ) ; break ; } } out += '\n' ; for ( const disable of disables ) { out += `// tslint:disable:${ disable . ruleName } ${ disable . why }\n` ; } out += '\n' ; if ( this . referencePaths . size > 0 ) { for ( const ref of this . referencePaths ) { out += `/// <reference path="${ ref }" />\n` ; } out += '\n' ; } out += this . members . map ( ( m ) => m . serialize ( ) ) . join ( '\n' ) ; if ( this . isEsModule === true && ! this . members . some ( ( m ) => m . kind === 'import' || m . kind === 'export' ) ) { out += '\n' + ( new Export ( { identifiers : [ ] } ) ) . serialize ( ) ; } return out ; } }