|
| 1 | +namespace ts.projectSystem { |
| 2 | + describe("unittests:: tsserver:: Semantic operations on Syntax server", () => { |
| 3 | + function setup() { |
| 4 | + const file1: File = { |
| 5 | + path: `${tscWatch.projectRoot}/a.ts`, |
| 6 | + content: `import { y } from "./b"; |
| 7 | +class c { prop = "hello"; foo() { return this.prop; } }` |
| 8 | + }; |
| 9 | + const file2: File = { |
| 10 | + path: `${tscWatch.projectRoot}/b.ts`, |
| 11 | + content: "export const y = 10;" |
| 12 | + }; |
| 13 | + const configFile: File = { |
| 14 | + path: `${tscWatch.projectRoot}/tsconfig.json`, |
| 15 | + content: "{}" |
| 16 | + }; |
| 17 | + const host = createServerHost([file1, file2, libFile, configFile]); |
| 18 | + const session = createSession(host, { syntaxOnly: true, useSingleInferredProject: true }); |
| 19 | + return { host, session, file1, file2, configFile }; |
| 20 | + } |
| 21 | + |
| 22 | + it("open files are added to inferred project even if config file is present and semantic operations succeed", () => { |
| 23 | + const { host, session, file1, file2 } = setup(); |
| 24 | + const service = session.getProjectService(); |
| 25 | + openFilesForSession([file1], session); |
| 26 | + checkNumberOfProjects(service, { inferredProjects: 1 }); |
| 27 | + const project = service.inferredProjects[0]; |
| 28 | + checkProjectActualFiles(project, [libFile.path, file1.path]); // Import is not resolved |
| 29 | + verifyCompletions(); |
| 30 | + |
| 31 | + openFilesForSession([file2], session); |
| 32 | + checkNumberOfProjects(service, { inferredProjects: 1 }); |
| 33 | + checkProjectActualFiles(project, [libFile.path, file1.path, file2.path]); |
| 34 | + verifyCompletions(); |
| 35 | + |
| 36 | + function verifyCompletions() { |
| 37 | + assert.isTrue(project.languageServiceEnabled); |
| 38 | + checkWatchedFiles(host, emptyArray); |
| 39 | + checkWatchedDirectories(host, emptyArray, /*recursive*/ true); |
| 40 | + checkWatchedDirectories(host, emptyArray, /*recursive*/ false); |
| 41 | + const response = session.executeCommandSeq<protocol.CompletionsRequest>({ |
| 42 | + command: protocol.CommandTypes.Completions, |
| 43 | + arguments: protocolFileLocationFromSubstring(file1, "prop", { index: 1 }) |
| 44 | + }).response as protocol.CompletionEntry[]; |
| 45 | + assert.deepEqual(response, [ |
| 46 | + completionEntry("foo", ScriptElementKind.memberFunctionElement), |
| 47 | + completionEntry("prop", ScriptElementKind.memberVariableElement), |
| 48 | + ]); |
| 49 | + } |
| 50 | + |
| 51 | + function completionEntry(name: string, kind: ScriptElementKind): protocol.CompletionEntry { |
| 52 | + return { |
| 53 | + name, |
| 54 | + kind, |
| 55 | + kindModifiers: "", |
| 56 | + sortText: Completions.SortText.LocationPriority, |
| 57 | + hasAction: undefined, |
| 58 | + insertText: undefined, |
| 59 | + isRecommended: undefined, |
| 60 | + replacementSpan: undefined, |
| 61 | + source: undefined |
| 62 | + }; |
| 63 | + } |
| 64 | + }); |
| 65 | + |
| 66 | + it("throws on unsupported commands", () => { |
| 67 | + const { session, file1 } = setup(); |
| 68 | + const service = session.getProjectService(); |
| 69 | + openFilesForSession([file1], session); |
| 70 | + let hasException = false; |
| 71 | + const request: protocol.SemanticDiagnosticsSyncRequest = { |
| 72 | + type: "request", |
| 73 | + seq: 1, |
| 74 | + command: protocol.CommandTypes.SemanticDiagnosticsSync, |
| 75 | + arguments: { file: file1.path } |
| 76 | + }; |
| 77 | + try { |
| 78 | + session.executeCommand(request); |
| 79 | + } |
| 80 | + catch (e) { |
| 81 | + assert.equal(e.message, `Request: semanticDiagnosticsSync not allowed on syntaxServer`); |
| 82 | + hasException = true; |
| 83 | + } |
| 84 | + assert.isTrue(hasException); |
| 85 | + |
| 86 | + hasException = false; |
| 87 | + const project = service.inferredProjects[0]; |
| 88 | + try { |
| 89 | + project.getLanguageService().getSemanticDiagnostics(file1.path); |
| 90 | + } |
| 91 | + catch (e) { |
| 92 | + assert.equal(e.message, `LanguageService Operation: getSemanticDiagnostics not allowed on syntaxServer`); |
| 93 | + hasException = true; |
| 94 | + } |
| 95 | + assert.isTrue(hasException); |
| 96 | + }); |
| 97 | + }); |
| 98 | +} |
0 commit comments