8
8
9
9
import type { PartialMessage , PartialNote } from 'esbuild' ;
10
10
import { platform } from 'node:os' ;
11
- import {
12
- Diagnostic ,
13
- DiagnosticRelatedInformation ,
14
- flattenDiagnosticMessageText ,
15
- getLineAndCharacterOfPosition ,
16
- getPositionOfLineAndCharacter ,
17
- } from 'typescript' ;
11
+ import type ts from 'typescript' ;
18
12
19
13
/**
20
14
* Converts TypeScript Diagnostic related information into an esbuild compatible note object.
@@ -24,11 +18,12 @@ import {
24
18
* @returns An esbuild diagnostic message as a PartialMessage object
25
19
*/
26
20
function convertTypeScriptDiagnosticInfo (
27
- info : DiagnosticRelatedInformation ,
21
+ typescript : typeof ts ,
22
+ info : ts . DiagnosticRelatedInformation ,
28
23
textPrefix ?: string ,
29
24
) : PartialNote {
30
25
const newLine = platform ( ) === 'win32' ? '\r\n' : '\n' ;
31
- let text = flattenDiagnosticMessageText ( info . messageText , newLine ) ;
26
+ let text = typescript . flattenDiagnosticMessageText ( info . messageText , newLine ) ;
32
27
if ( textPrefix ) {
33
28
text = textPrefix + text ;
34
29
}
@@ -43,23 +38,23 @@ function convertTypeScriptDiagnosticInfo(
43
38
44
39
// Calculate the line/column location and extract the full line text that has the diagnostic
45
40
if ( info . start ) {
46
- const { line, character } = getLineAndCharacterOfPosition ( info . file , info . start ) ;
41
+ const { line, character } = typescript . getLineAndCharacterOfPosition ( info . file , info . start ) ;
47
42
note . location . line = line + 1 ;
48
43
note . location . column = character ;
49
44
50
45
// The start position for the slice is the first character of the error line
51
- const lineStartPosition = getPositionOfLineAndCharacter ( info . file , line , 0 ) ;
46
+ const lineStartPosition = typescript . getPositionOfLineAndCharacter ( info . file , line , 0 ) ;
52
47
53
48
// The end position for the slice is the first character of the next line or the length of
54
49
// the entire file if the line is the last line of the file (getPositionOfLineAndCharacter
55
50
// will error if a nonexistent line is passed).
56
- const { line : lastLineOfFile } = getLineAndCharacterOfPosition (
51
+ const { line : lastLineOfFile } = typescript . getLineAndCharacterOfPosition (
57
52
info . file ,
58
53
info . file . text . length - 1 ,
59
54
) ;
60
55
const lineEndPosition =
61
56
line < lastLineOfFile
62
- ? getPositionOfLineAndCharacter ( info . file , line + 1 , 0 )
57
+ ? typescript . getPositionOfLineAndCharacter ( info . file , line + 1 , 0 )
63
58
: info . file . text . length ;
64
59
65
60
note . location . lineText = info . file . text . slice ( lineStartPosition , lineEndPosition ) . trimEnd ( ) ;
@@ -74,7 +69,10 @@ function convertTypeScriptDiagnosticInfo(
74
69
* @param diagnostic The TypeScript diagnostic to convert.
75
70
* @returns An esbuild diagnostic message as a PartialMessage object
76
71
*/
77
- export function convertTypeScriptDiagnostic ( diagnostic : Diagnostic ) : PartialMessage {
72
+ export function convertTypeScriptDiagnostic (
73
+ typescript : typeof ts ,
74
+ diagnostic : ts . Diagnostic ,
75
+ ) : PartialMessage {
78
76
let codePrefix = 'TS' ;
79
77
let code = `${ diagnostic . code } ` ;
80
78
if ( diagnostic . source === 'ngtsc' ) {
@@ -84,13 +82,14 @@ export function convertTypeScriptDiagnostic(diagnostic: Diagnostic): PartialMess
84
82
}
85
83
86
84
const message : PartialMessage = convertTypeScriptDiagnosticInfo (
85
+ typescript ,
87
86
diagnostic ,
88
87
`${ codePrefix } ${ code } : ` ,
89
88
) ;
90
89
91
90
if ( diagnostic . relatedInformation ?. length ) {
92
91
message . notes = diagnostic . relatedInformation . map ( ( info ) =>
93
- convertTypeScriptDiagnosticInfo ( info ) ,
92
+ convertTypeScriptDiagnosticInfo ( typescript , info ) ,
94
93
) ;
95
94
}
96
95
0 commit comments