@@ -543,18 +543,48 @@ namespace ts {
543
543
return newSignature !== oldSignature ;
544
544
}
545
545
546
- function forEachKeyOfExportedModulesMap ( state : BuilderProgramState , filePath : Path , fn : ( exportedFromPath : Path ) => void ) {
546
+ function forEachKeyOfExportedModulesMap < T > (
547
+ state : BuilderProgramState ,
548
+ filePath : Path ,
549
+ fn : ( exportedFromPath : Path ) => T | undefined ,
550
+ ) : T | undefined {
547
551
// Go through exported modules from cache first
548
- state . currentAffectedFilesExportedModulesMap ! . getKeys ( filePath ) ?. forEach ( fn ) ;
552
+ let keys = state . currentAffectedFilesExportedModulesMap ! . getKeys ( filePath ) ;
553
+ const result = keys && forEachKey ( keys , fn ) ;
554
+ if ( result ) return result ;
555
+
549
556
// If exported from path is not from cache and exported modules has path, all files referencing file exported from are affected
550
- state . exportedModulesMap ! . getKeys ( filePath ) ?. forEach ( exportedFromPath =>
557
+ keys = state . exportedModulesMap ! . getKeys ( filePath ) ;
558
+ return keys && forEachKey ( keys , exportedFromPath =>
551
559
// If the cache had an updated value, skip
552
560
! state . currentAffectedFilesExportedModulesMap ! . hasKey ( exportedFromPath ) &&
553
- ! state . currentAffectedFilesExportedModulesMap ! . deletedKeys ( ) ?. has ( exportedFromPath ) &&
554
- fn ( exportedFromPath )
561
+ ! state . currentAffectedFilesExportedModulesMap ! . deletedKeys ( ) ?. has ( exportedFromPath ) ?
562
+ fn ( exportedFromPath ) :
563
+ undefined
555
564
) ;
556
565
}
557
566
567
+ function handleDtsMayChangeOfGlobalScope (
568
+ state : BuilderProgramState ,
569
+ filePath : Path ,
570
+ cancellationToken : CancellationToken | undefined ,
571
+ computeHash : BuilderState . ComputeHash ,
572
+ host : BuilderProgramHost ,
573
+ ) : boolean {
574
+ if ( ! state . fileInfos . get ( filePath ) ?. affectsGlobalScope ) return false ;
575
+ // Every file needs to be handled
576
+ BuilderState . getAllFilesExcludingDefaultLibraryFile ( state , state . program ! , /*firstSourceFile*/ undefined )
577
+ . forEach ( file => handleDtsMayChangeOf (
578
+ state ,
579
+ file . resolvedPath ,
580
+ cancellationToken ,
581
+ computeHash ,
582
+ host ,
583
+ ) ) ;
584
+ removeDiagnosticsOfLibraryFiles ( state ) ;
585
+ return true ;
586
+ }
587
+
558
588
/**
559
589
* Iterate on referencing modules that export entities from affected file and delete diagnostics and add pending emit
560
590
*/
@@ -580,6 +610,7 @@ namespace ts {
580
610
const currentPath = queue . pop ( ) ! ;
581
611
if ( ! seenFileNamesMap . has ( currentPath ) ) {
582
612
seenFileNamesMap . set ( currentPath , true ) ;
613
+ if ( handleDtsMayChangeOfGlobalScope ( state , currentPath , cancellationToken , computeHash , host ) ) return ;
583
614
handleDtsMayChangeOf ( state , currentPath , cancellationToken , computeHash , host ) ;
584
615
if ( isChangedSignature ( state , currentPath ) ) {
585
616
const currentSourceFile = Debug . checkDefined ( state . program ) . getSourceFileByPath ( currentPath ) ! ;
@@ -593,8 +624,10 @@ namespace ts {
593
624
const seenFileAndExportsOfFile = new Set < string > ( ) ;
594
625
// Go through exported modules from cache first
595
626
// If exported modules has path, all files referencing file exported from are affected
596
- forEachKeyOfExportedModulesMap ( state , affectedFile . resolvedPath , exportedFromPath =>
597
- state . referencedMap ! . getKeys ( exportedFromPath ) ?. forEach ( filePath =>
627
+ forEachKeyOfExportedModulesMap ( state , affectedFile . resolvedPath , exportedFromPath => {
628
+ if ( handleDtsMayChangeOfGlobalScope ( state , exportedFromPath , cancellationToken , computeHash , host ) ) return true ;
629
+ const references = state . referencedMap ! . getKeys ( exportedFromPath ) ;
630
+ return references && forEachKey ( references , filePath =>
598
631
handleDtsMayChangeOfFileAndExportsOfFile (
599
632
state ,
600
633
filePath ,
@@ -603,12 +636,13 @@ namespace ts {
603
636
computeHash ,
604
637
host ,
605
638
)
606
- )
607
- ) ;
639
+ ) ;
640
+ } ) ;
608
641
}
609
642
610
643
/**
611
644
* handle dts and semantic diagnostics on file and iterate on anything that exports this file
645
+ * return true when all work is done and we can exit handling dts emit and semantic diagnostics
612
646
*/
613
647
function handleDtsMayChangeOfFileAndExportsOfFile (
614
648
state : BuilderProgramState ,
@@ -617,9 +651,10 @@ namespace ts {
617
651
cancellationToken : CancellationToken | undefined ,
618
652
computeHash : BuilderState . ComputeHash ,
619
653
host : BuilderProgramHost ,
620
- ) : void {
621
- if ( ! tryAddToSet ( seenFileAndExportsOfFile , filePath ) ) return ;
654
+ ) : boolean | undefined {
655
+ if ( ! tryAddToSet ( seenFileAndExportsOfFile , filePath ) ) return undefined ;
622
656
657
+ if ( handleDtsMayChangeOfGlobalScope ( state , filePath , cancellationToken , computeHash , host ) ) return true ;
623
658
handleDtsMayChangeOf ( state , filePath , cancellationToken , computeHash , host ) ;
624
659
Debug . assert ( ! ! state . currentAffectedFilesExportedModulesMap ) ;
625
660
@@ -646,6 +681,7 @@ namespace ts {
646
681
host ,
647
682
)
648
683
) ;
684
+ return undefined ;
649
685
}
650
686
651
687
/**
0 commit comments