Skip to content
This repository was archived by the owner on Sep 1, 2026. It is now read-only.

Commit 42c632e

Browse files
Add versions of get*Diagostics that allow passing in an array of files. (#4552)
1 parent 72787a4 commit 42c632e

7 files changed

Lines changed: 324 additions & 123 deletions

File tree

‎_packages/native-preview/src/api/async/api.ts‎

Lines changed: 35 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1039,64 +1039,81 @@ export class Program {
10391039
/**
10401040
* Get syntactic (parse) diagnostics for a specific file or all files.
10411041
* @param file - Optional file to get diagnostics for. If omitted, returns diagnostics for all files.
1042+
* Get syntactic (parse) diagnostics for specific files or all files.
1043+
* @param file - Optional file(s) to get diagnostics for. If omitted, returns diagnostics for all files.
10421044
*/
1043-
async getSyntacticDiagnostics(file?: DocumentIdentifier): Promise<readonly Diagnostic[]> {
1045+
async getSyntacticDiagnostics(file?: DocumentIdentifier | readonly DocumentIdentifier[]): Promise<readonly Diagnostic[]> {
1046+
const files = file === undefined ? undefined
1047+
: Array.isArray(file) ? file
1048+
: [file];
10441049
const data = await this.client.apiRequest<Diagnostic[]>("getSyntacticDiagnostics", {
10451050
snapshot: this.snapshotId,
10461051
project: this.project.id,
1047-
...(file !== undefined ? { file } : {}),
1052+
files,
10481053
});
10491054
return data ?? [];
10501055
}
10511056

10521057
/**
1053-
* Get binder diagnostics for a specific file or all files.
1054-
* @param file - Optional file to get diagnostics for. If omitted, returns diagnostics for all files.
1058+
* Get binder diagnostics for specific files or all files.
1059+
* @param file - Optional file(s) to get diagnostics for. If omitted, returns diagnostics for all files.
10551060
*/
1056-
async getBindDiagnostics(file?: DocumentIdentifier): Promise<readonly Diagnostic[]> {
1061+
async getBindDiagnostics(file?: DocumentIdentifier | readonly DocumentIdentifier[]): Promise<readonly Diagnostic[]> {
1062+
const files = file === undefined ? undefined
1063+
: Array.isArray(file) ? file
1064+
: [file];
10571065
const data = await this.client.apiRequest<Diagnostic[]>("getBindDiagnostics", {
10581066
snapshot: this.snapshotId,
10591067
project: this.project.id,
1060-
...(file !== undefined ? { file } : {}),
1068+
files,
10611069
});
10621070
return data ?? [];
10631071
}
10641072

10651073
/**
1066-
* Get semantic (type-check) diagnostics for a specific file or all files.
1067-
* @param file - Optional file to get diagnostics for. If omitted, returns diagnostics for all files.
1074+
* Get semantic (type-check) diagnostics for specific files or all files.
1075+
* @param file - Optional file(s) to get diagnostics for. If omitted, returns diagnostics for all files.
10681076
*/
1069-
async getSemanticDiagnostics(file?: DocumentIdentifier): Promise<readonly Diagnostic[]> {
1077+
async getSemanticDiagnostics(file?: DocumentIdentifier | readonly DocumentIdentifier[]): Promise<readonly Diagnostic[]> {
1078+
const files = file === undefined ? undefined
1079+
: Array.isArray(file) ? file
1080+
: [file];
10701081
const data = await this.client.apiRequest<Diagnostic[]>("getSemanticDiagnostics", {
10711082
snapshot: this.snapshotId,
10721083
project: this.project.id,
1073-
...(file !== undefined ? { file } : {}),
1084+
files,
10741085
});
10751086
return data ?? [];
10761087
}
10771088

10781089
/**
1079-
* Get suggestion diagnostics for a specific file or all files.
1080-
* @param file - Optional file to get diagnostics for. If omitted, returns diagnostics for all files.
1090+
* Get suggestion diagnostics for specific files or all files.
1091+
* @param file - Optional file(s) to get diagnostics for. If omitted, returns diagnostics for all files.
10811092
*/
1082-
async getSuggestionDiagnostics(file?: DocumentIdentifier): Promise<readonly Diagnostic[]> {
1093+
async getSuggestionDiagnostics(file?: DocumentIdentifier | readonly DocumentIdentifier[]): Promise<readonly Diagnostic[]> {
1094+
const files = file === undefined ? undefined
1095+
: Array.isArray(file) ? file
1096+
: [file];
10831097
const data = await this.client.apiRequest<Diagnostic[]>("getSuggestionDiagnostics", {
10841098
snapshot: this.snapshotId,
10851099
project: this.project.id,
1086-
...(file !== undefined ? { file } : {}),
1100+
files,
10871101
});
10881102
return data ?? [];
10891103
}
10901104

10911105
/**
1092-
* Get declaration emit diagnostics for a specific file or all files.
1093-
* @param file - Optional file to get diagnostics for. If omitted, returns diagnostics for all files.
1106+
* Get declaration emit diagnostics for specific files or all files.
1107+
* @param file - Optional file(s) to get diagnostics for. If omitted, returns diagnostics for all files.
10941108
*/
1095-
async getDeclarationDiagnostics(file?: DocumentIdentifier): Promise<readonly Diagnostic[]> {
1109+
async getDeclarationDiagnostics(file?: DocumentIdentifier | readonly DocumentIdentifier[]): Promise<readonly Diagnostic[]> {
1110+
const files = file === undefined ? undefined
1111+
: Array.isArray(file) ? file
1112+
: [file];
10961113
const data = await this.client.apiRequest<Diagnostic[]>("getDeclarationDiagnostics", {
10971114
snapshot: this.snapshotId,
10981115
project: this.project.id,
1099-
...(file !== undefined ? { file } : {}),
1116+
files,
11001117
});
11011118
return data ?? [];
11021119
}

‎_packages/native-preview/src/api/sync/api.ts‎

Lines changed: 35 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1047,64 +1047,81 @@ export class Program {
10471047
/**
10481048
* Get syntactic (parse) diagnostics for a specific file or all files.
10491049
* @param file - Optional file to get diagnostics for. If omitted, returns diagnostics for all files.
1050+
* Get syntactic (parse) diagnostics for specific files or all files.
1051+
* @param file - Optional file(s) to get diagnostics for. If omitted, returns diagnostics for all files.
10501052
*/
1051-
getSyntacticDiagnostics(file?: DocumentIdentifier): readonly Diagnostic[] {
1053+
getSyntacticDiagnostics(file?: DocumentIdentifier | readonly DocumentIdentifier[]): readonly Diagnostic[] {
1054+
const files = file === undefined ? undefined
1055+
: Array.isArray(file) ? file
1056+
: [file];
10521057
const data = this.client.apiRequest<Diagnostic[]>("getSyntacticDiagnostics", {
10531058
snapshot: this.snapshotId,
10541059
project: this.project.id,
1055-
...(file !== undefined ? { file } : {}),
1060+
files,
10561061
});
10571062
return data ?? [];
10581063
}
10591064

10601065
/**
1061-
* Get binder diagnostics for a specific file or all files.
1062-
* @param file - Optional file to get diagnostics for. If omitted, returns diagnostics for all files.
1066+
* Get binder diagnostics for specific files or all files.
1067+
* @param file - Optional file(s) to get diagnostics for. If omitted, returns diagnostics for all files.
10631068
*/
1064-
getBindDiagnostics(file?: DocumentIdentifier): readonly Diagnostic[] {
1069+
getBindDiagnostics(file?: DocumentIdentifier | readonly DocumentIdentifier[]): readonly Diagnostic[] {
1070+
const files = file === undefined ? undefined
1071+
: Array.isArray(file) ? file
1072+
: [file];
10651073
const data = this.client.apiRequest<Diagnostic[]>("getBindDiagnostics", {
10661074
snapshot: this.snapshotId,
10671075
project: this.project.id,
1068-
...(file !== undefined ? { file } : {}),
1076+
files,
10691077
});
10701078
return data ?? [];
10711079
}
10721080

10731081
/**
1074-
* Get semantic (type-check) diagnostics for a specific file or all files.
1075-
* @param file - Optional file to get diagnostics for. If omitted, returns diagnostics for all files.
1082+
* Get semantic (type-check) diagnostics for specific files or all files.
1083+
* @param file - Optional file(s) to get diagnostics for. If omitted, returns diagnostics for all files.
10761084
*/
1077-
getSemanticDiagnostics(file?: DocumentIdentifier): readonly Diagnostic[] {
1085+
getSemanticDiagnostics(file?: DocumentIdentifier | readonly DocumentIdentifier[]): readonly Diagnostic[] {
1086+
const files = file === undefined ? undefined
1087+
: Array.isArray(file) ? file
1088+
: [file];
10781089
const data = this.client.apiRequest<Diagnostic[]>("getSemanticDiagnostics", {
10791090
snapshot: this.snapshotId,
10801091
project: this.project.id,
1081-
...(file !== undefined ? { file } : {}),
1092+
files,
10821093
});
10831094
return data ?? [];
10841095
}
10851096

10861097
/**
1087-
* Get suggestion diagnostics for a specific file or all files.
1088-
* @param file - Optional file to get diagnostics for. If omitted, returns diagnostics for all files.
1098+
* Get suggestion diagnostics for specific files or all files.
1099+
* @param file - Optional file(s) to get diagnostics for. If omitted, returns diagnostics for all files.
10891100
*/
1090-
getSuggestionDiagnostics(file?: DocumentIdentifier): readonly Diagnostic[] {
1101+
getSuggestionDiagnostics(file?: DocumentIdentifier | readonly DocumentIdentifier[]): readonly Diagnostic[] {
1102+
const files = file === undefined ? undefined
1103+
: Array.isArray(file) ? file
1104+
: [file];
10911105
const data = this.client.apiRequest<Diagnostic[]>("getSuggestionDiagnostics", {
10921106
snapshot: this.snapshotId,
10931107
project: this.project.id,
1094-
...(file !== undefined ? { file } : {}),
1108+
files,
10951109
});
10961110
return data ?? [];
10971111
}
10981112

10991113
/**
1100-
* Get declaration emit diagnostics for a specific file or all files.
1101-
* @param file - Optional file to get diagnostics for. If omitted, returns diagnostics for all files.
1114+
* Get declaration emit diagnostics for specific files or all files.
1115+
* @param file - Optional file(s) to get diagnostics for. If omitted, returns diagnostics for all files.
11021116
*/
1103-
getDeclarationDiagnostics(file?: DocumentIdentifier): readonly Diagnostic[] {
1117+
getDeclarationDiagnostics(file?: DocumentIdentifier | readonly DocumentIdentifier[]): readonly Diagnostic[] {
1118+
const files = file === undefined ? undefined
1119+
: Array.isArray(file) ? file
1120+
: [file];
11041121
const data = this.client.apiRequest<Diagnostic[]>("getDeclarationDiagnostics", {
11051122
snapshot: this.snapshotId,
11061123
project: this.project.id,
1107-
...(file !== undefined ? { file } : {}),
1124+
files,
11081125
});
11091126
return data ?? [];
11101127
}

‎_packages/native-preview/test/async/api.test.ts‎

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5834,6 +5834,119 @@ describe("Program - diagnostics", () => {
58345834
await api.close();
58355835
}
58365836
});
5837+
5838+
test("getSyntacticDiagnostics with multiple files", async () => {
5839+
const sourceA = `const a: = 1;`;
5840+
const sourceB = `const b: = 2;`;
5841+
const api = spawnAPI({
5842+
"/tsconfig.json": "{}",
5843+
"/src/a.ts": sourceA,
5844+
"/src/b.ts": sourceB,
5845+
"/src/clean.ts": `const c = 3;`,
5846+
});
5847+
try {
5848+
const snapshot = await api.updateSnapshot({ openProject: "/tsconfig.json" });
5849+
const project = snapshot.getProject("/tsconfig.json")!;
5850+
const diags = await project.program.getSyntacticDiagnostics(["/src/a.ts", "/src/b.ts"]);
5851+
assert.deepEqual(diags, [
5852+
{
5853+
fileName: "/src/a.ts",
5854+
...rangeOf(sourceA, "="),
5855+
code: 1110,
5856+
category: DiagnosticCategory.Error,
5857+
text: "Type expected.",
5858+
},
5859+
{
5860+
fileName: "/src/b.ts",
5861+
...rangeOf(sourceB, "="),
5862+
code: 1110,
5863+
category: DiagnosticCategory.Error,
5864+
text: "Type expected.",
5865+
},
5866+
]);
5867+
}
5868+
finally {
5869+
await api.close();
5870+
}
5871+
});
5872+
5873+
test("getSemanticDiagnostics with multiple files", async () => {
5874+
const sourceA = `export const a: number = "hello";`;
5875+
const sourceB = `export const b: string = 42;`;
5876+
const api = spawnAPI({
5877+
"/tsconfig.json": "{}",
5878+
"/src/a.ts": sourceA,
5879+
"/src/b.ts": sourceB,
5880+
});
5881+
try {
5882+
const snapshot = await api.updateSnapshot({ openProject: "/tsconfig.json" });
5883+
const project = snapshot.getProject("/tsconfig.json")!;
5884+
const diags = await project.program.getSemanticDiagnostics(["/src/a.ts", "/src/b.ts"]);
5885+
assert.equal(diags.length, 2);
5886+
assert.equal(diags[0].fileName, "/src/a.ts");
5887+
assert.equal(diags[0].code, 2322);
5888+
assert.equal(diags[1].fileName, "/src/b.ts");
5889+
assert.equal(diags[1].code, 2322);
5890+
}
5891+
finally {
5892+
await api.close();
5893+
}
5894+
});
5895+
5896+
test("getBindDiagnostics with multiple files", async () => {
5897+
const sourceA = `let x = 1;\nlet x = 2;`;
5898+
const sourceB = `let y = 1;\nlet y = 2;`;
5899+
const api = spawnAPI({
5900+
"/tsconfig.json": "{}",
5901+
"/src/a.ts": sourceA,
5902+
"/src/b.ts": sourceB,
5903+
});
5904+
try {
5905+
const snapshot = await api.updateSnapshot({ openProject: "/tsconfig.json" });
5906+
const project = snapshot.getProject("/tsconfig.json")!;
5907+
const diags = await project.program.getBindDiagnostics(["/src/a.ts", "/src/b.ts"]);
5908+
assert.equal(diags.length, 4);
5909+
assert.equal(diags.filter(d => d.fileName === "/src/a.ts").length, 2);
5910+
assert.equal(diags.filter(d => d.fileName === "/src/b.ts").length, 2);
5911+
}
5912+
finally {
5913+
await api.close();
5914+
}
5915+
});
5916+
5917+
test("diagnostics with no files argument returns all", async () => {
5918+
const api = spawnAPI({
5919+
"/tsconfig.json": "{}",
5920+
"/src/a.ts": `const a: = 1;`,
5921+
"/src/b.ts": `const b: = 2;`,
5922+
});
5923+
try {
5924+
const snapshot = await api.updateSnapshot({ openProject: "/tsconfig.json" });
5925+
const project = snapshot.getProject("/tsconfig.json")!;
5926+
const diags = await project.program.getSyntacticDiagnostics();
5927+
assert.equal(diags.length, 2);
5928+
}
5929+
finally {
5930+
await api.close();
5931+
}
5932+
});
5933+
5934+
test("diagnostics with empty files array returns empty", async () => {
5935+
const api = spawnAPI({
5936+
"/tsconfig.json": "{}",
5937+
"/src/a.ts": `const a: = 1;`,
5938+
"/src/b.ts": `const b: = 2;`,
5939+
});
5940+
try {
5941+
const snapshot = await api.updateSnapshot({ openProject: "/tsconfig.json" });
5942+
const project = snapshot.getProject("/tsconfig.json")!;
5943+
const diags = await project.program.getSyntacticDiagnostics([]);
5944+
assert.deepEqual(diags, []);
5945+
}
5946+
finally {
5947+
await api.close();
5948+
}
5949+
});
58375950
});
58385951

58395952
describe("getDefaultProjectForFile", () => {

0 commit comments

Comments
 (0)