Merge pull request #9750 from Microsoft/fixFormatDiagnostics
use sys based host for formatting diagnostics
This commit is contained in:
commit
859bd1b823
|
@ -991,7 +991,13 @@ namespace ts {
|
||||||
return sortAndDeduplicateDiagnostics(diagnostics);
|
return sortAndDeduplicateDiagnostics(diagnostics);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function formatDiagnostics(diagnostics: Diagnostic[], host: CompilerHost): string {
|
export interface FormatDiagnosticsHost {
|
||||||
|
getCurrentDirectory(): string;
|
||||||
|
getCanonicalFileName(fileName: string): string;
|
||||||
|
getNewLine(): string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function formatDiagnostics(diagnostics: Diagnostic[], host: FormatDiagnosticsHost): string {
|
||||||
let output = "";
|
let output = "";
|
||||||
|
|
||||||
for (const diagnostic of diagnostics) {
|
for (const diagnostic of diagnostics) {
|
||||||
|
|
|
@ -6,9 +6,19 @@ namespace ts {
|
||||||
fileWatcher?: FileWatcher;
|
fileWatcher?: FileWatcher;
|
||||||
}
|
}
|
||||||
|
|
||||||
let reportDiagnostic = reportDiagnosticSimply;
|
const defaultFormatDiagnosticsHost: FormatDiagnosticsHost = {
|
||||||
|
getCurrentDirectory: () => sys.getCurrentDirectory(),
|
||||||
|
getNewLine: () => sys.newLine,
|
||||||
|
getCanonicalFileName: createGetCanonicalFileName(sys.useCaseSensitiveFileNames)
|
||||||
|
};
|
||||||
|
|
||||||
function reportDiagnostics(diagnostics: Diagnostic[], host: CompilerHost): void {
|
let reportDiagnosticWorker = reportDiagnosticSimply;
|
||||||
|
|
||||||
|
function reportDiagnostic(diagnostic: Diagnostic, host: FormatDiagnosticsHost) {
|
||||||
|
reportDiagnosticWorker(diagnostic, host || defaultFormatDiagnosticsHost);
|
||||||
|
}
|
||||||
|
|
||||||
|
function reportDiagnostics(diagnostics: Diagnostic[], host: FormatDiagnosticsHost): void {
|
||||||
for (const diagnostic of diagnostics) {
|
for (const diagnostic of diagnostics) {
|
||||||
reportDiagnostic(diagnostic, host);
|
reportDiagnostic(diagnostic, host);
|
||||||
}
|
}
|
||||||
|
@ -101,7 +111,7 @@ namespace ts {
|
||||||
return <string>diagnostic.messageText;
|
return <string>diagnostic.messageText;
|
||||||
}
|
}
|
||||||
|
|
||||||
function reportDiagnosticSimply(diagnostic: Diagnostic, host: CompilerHost): void {
|
function reportDiagnosticSimply(diagnostic: Diagnostic, host: FormatDiagnosticsHost): void {
|
||||||
sys.write(ts.formatDiagnostics([diagnostic], host));
|
sys.write(ts.formatDiagnostics([diagnostic], host));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -122,7 +132,7 @@ namespace ts {
|
||||||
return formatStyle + text + resetEscapeSequence;
|
return formatStyle + text + resetEscapeSequence;
|
||||||
}
|
}
|
||||||
|
|
||||||
function reportDiagnosticWithColorAndContext(diagnostic: Diagnostic, host: CompilerHost): void {
|
function reportDiagnosticWithColorAndContext(diagnostic: Diagnostic, host: FormatDiagnosticsHost): void {
|
||||||
let output = "";
|
let output = "";
|
||||||
|
|
||||||
if (diagnostic.file) {
|
if (diagnostic.file) {
|
||||||
|
@ -257,7 +267,7 @@ namespace ts {
|
||||||
|
|
||||||
if (commandLine.options.locale) {
|
if (commandLine.options.locale) {
|
||||||
if (!isJSONSupported()) {
|
if (!isJSONSupported()) {
|
||||||
reportDiagnostic(createCompilerDiagnostic(Diagnostics.The_current_host_does_not_support_the_0_option, "--locale"), /* compilerHost */ undefined);
|
reportDiagnostic(createCompilerDiagnostic(Diagnostics.The_current_host_does_not_support_the_0_option, "--locale"), /* host */ undefined);
|
||||||
return sys.exit(ExitStatus.DiagnosticsPresent_OutputsSkipped);
|
return sys.exit(ExitStatus.DiagnosticsPresent_OutputsSkipped);
|
||||||
}
|
}
|
||||||
validateLocaleAndSetLanguage(commandLine.options.locale, commandLine.errors);
|
validateLocaleAndSetLanguage(commandLine.options.locale, commandLine.errors);
|
||||||
|
@ -288,11 +298,11 @@ namespace ts {
|
||||||
|
|
||||||
if (commandLine.options.project) {
|
if (commandLine.options.project) {
|
||||||
if (!isJSONSupported()) {
|
if (!isJSONSupported()) {
|
||||||
reportDiagnostic(createCompilerDiagnostic(Diagnostics.The_current_host_does_not_support_the_0_option, "--project"), /* compilerHost */ undefined);
|
reportDiagnostic(createCompilerDiagnostic(Diagnostics.The_current_host_does_not_support_the_0_option, "--project"), /* host */ undefined);
|
||||||
return sys.exit(ExitStatus.DiagnosticsPresent_OutputsSkipped);
|
return sys.exit(ExitStatus.DiagnosticsPresent_OutputsSkipped);
|
||||||
}
|
}
|
||||||
if (commandLine.fileNames.length !== 0) {
|
if (commandLine.fileNames.length !== 0) {
|
||||||
reportDiagnostic(createCompilerDiagnostic(Diagnostics.Option_project_cannot_be_mixed_with_source_files_on_a_command_line), /* compilerHost */ undefined);
|
reportDiagnostic(createCompilerDiagnostic(Diagnostics.Option_project_cannot_be_mixed_with_source_files_on_a_command_line), /* host */ undefined);
|
||||||
return sys.exit(ExitStatus.DiagnosticsPresent_OutputsSkipped);
|
return sys.exit(ExitStatus.DiagnosticsPresent_OutputsSkipped);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -300,14 +310,14 @@ namespace ts {
|
||||||
if (!fileOrDirectory /* current directory "." */ || sys.directoryExists(fileOrDirectory)) {
|
if (!fileOrDirectory /* current directory "." */ || sys.directoryExists(fileOrDirectory)) {
|
||||||
configFileName = combinePaths(fileOrDirectory, "tsconfig.json");
|
configFileName = combinePaths(fileOrDirectory, "tsconfig.json");
|
||||||
if (!sys.fileExists(configFileName)) {
|
if (!sys.fileExists(configFileName)) {
|
||||||
reportDiagnostic(createCompilerDiagnostic(Diagnostics.Cannot_find_a_tsconfig_json_file_at_the_specified_directory_Colon_0, commandLine.options.project), /* compilerHost */ undefined);
|
reportDiagnostic(createCompilerDiagnostic(Diagnostics.Cannot_find_a_tsconfig_json_file_at_the_specified_directory_Colon_0, commandLine.options.project), /* host */ undefined);
|
||||||
return sys.exit(ExitStatus.DiagnosticsPresent_OutputsSkipped);
|
return sys.exit(ExitStatus.DiagnosticsPresent_OutputsSkipped);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
configFileName = fileOrDirectory;
|
configFileName = fileOrDirectory;
|
||||||
if (!sys.fileExists(configFileName)) {
|
if (!sys.fileExists(configFileName)) {
|
||||||
reportDiagnostic(createCompilerDiagnostic(Diagnostics.The_specified_path_does_not_exist_Colon_0, commandLine.options.project), /* compilerHost */ undefined);
|
reportDiagnostic(createCompilerDiagnostic(Diagnostics.The_specified_path_does_not_exist_Colon_0, commandLine.options.project), /* host */ undefined);
|
||||||
return sys.exit(ExitStatus.DiagnosticsPresent_OutputsSkipped);
|
return sys.exit(ExitStatus.DiagnosticsPresent_OutputsSkipped);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -325,7 +335,7 @@ namespace ts {
|
||||||
|
|
||||||
if (isWatchSet(commandLine.options)) {
|
if (isWatchSet(commandLine.options)) {
|
||||||
if (!sys.watchFile) {
|
if (!sys.watchFile) {
|
||||||
reportDiagnostic(createCompilerDiagnostic(Diagnostics.The_current_host_does_not_support_the_0_option, "--watch"), /* compilerHost */ undefined);
|
reportDiagnostic(createCompilerDiagnostic(Diagnostics.The_current_host_does_not_support_the_0_option, "--watch"), /* host */ undefined);
|
||||||
return sys.exit(ExitStatus.DiagnosticsPresent_OutputsSkipped);
|
return sys.exit(ExitStatus.DiagnosticsPresent_OutputsSkipped);
|
||||||
}
|
}
|
||||||
if (configFileName) {
|
if (configFileName) {
|
||||||
|
@ -378,7 +388,7 @@ namespace ts {
|
||||||
}
|
}
|
||||||
if (isWatchSet(configParseResult.options)) {
|
if (isWatchSet(configParseResult.options)) {
|
||||||
if (!sys.watchFile) {
|
if (!sys.watchFile) {
|
||||||
reportDiagnostic(createCompilerDiagnostic(Diagnostics.The_current_host_does_not_support_the_0_option, "--watch"), /* compilerHost */ undefined);
|
reportDiagnostic(createCompilerDiagnostic(Diagnostics.The_current_host_does_not_support_the_0_option, "--watch"), /* host */ undefined);
|
||||||
sys.exit(ExitStatus.DiagnosticsPresent_OutputsSkipped);
|
sys.exit(ExitStatus.DiagnosticsPresent_OutputsSkipped);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -417,7 +427,7 @@ namespace ts {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (compilerOptions.pretty) {
|
if (compilerOptions.pretty) {
|
||||||
reportDiagnostic = reportDiagnosticWithColorAndContext;
|
reportDiagnosticWorker = reportDiagnosticWithColorAndContext;
|
||||||
}
|
}
|
||||||
|
|
||||||
// reset the cache of existing files
|
// reset the cache of existing files
|
||||||
|
@ -742,7 +752,7 @@ namespace ts {
|
||||||
const currentDirectory = sys.getCurrentDirectory();
|
const currentDirectory = sys.getCurrentDirectory();
|
||||||
const file = normalizePath(combinePaths(currentDirectory, "tsconfig.json"));
|
const file = normalizePath(combinePaths(currentDirectory, "tsconfig.json"));
|
||||||
if (sys.fileExists(file)) {
|
if (sys.fileExists(file)) {
|
||||||
reportDiagnostic(createCompilerDiagnostic(Diagnostics.A_tsconfig_json_file_is_already_defined_at_Colon_0, file), /* compilerHost */ undefined);
|
reportDiagnostic(createCompilerDiagnostic(Diagnostics.A_tsconfig_json_file_is_already_defined_at_Colon_0, file), /* host */ undefined);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
const compilerOptions = extend(options, defaultInitCompilerOptions);
|
const compilerOptions = extend(options, defaultInitCompilerOptions);
|
||||||
|
@ -762,7 +772,7 @@ namespace ts {
|
||||||
}
|
}
|
||||||
|
|
||||||
sys.writeFile(file, JSON.stringify(configurations, undefined, 4));
|
sys.writeFile(file, JSON.stringify(configurations, undefined, 4));
|
||||||
reportDiagnostic(createCompilerDiagnostic(Diagnostics.Successfully_created_a_tsconfig_json_file), /* compilerHost */ undefined);
|
reportDiagnostic(createCompilerDiagnostic(Diagnostics.Successfully_created_a_tsconfig_json_file), /* host */ undefined);
|
||||||
}
|
}
|
||||||
|
|
||||||
return;
|
return;
|
||||||
|
|
Loading…
Reference in a new issue