forked from angular/angular-cli
-
Notifications
You must be signed in to change notification settings - Fork 1
/
diagnostics.ts
99 lines (86 loc) · 3.27 KB
/
diagnostics.ts
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import type { PartialMessage, PartialNote } from 'esbuild';
import { platform } from 'node:os';
import {
Diagnostic,
DiagnosticRelatedInformation,
flattenDiagnosticMessageText,
getLineAndCharacterOfPosition,
getPositionOfLineAndCharacter,
} from 'typescript';
/**
* Converts TypeScript Diagnostic related information into an esbuild compatible note object.
* Related information is a subset of a full TypeScript Diagnostic and also used for diagnostic
* notes associated with the main Diagnostic.
* @param info The TypeScript diagnostic relative information to convert.
* @returns An esbuild diagnostic message as a PartialMessage object
*/
function convertTypeScriptDiagnosticInfo(
info: DiagnosticRelatedInformation,
textPrefix?: string,
): PartialNote {
const newLine = platform() === 'win32' ? '\r\n' : '\n';
let text = flattenDiagnosticMessageText(info.messageText, newLine);
if (textPrefix) {
text = textPrefix + text;
}
const note: PartialNote = { text };
if (info.file) {
note.location = {
file: info.file.fileName,
length: info.length,
};
// Calculate the line/column location and extract the full line text that has the diagnostic
if (info.start) {
const { line, character } = getLineAndCharacterOfPosition(info.file, info.start);
note.location.line = line + 1;
note.location.column = character;
// The start position for the slice is the first character of the error line
const lineStartPosition = getPositionOfLineAndCharacter(info.file, line, 0);
// The end position for the slice is the first character of the next line or the length of
// the entire file if the line is the last line of the file (getPositionOfLineAndCharacter
// will error if a nonexistent line is passed).
const { line: lastLineOfFile } = getLineAndCharacterOfPosition(
info.file,
info.file.text.length - 1,
);
const lineEndPosition =
line < lastLineOfFile
? getPositionOfLineAndCharacter(info.file, line + 1, 0)
: info.file.text.length;
note.location.lineText = info.file.text.slice(lineStartPosition, lineEndPosition).trimEnd();
}
}
return note;
}
/**
* Converts a TypeScript Diagnostic message into an esbuild compatible message object.
* @param diagnostic The TypeScript diagnostic to convert.
* @returns An esbuild diagnostic message as a PartialMessage object
*/
export function convertTypeScriptDiagnostic(diagnostic: Diagnostic): PartialMessage {
let codePrefix = 'TS';
let code = `${diagnostic.code}`;
if (diagnostic.source === 'ngtsc') {
codePrefix = 'NG';
// Remove `-99` Angular prefix from diagnostic code
code = code.slice(3);
}
const message: PartialMessage = {
...convertTypeScriptDiagnosticInfo(diagnostic, `${codePrefix}${code}: `),
// Store original diagnostic for reference if needed downstream
detail: diagnostic,
};
if (diagnostic.relatedInformation?.length) {
message.notes = diagnostic.relatedInformation.map((info) =>
convertTypeScriptDiagnosticInfo(info),
);
}
return message;
}