@@ -48,6 +48,7 @@ namespace ts {
48
48
addScriptInfo ( program : Program , sourceFile : SourceFile ) : void ;
49
49
removeScriptInfo ( path : Path ) : void ;
50
50
updateScriptInfo ( program : Program , sourceFile : SourceFile ) : void ;
51
+ updaterScriptInfoWithSameVersion ( program : Program , sourceFile : SourceFile ) : boolean ;
51
52
/**
52
53
* Gets the files affected by the script info which has updated shape from the known one
53
54
*/
@@ -143,11 +144,15 @@ namespace ts {
143
144
}
144
145
145
146
function updateExistingFileInfo ( program : Program , existingInfo : FileInfo , sourceFile : SourceFile , hasInvalidatedResolution : HasInvalidatedResolution ) {
146
- if ( existingInfo . version !== sourceFile . version || hasInvalidatedResolution ( sourceFile . path ) ) {
147
+ if ( existingInfo . version !== sourceFile . version ) {
147
148
registerChangedFile ( sourceFile . path , sourceFile . fileName ) ;
148
149
existingInfo . version = sourceFile . version ;
149
150
emitHandler . updateScriptInfo ( program , sourceFile ) ;
150
151
}
152
+ else if ( hasInvalidatedResolution ( sourceFile . path ) &&
153
+ emitHandler . updaterScriptInfoWithSameVersion ( program , sourceFile ) ) {
154
+ registerChangedFile ( sourceFile . path , sourceFile . fileName ) ;
155
+ }
151
156
}
152
157
153
158
function ensureProgramGraph ( program : Program ) {
@@ -338,8 +343,8 @@ namespace ts {
338
343
/**
339
344
* Gets the referenced files for a file from the program with values for the keys as referenced file's path to be true
340
345
*/
341
- function getReferencedFiles ( program : Program , sourceFile : SourceFile ) : Map < true > {
342
- const referencedFiles = createMap < true > ( ) ;
346
+ function getReferencedFiles ( program : Program , sourceFile : SourceFile ) : Map < true > | undefined {
347
+ let referencedFiles : Map < true > | undefined ;
343
348
344
349
// We need to use a set here since the code can contain the same import twice,
345
350
// but that will only be one dependency.
@@ -351,7 +356,7 @@ namespace ts {
351
356
if ( symbol && symbol . declarations && symbol . declarations [ 0 ] ) {
352
357
const declarationSourceFile = getSourceFileOfNode ( symbol . declarations [ 0 ] ) ;
353
358
if ( declarationSourceFile ) {
354
- referencedFiles . set ( declarationSourceFile . path , true ) ;
359
+ addReferencedFile ( declarationSourceFile . path ) ;
355
360
}
356
361
}
357
362
}
@@ -362,7 +367,7 @@ namespace ts {
362
367
if ( sourceFile . referencedFiles && sourceFile . referencedFiles . length > 0 ) {
363
368
for ( const referencedFile of sourceFile . referencedFiles ) {
364
369
const referencedPath = toPath ( referencedFile . fileName , sourceFileDirectory , getCanonicalFileName ) ;
365
- referencedFiles . set ( referencedPath , true ) ;
370
+ addReferencedFile ( referencedPath ) ;
366
371
}
367
372
}
368
373
@@ -375,11 +380,18 @@ namespace ts {
375
380
376
381
const fileName = resolvedTypeReferenceDirective . resolvedFileName ;
377
382
const typeFilePath = toPath ( fileName , sourceFileDirectory , getCanonicalFileName ) ;
378
- referencedFiles . set ( typeFilePath , true ) ;
383
+ addReferencedFile ( typeFilePath ) ;
379
384
} ) ;
380
385
}
381
386
382
387
return referencedFiles ;
388
+
389
+ function addReferencedFile ( referencedPath : Path ) {
390
+ if ( ! referencedFiles ) {
391
+ referencedFiles = createMap < true > ( ) ;
392
+ }
393
+ referencedFiles . set ( referencedPath , true ) ;
394
+ }
383
395
}
384
396
385
397
/**
@@ -402,6 +414,7 @@ namespace ts {
402
414
addScriptInfo : noop ,
403
415
removeScriptInfo : noop ,
404
416
updateScriptInfo : noop ,
417
+ updaterScriptInfoWithSameVersion : returnFalse ,
405
418
getFilesAffectedByUpdatedShape
406
419
} ;
407
420
@@ -421,12 +434,45 @@ namespace ts {
421
434
return {
422
435
addScriptInfo : setReferences ,
423
436
removeScriptInfo,
424
- updateScriptInfo : setReferences ,
437
+ updateScriptInfo : updateReferences ,
438
+ updaterScriptInfoWithSameVersion : updateReferencesTrackingChangedReferences ,
425
439
getFilesAffectedByUpdatedShape
426
440
} ;
427
441
428
442
function setReferences ( program : Program , sourceFile : SourceFile ) {
429
- references . set ( sourceFile . path , getReferencedFiles ( program , sourceFile ) ) ;
443
+ const newReferences = getReferencedFiles ( program , sourceFile ) ;
444
+ if ( newReferences ) {
445
+ references . set ( sourceFile . path , getReferencedFiles ( program , sourceFile ) ) ;
446
+ }
447
+ }
448
+
449
+ function updateReferences ( program : Program , sourceFile : SourceFile ) {
450
+ const newReferences = getReferencedFiles ( program , sourceFile ) ;
451
+ if ( newReferences ) {
452
+ references . set ( sourceFile . path , newReferences ) ;
453
+ }
454
+ else {
455
+ references . delete ( sourceFile . path ) ;
456
+ }
457
+ }
458
+
459
+ function updateReferencesTrackingChangedReferences ( program : Program , sourceFile : SourceFile ) {
460
+ const newReferences = getReferencedFiles ( program , sourceFile ) ;
461
+ if ( ! newReferences ) {
462
+ // Changed if we had references
463
+ return references . delete ( sourceFile . path ) ;
464
+ }
465
+
466
+ const oldReferences = references . get ( sourceFile . path ) ;
467
+ references . set ( sourceFile . path , newReferences ) ;
468
+ if ( ! oldReferences || oldReferences . size !== newReferences . size ) {
469
+ return true ;
470
+ }
471
+
472
+ // If there are any new references that werent present previously there is change
473
+ return forEachEntry ( newReferences , ( _true , referencedPath ) => ! oldReferences . delete ( referencedPath ) ) ||
474
+ // Otherwise its changed if there are more references previously than now
475
+ ! ! oldReferences . size ;
430
476
}
431
477
432
478
function removeScriptInfo ( removedFilePath : Path ) {
0 commit comments