@@ -399,6 +399,30 @@ namespace ts {
399399 assert . isDefined ( program2 . getSourceFile ( "/a.ts" ) ! . resolvedModules ! . get ( "a" ) , "'a' is not an unresolved module after re-use" ) ;
400400 } ) ;
401401
402+ it ( "works with updated SourceFiles" , ( ) => {
403+ // adapted repro from https://github.com/Microsoft/TypeScript/issues/26166
404+ const files = [
405+ { name : "/a.ts" , text : SourceText . New ( "" , "" , 'import * as a from "a";a;' ) } ,
406+ { name : "/types/zzz/index.d.ts" , text : SourceText . New ( "" , "" , 'declare module "a" { }' ) } ,
407+ ] ;
408+ const host = createTestCompilerHost ( files , target ) ;
409+ const options : CompilerOptions = { target, typeRoots : [ "/types" ] } ;
410+ const program1 = createProgram ( [ "/a.ts" ] , options , host ) ;
411+ let sourceFile = program1 . getSourceFile ( "/a.ts" ) ! ;
412+ assert . isDefined ( sourceFile , "'/a.ts' is included in the program" ) ;
413+ sourceFile = updateSourceFile ( sourceFile , "'use strict';" + sourceFile . text , { newLength : "'use strict';" . length , span : { start : 0 , length : 0 } } ) ;
414+ assert . strictEqual ( sourceFile . statements [ 2 ] . getSourceFile ( ) , sourceFile , "parent pointers are updated" ) ;
415+ const updateHost : TestCompilerHost = {
416+ ...host ,
417+ getSourceFile ( fileName ) {
418+ return fileName === sourceFile . fileName ? sourceFile : program1 . getSourceFile ( fileName ) ;
419+ }
420+ } ;
421+ const program2 = createProgram ( [ "/a.ts" ] , options , updateHost , program1 ) ;
422+ assert . isDefined ( program2 . getSourceFile ( "/a.ts" ) ! . resolvedModules ! . get ( "a" ) , "'a' is not an unresolved module after re-use" ) ;
423+ assert . strictEqual ( sourceFile . statements [ 2 ] . getSourceFile ( ) , sourceFile , "parent pointers are not altered" ) ;
424+ } ) ;
425+
402426 it ( "resolved type directives cache follows type directives" , ( ) => {
403427 const files = [
404428 { name : "/a.ts" , text : SourceText . New ( "/// <reference types='typedefs'/>" , "" , "var x = $" ) } ,
0 commit comments