mirror of
https://github.com/ezolenko/rollup-plugin-typescript2.git
synced 2025-12-08 19:06:16 +00:00
* refactor: consolidate `diagnostics` funcs into single file
- move `IDiagnostic` and `convertDiagnostic` from `tscache` to `print-diagnostics`, then rename it to `diagnostics.ts`
- the diagnostic funcs being in `tscache` always felt like a strange place for them
- especially when `parse-tsconfig` or `print-diagnostics` would import them from `tscache`, which sounded like an unrelated file
- may want to move `diagnostics-format-host` into this file as well, as it is _only_ used in this file
- leaving as is for now, limiting this change to a smaller one
* test: add unit test for `convertDiagnostic`
- this was previously only covered in integration tests
- since unit tests don't currently touch `index` or `tscache`, and `convertDiagnostic` was previously in `tscache`
- another reason why consolidating these functions into one file made sense
* fix(diagnostics): use `formatHost.getNewLine()` instead of `\n`
- since new lines are host OS specific
- this fixes the `convertDiagnostic` test on Windows too, where it was failing
78 lines
2.0 KiB
TypeScript
78 lines
2.0 KiB
TypeScript
import * as tsTypes from "typescript";
|
|
import { red, white, yellow } from "colors/safe";
|
|
|
|
import { tsModule } from "./tsproxy";
|
|
import { RollupContext } from "./context";
|
|
import { formatHost } from "./diagnostics-format-host";
|
|
|
|
export interface IDiagnostics
|
|
{
|
|
flatMessage: string;
|
|
formatted: string;
|
|
fileLine?: string;
|
|
category: tsTypes.DiagnosticCategory;
|
|
code: number;
|
|
type: string;
|
|
}
|
|
|
|
export function convertDiagnostic(type: string, data: tsTypes.Diagnostic[]): IDiagnostics[]
|
|
{
|
|
return data.map((diagnostic) =>
|
|
{
|
|
const entry: IDiagnostics = {
|
|
flatMessage: tsModule.flattenDiagnosticMessageText(diagnostic.messageText, formatHost.getNewLine()),
|
|
formatted: tsModule.formatDiagnosticsWithColorAndContext(data, formatHost),
|
|
category: diagnostic.category,
|
|
code: diagnostic.code,
|
|
type,
|
|
};
|
|
|
|
if (diagnostic.file && diagnostic.start !== undefined)
|
|
{
|
|
const { line, character } = diagnostic.file.getLineAndCharacterOfPosition(diagnostic.start);
|
|
entry.fileLine = `${diagnostic.file.fileName}(${line + 1},${character + 1})`;
|
|
}
|
|
|
|
return entry;
|
|
});
|
|
}
|
|
|
|
export function printDiagnostics(context: RollupContext, diagnostics: IDiagnostics[], pretty = true): void
|
|
{
|
|
diagnostics.forEach((diagnostic) =>
|
|
{
|
|
let print;
|
|
let color;
|
|
let category;
|
|
switch (diagnostic.category)
|
|
{
|
|
case tsModule.DiagnosticCategory.Message:
|
|
print = context.info;
|
|
color = white;
|
|
category = "";
|
|
break;
|
|
case tsModule.DiagnosticCategory.Error:
|
|
print = context.error;
|
|
color = red;
|
|
category = "error";
|
|
break;
|
|
case tsModule.DiagnosticCategory.Warning:
|
|
default:
|
|
print = context.warn;
|
|
color = yellow;
|
|
category = "warning";
|
|
break;
|
|
}
|
|
|
|
const type = diagnostic.type + " ";
|
|
|
|
if (pretty)
|
|
return print.call(context, `${diagnostic.formatted}`);
|
|
|
|
if (diagnostic.fileLine !== undefined)
|
|
return print.call(context, `${diagnostic.fileLine}: ${type}${category} TS${diagnostic.code}: ${color(diagnostic.flatMessage)}`);
|
|
|
|
return print.call(context, `${type}${category} TS${diagnostic.code}: ${color(diagnostic.flatMessage)}`);
|
|
});
|
|
}
|