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