import * as ts from 'typescript' ; import { chainHosts } from './hosts-base' ; import { CodeTransformer , VisitorBasedTransformer } from './transformer' ; import { ChainableHost } from './hosts-base' ; import { MutableSourceCode } from './mutable-source-code' ; import { RawSourceMap } from 'source-map' ; import { defaultCompilerOptions } from './configuration' ; import { MultipleFilesHost } from './hosts' ; import { Visitor } from './visitor' ; const normalizePath = ts [ 'normalizePath' ] ; const getDirectoryPath = ts [ 'getDirectoryPath' ] ; const combinePaths = ts [ 'combinePaths' ] ; export class AstCacheHost extends ChainableHost { private cache : { [ fileName ] : ts . SourceFile ; } = { } ; getSourceFile ( fileName , languageVersion , onError ? ) { const cachedAst = this . cache [ fileName ] ; if ( ! cachedAst ) { const ast = this . source . getSourceFile ( fileName , languageVersion , onError ) ; this . cache [ fileName ] = ast ; return ast ; } else { return cachedAst ; } } } export class TransformationHost extends ChainableHost { private transformations : { [ fileName ] : MutableSourceCode ; } = { } ; private transformer : CodeTransformer ; constructor ( visitors , languageServiceProvider = ( ) => null ) { super ( ) ; this . transformer = new VisitorBasedTransformer ( visitors , languageServiceProvider ) ; } getSourceFile ( fileName , languageVersion , onError ? ) { const ast = super . getSourceFile ( fileName , languageVersion , onError ) ; if ( ast ) { const transformation = this . transformer . transform ( ast ) ; this . transformations [ ast . fileName ] = transformation ; return transformation . ast ; } else { return null ; } } getSourceMap ( fileName ) { const transformation = this . transformations [ fileName ] ; if ( transformation ) { return transformation . sourceMap ; } else { return null ; } } translateDiagnostic ( diagnostic ) { const transformation = this . transformations [ diagnostic . file . fileName ] ; return transformation ? transformation . translateDiagnostic ( diagnostic ) : diagnostic ; } } export class SemanticHost extends ChainableHost implements ts . LanguageServiceHost , ts . CompilerHost { constructor ( private files , private compilerOptions = defaultCompilerOptions , private libDir = 'node_modules' ) { super ( ) ; } getProjectVersion ( ) { return null ; } getScriptFileNames ( ) { return this . files . slice ( ) ; } getScriptVersion ( fileName ) { return null ; } getScriptSnapshot ( fileName ) { return ts . ScriptSnapshot . fromString ( this . readFile ( fileName ) ) ; } getLocalizedDiagnosticMessages ( ) { return null ; } getCompilationSettings ( ) { return this . compilerOptions ; } log ( s ) { } trace ( s ) { } error ( s ) { } resolveModuleNames ( moduleNames , containingFile ) { return moduleNames . map ( ( moduleName ) => { return ts . nodeModuleNameResolver ( moduleName , containingFile , this . compilerOptions , this ) . resolvedModule ; } ) ; } directoryExists ( directoryName ) { return this . source . directoryExists ? this . source . directoryExists ( directoryName ) : undefined ; } acquireDocument ( fileName , compilationSettings , scriptSnapshot , version ) { return this . source . getSourceFile ( fileName , compilationSettings . target ) ; } param fileName param compilationSettings param scriptSnapshot param version updateDocument ( fileName , compilationSettings , scriptSnapshot , version ) { return this . source . getSourceFile ( fileName , compilationSettings . target ) ; } param fileName param compilationSettings releaseDocument ( fileName , compilationSettings ) { } reportStats ( ) { return '' ; } }