@@ -38,6 +38,10 @@ namespace ts {
3838 * Already seen affected files
3939 */
4040 seenAffectedFiles : Map < true > | undefined ;
41+ /**
42+ * whether this program has cleaned semantic diagnostics cache for lib files
43+ */
44+ cleanedDiagnosticsOfLibFiles ?: boolean ;
4145 /**
4246 * True if the semantic diagnostics were copied from the old state
4347 */
@@ -64,9 +68,11 @@ namespace ts {
6468 state . semanticDiagnosticsPerFile = createMap < ReadonlyArray < Diagnostic > > ( ) ;
6569 }
6670 state . changedFilesSet = createMap < true > ( ) ;
71+
6772 const useOldState = BuilderState . canReuseOldState ( state . referencedMap , oldState ) ;
73+ const oldCompilerOptions = useOldState ? oldState ! . program . getCompilerOptions ( ) : undefined ;
6874 const canCopySemanticDiagnostics = useOldState && oldState ! . semanticDiagnosticsPerFile && ! ! state . semanticDiagnosticsPerFile &&
69- ! compilerOptionsAffectSemanticDiagnostics ( compilerOptions , oldState ! . program . getCompilerOptions ( ) ) ;
75+ ! compilerOptionsAffectSemanticDiagnostics ( compilerOptions , oldCompilerOptions ! ) ;
7076 if ( useOldState ) {
7177 // Verify the sanity of old state
7278 if ( ! oldState ! . currentChangedFilePath ) {
@@ -83,6 +89,8 @@ namespace ts {
8389 // Update changed files and copy semantic diagnostics if we can
8490 const referencedMap = state . referencedMap ;
8591 const oldReferencedMap = useOldState ? oldState ! . referencedMap : undefined ;
92+ const copyDeclarationFileDiagnostics = canCopySemanticDiagnostics && ! compilerOptions . skipLibCheck === ! oldCompilerOptions ! . skipLibCheck ;
93+ const copyLibFileDiagnostics = copyDeclarationFileDiagnostics && ! compilerOptions . skipDefaultLibCheck === ! oldCompilerOptions ! . skipDefaultLibCheck ;
8694 state . fileInfos . forEach ( ( info , sourceFilePath ) => {
8795 let oldInfo : Readonly < BuilderState . FileInfo > | undefined ;
8896 let newReferences : BuilderState . ReferencedSet | undefined ;
@@ -101,6 +109,11 @@ namespace ts {
101109 state . changedFilesSet . set ( sourceFilePath , true ) ;
102110 }
103111 else if ( canCopySemanticDiagnostics ) {
112+ const sourceFile = state . program . getSourceFileByPath ( sourceFilePath as Path ) ! ;
113+
114+ if ( sourceFile . isDeclarationFile && ! copyDeclarationFileDiagnostics ) { return ; }
115+ if ( sourceFile . hasNoDefaultLib && ! copyLibFileDiagnostics ) { return ; }
116+
104117 // Unchanged file copy diagnostics
105118 const diagnostics = oldState ! . semanticDiagnosticsPerFile ! . get ( sourceFilePath ) ;
106119 if ( diagnostics ) {
@@ -193,6 +206,19 @@ namespace ts {
193206 return ;
194207 }
195208
209+ // Clean lib file diagnostics if its all files excluding default files to emit
210+ if ( state . allFilesExcludingDefaultLibraryFile === state . affectedFiles && ! state . cleanedDiagnosticsOfLibFiles ) {
211+ state . cleanedDiagnosticsOfLibFiles = true ;
212+ const options = state . program . getCompilerOptions ( ) ;
213+ if ( forEach ( state . program . getSourceFiles ( ) , f =>
214+ state . program . isSourceFileDefaultLibrary ( f ) &&
215+ ! skipTypeChecking ( f , options ) &&
216+ removeSemanticDiagnosticsOf ( state , f . path )
217+ ) ) {
218+ return ;
219+ }
220+ }
221+
196222 // If there was change in signature for the changed file,
197223 // then delete the semantic diagnostics for files that are affected by using exports of this module
198224
@@ -268,7 +294,7 @@ namespace ts {
268294 */
269295 function removeSemanticDiagnosticsOf ( state : BuilderProgramState , path : Path ) {
270296 if ( ! state . semanticDiagnosticsFromOldState ) {
271- return false ;
297+ return true ;
272298 }
273299 state . semanticDiagnosticsFromOldState . delete ( path ) ;
274300 state . semanticDiagnosticsPerFile ! . delete ( path ) ;
0 commit comments