-
Notifications
You must be signed in to change notification settings - Fork 12.2k
/
partialSemanticServer.ts
223 lines (210 loc) · 10.8 KB
/
partialSemanticServer.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
namespace ts.projectSystem {
describe("unittests:: tsserver:: Semantic operations on partialSemanticServer", () => {
function setup() {
const file1: File = {
path: `${tscWatch.projectRoot}/a.ts`,
content: `import { y, cc } from "./b";
import { something } from "something";
class c { prop = "hello"; foo() { return this.prop; } }`
};
const file2: File = {
path: `${tscWatch.projectRoot}/b.ts`,
content: `export { cc } from "./c";
import { something } from "something";
export const y = 10;`
};
const file3: File = {
path: `${tscWatch.projectRoot}/c.ts`,
content: `export const cc = 10;`
};
const something: File = {
path: `${tscWatch.projectRoot}/node_modules/something/index.d.ts`,
content: "export const something = 10;"
};
const configFile: File = {
path: `${tscWatch.projectRoot}/tsconfig.json`,
content: "{}"
};
const host = createServerHost([file1, file2, file3, something, libFile, configFile]);
const session = createSession(host, { serverMode: LanguageServiceMode.PartialSemantic, useSingleInferredProject: true });
return { host, session, file1, file2, file3, something, configFile };
}
it("open files are added to inferred project even if config file is present and semantic operations succeed", () => {
const { host, session, file1, file2 } = setup();
const service = session.getProjectService();
openFilesForSession([file1], session);
checkNumberOfProjects(service, { inferredProjects: 1 });
const project = service.inferredProjects[0];
checkProjectActualFiles(project, [libFile.path, file1.path]); // no imports are resolved
verifyCompletions();
openFilesForSession([file2], session);
checkNumberOfProjects(service, { inferredProjects: 1 });
checkProjectActualFiles(project, [libFile.path, file1.path, file2.path]);
verifyCompletions();
function verifyCompletions() {
assert.isTrue(project.languageServiceEnabled);
checkWatchedFiles(host, emptyArray);
checkWatchedDirectories(host, emptyArray, /*recursive*/ true);
checkWatchedDirectories(host, emptyArray, /*recursive*/ false);
const response = session.executeCommandSeq<protocol.CompletionsRequest>({
command: protocol.CommandTypes.Completions,
arguments: protocolFileLocationFromSubstring(file1, "prop", { index: 1 })
}).response as protocol.CompletionEntry[];
assert.deepEqual(response, [
completionEntry("foo", ScriptElementKind.memberFunctionElement),
completionEntry("prop", ScriptElementKind.memberVariableElement),
]);
}
function completionEntry(name: string, kind: ScriptElementKind): protocol.CompletionEntry {
return {
name,
kind,
kindModifiers: "",
sortText: Completions.SortText.LocationPriority,
hasAction: undefined,
insertText: undefined,
isPackageJsonImport: undefined,
isRecommended: undefined,
replacementSpan: undefined,
source: undefined,
data: undefined,
};
}
});
it("throws on unsupported commands", () => {
const { session, file1 } = setup();
const service = session.getProjectService();
openFilesForSession([file1], session);
let hasException = false;
const request: protocol.SemanticDiagnosticsSyncRequest = {
type: "request",
seq: 1,
command: protocol.CommandTypes.SemanticDiagnosticsSync,
arguments: { file: file1.path }
};
try {
session.executeCommand(request);
}
catch (e) {
assert.equal(e.message, `Request: semanticDiagnosticsSync not allowed in LanguageServiceMode.PartialSemantic`);
hasException = true;
}
assert.isTrue(hasException);
hasException = false;
const project = service.inferredProjects[0];
try {
project.getLanguageService().getSemanticDiagnostics(file1.path);
}
catch (e) {
assert.equal(e.message, `LanguageService Operation: getSemanticDiagnostics not allowed in LanguageServiceMode.PartialSemantic`);
hasException = true;
}
assert.isTrue(hasException);
});
it("should not include auto type reference directives", () => {
const { host, session, file1 } = setup();
const atTypes: File = {
path: `/node_modules/@types/somemodule/index.d.ts`,
content: "export const something = 10;"
};
host.ensureFileOrFolder(atTypes);
const service = session.getProjectService();
openFilesForSession([file1], session);
checkNumberOfProjects(service, { inferredProjects: 1 });
const project = service.inferredProjects[0];
checkProjectActualFiles(project, [libFile.path, file1.path]); // Should not contain atTypes
});
it("should not include referenced files from unopened files", () => {
const file1: File = {
path: `${tscWatch.projectRoot}/a.ts`,
content: `///<reference path="b.ts"/>
///<reference path="${tscWatch.projectRoot}/node_modules/something/index.d.ts"/>
function fooA() { }`
};
const file2: File = {
path: `${tscWatch.projectRoot}/b.ts`,
content: `///<reference path="./c.ts"/>
///<reference path="${tscWatch.projectRoot}/node_modules/something/index.d.ts"/>
function fooB() { }`
};
const file3: File = {
path: `${tscWatch.projectRoot}/c.ts`,
content: `function fooC() { }`
};
const something: File = {
path: `${tscWatch.projectRoot}/node_modules/something/index.d.ts`,
content: "function something() {}"
};
const configFile: File = {
path: `${tscWatch.projectRoot}/tsconfig.json`,
content: "{}"
};
const host = createServerHost([file1, file2, file3, something, libFile, configFile]);
const session = createSession(host, { serverMode: LanguageServiceMode.PartialSemantic, useSingleInferredProject: true });
const service = session.getProjectService();
openFilesForSession([file1], session);
checkNumberOfProjects(service, { inferredProjects: 1 });
const project = service.inferredProjects[0];
checkProjectActualFiles(project, [libFile.path, file1.path]); // no resolve
});
it("should not crash when external module name resolution is reused", () => {
const { session, file1, file2, file3 } = setup();
const service = session.getProjectService();
openFilesForSession([file1], session);
checkNumberOfProjects(service, { inferredProjects: 1 });
const project = service.inferredProjects[0];
checkProjectActualFiles(project, [libFile.path, file1.path]);
// Close the file that contains non relative external module name and open some file that doesnt have non relative external module import
closeFilesForSession([file1], session);
openFilesForSession([file3], session);
checkProjectActualFiles(project, [libFile.path, file3.path]);
// Open file with non relative external module name
openFilesForSession([file2], session);
checkProjectActualFiles(project, [libFile.path, file2.path, file3.path]);
});
it("should not create autoImportProvider or handle package jsons", () => {
const angularFormsDts: File = {
path: "/node_modules/@angular/forms/forms.d.ts",
content: "export declare class PatternValidator {}",
};
const angularFormsPackageJson: File = {
path: "/node_modules/@angular/forms/package.json",
content: `{ "name": "@angular/forms", "typings": "./forms.d.ts" }`,
};
const tsconfig: File = {
path: "/tsconfig.json",
content: `{ "compilerOptions": { "module": "commonjs" } }`,
};
const packageJson: File = {
path: "/package.json",
content: `{ "dependencies": { "@angular/forms": "*", "@angular/core": "*" } }`
};
const indexTs: File = {
path: "/index.ts",
content: ""
};
const host = createServerHost([angularFormsDts, angularFormsPackageJson, tsconfig, packageJson, indexTs, libFile]);
const session = createSession(host, { serverMode: LanguageServiceMode.PartialSemantic, useSingleInferredProject: true });
const service = session.getProjectService();
openFilesForSession([indexTs], session);
const project = service.inferredProjects[0];
assert.isFalse(project.autoImportProviderHost);
assert.isUndefined(project.getPackageJsonAutoImportProvider());
assert.deepEqual(project.getPackageJsonsForAutoImport(), emptyArray);
});
it("should support go-to-definition on module specifiers", () => {
const { session, file1, file2 } = setup();
openFilesForSession([file1], session);
const response = session.executeCommandSeq<protocol.DefinitionAndBoundSpanRequest>({
command: protocol.CommandTypes.DefinitionAndBoundSpan,
arguments: protocolFileLocationFromSubstring(file1, `"./b"`)
}).response as protocol.DefinitionInfoAndBoundSpan;
assert.isDefined(response);
assert.deepEqual(response.definitions, [{
file: file2.path,
start: { line: 1, offset: 1 },
end: { line: 1, offset: 1 }
}]);
});
});
}