• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1namespace ts.projectSystem {
2    describe("unittests:: tsserver:: navigate-to for javascript project", () => {
3        function findNavToItem(items: protocol.NavtoItem[], itemName: string, itemKind: string) {
4            return find(items, item => item.name === itemName && item.kind === itemKind);
5        }
6
7        function containsNavToItem(items: protocol.NavtoItem[], itemName: string, itemKind: string) {
8            return findNavToItem(items, itemName, itemKind) !== undefined;
9        }
10
11        it("should not include type symbols", () => {
12            const file1: File = {
13                path: "/a/b/file1.js",
14                content: "function foo() {}"
15            };
16            const configFile: File = {
17                path: "/a/b/jsconfig.json",
18                content: "{}"
19            };
20            const host = createServerHost([file1, configFile, libFile]);
21            const session = createSession(host);
22            openFilesForSession([file1], session);
23
24            // Try to find some interface type defined in lib.d.ts
25            const libTypeNavToRequest = makeSessionRequest<protocol.NavtoRequestArgs>(CommandNames.Navto, { searchValue: "Document", file: file1.path, projectFileName: configFile.path });
26            const items = session.executeCommand(libTypeNavToRequest).response as protocol.NavtoItem[];
27            assert.isFalse(containsNavToItem(items, "Document", "interface"), `Found lib.d.ts symbol in JavaScript project nav to request result.`);
28
29            const localFunctionNavToRequst = makeSessionRequest<protocol.NavtoRequestArgs>(CommandNames.Navto, { searchValue: "foo", file: file1.path, projectFileName: configFile.path });
30            const items2 = session.executeCommand(localFunctionNavToRequst).response as protocol.NavtoItem[];
31            assert.isTrue(containsNavToItem(items2, "foo", "function"), `Cannot find function symbol "foo".`);
32        });
33
34        it("should de-duplicate symbols", () => {
35            const configFile1: File = {
36                path: "/a/tsconfig.json",
37                content: `{
38    "compilerOptions": {
39        "composite": true
40    }
41}`
42            };
43            const file1: File = {
44                path: "/a/index.ts",
45                content: "export const abcdef = 1;"
46            };
47            const configFile2: File = {
48                path: "/b/tsconfig.json",
49                content: `{
50    "compilerOptions": {
51        "composite": true
52    },
53    "references": [
54        { "path": "../a" }
55    ]
56}`
57            };
58            const file2: File = {
59                path: "/b/index.ts",
60                content: `import a = require("../a");
61export const ghijkl = a.abcdef;`
62            };
63            const host = createServerHost([configFile1, file1, configFile2, file2]);
64            const session = createSession(host, { logger: createLoggerWithInMemoryLogs(host) });
65            openFilesForSession([file1, file2], session);
66
67            const request = makeSessionRequest<protocol.NavtoRequestArgs>(CommandNames.Navto, { searchValue: "abcdef", file: file1.path });
68            session.executeCommand(request).response as protocol.NavtoItem[];
69
70            baselineTsserverLogs("navTo", "should de-duplicate symbols", session);
71        });
72
73        it("should de-duplicate symbols when searching all projects", () => {
74            const solutionConfig: File = {
75                path: "/tsconfig.json",
76                content: JSON.stringify({
77                    references: [{ path: "./a" }, { path: "./b" }],
78                    files: [],
79                })
80            };
81            const configFile1: File = {
82                path: "/a/tsconfig.json",
83                content: `{
84    "compilerOptions": {
85        "composite": true
86    }
87}`
88            };
89            const file1: File = {
90                path: "/a/index.ts",
91                content: "export const abcdef = 1;"
92            };
93            const configFile2: File = {
94                path: "/b/tsconfig.json",
95                content: `{
96    "compilerOptions": {
97        "composite": true
98    },
99    "references": [
100        { "path": "../a" }
101    ]
102}`
103            };
104            const file2: File = {
105                path: "/b/index.ts",
106                content: `import a = require("../a");
107export const ghijkl = a.abcdef;`
108            };
109            const host = createServerHost([configFile1, file1, configFile2, file2, solutionConfig]);
110            const session = createSession(host, { logger: createLoggerWithInMemoryLogs(host) });
111            openFilesForSession([file1], session);
112
113            const request = makeSessionRequest<protocol.NavtoRequestArgs>(CommandNames.Navto, { searchValue: "abcdef" });
114            session.executeCommand(request).response as protocol.NavtoItem[];
115            baselineTsserverLogs("navTo", "should de-duplicate symbols when searching all projects", session);
116        });
117
118        it("should work with Deprecated", () => {
119            const file1: File = {
120                path: "/a/b/file1.js",
121                content: "/** @deprecated */\nfunction foo () {}"
122            };
123            const configFile: File = {
124                path: "/a/b/jsconfig.json",
125                content: "{}"
126            };
127            const host = createServerHost([file1, configFile, libFile]);
128            const session = createSession(host);
129            openFilesForSession([file1], session);
130
131            // Try to find some interface type defined in lib.d.ts
132            const libTypeNavToRequest = makeSessionRequest<protocol.NavtoRequestArgs>(CommandNames.Navto, { searchValue: "foo", file: file1.path, projectFileName: configFile.path });
133            const items = session.executeCommand(libTypeNavToRequest).response as protocol.NavtoItem[];
134            const fooItem = findNavToItem(items, "foo", "function");
135            assert.isNotNull(fooItem, `Cannot find function symbol "foo".`);
136            assert.isTrue(fooItem?.kindModifiers?.includes("deprecated"));
137        });
138    });
139}
140