import { errors } from "../errors" ; import { StandardizedFilePath , TransactionalFileSystem } from "../fileSystem" ; import { CompilerOptions , ScriptKind , ts } from "../typescript" ; import { createCompilerSourceFile } from "./createCompilerSourceFile" ; /**
 * An implementation of a ts.DocumentRegistry that uses a transactional file system.
 */ export class DocumentRegistry implements ts . DocumentRegistry { private readonly sourceFileCacheByFilePath = new Map < StandardizedFilePath , ts . SourceFile > ( ) ; private static readonly initialVersion = "0" ; param transactionalFileSystem constructor ( private readonly transactionalFileSystem ) { } param fileName param compilationSettings param scriptSnapshot param scriptKind createOrUpdateSourceFile ( fileName , compilationSettings , scriptSnapshot , scriptKind ) { let sourceFile = this . sourceFileCacheByFilePath . get ( fileName ) ; if ( sourceFile == null ) sourceFile = this . updateSourceFile ( fileName , compilationSettings , scriptSnapshot , DocumentRegistry . initialVersion , scriptKind ) ; else sourceFile = this . updateSourceFile ( fileName , compilationSettings , scriptSnapshot , this . getNextSourceFileVersion ( sourceFile ) , scriptKind ) ; return sourceFile ; } param fileName removeSourceFile ( fileName ) { this . sourceFileCacheByFilePath . delete ( fileName ) ; } inheritdoc acquireDocument ( fileName , compilationSettings , scriptSnapshot , version , scriptKind ) { const standardizedFilePath = this . transactionalFileSystem . getStandardizedAbsolutePath ( fileName ) ; let sourceFile = this . sourceFileCacheByFilePath . get ( standardizedFilePath ) ; if ( sourceFile == null || this . getSourceFileVersion ( sourceFile ) !== version ) sourceFile = this . updateSourceFile ( standardizedFilePath , compilationSettings , scriptSnapshot , version , scriptKind ) ; return sourceFile ; } inheritdoc acquireDocumentWithKey ( fileName , path , compilationSettings , key , scriptSnapshot , version , scriptKind ) { return this . acquireDocument ( fileName , compilationSettings , scriptSnapshot , version , scriptKind ) ; } inheritdoc updateDocument ( fileName , compilationSettings , scriptSnapshot , version , scriptKind ) { return this . acquireDocument ( fileName , compilationSettings , scriptSnapshot , version , scriptKind ) ; } inheritdoc updateDocumentWithKey ( fileName , path , compilationSettings , key , scriptSnapshot , version , scriptKind ) { return this . updateDocument ( fileName , compilationSettings , scriptSnapshot , version , scriptKind ) ; } inheritdoc getKeyForCompilationSettings ( settings ) { return "defaultKey" as ts . DocumentRegistryBucketKey ; } inheritdoc releaseDocument ( fileName , compilationSettings ) { } inheritdoc releaseDocumentWithKey ( path , key ) { } inheritdoc reportStats ( ) { throw new errors . NotImplementedError ( ) ; } inheritdoc getSourceFileVersion ( sourceFile ) { return ( sourceFile as any ) . version || "0" ; } private getNextSourceFileVersion ( sourceFile ) { const currentVersion = parseInt ( this . getSourceFileVersion ( sourceFile ) , 10 ) || 0 ; return ( currentVersion + 1 ) . toString ( ) ; } private updateSourceFile ( fileName , compilationSettings , scriptSnapshot , version , scriptKind ) { const newSourceFile = createCompilerSourceFile ( fileName , scriptSnapshot , compilationSettings . target , version , true , scriptKind ) ; this . sourceFileCacheByFilePath . set ( fileName , newSourceFile ) ; return newSourceFile ; } }