Skip to content

Commit 58a0e75

Browse files
committed
Add getSyntacticDiagnostics unit test
1 parent 883e19e commit 58a0e75

File tree

2 files changed

+55
-2
lines changed

2 files changed

+55
-2
lines changed

src/server/client.ts

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -397,11 +397,41 @@ namespace ts.server {
397397
}
398398

399399
getSyntacticDiagnostics(fileName: string): Diagnostic[] {
400-
throw new Error("Not Implemented Yet.");
400+
const args: protocol.SyntacticDiagnosticsSyncRequestArgs = { file: fileName, includeLinePosition: true };
401+
402+
const request = this.processRequest<protocol.SyntacticDiagnosticsSyncRequest>(CommandNames.SyntacticDiagnosticsSync, args);
403+
const response = this.processResponse<protocol.SyntacticDiagnosticsSyncResponse>(request);
404+
405+
return (<protocol.DiagnosticWithLinePosition[]>response.body).map(entry => this.convertDiagnostic(entry, fileName));
401406
}
402407

403408
getSemanticDiagnostics(fileName: string): Diagnostic[] {
404-
throw new Error("Not Implemented Yet.");
409+
const args: protocol.SemanticDiagnosticsSyncRequestArgs = { file: fileName, includeLinePosition: true };
410+
411+
const request = this.processRequest<protocol.SemanticDiagnosticsSyncRequest>(CommandNames.SemanticDiagnosticsSync, args);
412+
const response = this.processResponse<protocol.SemanticDiagnosticsSyncResponse>(request);
413+
414+
return (<protocol.DiagnosticWithLinePosition[]>response.body).map(entry => this.convertDiagnostic(entry, fileName));
415+
}
416+
417+
convertDiagnostic(entry: protocol.DiagnosticWithLinePosition, fileName: string): Diagnostic {
418+
let category: DiagnosticCategory;
419+
for (const id in DiagnosticCategory) {
420+
if (typeof id === "string" && entry.category === id.toLowerCase()) {
421+
category = (<any>DiagnosticCategory)[id];
422+
}
423+
}
424+
425+
Debug.assert(category !== undefined, "convertDiagnostic: category should not be undefined");
426+
427+
return {
428+
file: undefined,
429+
start: entry.start,
430+
length: entry.length,
431+
messageText: entry.message,
432+
category: category,
433+
code: entry.code
434+
};
405435
}
406436

407437
getCompilerOptionsDiagnostics(): Diagnostic[] {
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/// <reference path="../fourslash.ts" />
2+
3+
// @allowJs: true
4+
// @Filename: a.js
5+
//// var ===;
6+
7+
verify.getSyntacticDiagnostics(`[
8+
{
9+
"message": "Variable declaration expected.",
10+
"start": 4,
11+
"length": 3,
12+
"category": "error",
13+
"code": 1134
14+
},
15+
{
16+
"message": "Expression expected.",
17+
"start": 7,
18+
"length": 1,
19+
"category": "error",
20+
"code": 1109
21+
}
22+
]`);
23+
verify.getSemanticDiagnostics(`[]`);

0 commit comments

Comments
 (0)