@@ -80,24 +80,96 @@ namespace ts {
80
80
return < string > diagnostic . messageText ;
81
81
}
82
82
83
- function reportDiagnostic ( diagnostic : Diagnostic ) {
84
- var output = "" ;
83
+ function reportDiagnostic ( diagnostic : Diagnostic ) : void {
84
+ let output = "" ;
85
85
86
86
if ( diagnostic . file ) {
87
- var loc = getLineAndCharacterOfPosition ( diagnostic . file , diagnostic . start ) ;
87
+ let { line , character } = getLineAndCharacterOfPosition ( diagnostic . file , diagnostic . start ) ;
88
88
89
- output += `${ diagnostic . file . fileName } (${ loc . line + 1 } ,${ loc . character + 1 } ): ` ;
89
+ output += `${ diagnostic . file . fileName } (${ line + 1 } ,${ character + 1 } ): ` ;
90
90
}
91
91
92
- var category = DiagnosticCategory [ diagnostic . category ] . toLowerCase ( ) ;
92
+ let category = DiagnosticCategory [ diagnostic . category ] . toLowerCase ( ) ;
93
93
output += `${ category } TS${ diagnostic . code } : ${ flattenDiagnosticMessageText ( diagnostic . messageText , sys . newLine ) } ${ sys . newLine } ` ;
94
94
95
95
sys . write ( output ) ;
96
96
}
97
97
98
+ const redColorControlChar = "\u001b[31m" ;
99
+ const resetColorControlChar = "\u001b[0m" ;
100
+
101
+ function reportDiagnosticAllPrettyLike ( diagnostic : Diagnostic ) : void {
102
+ let output = "" ;
103
+
104
+ if ( diagnostic . file ) {
105
+ let { start, length, file } = diagnostic ;
106
+ let { line : firstLine , character : firstLineChar } = getLineAndCharacterOfPosition ( file , start ) ;
107
+ let { line : lastLine , character : lastLineChar } = getLineAndCharacterOfPosition ( file , start + length ) ;
108
+
109
+ output += sys . newLine ;
110
+ for ( let i = 0 , indexOfLastLine = lastLine - firstLine ; i <= indexOfLastLine ; i ++ ) {
111
+ let lineStart = getPositionOfLineAndCharacter ( file , firstLine + i , 0 ) ;
112
+ let lineEnd = getPositionOfLineAndCharacter ( file , firstLine + i + 1 , 0 ) ;
113
+ let lineContent = file . text . slice ( lineStart , lineEnd ) ;
114
+ lineContent = lineContent . replace ( / \s + $ / g, "" ) ; // trim from end
115
+ lineContent = lineContent . replace ( "\t" , " " ) ; // normalize tabs to 4 spaces
116
+
117
+ output += lineContent + sys . newLine ;
118
+
119
+ if ( i === 0 ) {
120
+ let lastCharForLine = i === indexOfLastLine ? lastLineChar : undefined ;
121
+
122
+ output += lineContent . slice ( 0 , firstLineChar ) . replace ( / \S / g, " " ) ;
123
+ output += redColorControlChar ;
124
+ output += lineContent . slice ( firstLineChar , lastCharForLine ) . replace ( / ./ g, "~" ) ;
125
+ output += resetColorControlChar ;
126
+ }
127
+ else if ( i === indexOfLastLine ) {
128
+ output += redColorControlChar ;
129
+ output += lineContent . slice ( 0 , lastLineChar ) . replace ( / ./ g, "~" ) ;
130
+ output += resetColorControlChar ;
131
+ // Don't bother "filling" at the end.
132
+ }
133
+ else {
134
+ output += lineContent . replace ( / ^ ( \s * ) ( .* ) $ / , replaceLineWithRedSquiggles ) ;
135
+ }
136
+
137
+ output += sys . newLine ;
138
+ }
139
+
140
+ output += `${ file . fileName } (${ firstLine + 1 } ,${ firstLineChar + 1 } ): ` ;
141
+ }
142
+
143
+ let category = DiagnosticCategory [ diagnostic . category ] . toLowerCase ( ) ;
144
+ output += `${ category } TS${ diagnostic . code } : ${ flattenDiagnosticMessageText ( diagnostic . messageText , sys . newLine ) } ` ;
145
+ output += sys . newLine + sys . newLine ;
146
+
147
+ sys . write ( output ) ;
148
+ }
149
+
150
+ function replaceLineWithRedSquiggles ( orig : string , leadingWhitespace : string , content : string ) : string {
151
+ return leadingWhitespace + redColorControlChar + content . replace ( / ./ g, "~" ) + resetColorControlChar ;
152
+ }
153
+
154
+ /** Splits the given string on \r\n, or on only \n if that fails, or on only \r if *that* fails. */
155
+ function splitContentByNewlines ( content : string ) {
156
+ // Split up the input file by line
157
+ // Note: IE JS engine incorrectly handles consecutive delimiters here when using RegExp split, so
158
+ // we have to use string-based splitting instead and try to figure out the delimiting chars
159
+ var lines = content . split ( '\r\n' ) ;
160
+ if ( lines . length === 1 ) {
161
+ lines = content . split ( '\n' ) ;
162
+
163
+ if ( lines . length === 1 ) {
164
+ lines = content . split ( "\r" ) ;
165
+ }
166
+ }
167
+ return lines ;
168
+ }
169
+
98
170
function reportDiagnostics ( diagnostics : Diagnostic [ ] ) {
99
171
for ( var i = 0 ; i < diagnostics . length ; i ++ ) {
100
- reportDiagnostic ( diagnostics [ i ] ) ;
172
+ reportDiagnosticAllPrettyLike ( diagnostics [ i ] ) ;
101
173
}
102
174
}
103
175
0 commit comments