Skip to content

Commit c1751dc

Browse files
committed
More refactor
1 parent f3a6ea6 commit c1751dc

File tree

5 files changed

+171
-293
lines changed

5 files changed

+171
-293
lines changed

src/testRunner/unittests/tsserver/projectErrors.ts

Lines changed: 39 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -292,42 +292,21 @@ namespace ts.projectSystem {
292292
// Since this is not js project so no typings are queued
293293
host.checkTimeoutQueueLength(0);
294294

295-
const newTimeoutId = host.getNextTimeoutId();
296-
const expectedSequenceId = session.getNextSeq();
297-
session.executeCommandSeq<protocol.GeterrRequest>({
298-
command: server.CommandNames.Geterr,
299-
arguments: {
300-
delay: 0,
301-
files: [untitledFile]
302-
}
303-
});
304-
host.checkTimeoutQueueLength(1);
305-
306-
// Run the last one = get error request
307-
host.runQueuedTimeoutCallbacks(newTimeoutId);
308-
309-
assert.isFalse(hasError());
310-
host.checkTimeoutQueueLength(0);
311-
checkErrorMessage(session, "syntaxDiag", { file: untitledFile, diagnostics: [] });
312-
session.clearMessages();
313-
314-
host.runQueuedImmediateCallbacks();
315-
assert.isFalse(hasError());
316295
const errorOffset = fileContent.indexOf(refPathNotFound1) + 1;
317-
checkErrorMessage(session, "semanticDiag", {
318-
file: untitledFile,
319-
diagnostics: [
320-
createDiagnostic({ line: 1, offset: errorOffset }, { line: 1, offset: errorOffset + refPathNotFound1.length }, Diagnostics.File_0_not_found, [refPathNotFound1], "error"),
321-
createDiagnostic({ line: 2, offset: errorOffset }, { line: 2, offset: errorOffset + refPathNotFound2.length }, Diagnostics.File_0_not_found, [refPathNotFound2.substr(2)], "error")
322-
]
296+
verifyGetErrRequest({
297+
session,
298+
host,
299+
expected: [{
300+
file: untitledFile,
301+
syntax: [],
302+
semantic: [
303+
createDiagnostic({ line: 1, offset: errorOffset }, { line: 1, offset: errorOffset + refPathNotFound1.length }, Diagnostics.File_0_not_found, [refPathNotFound1], "error"),
304+
createDiagnostic({ line: 2, offset: errorOffset }, { line: 2, offset: errorOffset + refPathNotFound2.length }, Diagnostics.File_0_not_found, [refPathNotFound2.substr(2)], "error")
305+
],
306+
suggestion: []
307+
}],
308+
onErrEvent: () => assert.isFalse(hasError())
323309
});
324-
session.clearMessages();
325-
326-
host.runQueuedImmediateCallbacks(1);
327-
assert.isFalse(hasError());
328-
checkErrorMessage(session, "suggestionDiag", { file: untitledFile, diagnostics: [] });
329-
checkCompleteEvent(session, 2, expectedSequenceId);
330-
session.clearMessages();
331310
}
332311

333312
it("has projectRoot", () => {
@@ -371,27 +350,16 @@ namespace ts.projectSystem {
371350
verifyErrorsInApp();
372351

373352
function verifyErrorsInApp() {
374-
session.clearMessages();
375-
const expectedSequenceId = session.getNextSeq();
376-
session.executeCommandSeq<protocol.GeterrRequest>({
377-
command: server.CommandNames.Geterr,
378-
arguments: {
379-
delay: 0,
380-
files: [app.path]
381-
}
353+
verifyGetErrRequest({
354+
session,
355+
host,
356+
expected: [{
357+
file: app,
358+
syntax: [],
359+
semantic: [],
360+
suggestion: []
361+
}],
382362
});
383-
host.checkTimeoutQueueLengthAndRun(1);
384-
checkErrorMessage(session, "syntaxDiag", { file: app.path, diagnostics: [] });
385-
session.clearMessages();
386-
387-
host.runQueuedImmediateCallbacks();
388-
checkErrorMessage(session, "semanticDiag", { file: app.path, diagnostics: [] });
389-
session.clearMessages();
390-
391-
host.runQueuedImmediateCallbacks(1);
392-
checkErrorMessage(session, "suggestionDiag", { file: app.path, diagnostics: [] });
393-
checkCompleteEvent(session, 2, expectedSequenceId);
394-
session.clearMessages();
395363
}
396364
});
397365

@@ -450,32 +418,12 @@ namespace ts.projectSystem {
450418
checkErrors([serverUtilities.path, app.path]);
451419

452420
function checkErrors(openFiles: [string, string]) {
453-
const expectedSequenceId = session.getNextSeq();
454-
session.executeCommandSeq<protocol.GeterrRequest>({
455-
command: protocol.CommandTypes.Geterr,
456-
arguments: {
457-
delay: 0,
458-
files: openFiles
459-
}
421+
verifyGetErrRequest({
422+
session,
423+
host,
424+
expected: openFiles.map(file => ({ file, syntax: [], semantic: [], suggestion: [] })),
425+
existingTimeouts: 2
460426
});
461-
462-
for (const openFile of openFiles) {
463-
session.clearMessages();
464-
host.checkTimeoutQueueLength(3);
465-
host.runQueuedTimeoutCallbacks(host.getNextTimeoutId() - 1);
466-
467-
checkErrorMessage(session, "syntaxDiag", { file: openFile, diagnostics: [] });
468-
session.clearMessages();
469-
470-
host.runQueuedImmediateCallbacks();
471-
checkErrorMessage(session, "semanticDiag", { file: openFile, diagnostics: [] });
472-
session.clearMessages();
473-
474-
host.runQueuedImmediateCallbacks(1);
475-
checkErrorMessage(session, "suggestionDiag", { file: openFile, diagnostics: [] });
476-
}
477-
checkCompleteEvent(session, 2, expectedSequenceId);
478-
session.clearMessages();
479427
}
480428
});
481429

@@ -530,36 +478,19 @@ declare module '@custom/plugin' {
530478

531479
function checkErrors() {
532480
host.checkTimeoutQueueLength(0);
533-
const expectedSequenceId = session.getNextSeq();
534-
session.executeCommandSeq<protocol.GeterrRequest>({
535-
command: server.CommandNames.Geterr,
536-
arguments: {
537-
delay: 0,
538-
files: [aFile.path],
539-
}
540-
});
541-
542-
host.checkTimeoutQueueLengthAndRun(1);
543-
544-
checkErrorMessage(session, "syntaxDiag", { file: aFile.path, diagnostics: [] }, /*isMostRecent*/ true);
545-
session.clearMessages();
546-
547-
host.runQueuedImmediateCallbacks(1);
548-
549-
checkErrorMessage(session, "semanticDiag", { file: aFile.path, diagnostics: [] });
550-
session.clearMessages();
551-
552-
host.runQueuedImmediateCallbacks(1);
553-
554-
checkErrorMessage(session, "suggestionDiag", {
555-
file: aFile.path,
556-
diagnostics: [
557-
createDiagnostic({ line: 1, offset: 1 }, { line: 1, offset: 44 }, Diagnostics._0_is_declared_but_its_value_is_never_read, ["myModule"], "suggestion", /*reportsUnnecessary*/ true),
558-
createDiagnostic({ line: 2, offset: 10 }, { line: 2, offset: 13 }, Diagnostics._0_is_declared_but_its_value_is_never_read, ["foo"], "suggestion", /*reportsUnnecessary*/ true)
559-
],
481+
verifyGetErrRequest({
482+
session,
483+
host,
484+
expected: [{
485+
file: aFile,
486+
syntax: [],
487+
semantic: [],
488+
suggestion: [
489+
createDiagnostic({ line: 1, offset: 1 }, { line: 1, offset: 44 }, Diagnostics._0_is_declared_but_its_value_is_never_read, ["myModule"], "suggestion", /*reportsUnnecessary*/ true),
490+
createDiagnostic({ line: 2, offset: 10 }, { line: 2, offset: 13 }, Diagnostics._0_is_declared_but_its_value_is_never_read, ["foo"], "suggestion", /*reportsUnnecessary*/ true)
491+
]
492+
}]
560493
});
561-
checkCompleteEvent(session, 2, expectedSequenceId);
562-
session.clearMessages();
563494
}
564495
});
565496
});

src/testRunner/unittests/tsserver/projectReferenceErrors.ts

Lines changed: 84 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,100 @@
11
namespace ts.projectSystem {
2-
describe("unittests:: tsserver:: with project references and error reporting", () => {
3-
const dependecyLocation = `${projectRoot}/dependency`;
4-
const usageLocation = `${projectRoot}/usage`;
2+
export interface GetErrDiagnostics {
3+
file: string | File;
4+
syntax?: protocol.Diagnostic[];
5+
semantic?: protocol.Diagnostic[];
6+
suggestion?: protocol.Diagnostic[];
7+
}
8+
export interface VerifyGetErrRequestBase {
9+
session: TestSession;
10+
host: TestServerHost;
11+
onErrEvent?: () => void;
12+
existingTimeouts?: number;
13+
}
14+
export interface VerifyGetErrRequest extends VerifyGetErrRequestBase {
15+
expected: readonly GetErrDiagnostics[];
16+
}
17+
export function verifyGetErrRequest(request: VerifyGetErrRequest) {
18+
const { session, expected } = request;
19+
session.clearMessages();
20+
const expectedSequenceId = session.getNextSeq();
21+
session.executeCommandSeq<protocol.GeterrRequest>({
22+
command: protocol.CommandTypes.Geterr,
23+
arguments: {
24+
delay: 0,
25+
files: expected.map(f => filePath(f.file))
26+
}
27+
});
28+
checkAllErrors({ ...request, expectedSequenceId });
29+
}
530

6-
interface CheckErrorsInFile {
7-
session: TestSession;
8-
host: TestServerHost;
9-
expected: GetErrDiagnostics;
10-
expectedSequenceId?: number;
31+
export interface CheckAllErrors extends VerifyGetErrRequest {
32+
expectedSequenceId: number;
33+
}
34+
function checkAllErrors({ expected, expectedSequenceId, ...rest }: CheckAllErrors) {
35+
for (let i = 0; i < expected.length; i++) {
36+
checkErrorsInFile({
37+
...rest,
38+
expected: expected[i],
39+
expectedSequenceId: i === expected.length - 1 ? expectedSequenceId : undefined,
40+
});
1141
}
12-
function checkErrorsInFile({ session, host, expected: { file, syntax, semantic, suggestion }, expectedSequenceId }: CheckErrorsInFile) {
42+
}
43+
44+
function filePath(file: string | File) {
45+
return isString(file) ? file : file.path;
46+
}
47+
interface CheckErrorsInFile extends VerifyGetErrRequestBase {
48+
expected: GetErrDiagnostics;
49+
expectedSequenceId?: number;
50+
}
51+
function checkErrorsInFile({
52+
session, host, onErrEvent, existingTimeouts, expectedSequenceId,
53+
expected: { file, syntax, semantic, suggestion },
54+
}: CheckErrorsInFile) {
55+
onErrEvent = onErrEvent || noop;
56+
if (existingTimeouts !== undefined) {
57+
host.checkTimeoutQueueLength(existingTimeouts + 1);
58+
host.runQueuedTimeoutCallbacks(host.getNextTimeoutId() - 1);
59+
}
60+
else {
1361
host.checkTimeoutQueueLengthAndRun(1);
14-
checkErrorMessage(session, "syntaxDiag", { file: file.path, diagnostics: syntax });
62+
}
63+
if (syntax) {
64+
onErrEvent();
65+
checkErrorMessage(session, "syntaxDiag", { file: filePath(file), diagnostics: syntax });
66+
}
67+
if (semantic) {
1568
session.clearMessages();
1669

1770
host.runQueuedImmediateCallbacks(1);
18-
checkErrorMessage(session, "semanticDiag", { file: file.path, diagnostics: semantic });
71+
onErrEvent();
72+
checkErrorMessage(session, "semanticDiag", { file: filePath(file), diagnostics: semantic });
73+
}
74+
if (suggestion) {
1975
session.clearMessages();
2076

2177
host.runQueuedImmediateCallbacks(1);
22-
checkErrorMessage(session, "suggestionDiag", { file: file.path, diagnostics: suggestion });
23-
if (expectedSequenceId !== undefined) {
24-
checkCompleteEvent(session, 2, expectedSequenceId);
25-
}
26-
session.clearMessages();
78+
onErrEvent();
79+
checkErrorMessage(session, "suggestionDiag", { file: filePath(file), diagnostics: suggestion });
2780
}
28-
29-
interface CheckAllErrors {
30-
session: TestSession;
31-
host: TestServerHost;
32-
expected: readonly GetErrDiagnostics[];
33-
expectedSequenceId: number;
34-
}
35-
function checkAllErrors({ session, host, expected, expectedSequenceId }: CheckAllErrors) {
36-
for (let i = 0; i < expected.length; i++) {
37-
checkErrorsInFile({
38-
session,
39-
host,
40-
expected: expected[i],
41-
expectedSequenceId: i === expected.length - 1 ? expectedSequenceId : undefined
42-
});
43-
}
81+
if (expectedSequenceId !== undefined) {
82+
checkCompleteEvent(session, syntax || semantic || suggestion ? 2 : 1, expectedSequenceId);
4483
}
84+
session.clearMessages();
85+
}
86+
87+
describe("unittests:: tsserver:: with project references and error reporting", () => {
88+
const dependecyLocation = `${projectRoot}/dependency`;
89+
const usageLocation = `${projectRoot}/usage`;
4590

4691
function verifyErrorsUsingGeterr({ allFiles, openFiles, expectedGetErr }: VerifyScenario) {
4792
it("verifies the errors in open file", () => {
4893
const host = createServerHost([...allFiles(), libFile]);
4994
const session = createSession(host, { canUseEvents: true, });
5095
openFilesForSession(openFiles(), session);
5196

52-
session.clearMessages();
53-
const expectedSequenceId = session.getNextSeq();
54-
const expected = expectedGetErr();
55-
session.executeCommandSeq<protocol.GeterrRequest>({
56-
command: protocol.CommandTypes.Geterr,
57-
arguments: {
58-
delay: 0,
59-
files: expected.map(f => f.file.path)
60-
}
61-
});
62-
63-
checkAllErrors({ session, host, expected, expectedSequenceId });
97+
verifyGetErrRequest({ session, host, expected: expectedGetErr() });
6498
});
6599
}
66100

@@ -95,27 +129,27 @@ namespace ts.projectSystem {
95129
const actualSyntax = session.executeCommandSeq<protocol.SyntacticDiagnosticsSyncRequest>({
96130
command: protocol.CommandTypes.SyntacticDiagnosticsSync,
97131
arguments: {
98-
file: file.path,
132+
file: filePath(file),
99133
projectFileName: project
100134
}
101135
}).response as protocol.Diagnostic[];
102-
assert.deepEqual(actualSyntax, syntax, `Syntax diagnostics for file: ${file.path}, project: ${project}`);
136+
assert.deepEqual(actualSyntax, syntax, `Syntax diagnostics for file: ${filePath(file)}, project: ${project}`);
103137
const actualSemantic = session.executeCommandSeq<protocol.SemanticDiagnosticsSyncRequest>({
104138
command: protocol.CommandTypes.SemanticDiagnosticsSync,
105139
arguments: {
106-
file: file.path,
140+
file: filePath(file),
107141
projectFileName: project
108142
}
109143
}).response as protocol.Diagnostic[];
110-
assert.deepEqual(actualSemantic, semantic, `Semantic diagnostics for file: ${file.path}, project: ${project}`);
144+
assert.deepEqual(actualSemantic, semantic, `Semantic diagnostics for file: ${filePath(file)}, project: ${project}`);
111145
const actualSuggestion = session.executeCommandSeq<protocol.SuggestionDiagnosticsSyncRequest>({
112146
command: protocol.CommandTypes.SuggestionDiagnosticsSync,
113147
arguments: {
114-
file: file.path,
148+
file: filePath(file),
115149
projectFileName: project
116150
}
117151
}).response as protocol.Diagnostic[];
118-
assert.deepEqual(actualSuggestion, suggestion, `Suggestion diagnostics for file: ${file.path}, project: ${project}`);
152+
assert.deepEqual(actualSuggestion, suggestion, `Suggestion diagnostics for file: ${filePath(file)}, project: ${project}`);
119153
}
120154
});
121155
}
@@ -139,12 +173,6 @@ namespace ts.projectSystem {
139173
});
140174
}
141175

142-
interface GetErrDiagnostics {
143-
file: File;
144-
syntax: protocol.Diagnostic[];
145-
semantic: protocol.Diagnostic[];
146-
suggestion: protocol.Diagnostic[];
147-
}
148176
interface GetErrForProjectDiagnostics {
149177
project: string;
150178
errors: readonly GetErrDiagnostics[];

0 commit comments

Comments
 (0)