Skip to content

Commit 2b8c1f9

Browse files
authored
Merge pull request #9764 from Microsoft/port-9750
Port #9750 into release 2.0
2 parents 5319fa3 + bf8937c commit 2b8c1f9

File tree

2 files changed

+31
-15
lines changed

2 files changed

+31
-15
lines changed

src/compiler/program.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -997,7 +997,13 @@ namespace ts {
997997
return sortAndDeduplicateDiagnostics(diagnostics);
998998
}
999999

1000-
export function formatDiagnostics(diagnostics: Diagnostic[], host: CompilerHost): string {
1000+
export interface FormatDiagnosticsHost {
1001+
getCurrentDirectory(): string;
1002+
getCanonicalFileName(fileName: string): string;
1003+
getNewLine(): string;
1004+
}
1005+
1006+
export function formatDiagnostics(diagnostics: Diagnostic[], host: FormatDiagnosticsHost): string {
10011007
let output = "";
10021008

10031009
for (const diagnostic of diagnostics) {

src/compiler/tsc.ts

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,19 @@ namespace ts {
66
fileWatcher?: FileWatcher;
77
}
88

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+
}
1020

11-
function reportDiagnostics(diagnostics: Diagnostic[], host: CompilerHost): void {
21+
function reportDiagnostics(diagnostics: Diagnostic[], host: FormatDiagnosticsHost): void {
1222
for (const diagnostic of diagnostics) {
1323
reportDiagnostic(diagnostic, host);
1424
}
@@ -101,7 +111,7 @@ namespace ts {
101111
return <string>diagnostic.messageText;
102112
}
103113

104-
function reportDiagnosticSimply(diagnostic: Diagnostic, host: CompilerHost): void {
114+
function reportDiagnosticSimply(diagnostic: Diagnostic, host: FormatDiagnosticsHost): void {
105115
sys.write(ts.formatDiagnostics([diagnostic], host));
106116
}
107117

@@ -122,7 +132,7 @@ namespace ts {
122132
return formatStyle + text + resetEscapeSequence;
123133
}
124134

125-
function reportDiagnosticWithColorAndContext(diagnostic: Diagnostic, host: CompilerHost): void {
135+
function reportDiagnosticWithColorAndContext(diagnostic: Diagnostic, host: FormatDiagnosticsHost): void {
126136
let output = "";
127137

128138
if (diagnostic.file) {
@@ -257,7 +267,7 @@ namespace ts {
257267

258268
if (commandLine.options.locale) {
259269
if (!isJSONSupported()) {
260-
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);
261271
return sys.exit(ExitStatus.DiagnosticsPresent_OutputsSkipped);
262272
}
263273
validateLocaleAndSetLanguage(commandLine.options.locale, commandLine.errors);
@@ -288,26 +298,26 @@ namespace ts {
288298

289299
if (commandLine.options.project) {
290300
if (!isJSONSupported()) {
291-
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);
292302
return sys.exit(ExitStatus.DiagnosticsPresent_OutputsSkipped);
293303
}
294304
if (commandLine.fileNames.length !== 0) {
295-
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);
296306
return sys.exit(ExitStatus.DiagnosticsPresent_OutputsSkipped);
297307
}
298308

299309
const fileOrDirectory = normalizePath(commandLine.options.project);
300310
if (!fileOrDirectory /* current directory "." */ || sys.directoryExists(fileOrDirectory)) {
301311
configFileName = combinePaths(fileOrDirectory, "tsconfig.json");
302312
if (!sys.fileExists(configFileName)) {
303-
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);
304314
return sys.exit(ExitStatus.DiagnosticsPresent_OutputsSkipped);
305315
}
306316
}
307317
else {
308318
configFileName = fileOrDirectory;
309319
if (!sys.fileExists(configFileName)) {
310-
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);
311321
return sys.exit(ExitStatus.DiagnosticsPresent_OutputsSkipped);
312322
}
313323
}
@@ -325,7 +335,7 @@ namespace ts {
325335

326336
if (isWatchSet(commandLine.options)) {
327337
if (!sys.watchFile) {
328-
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);
329339
return sys.exit(ExitStatus.DiagnosticsPresent_OutputsSkipped);
330340
}
331341
if (configFileName) {
@@ -378,7 +388,7 @@ namespace ts {
378388
}
379389
if (isWatchSet(configParseResult.options)) {
380390
if (!sys.watchFile) {
381-
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);
382392
sys.exit(ExitStatus.DiagnosticsPresent_OutputsSkipped);
383393
}
384394

@@ -417,7 +427,7 @@ namespace ts {
417427
}
418428

419429
if (compilerOptions.pretty) {
420-
reportDiagnostic = reportDiagnosticWithColorAndContext;
430+
reportDiagnosticWorker = reportDiagnosticWithColorAndContext;
421431
}
422432

423433
// reset the cache of existing files
@@ -742,7 +752,7 @@ namespace ts {
742752
const currentDirectory = sys.getCurrentDirectory();
743753
const file = normalizePath(combinePaths(currentDirectory, "tsconfig.json"));
744754
if (sys.fileExists(file)) {
745-
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);
746756
}
747757
else {
748758
const compilerOptions = extend(options, defaultInitCompilerOptions);
@@ -762,7 +772,7 @@ namespace ts {
762772
}
763773

764774
sys.writeFile(file, JSON.stringify(configurations, undefined, 4));
765-
reportDiagnostic(createCompilerDiagnostic(Diagnostics.Successfully_created_a_tsconfig_json_file), /* compilerHost */ undefined);
775+
reportDiagnostic(createCompilerDiagnostic(Diagnostics.Successfully_created_a_tsconfig_json_file), /* host */ undefined);
766776
}
767777

768778
return;

0 commit comments

Comments
 (0)