Skip to content

Commit e37ff25

Browse files
committed
Test suppression of diagnostic events
1 parent 517cf6a commit e37ff25

File tree

2 files changed

+92
-3
lines changed

2 files changed

+92
-3
lines changed

src/harness/unittests/tsserverProjectSystem.ts

+86-3
Original file line numberDiff line numberDiff line change
@@ -168,11 +168,12 @@ namespace ts.projectSystem {
168168
readonly session: TestSession;
169169
readonly service: server.ProjectService;
170170
readonly host: TestServerHost;
171-
constructor(files: FileOrFolder[]) {
171+
constructor(files: FileOrFolder[], suppressDiagnosticEvents?: boolean) {
172172
this.host = createServerHost(files);
173173
this.session = createSession(this.host, {
174174
canUseEvents: true,
175175
eventHandler: event => this.events.push(event),
176+
suppressDiagnosticEvents,
176177
});
177178
this.service = this.session.getProjectService();
178179
}
@@ -485,6 +486,12 @@ namespace ts.projectSystem {
485486
checkNthEvent(session, server.toEvent("projectsUpdatedInBackground", { openFiles }), 0, /*isMostRecent*/ true);
486487
}
487488

489+
function checkNoDiagnosticEvents(session: TestSession) {
490+
for (const event of session.events) {
491+
assert.isFalse(event.event.endsWith("Diag"), JSON.stringify(event));
492+
}
493+
}
494+
488495
function checkNthEvent(session: TestSession, expectedEvent: protocol.Event, index: number, isMostRecent: boolean) {
489496
const events = session.events;
490497
assert.deepEqual(events[index], expectedEvent);
@@ -4074,6 +4081,63 @@ namespace ts.projectSystem {
40744081
session.clearMessages();
40754082
});
40764083

4084+
it("suppressed diagnostic events", () => {
4085+
const file: FileOrFolder = {
4086+
path: "/a.ts",
4087+
content: "1 = 2;",
4088+
};
4089+
4090+
const host = createServerHost([file]);
4091+
const session = createSession(host, { canUseEvents: true, suppressDiagnosticEvents: true });
4092+
const service = session.getProjectService();
4093+
4094+
session.executeCommandSeq<protocol.OpenRequest>({
4095+
command: server.CommandNames.Open,
4096+
arguments: { file: file.path, fileContent: file.content },
4097+
});
4098+
4099+
checkNumberOfProjects(service, { inferredProjects: 1 });
4100+
4101+
host.checkTimeoutQueueLength(0);
4102+
checkNoDiagnosticEvents(session);
4103+
4104+
session.clearMessages();
4105+
4106+
let expectedSequenceId = session.getNextSeq();
4107+
4108+
session.executeCommandSeq<protocol.GeterrRequest>({
4109+
command: server.CommandNames.Geterr,
4110+
arguments: {
4111+
delay: 0,
4112+
files: [file.path],
4113+
}
4114+
});
4115+
4116+
host.checkTimeoutQueueLength(0);
4117+
checkNoDiagnosticEvents(session);
4118+
4119+
checkCompleteEvent(session, 1, expectedSequenceId);
4120+
4121+
session.clearMessages();
4122+
4123+
expectedSequenceId = session.getNextSeq();
4124+
4125+
session.executeCommandSeq<protocol.GeterrForProjectRequest>({
4126+
command: server.CommandNames.Geterr,
4127+
arguments: {
4128+
delay: 0,
4129+
file: file.path,
4130+
}
4131+
});
4132+
4133+
host.checkTimeoutQueueLength(0);
4134+
checkNoDiagnosticEvents(session);
4135+
4136+
checkCompleteEvent(session, 1, expectedSequenceId);
4137+
4138+
session.clearMessages();
4139+
});
4140+
40774141
function createDiagnostic(start: protocol.Location, end: protocol.Location, message: DiagnosticMessage, args: ReadonlyArray<string> = []): protocol.Diagnostic {
40784142
return { start, end, text: formatStringFromArgs(message.message, args), code: message.code, category: diagnosticCategoryName(message), source: undefined };
40794143
}
@@ -4149,7 +4213,7 @@ namespace ts.projectSystem {
41494213
serverEventManager.checkSingleConfigFileDiagEvent(configFile.path, configFile.path);
41504214
});
41514215

4152-
it("are not generated when the config file doesnot include file opened and config file has errors", () => {
4216+
it("are not generated when the config file does not include file opened and config file has errors", () => {
41534217
const file = {
41544218
path: "/a/b/app.ts",
41554219
content: "let x = 10"
@@ -4173,7 +4237,26 @@ namespace ts.projectSystem {
41734237
serverEventManager.hasZeroEvent("configFileDiag");
41744238
});
41754239

4176-
it("are not generated when the config file doesnot include file opened and doesnt contain any errors", () => {
4240+
it("are not generated when the config file has errors but suppressDiagnosticEvents is true", () => {
4241+
const file = {
4242+
path: "/a/b/app.ts",
4243+
content: "let x = 10"
4244+
};
4245+
const configFile = {
4246+
path: "/a/b/tsconfig.json",
4247+
content: `{
4248+
"compilerOptions": {
4249+
"foo": "bar",
4250+
"allowJS": true
4251+
}
4252+
}`
4253+
};
4254+
const serverEventManager = new TestServerEventManager([file, configFile], /*suppressDiagnosticEvents*/ true);
4255+
openFilesForSession([file], serverEventManager.session);
4256+
serverEventManager.hasZeroEvent("configFileDiag");
4257+
});
4258+
4259+
it("are not generated when the config file does not include file opened and doesnt contain any errors", () => {
41774260
const file = {
41784261
path: "/a/b/app.ts",
41794262
content: "let x = 10"

tests/baselines/reference/api/tsserverlibrary.d.ts

+6
Original file line numberDiff line numberDiff line change
@@ -7256,6 +7256,8 @@ declare namespace ts.server {
72567256
*/
72577257
canUseEvents: boolean;
72587258
eventHandler?: ProjectServiceEventHandler;
7259+
/** Has no effect if eventHandler is also specified. */
7260+
suppressDiagnosticEvents?: boolean;
72597261
throttleWaitMilliseconds?: number;
72607262
globalPlugins?: ReadonlyArray<string>;
72617263
pluginProbeLocations?: ReadonlyArray<string>;
@@ -7274,6 +7276,7 @@ declare namespace ts.server {
72747276
private hrtime;
72757277
protected logger: Logger;
72767278
protected canUseEvents: boolean;
7279+
private suppressDiagnosticEvents?;
72777280
private eventHandler;
72787281
constructor(opts: SessionOptions);
72797282
private sendRequestCompletedEvent;
@@ -7289,6 +7292,7 @@ declare namespace ts.server {
72897292
private syntacticCheck;
72907293
private infoCheck;
72917294
private sendDiagnosticsEvent;
7295+
/** It is the caller's responsibility to verify that `!this.suppressDiagnosticEvents`. */
72927296
private updateErrorCheck;
72937297
private cleanProjects;
72947298
private cleanup;
@@ -7817,6 +7821,7 @@ declare namespace ts.server {
78177821
useInferredProjectPerProjectRoot: boolean;
78187822
typingsInstaller: ITypingsInstaller;
78197823
eventHandler?: ProjectServiceEventHandler;
7824+
suppressDiagnosticEvents?: boolean;
78207825
throttleWaitMilliseconds?: number;
78217826
globalPlugins?: ReadonlyArray<string>;
78227827
pluginProbeLocations?: ReadonlyArray<string>;
@@ -7883,6 +7888,7 @@ declare namespace ts.server {
78837888
readonly typingsInstaller: ITypingsInstaller;
78847889
readonly throttleWaitMilliseconds?: number;
78857890
private readonly eventHandler?;
7891+
private readonly suppressDiagnosticEvents?;
78867892
readonly globalPlugins: ReadonlyArray<string>;
78877893
readonly pluginProbeLocations: ReadonlyArray<string>;
78887894
readonly allowLocalPluginLoads: boolean;

0 commit comments

Comments
 (0)