Add test to verify the document is released from source file when info is orphan

This commit is contained in:
Sheetal Nandi 2018-05-15 16:11:25 -07:00
parent aa7e2b0f07
commit 59d19251cf
4 changed files with 53 additions and 2 deletions

View file

@ -8383,4 +8383,47 @@ new C();`
verifyCompletionListWithNewFileInSubFolder(TestFSWithWatch.Tsc_WatchDirectory.DynamicPolling);
});
});
describe("document registry in project service", () => {
it("Caches the source file if script info is orphan", () => {
const projectRootPath = "/user/username/projects/project";
const importModuleContent = `import {a} from "./module1"`;
const file: File = {
path: `${projectRootPath}/index.ts`,
content: importModuleContent
};
const moduleFile: File = {
path: `${projectRootPath}/module1.d.ts`,
content: "export const a: number;"
};
const configFile: File = {
path: `${projectRootPath}/tsconfig.json`,
content: JSON.stringify({ files: ["index.ts"] })
};
const host = createServerHost([file, moduleFile, libFile, configFile]);
const service = createProjectService(host);
service.openClientFile(file.path);
const project = service.configuredProjects.get(configFile.path);
checkProject(/*moduleIsOrphan*/ false);
// edit file
const info = service.getScriptInfo(file.path);
service.applyChangesToFile(info, [{ span: { start: 0, length: importModuleContent.length }, newText: "" }]);
checkProject(/*moduleIsOrphan*/ true);
// write content back
service.applyChangesToFile(info, [{ span: { start: 0, length: 0 }, newText: importModuleContent }]);
checkProject(/*moduleIsOrphan*/ false);
function checkProject(moduleIsOrphan: boolean) {
// Update the project
project.getLanguageService();
checkProjectActualFiles(project, [file.path, libFile.path, configFile.path, ...(moduleIsOrphan ? [] : [moduleFile.path])]);
const moduleInfo = service.getScriptInfo(moduleFile.path);
assert.isDefined(moduleInfo);
assert.equal(moduleInfo.isOrphan(), moduleIsOrphan);
assert.equal(service.documentRegistry.hasDocument(moduleInfo.path), !moduleIsOrphan);
}
});
});
}

View file

@ -339,7 +339,8 @@ namespace ts.server {
/*@internal*/
readonly typingsCache: TypingsCache;
private readonly documentRegistry: DocumentRegistry;
/*@internal*/
readonly documentRegistry: DocumentRegistry;
/**
* Container of all known scripts

View file

@ -87,6 +87,9 @@ namespace ts {
releaseDocumentWithKey(path: Path, key: DocumentRegistryBucketKey): void;
/*@internal*/
hasDocument(path: Path): boolean;
reportStats(): string;
}
@ -225,6 +228,10 @@ namespace ts {
}
}
function hasDocument(path: Path) {
return !!forEachEntry(buckets, bucket => bucket.has(path));
}
return {
acquireDocument,
acquireDocumentWithKey,
@ -232,6 +239,7 @@ namespace ts {
updateDocumentWithKey,
releaseDocument,
releaseDocumentWithKey,
hasDocument,
reportStats,
getKeyForCompilationSettings
};

View file

@ -8124,7 +8124,6 @@ declare namespace ts.server {
syntaxOnly?: boolean;
}
class ProjectService {
private readonly documentRegistry;
/**
* Container of all known scripts
*/