@@ -6,9 +6,19 @@ namespace ts {
6
6
fileWatcher ?: FileWatcher ;
7
7
}
8
8
9
- let reportDiagnostic = reportDiagnosticSimply ;
9
+ const defaultFormatDiagnosticsHost : FormatDiagnosticsHost = {
10
+ getCurrentDirectory : ( ) => sys . getCurrentDirectory ( ) ,
11
+ getNewLine : ( ) => sys . newLine ,
12
+ getCanonicalFileName : createGetCanonicalFileName ( sys . useCaseSensitiveFileNames )
13
+ } ;
14
+
15
+ let reportDiagnosticWorker = reportDiagnosticSimply ;
16
+
17
+ function reportDiagnostic ( diagnostic : Diagnostic , host : FormatDiagnosticsHost ) {
18
+ reportDiagnosticWorker ( diagnostic , host || defaultFormatDiagnosticsHost ) ;
19
+ }
10
20
11
- function reportDiagnostics ( diagnostics : Diagnostic [ ] , host : CompilerHost ) : void {
21
+ function reportDiagnostics ( diagnostics : Diagnostic [ ] , host : FormatDiagnosticsHost ) : void {
12
22
for ( const diagnostic of diagnostics ) {
13
23
reportDiagnostic ( diagnostic , host ) ;
14
24
}
@@ -101,23 +111,8 @@ namespace ts {
101
111
return < string > diagnostic . messageText ;
102
112
}
103
113
104
- function getRelativeFileName ( fileName : string , host : CompilerHost ) : string {
105
- return host ? convertToRelativePath ( fileName , host . getCurrentDirectory ( ) , fileName => host . getCanonicalFileName ( fileName ) ) : fileName ;
106
- }
107
-
108
- function reportDiagnosticSimply ( diagnostic : Diagnostic , host : CompilerHost ) : void {
109
- let output = "" ;
110
-
111
- if ( diagnostic . file ) {
112
- const { line, character } = getLineAndCharacterOfPosition ( diagnostic . file , diagnostic . start ) ;
113
- const relativeFileName = getRelativeFileName ( diagnostic . file . fileName , host ) ;
114
- output += `${ relativeFileName } (${ line + 1 } ,${ character + 1 } ): ` ;
115
- }
116
-
117
- const category = DiagnosticCategory [ diagnostic . category ] . toLowerCase ( ) ;
118
- output += `${ category } TS${ diagnostic . code } : ${ flattenDiagnosticMessageText ( diagnostic . messageText , sys . newLine ) } ${ sys . newLine } ` ;
119
-
120
- sys . write ( output ) ;
114
+ function reportDiagnosticSimply ( diagnostic : Diagnostic , host : FormatDiagnosticsHost ) : void {
115
+ sys . write ( ts . formatDiagnostics ( [ diagnostic ] , host ) ) ;
121
116
}
122
117
123
118
const redForegroundEscapeSequence = "\u001b[91m" ;
@@ -137,15 +132,15 @@ namespace ts {
137
132
return formatStyle + text + resetEscapeSequence ;
138
133
}
139
134
140
- function reportDiagnosticWithColorAndContext ( diagnostic : Diagnostic , host : CompilerHost ) : void {
135
+ function reportDiagnosticWithColorAndContext ( diagnostic : Diagnostic , host : FormatDiagnosticsHost ) : void {
141
136
let output = "" ;
142
137
143
138
if ( diagnostic . file ) {
144
139
const { start, length, file } = diagnostic ;
145
140
const { line : firstLine , character : firstLineChar } = getLineAndCharacterOfPosition ( file , start ) ;
146
141
const { line : lastLine , character : lastLineChar } = getLineAndCharacterOfPosition ( file , start + length ) ;
147
142
const lastLineInFile = getLineAndCharacterOfPosition ( file , file . text . length ) . line ;
148
- const relativeFileName = getRelativeFileName ( file . fileName , host ) ;
143
+ const relativeFileName = host ? convertToRelativePath ( file . fileName , host . getCurrentDirectory ( ) , fileName => host . getCanonicalFileName ( fileName ) ) : file . fileName ;
149
144
150
145
const hasMoreThanFiveLines = ( lastLine - firstLine ) >= 4 ;
151
146
let gutterWidth = ( lastLine + 1 + "" ) . length ;
@@ -272,7 +267,7 @@ namespace ts {
272
267
273
268
if ( commandLine . options . locale ) {
274
269
if ( ! isJSONSupported ( ) ) {
275
- reportDiagnostic ( createCompilerDiagnostic ( Diagnostics . The_current_host_does_not_support_the_0_option , "--locale" ) , /* compilerHost */ undefined ) ;
270
+ reportDiagnostic ( createCompilerDiagnostic ( Diagnostics . The_current_host_does_not_support_the_0_option , "--locale" ) , /* host */ undefined ) ;
276
271
return sys . exit ( ExitStatus . DiagnosticsPresent_OutputsSkipped ) ;
277
272
}
278
273
validateLocaleAndSetLanguage ( commandLine . options . locale , commandLine . errors ) ;
@@ -303,26 +298,26 @@ namespace ts {
303
298
304
299
if ( commandLine . options . project ) {
305
300
if ( ! isJSONSupported ( ) ) {
306
- reportDiagnostic ( createCompilerDiagnostic ( Diagnostics . The_current_host_does_not_support_the_0_option , "--project" ) , /* compilerHost */ undefined ) ;
301
+ reportDiagnostic ( createCompilerDiagnostic ( Diagnostics . The_current_host_does_not_support_the_0_option , "--project" ) , /* host */ undefined ) ;
307
302
return sys . exit ( ExitStatus . DiagnosticsPresent_OutputsSkipped ) ;
308
303
}
309
304
if ( commandLine . fileNames . length !== 0 ) {
310
- reportDiagnostic ( createCompilerDiagnostic ( Diagnostics . Option_project_cannot_be_mixed_with_source_files_on_a_command_line ) , /* compilerHost */ undefined ) ;
305
+ reportDiagnostic ( createCompilerDiagnostic ( Diagnostics . Option_project_cannot_be_mixed_with_source_files_on_a_command_line ) , /* host */ undefined ) ;
311
306
return sys . exit ( ExitStatus . DiagnosticsPresent_OutputsSkipped ) ;
312
307
}
313
308
314
309
const fileOrDirectory = normalizePath ( commandLine . options . project ) ;
315
310
if ( ! fileOrDirectory /* current directory "." */ || sys . directoryExists ( fileOrDirectory ) ) {
316
311
configFileName = combinePaths ( fileOrDirectory , "tsconfig.json" ) ;
317
312
if ( ! sys . fileExists ( configFileName ) ) {
318
- reportDiagnostic ( createCompilerDiagnostic ( Diagnostics . Cannot_find_a_tsconfig_json_file_at_the_specified_directory_Colon_0 , commandLine . options . project ) , /* compilerHost */ undefined ) ;
313
+ reportDiagnostic ( createCompilerDiagnostic ( Diagnostics . Cannot_find_a_tsconfig_json_file_at_the_specified_directory_Colon_0 , commandLine . options . project ) , /* host */ undefined ) ;
319
314
return sys . exit ( ExitStatus . DiagnosticsPresent_OutputsSkipped ) ;
320
315
}
321
316
}
322
317
else {
323
318
configFileName = fileOrDirectory ;
324
319
if ( ! sys . fileExists ( configFileName ) ) {
325
- reportDiagnostic ( createCompilerDiagnostic ( Diagnostics . The_specified_path_does_not_exist_Colon_0 , commandLine . options . project ) , /* compilerHost */ undefined ) ;
320
+ reportDiagnostic ( createCompilerDiagnostic ( Diagnostics . The_specified_path_does_not_exist_Colon_0 , commandLine . options . project ) , /* host */ undefined ) ;
326
321
return sys . exit ( ExitStatus . DiagnosticsPresent_OutputsSkipped ) ;
327
322
}
328
323
}
@@ -340,7 +335,7 @@ namespace ts {
340
335
341
336
if ( isWatchSet ( commandLine . options ) ) {
342
337
if ( ! sys . watchFile ) {
343
- reportDiagnostic ( createCompilerDiagnostic ( Diagnostics . The_current_host_does_not_support_the_0_option , "--watch" ) , /* compilerHost */ undefined ) ;
338
+ reportDiagnostic ( createCompilerDiagnostic ( Diagnostics . The_current_host_does_not_support_the_0_option , "--watch" ) , /* host */ undefined ) ;
344
339
return sys . exit ( ExitStatus . DiagnosticsPresent_OutputsSkipped ) ;
345
340
}
346
341
if ( configFileName ) {
@@ -393,7 +388,7 @@ namespace ts {
393
388
}
394
389
if ( isWatchSet ( configParseResult . options ) ) {
395
390
if ( ! sys . watchFile ) {
396
- reportDiagnostic ( createCompilerDiagnostic ( Diagnostics . The_current_host_does_not_support_the_0_option , "--watch" ) , /* compilerHost */ undefined ) ;
391
+ reportDiagnostic ( createCompilerDiagnostic ( Diagnostics . The_current_host_does_not_support_the_0_option , "--watch" ) , /* host */ undefined ) ;
397
392
sys . exit ( ExitStatus . DiagnosticsPresent_OutputsSkipped ) ;
398
393
}
399
394
@@ -432,7 +427,7 @@ namespace ts {
432
427
}
433
428
434
429
if ( compilerOptions . pretty ) {
435
- reportDiagnostic = reportDiagnosticWithColorAndContext ;
430
+ reportDiagnosticWorker = reportDiagnosticWithColorAndContext ;
436
431
}
437
432
438
433
// reset the cache of existing files
@@ -757,7 +752,7 @@ namespace ts {
757
752
const currentDirectory = sys . getCurrentDirectory ( ) ;
758
753
const file = normalizePath ( combinePaths ( currentDirectory , "tsconfig.json" ) ) ;
759
754
if ( sys . fileExists ( file ) ) {
760
- reportDiagnostic ( createCompilerDiagnostic ( Diagnostics . A_tsconfig_json_file_is_already_defined_at_Colon_0 , file ) , /* compilerHost */ undefined ) ;
755
+ reportDiagnostic ( createCompilerDiagnostic ( Diagnostics . A_tsconfig_json_file_is_already_defined_at_Colon_0 , file ) , /* host */ undefined ) ;
761
756
}
762
757
else {
763
758
const compilerOptions = extend ( options , defaultInitCompilerOptions ) ;
@@ -777,7 +772,7 @@ namespace ts {
777
772
}
778
773
779
774
sys . writeFile ( file , JSON . stringify ( configurations , undefined , 4 ) ) ;
780
- reportDiagnostic ( createCompilerDiagnostic ( Diagnostics . Successfully_created_a_tsconfig_json_file ) , /* compilerHost */ undefined ) ;
775
+ reportDiagnostic ( createCompilerDiagnostic ( Diagnostics . Successfully_created_a_tsconfig_json_file ) , /* host */ undefined ) ;
781
776
}
782
777
783
778
return ;
0 commit comments