@@ -80,24 +80,96 @@ namespace ts {
8080 return < string > diagnostic . messageText ;
8181 }
8282
83- function reportDiagnostic ( diagnostic : Diagnostic ) {
84- var output = "" ;
83+ function reportDiagnostic ( diagnostic : Diagnostic ) : void {
84+ let output = "" ;
8585
8686 if ( diagnostic . file ) {
87- var loc = getLineAndCharacterOfPosition ( diagnostic . file , diagnostic . start ) ;
87+ let { line , character } = getLineAndCharacterOfPosition ( diagnostic . file , diagnostic . start ) ;
8888
89- output += `${ diagnostic . file . fileName } (${ loc . line + 1 } ,${ loc . character + 1 } ): ` ;
89+ output += `${ diagnostic . file . fileName } (${ line + 1 } ,${ character + 1 } ): ` ;
9090 }
9191
92- var category = DiagnosticCategory [ diagnostic . category ] . toLowerCase ( ) ;
92+ let category = DiagnosticCategory [ diagnostic . category ] . toLowerCase ( ) ;
9393 output += `${ category } TS${ diagnostic . code } : ${ flattenDiagnosticMessageText ( diagnostic . messageText , sys . newLine ) } ${ sys . newLine } ` ;
9494
9595 sys . write ( output ) ;
9696 }
9797
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+
98170 function reportDiagnostics ( diagnostics : Diagnostic [ ] ) {
99171 for ( var i = 0 ; i < diagnostics . length ; i ++ ) {
100- reportDiagnostic ( diagnostics [ i ] ) ;
172+ reportDiagnosticAllPrettyLike ( diagnostics [ i ] ) ;
101173 }
102174 }
103175
0 commit comments