Skip to content

Commit f1d87dc

Browse files
committed
refactor updateErrorCheck to delay work again
1 parent ec6772c commit f1d87dc

File tree

2 files changed

+32
-28
lines changed

2 files changed

+32
-28
lines changed

src/server/session.ts

+31-28
Original file line numberDiff line numberDiff line change
@@ -301,8 +301,7 @@ export function formatDiagnosticToProtocol(diag: Diagnostic, includeFileName: bo
301301

302302
interface PendingErrorCheck {
303303
fileName: NormalizedPath;
304-
ranges?: TextRange[];
305-
project?: Project;
304+
project: Project;
306305
}
307306

308307
function allEditsBeforePos(edits: readonly TextChange[], pos: number): boolean {
@@ -1143,7 +1142,7 @@ export class Session<TMessage = string> implements EventSender {
11431142
if (!this.suppressDiagnosticEvents && !this.noGetErrOnBackgroundUpdate) {
11441143
this.projectService.logger.info(`Queueing diagnostics update for ${openFiles}`);
11451144
// For now only queue error checking for open files. We can change this to include non open files as well
1146-
this.errorCheck.startNew(next => this.updateErrorCheck(next, mapDefined(openFiles, file => ({ fileName: toNormalizedPath(file) })), 100, /*requireOpen*/ true));
1145+
this.errorCheck.startNew(next => this.updateErrorCheck(next, openFiles, 100, /*requireOpen*/ true));
11471146
}
11481147

11491148
// Send project changed event
@@ -1336,7 +1335,7 @@ export class Session<TMessage = string> implements EventSender {
13361335
/** It is the caller's responsibility to verify that `!this.suppressDiagnosticEvents`. */
13371336
private updateErrorCheck(
13381337
next: NextStep,
1339-
checkList: PendingErrorCheck[],
1338+
checkList: PendingErrorCheck[] | (string | protocol.FileRangesRequestArgs)[],
13401339
ms: number,
13411340
requireOpen = true,
13421341
) {
@@ -1363,8 +1362,7 @@ export class Session<TMessage = string> implements EventSender {
13631362
}
13641363

13651364
if (this.getPreferences(fileName).disableSuggestions) {
1366-
goNext();
1367-
return;
1365+
return goNext();
13681366
}
13691367
next.immediate("suggestionCheck", () => {
13701368
this.suggestionCheck(fileName, project);
@@ -1377,11 +1375,23 @@ export class Session<TMessage = string> implements EventSender {
13771375
return;
13781376
}
13791377

1380-
const { fileName, project = this.projectService.tryGetDefaultProjectForFile(fileName), ranges } = checkList[index];
1381-
if (!project) {
1382-
return;
1378+
let ranges: protocol.FileRange[] | undefined;
1379+
let item: string | protocol.FileRangesRequestArgs | PendingErrorCheck | undefined = checkList[index];
1380+
if (isString(item)) {
1381+
item = this.toPendingErrorCheck(item);
1382+
}
1383+
// eslint-disable-next-line local/no-in-operator
1384+
else if ("ranges" in item) {
1385+
ranges = item.ranges;
1386+
item = this.toPendingErrorCheck(item.file);
13831387
}
13841388

1389+
if (!item) {
1390+
return goNext();
1391+
}
1392+
1393+
const { fileName, project } = item;
1394+
13851395
// Ensure the project is up to date before checking if this file is present in the project.
13861396
updateProjectIfDirty(project);
13871397
if (!project.containsFile(fileName, requireOpen)) {
@@ -1395,13 +1405,15 @@ export class Session<TMessage = string> implements EventSender {
13951405

13961406
// Don't provide semantic diagnostics unless we're in full semantic mode.
13971407
if (project.projectService.serverMode !== LanguageServiceMode.Semantic) {
1398-
goNext();
1399-
return;
1408+
return goNext();
14001409
}
14011410

14021411
if (ranges) {
14031412
return next.immediate("regionSemanticCheck", () => {
1404-
this.regionSemanticCheck(fileName, project, ranges);
1413+
const scriptInfo = this.projectService.getScriptInfoForNormalizedPath(fileName);
1414+
if (scriptInfo) {
1415+
this.regionSemanticCheck(fileName, project, ranges.map(range => this.getRange({ file: fileName, ...range }, scriptInfo)));
1416+
}
14051417
if (this.changeSeq !== seq) {
14061418
return;
14071419
}
@@ -2560,28 +2572,19 @@ export class Session<TMessage = string> implements EventSender {
25602572
}
25612573
}
25622574

2575+
private toPendingErrorCheck(uncheckedFileName: string): PendingErrorCheck | undefined {
2576+
const fileName = toNormalizedPath(uncheckedFileName);
2577+
const project = this.projectService.tryGetDefaultProjectForFile(fileName);
2578+
return project && { fileName, project };
2579+
}
2580+
25632581
private getDiagnostics(next: NextStep, delay: number, fileArgs: (string | protocol.FileRangesRequestArgs)[]): void {
25642582
if (this.suppressDiagnosticEvents) {
25652583
return;
25662584
}
25672585

25682586
if (fileArgs.length > 0) {
2569-
const files = mapDefined(fileArgs, fileArg => {
2570-
if (isString(fileArg)) {
2571-
return { fileName: toNormalizedPath(fileArg) };
2572-
}
2573-
const fileName = toNormalizedPath(fileArg.file);
2574-
const scriptInfo = this.projectService.getScriptInfo(fileName);
2575-
if (!scriptInfo) {
2576-
return undefined;
2577-
}
2578-
const ranges = fileArg.ranges.map(range => this.getRange({ file: fileArg.file, ...range }, scriptInfo));
2579-
return {
2580-
fileName,
2581-
ranges,
2582-
};
2583-
});
2584-
this.updateErrorCheck(next, files, delay);
2587+
this.updateErrorCheck(next, fileArgs, delay);
25852588
}
25862589
}
25872590

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

+1
Original file line numberDiff line numberDiff line change
@@ -3548,6 +3548,7 @@ declare namespace ts {
35483548
private getCompileOnSaveAffectedFileList;
35493549
private emitFile;
35503550
private getSignatureHelpItems;
3551+
private toPendingErrorCheck;
35513552
private getDiagnostics;
35523553
private change;
35533554
private reload;

0 commit comments

Comments
 (0)