forked from sourcegraph/javascript-typescript-langserver
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiagnostics.ts
More file actions
41 lines (39 loc) · 1.49 KB
/
diagnostics.ts
File metadata and controls
41 lines (39 loc) · 1.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import * as ts from 'typescript'
import { Diagnostic, DiagnosticSeverity, Range } from 'vscode-languageserver'
/**
* Converts a TypeScript Diagnostic to an LSP Diagnostic
*/
export function convertTsDiagnostic(diagnostic: ts.Diagnostic): Diagnostic {
const text = ts.flattenDiagnosticMessageText(diagnostic.messageText, '\n')
let range: Range = { start: { character: 0, line: 0 }, end: { character: 0, line: 0 } }
if (diagnostic.file && diagnostic.start && diagnostic.length) {
range = {
start: diagnostic.file.getLineAndCharacterOfPosition(diagnostic.start),
end: diagnostic.file.getLineAndCharacterOfPosition(diagnostic.start + diagnostic.length),
}
}
return {
range,
message: text,
severity: convertDiagnosticCategory(diagnostic.category),
code: diagnostic.code,
source: diagnostic.source || 'ts',
}
}
/**
* Converts a diagnostic category to an LSP DiagnosticSeverity
*
* @param category The Typescript DiagnosticCategory
*/
function convertDiagnosticCategory(category: ts.DiagnosticCategory): DiagnosticSeverity {
switch (category) {
case ts.DiagnosticCategory.Error:
return DiagnosticSeverity.Error
case ts.DiagnosticCategory.Warning:
return DiagnosticSeverity.Warning
case ts.DiagnosticCategory.Message:
return DiagnosticSeverity.Information
case ts.DiagnosticCategory.Suggestion:
return DiagnosticSeverity.Hint
}
}