• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1import * as ts from "../../_namespaces/ts";
2
3describe("unittests:: tsserver:: document registry in project service", () => {
4    const importModuleContent = `import {a} from "./module1"`;
5    const file: ts.projectSystem.File = {
6        path: `${ts.tscWatch.projectRoot}/index.ts`,
7        content: importModuleContent
8    };
9    const moduleFile: ts.projectSystem.File = {
10        path: `${ts.tscWatch.projectRoot}/module1.d.ts`,
11        content: "export const a: number;"
12    };
13    const configFile: ts.projectSystem.File = {
14        path: `${ts.tscWatch.projectRoot}/tsconfig.json`,
15        content: JSON.stringify({ files: ["index.ts"] })
16    };
17
18    function getProject(service: ts.projectSystem.TestProjectService) {
19        return service.configuredProjects.get(configFile.path)!;
20    }
21
22    function checkProject(service: ts.projectSystem.TestProjectService, moduleIsOrphan: boolean) {
23        // Update the project
24        const project = getProject(service);
25        project.getLanguageService();
26        ts.projectSystem.checkProjectActualFiles(project, [file.path, ts.projectSystem.libFile.path, configFile.path, ...(moduleIsOrphan ? [] : [moduleFile.path])]);
27        const moduleInfo = service.getScriptInfo(moduleFile.path)!;
28        assert.isDefined(moduleInfo);
29        assert.equal(moduleInfo.isOrphan(), moduleIsOrphan);
30        const key = service.documentRegistry.getKeyForCompilationSettings(project.getCompilationSettings());
31        assert.deepEqual(service.documentRegistry.getLanguageServiceRefCounts(moduleInfo.path, moduleInfo.scriptKind), [[key, moduleIsOrphan ? undefined : 1]]);
32    }
33
34    function createServiceAndHost() {
35        const host = ts.projectSystem.createServerHost([file, moduleFile, ts.projectSystem.libFile, configFile]);
36        const service = ts.projectSystem.createProjectService(host);
37        service.openClientFile(file.path);
38        checkProject(service, /*moduleIsOrphan*/ false);
39        return { host, service };
40    }
41
42    function changeFileToNotImportModule(service: ts.projectSystem.TestProjectService) {
43        const info = service.getScriptInfo(file.path)!;
44        service.applyChangesToFile(info, ts.singleIterator({ span: { start: 0, length: importModuleContent.length }, newText: "" }));
45        checkProject(service, /*moduleIsOrphan*/ true);
46    }
47
48    function changeFileToImportModule(service: ts.projectSystem.TestProjectService) {
49        const info = service.getScriptInfo(file.path)!;
50        service.applyChangesToFile(info, ts.singleIterator({ span: { start: 0, length: 0 }, newText: importModuleContent }));
51        checkProject(service, /*moduleIsOrphan*/ false);
52    }
53
54    it("Caches the source file if script info is orphan", () => {
55        const { service } = createServiceAndHost();
56        const project = getProject(service);
57
58        const moduleInfo = service.getScriptInfo(moduleFile.path)!;
59        const sourceFile = moduleInfo.cacheSourceFile!.sourceFile;
60        assert.equal(project.getSourceFile(moduleInfo.path), sourceFile);
61
62        // edit file
63        changeFileToNotImportModule(service);
64        assert.equal(moduleInfo.cacheSourceFile!.sourceFile, sourceFile);
65
66        // write content back
67        changeFileToImportModule(service);
68        assert.equal(moduleInfo.cacheSourceFile!.sourceFile, sourceFile);
69        assert.equal(project.getSourceFile(moduleInfo.path), sourceFile);
70    });
71
72    it("Caches the source file if script info is orphan, and orphan script info changes", () => {
73        const { host, service } = createServiceAndHost();
74        const project = getProject(service);
75
76        const moduleInfo = service.getScriptInfo(moduleFile.path)!;
77        const sourceFile = moduleInfo.cacheSourceFile!.sourceFile;
78        assert.equal(project.getSourceFile(moduleInfo.path), sourceFile);
79
80        // edit file
81        changeFileToNotImportModule(service);
82        assert.equal(moduleInfo.cacheSourceFile!.sourceFile, sourceFile);
83
84        const updatedModuleContent = moduleFile.content + "\nexport const b: number;";
85        host.writeFile(moduleFile.path, updatedModuleContent);
86
87        // write content back
88        changeFileToImportModule(service);
89        assert.notEqual(moduleInfo.cacheSourceFile!.sourceFile, sourceFile);
90        assert.equal(project.getSourceFile(moduleInfo.path), moduleInfo.cacheSourceFile!.sourceFile);
91        assert.equal(moduleInfo.cacheSourceFile!.sourceFile.text, updatedModuleContent);
92    });
93});
94