@@ -154,6 +154,38 @@ namespace ts {
154
154
return state ;
155
155
}
156
156
157
+ /**
158
+ * Releases program and other related not needed properties
159
+ */
160
+ function releaseCache ( state : BuilderProgramState ) {
161
+ BuilderState . releaseCache ( state ) ;
162
+ state . program = undefined ;
163
+ }
164
+
165
+ /**
166
+ * Creates a clone of the state
167
+ */
168
+ function cloneBuilderProgramState ( state : Readonly < BuilderProgramState > ) : BuilderProgramState {
169
+ const newState = BuilderState . clone ( state ) as BuilderProgramState ;
170
+ newState . semanticDiagnosticsPerFile = cloneMapOrUndefined ( state . semanticDiagnosticsPerFile ) ;
171
+ newState . changedFilesSet = cloneMap ( state . changedFilesSet ) ;
172
+ newState . affectedFiles = state . affectedFiles ;
173
+ newState . affectedFilesIndex = state . affectedFilesIndex ;
174
+ newState . currentChangedFilePath = state . currentChangedFilePath ;
175
+ newState . currentAffectedFilesSignatures = cloneMapOrUndefined ( state . currentAffectedFilesSignatures ) ;
176
+ newState . currentAffectedFilesExportedModulesMap = cloneMapOrUndefined ( state . currentAffectedFilesExportedModulesMap ) ;
177
+ newState . seenAffectedFiles = cloneMapOrUndefined ( state . seenAffectedFiles ) ;
178
+ newState . cleanedDiagnosticsOfLibFiles = state . cleanedDiagnosticsOfLibFiles ;
179
+ newState . semanticDiagnosticsFromOldState = cloneMapOrUndefined ( state . semanticDiagnosticsFromOldState ) ;
180
+ newState . program = state . program ;
181
+ newState . compilerOptions = state . compilerOptions ;
182
+ newState . affectedFilesPendingEmit = state . affectedFilesPendingEmit ;
183
+ newState . affectedFilesPendingEmitIndex = state . affectedFilesPendingEmitIndex ;
184
+ newState . seenEmittedFiles = cloneMapOrUndefined ( state . seenEmittedFiles ) ;
185
+ newState . programEmitComplete = state . programEmitComplete ;
186
+ return newState ;
187
+ }
188
+
157
189
/**
158
190
* Verifies that source file is ok to be used in calls that arent handled by next
159
191
*/
@@ -458,7 +490,8 @@ namespace ts {
458
490
* Computing hash to for signature verification
459
491
*/
460
492
const computeHash = host . createHash || generateDjb2Hash ;
461
- const state = createBuilderProgramState ( newProgram , getCanonicalFileName , oldState ) ;
493
+ let state = createBuilderProgramState ( newProgram , getCanonicalFileName , oldState ) ;
494
+ let backupState : BuilderProgramState | undefined ;
462
495
463
496
// To ensure that we arent storing any references to old program or new program without state
464
497
newProgram = undefined ! ; // TODO: GH#18217
@@ -467,9 +500,21 @@ namespace ts {
467
500
468
501
const result = createRedirectedBuilderProgram ( state , configFileParsingDiagnostics ) ;
469
502
result . getState = ( ) => state ;
503
+ result . backupCurrentState = ( ) => {
504
+ Debug . assert ( backupState === undefined ) ;
505
+ backupState = cloneBuilderProgramState ( state ) ;
506
+ } ;
507
+ result . useBackupState = ( ) => {
508
+ state = Debug . assertDefined ( backupState ) ;
509
+ backupState = undefined ;
510
+ } ;
470
511
result . getAllDependencies = sourceFile => BuilderState . getAllDependencies ( state , Debug . assertDefined ( state . program ) , sourceFile ) ;
471
512
result . getSemanticDiagnostics = getSemanticDiagnostics ;
472
513
result . emit = emit ;
514
+ result . releaseProgram = ( ) => {
515
+ releaseCache ( state ) ;
516
+ backupState = undefined ;
517
+ } ;
473
518
474
519
if ( kind === BuilderProgramKind . SemanticDiagnosticsBuilderProgram ) {
475
520
( result as SemanticDiagnosticsBuilderProgram ) . getSemanticDiagnosticsOfNextAffectedFile = getSemanticDiagnosticsOfNextAffectedFile ;
@@ -650,6 +695,8 @@ namespace ts {
650
695
export function createRedirectedBuilderProgram ( state : { program : Program | undefined ; compilerOptions : CompilerOptions ; } , configFileParsingDiagnostics : ReadonlyArray < Diagnostic > ) : BuilderProgram {
651
696
return {
652
697
getState : notImplemented ,
698
+ backupCurrentState : noop ,
699
+ useBackupState : noop ,
653
700
getProgram : ( ) => Debug . assertDefined ( state . program ) ,
654
701
getProgramOrUndefined : ( ) => state . program ,
655
702
releaseProgram : ( ) => state . program = undefined ,
@@ -694,6 +741,10 @@ namespace ts {
694
741
export interface BuilderProgram {
695
742
/*@internal */
696
743
getState ( ) : BuilderProgramState ;
744
+ /*@internal */
745
+ backupCurrentState ( ) : void ;
746
+ /*@internal */
747
+ useBackupState ( ) : void ;
697
748
/**
698
749
* Returns current program
699
750
*/
0 commit comments