@@ -524,16 +524,31 @@ namespace ts {
524
524
return newSignature !== oldSignature ;
525
525
}
526
526
527
- function forEachKeyOfExportedModulesMap ( state : BuilderProgramState , filePath : Path , fn : ( exportedFromPath : Path ) => void ) {
527
+ function forEachKeyOfExportedModulesMap < T > ( state : BuilderProgramState , filePath : Path , fn : ( exportedFromPath : Path ) => T | undefined ) : T | undefined {
528
528
// Go through exported modules from cache first
529
- state . currentAffectedFilesExportedModulesMap ! . getKeys ( filePath ) ?. forEach ( fn ) ;
529
+ let keys = state . currentAffectedFilesExportedModulesMap ! . getKeys ( filePath ) ;
530
+ const result = keys && forEachKey ( keys , fn ) ;
531
+ if ( result ) return result ;
532
+
530
533
// If exported from path is not from cache and exported modules has path, all files referencing file exported from are affected
531
- state . exportedModulesMap ! . getKeys ( filePath ) ?. forEach ( exportedFromPath =>
534
+ keys = state . exportedModulesMap ! . getKeys ( filePath ) ;
535
+ return keys && forEachKey ( keys , exportedFromPath =>
532
536
// If the cache had an updated value, skip
533
537
! state . currentAffectedFilesExportedModulesMap ! . hasKey ( exportedFromPath ) &&
534
- ! state . currentAffectedFilesExportedModulesMap ! . deletedKeys ( ) ?. has ( exportedFromPath ) &&
535
- fn ( exportedFromPath )
538
+ ! state . currentAffectedFilesExportedModulesMap ! . deletedKeys ( ) ?. has ( exportedFromPath ) ?
539
+ fn ( exportedFromPath ) :
540
+ undefined
541
+ ) ;
542
+ }
543
+
544
+ function handleDtsMayChangeOfGlobalScope ( state : BuilderProgramState , filePath : Path , cancellationToken : CancellationToken | undefined , computeHash : BuilderState . ComputeHash ) : boolean {
545
+ if ( ! state . fileInfos . get ( filePath ) ?. affectsGlobalScope ) return false ;
546
+ // Every file needs to be handled
547
+ BuilderState . getAllFilesExcludingDefaultLibraryFile ( state , state . program ! , /*firstSourceFile*/ undefined ) . forEach (
548
+ file => handleDtsMayChangeOf ( state , file . resolvedPath , cancellationToken , computeHash )
536
549
) ;
550
+ removeDiagnosticsOfLibraryFiles ( state ) ;
551
+ return true ;
537
552
}
538
553
539
554
/**
@@ -556,6 +571,7 @@ namespace ts {
556
571
if ( ! seenFileNamesMap . has ( currentPath ) ) {
557
572
seenFileNamesMap . set ( currentPath , true ) ;
558
573
handleDtsMayChangeOf ( state , currentPath , cancellationToken , computeHash ) ;
574
+ if ( handleDtsMayChangeOfGlobalScope ( state , currentPath , cancellationToken , computeHash ) ) return ;
559
575
if ( isChangedSignature ( state , currentPath ) ) {
560
576
const currentSourceFile = Debug . checkDefined ( state . program ) . getSourceFileByPath ( currentPath ) ! ;
561
577
queue . push ( ...BuilderState . getReferencedByPaths ( state , currentSourceFile . resolvedPath ) ) ;
@@ -568,19 +584,23 @@ namespace ts {
568
584
const seenFileAndExportsOfFile = new Set < string > ( ) ;
569
585
// Go through exported modules from cache first
570
586
// If exported modules has path, all files referencing file exported from are affected
571
- forEachKeyOfExportedModulesMap ( state , affectedFile . resolvedPath , exportedFromPath =>
572
- state . referencedMap ! . getKeys ( exportedFromPath ) ?. forEach ( filePath =>
587
+ forEachKeyOfExportedModulesMap ( state , affectedFile . resolvedPath , exportedFromPath => {
588
+ if ( handleDtsMayChangeOfGlobalScope ( state , exportedFromPath , cancellationToken , computeHash ) ) return true ;
589
+ const references = state . referencedMap ! . getKeys ( exportedFromPath ) ;
590
+ return references && forEachKey ( references , filePath =>
573
591
handleDtsMayChangeOfFileAndExportsOfFile ( state , filePath , seenFileAndExportsOfFile , cancellationToken , computeHash )
574
- )
575
- ) ;
592
+ ) ;
593
+ } ) ;
576
594
}
577
595
578
596
/**
579
597
* handle dts and semantic diagnostics on file and iterate on anything that exports this file
598
+ * return true when all work is done and we can exit handling dts emit and semantic diagnostics
580
599
*/
581
- function handleDtsMayChangeOfFileAndExportsOfFile ( state : BuilderProgramState , filePath : Path , seenFileAndExportsOfFile : Set < string > , cancellationToken : CancellationToken | undefined , computeHash : BuilderState . ComputeHash ) : void {
582
- if ( ! tryAddToSet ( seenFileAndExportsOfFile , filePath ) ) return ;
600
+ function handleDtsMayChangeOfFileAndExportsOfFile ( state : BuilderProgramState , filePath : Path , seenFileAndExportsOfFile : Set < string > , cancellationToken : CancellationToken | undefined , computeHash : BuilderState . ComputeHash ) : boolean | undefined {
601
+ if ( ! tryAddToSet ( seenFileAndExportsOfFile , filePath ) ) return undefined ;
583
602
603
+ if ( handleDtsMayChangeOfGlobalScope ( state , filePath , cancellationToken , computeHash ) ) return true ;
584
604
handleDtsMayChangeOf ( state , filePath , cancellationToken , computeHash ) ;
585
605
Debug . assert ( ! ! state . currentAffectedFilesExportedModulesMap ) ;
586
606
@@ -594,6 +614,7 @@ namespace ts {
594
614
! seenFileAndExportsOfFile . has ( referencingFilePath ) && // Not already removed diagnostic file
595
615
handleDtsMayChangeOf ( state , referencingFilePath , cancellationToken , computeHash ) // Dont add to seen since this is not yet done with the export removal
596
616
) ;
617
+ return undefined ;
597
618
}
598
619
599
620
/**
0 commit comments