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