• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1describe("unittests:: services:: DocumentRegistry", () => {
2    it("documents are shared between projects", () => {
3        const documentRegistry = ts.createDocumentRegistry();
4        const defaultCompilerOptions = ts.getDefaultCompilerOptions();
5
6        const f1 = documentRegistry.acquireDocument("file1.ts", defaultCompilerOptions, ts.ScriptSnapshot.fromString("var x = 1;"), /* version */ "1");
7        const f2 = documentRegistry.acquireDocument("file1.ts", defaultCompilerOptions, ts.ScriptSnapshot.fromString("var x = 1;"), /* version */ "1");
8
9        assert(f1 === f2, "DocumentRegistry should return the same document for the same name");
10    });
11
12    it("documents are refreshed when settings in compilation settings affect syntax", () => {
13        const documentRegistry = ts.createDocumentRegistry();
14        const compilerOptions: ts.CompilerOptions = { target: ts.ScriptTarget.ES5, module: ts.ModuleKind.AMD };
15
16        // change compilation setting that doesn't affect parsing - should have the same document
17        compilerOptions.declaration = true;
18        const f1 = documentRegistry.acquireDocument("file1.ts", compilerOptions, ts.ScriptSnapshot.fromString("var x = 1;"), /* version */ "1");
19        compilerOptions.declaration = false;
20        const f2 = documentRegistry.acquireDocument("file1.ts", compilerOptions, ts.ScriptSnapshot.fromString("var x = 1;"), /* version */ "1");
21
22        assert(f1 === f2, "Expected to have the same document instance");
23
24
25        // change value of compilation setting that is used during production of AST - new document is required
26        compilerOptions.target = ts.ScriptTarget.ES3;
27        const f3 = documentRegistry.acquireDocument("file1.ts", compilerOptions, ts.ScriptSnapshot.fromString("var x = 1;"), /* version */ "1");
28
29        assert(f1 !== f3, "Changed target: Expected to have different instances of document");
30
31        compilerOptions.preserveConstEnums = true;
32        const f4 = documentRegistry.acquireDocument("file1.ts", compilerOptions, ts.ScriptSnapshot.fromString("var x = 1;"), /* version */ "1");
33
34        assert(f3 === f4, "Changed preserveConstEnums: Expected to have the same instance of the document");
35
36        compilerOptions.module = ts.ModuleKind.System;
37        const f5 = documentRegistry.acquireDocument("file1.ts", compilerOptions, ts.ScriptSnapshot.fromString("var x = 1;"), /* version */ "1");
38
39        assert(f4 !== f5, "Changed module: Expected to have different instances of the document");
40    });
41
42    it("Acquiring document gets correct version 1", () => {
43        const documentRegistry = ts.createDocumentRegistry();
44        const defaultCompilerOptions = ts.getDefaultCompilerOptions();
45
46        // Simulate one LS getting the document.
47        documentRegistry.acquireDocument("file1.ts", defaultCompilerOptions, ts.ScriptSnapshot.fromString("var x = 1;"), /* version */ "1");
48
49        // Simulate another LS getting the document at another version.
50        const f2 = documentRegistry.acquireDocument("file1.ts", defaultCompilerOptions, ts.ScriptSnapshot.fromString("var x = 1;"), /* version */ "2");
51
52        assert(f2.version === "2");
53    });
54
55    it("Acquiring document gets correct version 2", () => {
56        const documentRegistry = ts.createDocumentRegistry();
57        const defaultCompilerOptions = ts.getDefaultCompilerOptions();
58
59        const contents = "var x = 1;";
60        const snapshot = ts.ScriptSnapshot.fromString(contents);
61
62        // Always treat any change as a full change.
63        snapshot.getChangeRange = () => ts.createTextChangeRange(ts.createTextSpan(0, contents.length), contents.length);
64
65        // Simulate one LS getting the document.
66        documentRegistry.acquireDocument("file1.ts", defaultCompilerOptions, snapshot, /* version */ "1");
67
68        // Simulate another LS getting that document.
69        documentRegistry.acquireDocument("file1.ts", defaultCompilerOptions, snapshot, /* version */ "1");
70
71        // Now LS1 updates their document.
72        documentRegistry.updateDocument("file1.ts", defaultCompilerOptions, snapshot, /* version */ "2");
73
74        // Now LS2 tries to update their document.
75        documentRegistry.updateDocument("file1.ts", defaultCompilerOptions, snapshot, /* version */ "3");
76    });
77});
78