@@ -8416,52 +8416,95 @@ new C();`
8416
8416
} ) ;
8417
8417
8418
8418
describe ( "document registry in project service" , ( ) => {
8419
- it ( "Caches the source file if script info is orphan" , ( ) => {
8420
- const projectRootPath = "/user/username/projects/project" ;
8421
- const importModuleContent = `import {a} from "./module1"` ;
8422
- const file : File = {
8423
- path : `${ projectRootPath } /index.ts` ,
8424
- content : importModuleContent
8425
- } ;
8426
- const moduleFile : File = {
8427
- path : `${ projectRootPath } /module1.d.ts` ,
8428
- content : "export const a: number;"
8429
- } ;
8430
- const configFile : File = {
8431
- path : `${ projectRootPath } /tsconfig.json` ,
8432
- content : JSON . stringify ( { files : [ "index.ts" ] } )
8433
- } ;
8419
+ const projectRootPath = "/user/username/projects/project" ;
8420
+ const importModuleContent = `import {a} from "./module1"` ;
8421
+ const file : File = {
8422
+ path : `${ projectRootPath } /index.ts` ,
8423
+ content : importModuleContent
8424
+ } ;
8425
+ const moduleFile : File = {
8426
+ path : `${ projectRootPath } /module1.d.ts` ,
8427
+ content : "export const a: number;"
8428
+ } ;
8429
+ const configFile : File = {
8430
+ path : `${ projectRootPath } /tsconfig.json` ,
8431
+ content : JSON . stringify ( { files : [ "index.ts" ] } )
8432
+ } ;
8433
+
8434
+ function getProject ( service : TestProjectService ) {
8435
+ return service . configuredProjects . get ( configFile . path ) ;
8436
+ }
8437
+
8438
+ function checkProject ( service : TestProjectService , moduleIsOrphan : boolean ) {
8439
+ // Update the project
8440
+ const project = getProject ( service ) ;
8441
+ project . getLanguageService ( ) ;
8442
+ checkProjectActualFiles ( project , [ file . path , libFile . path , configFile . path , ...( moduleIsOrphan ? [ ] : [ moduleFile . path ] ) ] ) ;
8443
+ const moduleInfo = service . getScriptInfo ( moduleFile . path ) ;
8444
+ assert . isDefined ( moduleInfo ) ;
8445
+ assert . equal ( moduleInfo . isOrphan ( ) , moduleIsOrphan ) ;
8446
+ const key = service . documentRegistry . getKeyForCompilationSettings ( project . getCompilationSettings ( ) ) ;
8447
+ assert . deepEqual ( service . documentRegistry . getLanguageServiceRefCounts ( moduleInfo . path ) , [ [ key , moduleIsOrphan ? undefined : 1 ] ] ) ;
8448
+ }
8449
+
8450
+ function createServiceAndHost ( ) {
8434
8451
const host = createServerHost ( [ file , moduleFile , libFile , configFile ] ) ;
8435
8452
const service = createProjectService ( host ) ;
8436
8453
service . openClientFile ( file . path ) ;
8437
- const project = service . configuredProjects . get ( configFile . path ) ;
8438
- checkProject ( /*moduleIsOrphan*/ false ) ;
8454
+ checkProject ( service , /*moduleIsOrphan*/ false ) ;
8455
+ return { host, service } ;
8456
+ }
8457
+
8458
+ function changeFileToNotImportModule ( service : TestProjectService ) {
8459
+ const info = service . getScriptInfo ( file . path ) ;
8460
+ service . applyChangesToFile ( info , [ { span : { start : 0 , length : importModuleContent . length } , newText : "" } ] ) ;
8461
+ checkProject ( service , /*moduleIsOrphan*/ true ) ;
8462
+ }
8463
+
8464
+ function changeFileToImportModule ( service : TestProjectService ) {
8465
+ const info = service . getScriptInfo ( file . path ) ;
8466
+ service . applyChangesToFile ( info , [ { span : { start : 0 , length : 0 } , newText : importModuleContent } ] ) ;
8467
+ checkProject ( service , /*moduleIsOrphan*/ false ) ;
8468
+ }
8469
+
8470
+ it ( "Caches the source file if script info is orphan" , ( ) => {
8471
+ const { service } = createServiceAndHost ( ) ;
8472
+ const project = getProject ( service ) ;
8473
+
8439
8474
const moduleInfo = service . getScriptInfo ( moduleFile . path ) ;
8440
8475
const sourceFile = moduleInfo . cacheSourceFile . sourceFile ;
8441
8476
assert . equal ( project . getSourceFile ( moduleInfo . path ) , sourceFile ) ;
8442
8477
8443
8478
// edit file
8444
- const info = service . getScriptInfo ( file . path ) ;
8445
- service . applyChangesToFile ( info , [ { span : { start : 0 , length : importModuleContent . length } , newText : "" } ] ) ;
8446
- checkProject ( /*moduleIsOrphan*/ true ) ;
8479
+ changeFileToNotImportModule ( service ) ;
8447
8480
assert . equal ( moduleInfo . cacheSourceFile . sourceFile , sourceFile ) ;
8448
8481
8449
8482
// write content back
8450
- service . applyChangesToFile ( info , [ { span : { start : 0 , length : 0 } , newText : importModuleContent } ] ) ;
8451
- checkProject ( /*moduleIsOrphan*/ false ) ;
8483
+ changeFileToImportModule ( service ) ;
8452
8484
assert . equal ( moduleInfo . cacheSourceFile . sourceFile , sourceFile ) ;
8453
8485
assert . equal ( project . getSourceFile ( moduleInfo . path ) , sourceFile ) ;
8486
+ } ) ;
8487
+
8488
+ it ( "Caches the source file if script info is orphan, and orphan script info changes" , ( ) => {
8489
+ const { host, service } = createServiceAndHost ( ) ;
8490
+ const project = getProject ( service ) ;
8491
+
8492
+ const moduleInfo = service . getScriptInfo ( moduleFile . path ) ;
8493
+ const sourceFile = moduleInfo . cacheSourceFile . sourceFile ;
8494
+ assert . equal ( project . getSourceFile ( moduleInfo . path ) , sourceFile ) ;
8454
8495
8455
- function checkProject ( moduleIsOrphan : boolean ) {
8456
- // Update the project
8457
- project . getLanguageService ( ) ;
8458
- checkProjectActualFiles ( project , [ file . path , libFile . path , configFile . path , ...( moduleIsOrphan ? [ ] : [ moduleFile . path ] ) ] ) ;
8459
- const moduleInfo = service . getScriptInfo ( moduleFile . path ) ;
8460
- assert . isDefined ( moduleInfo ) ;
8461
- assert . equal ( moduleInfo . isOrphan ( ) , moduleIsOrphan ) ;
8462
- const key = service . documentRegistry . getKeyForCompilationSettings ( project . getCompilationSettings ( ) ) ;
8463
- assert . deepEqual ( service . documentRegistry . getLanguageServiceRefCounts ( moduleInfo . path ) , [ [ key , moduleIsOrphan ? undefined : 1 ] ] ) ;
8464
- }
8496
+ // edit file
8497
+ changeFileToNotImportModule ( service ) ;
8498
+ assert . equal ( moduleInfo . cacheSourceFile . sourceFile , sourceFile ) ;
8499
+
8500
+ const updatedModuleContent = moduleFile . content + "\nexport const b: number;" ;
8501
+ host . writeFile ( moduleFile . path , updatedModuleContent ) ;
8502
+
8503
+ // write content back
8504
+ changeFileToImportModule ( service ) ;
8505
+ assert . notEqual ( moduleInfo . cacheSourceFile . sourceFile , sourceFile ) ;
8506
+ assert . equal ( project . getSourceFile ( moduleInfo . path ) , moduleInfo . cacheSourceFile . sourceFile ) ;
8507
+ assert . equal ( moduleInfo . cacheSourceFile . sourceFile . text , updatedModuleContent ) ;
8465
8508
} ) ;
8466
8509
} ) ;
8467
8510
}
0 commit comments