Skip to content

Commit e8a0e56

Browse files
committed
Add a test when orphan file changes before it gets added back to project
1 parent 66590a9 commit e8a0e56

File tree

1 file changed

+75
-32
lines changed

1 file changed

+75
-32
lines changed

src/harness/unittests/tsserverProjectSystem.ts

Lines changed: 75 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -8416,52 +8416,95 @@ new C();`
84168416
});
84178417

84188418
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() {
84348451
const host = createServerHost([file, moduleFile, libFile, configFile]);
84358452
const service = createProjectService(host);
84368453
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+
84398474
const moduleInfo = service.getScriptInfo(moduleFile.path);
84408475
const sourceFile = moduleInfo.cacheSourceFile.sourceFile;
84418476
assert.equal(project.getSourceFile(moduleInfo.path), sourceFile);
84428477

84438478
// 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);
84478480
assert.equal(moduleInfo.cacheSourceFile.sourceFile, sourceFile);
84488481

84498482
// write content back
8450-
service.applyChangesToFile(info, [{ span: { start: 0, length: 0 }, newText: importModuleContent }]);
8451-
checkProject(/*moduleIsOrphan*/ false);
8483+
changeFileToImportModule(service);
84528484
assert.equal(moduleInfo.cacheSourceFile.sourceFile, sourceFile);
84538485
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);
84548495

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);
84658508
});
84668509
});
84678510
}

0 commit comments

Comments
 (0)