fix(compiler-cli): diagnostics should respect "newLine" compiler option (#28550)
PR Close #28550
This commit is contained in:

committed by
Matias Niemelä

parent
beba944843
commit
ce750e6f5b
@ -166,17 +166,32 @@ export function readCommandLineAndConfiguration(
|
||||
};
|
||||
}
|
||||
|
||||
function getFormatDiagnosticsHost(options?: api.CompilerOptions): ts.FormatDiagnosticsHost {
|
||||
const basePath = options ? options.basePath : undefined;
|
||||
return {
|
||||
getCurrentDirectory: () => basePath || ts.sys.getCurrentDirectory(),
|
||||
// We need to normalize the path separators here because by default, TypeScript
|
||||
// compiler hosts use posix canonical paths. In order to print consistent diagnostics,
|
||||
// we also normalize the paths.
|
||||
getCanonicalFileName: fileName => fileName.replace(/\\/g, '/'),
|
||||
getNewLine: () => {
|
||||
// Manually determine the proper new line string based on the passed compiler
|
||||
// options. There is no public TypeScript function that returns the corresponding
|
||||
// new line string. see: https://github.com/Microsoft/TypeScript/issues/29581
|
||||
if (options && options.newLine !== undefined) {
|
||||
return options.newLine === ts.NewLineKind.LineFeed ? '\n' : '\r\n';
|
||||
}
|
||||
return ts.sys.newLine;
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
function reportErrorsAndExit(
|
||||
allDiagnostics: Diagnostics, options?: api.CompilerOptions,
|
||||
consoleError: (s: string) => void = console.error): number {
|
||||
const errorsAndWarnings = filterErrorsAndWarnings(allDiagnostics);
|
||||
if (errorsAndWarnings.length) {
|
||||
let currentDir = options ? options.basePath : undefined;
|
||||
const formatHost: ts.FormatDiagnosticsHost = {
|
||||
getCurrentDirectory: () => currentDir || ts.sys.getCurrentDirectory(),
|
||||
getCanonicalFileName: fileName => fileName,
|
||||
getNewLine: () => ts.sys.newLine
|
||||
};
|
||||
const formatHost = getFormatDiagnosticsHost(options);
|
||||
if (options && (options.enableIvy === true || options.enableIvy === 'ngtsc')) {
|
||||
const ngDiagnostics = errorsAndWarnings.filter(api.isNgDiagnostic);
|
||||
const tsDiagnostics = errorsAndWarnings.filter(api.isTsDiagnostic);
|
||||
@ -193,7 +208,7 @@ function reportErrorsAndExit(
|
||||
export function watchMode(
|
||||
project: string, options: api.CompilerOptions, consoleError: (s: string) => void) {
|
||||
return performWatchCompilation(createPerformWatchHost(project, diagnostics => {
|
||||
consoleError(formatDiagnostics(diagnostics));
|
||||
consoleError(formatDiagnostics(diagnostics, getFormatDiagnosticsHost(options)));
|
||||
}, options, options => createEmitCallback(options)));
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user