Skip to content

Commit 59d1925

Browse files
committed
Add test to verify the document is released from source file when info is orphan
1 parent aa7e2b0 commit 59d1925

File tree

4 files changed

+53
-2
lines changed

4 files changed

+53
-2
lines changed

src/harness/unittests/tsserverProjectSystem.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8383,4 +8383,47 @@ new C();`
83838383
verifyCompletionListWithNewFileInSubFolder(TestFSWithWatch.Tsc_WatchDirectory.DynamicPolling);
83848384
});
83858385
});
8386+
8387+
describe("document registry in project service", () => {
8388+
it("Caches the source file if script info is orphan", () => {
8389+
const projectRootPath = "/user/username/projects/project";
8390+
const importModuleContent = `import {a} from "./module1"`;
8391+
const file: File = {
8392+
path: `${projectRootPath}/index.ts`,
8393+
content: importModuleContent
8394+
};
8395+
const moduleFile: File = {
8396+
path: `${projectRootPath}/module1.d.ts`,
8397+
content: "export const a: number;"
8398+
};
8399+
const configFile: File = {
8400+
path: `${projectRootPath}/tsconfig.json`,
8401+
content: JSON.stringify({ files: ["index.ts"] })
8402+
};
8403+
const host = createServerHost([file, moduleFile, libFile, configFile]);
8404+
const service = createProjectService(host);
8405+
service.openClientFile(file.path);
8406+
const project = service.configuredProjects.get(configFile.path);
8407+
checkProject(/*moduleIsOrphan*/ false);
8408+
8409+
// edit file
8410+
const info = service.getScriptInfo(file.path);
8411+
service.applyChangesToFile(info, [{ span: { start: 0, length: importModuleContent.length }, newText: "" }]);
8412+
checkProject(/*moduleIsOrphan*/ true);
8413+
8414+
// write content back
8415+
service.applyChangesToFile(info, [{ span: { start: 0, length: 0 }, newText: importModuleContent }]);
8416+
checkProject(/*moduleIsOrphan*/ false);
8417+
8418+
function checkProject(moduleIsOrphan: boolean) {
8419+
// Update the project
8420+
project.getLanguageService();
8421+
checkProjectActualFiles(project, [file.path, libFile.path, configFile.path, ...(moduleIsOrphan ? [] : [moduleFile.path])]);
8422+
const moduleInfo = service.getScriptInfo(moduleFile.path);
8423+
assert.isDefined(moduleInfo);
8424+
assert.equal(moduleInfo.isOrphan(), moduleIsOrphan);
8425+
assert.equal(service.documentRegistry.hasDocument(moduleInfo.path), !moduleIsOrphan);
8426+
}
8427+
});
8428+
});
83868429
}

src/server/editorServices.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,8 @@ namespace ts.server {
339339
/*@internal*/
340340
readonly typingsCache: TypingsCache;
341341

342-
private readonly documentRegistry: DocumentRegistry;
342+
/*@internal*/
343+
readonly documentRegistry: DocumentRegistry;
343344

344345
/**
345346
* Container of all known scripts

src/services/documentRegistry.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,9 @@ namespace ts {
8787

8888
releaseDocumentWithKey(path: Path, key: DocumentRegistryBucketKey): void;
8989

90+
/*@internal*/
91+
hasDocument(path: Path): boolean;
92+
9093
reportStats(): string;
9194
}
9295

@@ -225,13 +228,18 @@ namespace ts {
225228
}
226229
}
227230

231+
function hasDocument(path: Path) {
232+
return !!forEachEntry(buckets, bucket => bucket.has(path));
233+
}
234+
228235
return {
229236
acquireDocument,
230237
acquireDocumentWithKey,
231238
updateDocument,
232239
updateDocumentWithKey,
233240
releaseDocument,
234241
releaseDocumentWithKey,
242+
hasDocument,
235243
reportStats,
236244
getKeyForCompilationSettings
237245
};

tests/baselines/reference/api/tsserverlibrary.d.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8124,7 +8124,6 @@ declare namespace ts.server {
81248124
syntaxOnly?: boolean;
81258125
}
81268126
class ProjectService {
8127-
private readonly documentRegistry;
81288127
/**
81298128
* Container of all known scripts
81308129
*/

0 commit comments

Comments
 (0)