@@ -32,6 +32,7 @@ export interface VirtualTypeScriptEnvironment {
3232 getSourceFile : ( fileName : string ) => import ( "typescript" ) . SourceFile | undefined
3333 createFile : ( fileName : string , content : string ) => void
3434 updateFile : ( fileName : string , content : string , replaceTextSpan ?: import ( "typescript" ) . TextSpan ) => void
35+ deleteFile : ( fileName : string ) => void
3536}
3637
3738/**
@@ -54,7 +55,7 @@ export function createVirtualTypeScriptEnvironment(
5455) : VirtualTypeScriptEnvironment {
5556 const mergedCompilerOpts = { ...defaultCompilerOptions ( ts ) , ...compilerOptions }
5657
57- const { languageServiceHost, updateFile } = createVirtualLanguageServiceHost (
58+ const { languageServiceHost, updateFile, deleteFile } = createVirtualLanguageServiceHost (
5859 sys ,
5960 rootFiles ,
6061 mergedCompilerOpts ,
@@ -99,6 +100,12 @@ export function createVirtualTypeScriptEnvironment(
99100
100101 updateFile ( newSourceFile )
101102 } ,
103+ deleteFile ( fileName ) {
104+ const sourceFile = languageService . getProgram ( ) ! . getSourceFile ( fileName )
105+ if ( sourceFile ) {
106+ deleteFile ( sourceFile )
107+ }
108+ }
102109 }
103110}
104111
@@ -466,6 +473,9 @@ export function createSystem(files: Map<string, string>): System {
466473 writeFile : ( fileName , contents ) => {
467474 files . set ( fileName , contents )
468475 } ,
476+ deleteFile : ( fileName ) => {
477+ files . delete ( fileName )
478+ } ,
469479 }
470480}
471481
@@ -545,6 +555,9 @@ export function createFSBackedSystem(
545555 writeFile : ( fileName , contents ) => {
546556 files . set ( fileName , contents )
547557 } ,
558+ deleteFile : ( fileName ) => {
559+ files . delete ( fileName )
560+ } ,
548561 realpath : nodeSys . realpath ,
549562 }
550563}
@@ -564,6 +577,7 @@ export function createVirtualCompilerHost(sys: System, compilerOptions: Compiler
564577 type Return = {
565578 compilerHost : CompilerHost
566579 updateFile : ( sourceFile : SourceFile ) => boolean
580+ deleteFile : ( sourceFile : SourceFile ) => boolean
567581 }
568582
569583 const vHost : Return = {
@@ -594,6 +608,12 @@ export function createVirtualCompilerHost(sys: System, compilerOptions: Compiler
594608 sourceFiles . set ( sourceFile . fileName , sourceFile )
595609 return alreadyExists
596610 } ,
611+ deleteFile : sourceFile => {
612+ const alreadyExists = sourceFiles . has ( sourceFile . fileName )
613+ sourceFiles . delete ( sourceFile . fileName )
614+ sys . deleteFile ! ( sourceFile . fileName )
615+ return alreadyExists
616+ }
597617 }
598618 return vHost
599619}
@@ -609,7 +629,7 @@ export function createVirtualLanguageServiceHost(
609629 customTransformers ?: CustomTransformers
610630) {
611631 const fileNames = [ ...rootFiles ]
612- const { compilerHost, updateFile } = createVirtualCompilerHost ( sys , compilerOptions , ts )
632+ const { compilerHost, updateFile, deleteFile } = createVirtualCompilerHost ( sys , compilerOptions , ts )
613633 const fileVersions = new Map < string , string > ( )
614634 let projectVersion = 0
615635 const languageServiceHost : LanguageServiceHost = {
@@ -642,6 +662,7 @@ export function createVirtualLanguageServiceHost(
642662 type Return = {
643663 languageServiceHost : LanguageServiceHost
644664 updateFile : ( sourceFile : import ( "typescript" ) . SourceFile ) => void
665+ deleteFile : ( sourceFile : import ( "typescript" ) . SourceFile ) => void
645666 }
646667
647668 const lsHost : Return = {
@@ -654,6 +675,15 @@ export function createVirtualLanguageServiceHost(
654675 }
655676 updateFile ( sourceFile )
656677 } ,
678+ deleteFile : sourceFile => {
679+ projectVersion ++
680+ fileVersions . set ( sourceFile . fileName , projectVersion . toString ( ) )
681+ const index = fileNames . indexOf ( sourceFile . fileName )
682+ if ( index !== - 1 ) {
683+ fileNames . splice ( index , 1 )
684+ }
685+ deleteFile ( sourceFile )
686+ }
657687 }
658688 return lsHost
659689}
0 commit comments