@@ -29,27 +29,6 @@ namespace ts {
29
29
* The map has key by source file's path that has been changed
30
30
*/
31
31
changedFilesSet ?: Set < Path > ;
32
- /**
33
- * Set of affected files being iterated
34
- */
35
- affectedFiles ?: readonly SourceFile [ ] | undefined ;
36
- /**
37
- * Current changed file for iterating over affected files
38
- */
39
- currentChangedFilePath ?: Path | undefined ;
40
- /**
41
- * Map of file signatures, with key being file path, calculated while getting current changed file's affected files
42
- * These will be committed whenever the iteration through affected files of current changed file is complete
43
- */
44
- currentAffectedFilesSignatures ?: ESMap < Path , string > | undefined ;
45
- /**
46
- * Newly computed visible to outside referencedSet
47
- */
48
- currentAffectedFilesExportedModulesMap ?: BuilderState . ManyToManyPathMap | undefined ;
49
- /**
50
- * True if the semantic diagnostics were copied from the old state
51
- */
52
- semanticDiagnosticsFromOldState ?: Set < Path > ;
53
32
/**
54
33
* program corresponding to this state
55
34
*/
@@ -106,10 +85,18 @@ namespace ts {
106
85
* The map has key by source file's path that has been changed
107
86
*/
108
87
changedFilesSet : Set < Path > ;
88
+ /**
89
+ * Set of affected files being iterated
90
+ */
91
+ affectedFiles ?: readonly SourceFile [ ] | undefined ;
109
92
/**
110
93
* Current index to retrieve affected file from
111
94
*/
112
95
affectedFilesIndex : number | undefined ;
96
+ /**
97
+ * Current changed file for iterating over affected files
98
+ */
99
+ currentChangedFilePath ?: Path | undefined ;
113
100
/**
114
101
* Already seen affected files
115
102
*/
@@ -118,6 +105,10 @@ namespace ts {
118
105
* whether this program has cleaned semantic diagnostics cache for lib files
119
106
*/
120
107
cleanedDiagnosticsOfLibFiles ?: boolean ;
108
+ /**
109
+ * True if the semantic diagnostics were copied from the old state
110
+ */
111
+ semanticDiagnosticsFromOldState ?: Set < Path > ;
121
112
/**
122
113
* Records if change in dts emit was detected
123
114
*/
@@ -187,18 +178,8 @@ namespace ts {
187
178
! outFilePath &&
188
179
! compilerOptionsAffectDeclarationPath ( compilerOptions , oldCompilerOptions ! ) ;
189
180
if ( useOldState ) {
190
- // Verify the sanity of old state
191
- if ( ! oldState ! . currentChangedFilePath ) {
192
- const affectedSignatures = oldState ! . currentAffectedFilesSignatures ;
193
- Debug . assert ( ! oldState ! . affectedFiles && ( ! affectedSignatures || ! affectedSignatures . size ) , "Cannot reuse if only few affected files of currentChangedFile were iterated" ) ;
194
- }
195
- const changedFilesSet = oldState ! . changedFilesSet ;
196
- if ( canCopySemanticDiagnostics ) {
197
- Debug . assert ( ! changedFilesSet ?. size || ! forEachKey ( changedFilesSet , path => oldState ! . semanticDiagnosticsPerFile ! . has ( path ) ) , "Semantic diagnostics shouldnt be available for changed files" ) ;
198
- }
199
-
200
181
// Copy old state's changed files set
201
- changedFilesSet ?. forEach ( value => state . changedFilesSet . add ( value ) ) ;
182
+ oldState ! . changedFilesSet ?. forEach ( value => state . changedFilesSet . add ( value ) ) ;
202
183
if ( ! outFilePath && oldState ! . affectedFilesPendingEmit ) {
203
184
state . affectedFilesPendingEmit = oldState ! . affectedFilesPendingEmit . slice ( ) ;
204
185
state . affectedFilesPendingEmitKind = oldState ! . affectedFilesPendingEmitKind && new Map ( oldState ! . affectedFilesPendingEmitKind ) ;
@@ -373,10 +354,8 @@ namespace ts {
373
354
state . changedFilesSet . delete ( state . currentChangedFilePath ! ) ;
374
355
state . currentChangedFilePath = undefined ;
375
356
// Commit the changes in file signature
376
- BuilderState . updateSignaturesFromCache ( state , state . currentAffectedFilesSignatures ! ) ;
377
- state . currentAffectedFilesSignatures ! . clear ( ) ;
378
- BuilderState . updateExportedFilesMapFromCache ( state , state . currentAffectedFilesExportedModulesMap ) ;
379
- state . currentAffectedFilesExportedModulesMap ?. clear ( ) ;
357
+ state . oldSignatures ?. clear ( ) ;
358
+ state . oldExportedModulesMap ?. clear ( ) ;
380
359
state . affectedFiles = undefined ;
381
360
}
382
361
@@ -397,11 +376,7 @@ namespace ts {
397
376
}
398
377
399
378
// Get next batch of affected files
400
- if ( ! state . currentAffectedFilesSignatures ) state . currentAffectedFilesSignatures = new Map ( ) ;
401
- if ( state . exportedModulesMap ) {
402
- state . currentAffectedFilesExportedModulesMap ||= BuilderState . createManyToManyPathMap ( ) ;
403
- }
404
- state . affectedFiles = BuilderState . getFilesAffectedBy ( state , program , nextKey . value , cancellationToken , computeHash , state . currentAffectedFilesSignatures , state . currentAffectedFilesExportedModulesMap ) ;
379
+ state . affectedFiles = BuilderState . getFilesAffectedByWithOldState ( state , program , nextKey . value , cancellationToken , computeHash ) ;
405
380
state . currentChangedFilePath = nextKey . value ;
406
381
state . affectedFilesIndex = 0 ;
407
382
if ( ! state . seenAffectedFiles ) state . seenAffectedFiles = new Set ( ) ;
@@ -474,14 +449,11 @@ namespace ts {
474
449
state ,
475
450
Debug . checkDefined ( state . program ) ,
476
451
affectedFile ,
477
- Debug . checkDefined ( state . currentAffectedFilesSignatures ) ,
478
452
cancellationToken ,
479
453
computeHash ,
480
- state . currentAffectedFilesExportedModulesMap
481
454
) ;
482
455
return ;
483
456
}
484
- Debug . assert ( state . hasCalledUpdateShapeSignature ?. has ( affectedFile . resolvedPath ) || state . currentAffectedFilesSignatures ?. has ( affectedFile . resolvedPath ) , `Signature not updated for affected file: ${ affectedFile . fileName } ` ) ;
485
457
if ( state . compilerOptions . assumeChangesOnlyAffectDirectDependencies ) return ;
486
458
handleDtsMayChangeOfReferencingExportOfAffectedFile ( state , affectedFile , cancellationToken , computeHash , host ) ;
487
459
}
@@ -512,10 +484,8 @@ namespace ts {
512
484
state ,
513
485
program ,
514
486
sourceFile ,
515
- Debug . checkDefined ( state . currentAffectedFilesSignatures ) ,
516
487
cancellationToken ,
517
488
computeHash ,
518
- state . currentAffectedFilesExportedModulesMap ,
519
489
! host . disableUseFileVersionAsSignature
520
490
) ;
521
491
// If not dts emit, nothing more to do
@@ -540,32 +510,11 @@ namespace ts {
540
510
}
541
511
542
512
function isChangedSignature ( state : BuilderProgramState , path : Path ) {
543
- const newSignature = Debug . checkDefined ( state . currentAffectedFilesSignatures ) . get ( path ) ;
544
- const oldSignature = Debug . checkDefined ( state . fileInfos . get ( path ) ) . signature ;
513
+ const oldSignature = Debug . checkDefined ( state . oldSignatures ) . get ( path ) || undefined ;
514
+ const newSignature = Debug . checkDefined ( state . fileInfos . get ( path ) ) . signature ;
545
515
return newSignature !== oldSignature ;
546
516
}
547
517
548
- function forEachKeyOfExportedModulesMap < T > (
549
- state : BuilderProgramState ,
550
- filePath : Path ,
551
- fn : ( exportedFromPath : Path ) => T | undefined ,
552
- ) : T | undefined {
553
- // Go through exported modules from cache first
554
- let keys = state . currentAffectedFilesExportedModulesMap ! . getKeys ( filePath ) ;
555
- const result = keys && forEachKey ( keys , fn ) ;
556
- if ( result ) return result ;
557
-
558
- // If exported from path is not from cache and exported modules has path, all files referencing file exported from are affected
559
- keys = state . exportedModulesMap ! . getKeys ( filePath ) ;
560
- return keys && forEachKey ( keys , exportedFromPath =>
561
- // If the cache had an updated value, skip
562
- ! state . currentAffectedFilesExportedModulesMap ! . hasKey ( exportedFromPath ) &&
563
- ! state . currentAffectedFilesExportedModulesMap ! . deletedKeys ( ) ?. has ( exportedFromPath ) ?
564
- fn ( exportedFromPath ) :
565
- undefined
566
- ) ;
567
- }
568
-
569
518
function handleDtsMayChangeOfGlobalScope (
570
519
state : BuilderProgramState ,
571
520
filePath : Path ,
@@ -622,11 +571,10 @@ namespace ts {
622
571
}
623
572
}
624
573
625
- Debug . assert ( ! ! state . currentAffectedFilesExportedModulesMap ) ;
626
574
const seenFileAndExportsOfFile = new Set < string > ( ) ;
627
575
// Go through exported modules from cache first
628
576
// If exported modules has path, all files referencing file exported from are affected
629
- forEachKeyOfExportedModulesMap ( state , affectedFile . resolvedPath , exportedFromPath => {
577
+ state . exportedModulesMap . getKeys ( affectedFile . resolvedPath ) ?. forEach ( exportedFromPath => {
630
578
if ( handleDtsMayChangeOfGlobalScope ( state , exportedFromPath , cancellationToken , computeHash , host ) ) return true ;
631
579
const references = state . referencedMap ! . getKeys ( exportedFromPath ) ;
632
580
return references && forEachKey ( references , filePath =>
@@ -658,10 +606,9 @@ namespace ts {
658
606
659
607
if ( handleDtsMayChangeOfGlobalScope ( state , filePath , cancellationToken , computeHash , host ) ) return true ;
660
608
handleDtsMayChangeOf ( state , filePath , cancellationToken , computeHash , host ) ;
661
- Debug . assert ( ! ! state . currentAffectedFilesExportedModulesMap ) ;
662
609
663
610
// If exported modules has path, all files referencing file exported from are affected
664
- forEachKeyOfExportedModulesMap ( state , filePath , exportedFromPath =>
611
+ state . exportedModulesMap ! . getKeys ( filePath ) ?. forEach ( exportedFromPath =>
665
612
handleDtsMayChangeOfFileAndExportsOfFile (
666
613
state ,
667
614
exportedFromPath ,
@@ -849,8 +796,8 @@ namespace ts {
849
796
// Ensure fileId
850
797
const fileId = toFileId ( key ) ;
851
798
Debug . assert ( fileNames [ fileId - 1 ] === relativeToBuildInfo ( key ) ) ;
852
- const signature = state . currentAffectedFilesSignatures && state . currentAffectedFilesSignatures . get ( key ) ;
853
- const actualSignature = signature ?? value . signature ;
799
+ const oldSignature = state . oldSignatures ? .get ( key ) ;
800
+ const actualSignature = oldSignature !== undefined ? oldSignature || undefined : value . signature ;
854
801
if ( state . compilerOptions . composite ) {
855
802
const file = state . program ! . getSourceFileByPath ( key ) ! ;
856
803
if ( ! isJsonSourceFile ( file ) && sourceFileMayBeEmitted ( file , state . program ! ) ) {
@@ -867,11 +814,11 @@ namespace ts {
867
814
// If file info only contains version and signature and both are same we can just write string
868
815
value . version :
869
816
actualSignature !== undefined ? // If signature is not same as version, encode signature in the fileInfo
870
- signature === undefined ?
817
+ oldSignature === undefined ?
871
818
// If we havent computed signature, use fileInfo as is
872
819
value :
873
820
// Serialize fileInfo with new updated signature
874
- { version : value . version , signature, affectsGlobalScope : value . affectsGlobalScope , impliedFormat : value . impliedFormat } :
821
+ { version : value . version , signature : actualSignature , affectsGlobalScope : value . affectsGlobalScope , impliedFormat : value . impliedFormat } :
875
822
// Signature of the FileInfo is undefined, serialize it as false
876
823
{ version : value . version , signature : false , affectsGlobalScope : value . affectsGlobalScope , impliedFormat : value . impliedFormat } ;
877
824
} ) ;
@@ -887,19 +834,11 @@ namespace ts {
887
834
let exportedModulesMap : ProgramBuildInfoReferencedMap | undefined ;
888
835
if ( state . exportedModulesMap ) {
889
836
exportedModulesMap = mapDefined ( arrayFrom ( state . exportedModulesMap . keys ( ) ) . sort ( compareStringsCaseSensitive ) , key => {
890
- if ( state . currentAffectedFilesExportedModulesMap ) {
891
- if ( state . currentAffectedFilesExportedModulesMap . deletedKeys ( ) ?. has ( key ) ) {
892
- return undefined ;
893
- }
894
-
895
- const newValue = state . currentAffectedFilesExportedModulesMap . getValues ( key ) ;
896
- if ( newValue ) {
897
- return [ toFileId ( key ) , toFileIdListId ( newValue ) ] ;
898
- }
899
- }
900
-
837
+ const oldValue = state . oldExportedModulesMap ?. get ( key ) ;
901
838
// Not in temporary cache, use existing value
902
- return [ toFileId ( key ) , toFileIdListId ( state . exportedModulesMap ! . getValues ( key ) ! ) ] ;
839
+ if ( oldValue === undefined ) return [ toFileId ( key ) , toFileIdListId ( state . exportedModulesMap ! . getValues ( key ) ! ) ] ;
840
+ if ( oldValue ) return [ toFileId ( key ) , toFileIdListId ( oldValue ) ] ;
841
+ return undefined ;
903
842
} ) ;
904
843
}
905
844
@@ -1221,18 +1160,21 @@ namespace ts {
1221
1160
if ( ! customTransformers ) {
1222
1161
const file = sourceFiles [ 0 ] ;
1223
1162
const info = state . fileInfos . get ( file . resolvedPath ) ! ;
1224
- const signature = state . currentAffectedFilesSignatures ?. get ( file . resolvedPath ) || info . signature ;
1225
- if ( signature === file . version ) {
1163
+ if ( info . signature === file . version ) {
1226
1164
newSignature = computeSignature ( text , data , computeHash ) ;
1227
1165
if ( newSignature !== file . version ) { // Update it
1228
1166
if ( host . storeFilesChangingSignatureDuringEmit ) ( state . filesChangingSignature ||= new Set ( ) ) . add ( file . resolvedPath ) ;
1229
- if ( state . exportedModulesMap ) BuilderState . updateExportedModules ( file , file . exportedModulesFromDeclarationEmit , state . currentAffectedFilesExportedModulesMap ||= BuilderState . createManyToManyPathMap ( ) ) ;
1230
- if ( state . affectedFiles && state . affectedFilesIndex ! < state . affectedFiles . length ) {
1231
- state . currentAffectedFilesSignatures ! . set ( file . resolvedPath , newSignature ) ;
1167
+ if ( state . exportedModulesMap ) BuilderState . updateExportedModules ( state , file , file . exportedModulesFromDeclarationEmit ) ;
1168
+ if ( state . affectedFiles ) {
1169
+ // Keep old signature so we know what to undo if cancellation happens
1170
+ const existing = state . oldSignatures ?. get ( file . resolvedPath ) ;
1171
+ if ( existing === undefined ) ( state . oldSignatures ||= new Map ( ) ) . set ( file . resolvedPath , info . signature || false ) ;
1172
+ info . signature = newSignature ;
1232
1173
}
1233
1174
else {
1175
+ // These are directly commited
1234
1176
info . signature = newSignature ;
1235
- if ( state . exportedModulesMap ) BuilderState . updateExportedFilesMapFromCache ( state , state . currentAffectedFilesExportedModulesMap ) ;
1177
+ state . oldExportedModulesMap ?. clear ( ) ;
1236
1178
}
1237
1179
}
1238
1180
}
0 commit comments