Skip to content

Commit ba26293

Browse files
authored
refactor: consolidate diagnostics funcs into single file (#416)
* 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
1 parent e98e0ed commit ba26293

6 files changed

+60
-45
lines changed

CONTRIBUTING.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,5 +73,5 @@ A useful resource as you dive deeper are the [unit tests](__tests__/). They're g
7373
1. At this point, you may be ready to read the more complicated bits of [`index`](src/index.ts) in detail and see how it interacts with the other modules.
7474
- The [integration tests](__tests__/integration/) could be useful to review at this point as well.
7575
1. Once you're pretty familiar with `index`, you can dive into some of the cache code in [`tscache`](src/tscache.ts) and [`rollingcache`](src/rollingcache.ts).
76-
1. And finally, you can see some of the Rollup logging nuances in [`context`](src/context.ts) and then the TS logging nuances in [`print-diagnostics`](src/print-diagnostics.ts), and [`diagnostics-format-host`](src/diagnostics-format-host.ts)
76+
1. And finally, you can see some of the Rollup logging nuances in [`context`](src/context.ts) and then the TS logging nuances in [`diagnostics`](src/diagnostics.ts), and [`diagnostics-format-host`](src/diagnostics-format-host.ts)
7777
- While these are necessary to the implementation, they are fairly ancillary to understanding and working with the codebase.

__tests__/print-diagnostics.spec.ts __tests__/diagnostics.spec.ts

+21-5
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,35 @@ import { red } from "colors/safe";
44

55
import { makeContext } from "./fixtures/context";
66
import { setTypescriptModule } from "../src/tsproxy";
7-
import { printDiagnostics } from "../src/print-diagnostics";
7+
import { formatHost } from "../src/diagnostics-format-host";
8+
import { convertDiagnostic, printDiagnostics } from "../src/diagnostics";
89

910
setTypescriptModule(ts);
1011

12+
const tsDiagnostic = {
13+
file: undefined,
14+
start: undefined,
15+
length: undefined,
16+
messageText: "Compiler option 'include' requires a value of type Array.",
17+
category: ts.DiagnosticCategory.Error,
18+
code: 5024,
19+
reportsUnnecessary: undefined,
20+
reportsDeprecated: undefined,
21+
};
22+
1123
const diagnostic = {
1224
flatMessage: "Compiler option 'include' requires a value of type Array.",
13-
formatted: "\x1B[91merror\x1B[0m\x1B[90m TS5024: \x1B[0mCompiler option 'include' requires a value of type Array.\n",
25+
formatted: `\x1B[91merror\x1B[0m\x1B[90m TS5024: \x1B[0mCompiler option 'include' requires a value of type Array.${formatHost.getNewLine()}`,
1426
category: ts.DiagnosticCategory.Error,
1527
code: 5024,
16-
type: 'config'
28+
type: "config",
1729
};
1830

19-
test("print-diagnostics - categories", () => {
31+
test("convertDiagnostic", () => {
32+
expect(convertDiagnostic("config", [tsDiagnostic])).toStrictEqual([diagnostic]);
33+
});
34+
35+
test("printDiagnostics - categories", () => {
2036
const context = makeContext();
2137

2238
printDiagnostics(context, [diagnostic]);
@@ -38,7 +54,7 @@ test("print-diagnostics - categories", () => {
3854
expect(context.debug).toBeCalledTimes(0);
3955
});
4056

41-
test("print-diagnostics - formatting / style", () => {
57+
test("printDiagnostics - formatting / style", () => {
4258
const context = makeContext();
4359
const category = "error"; // string version
4460

src/print-diagnostics.ts src/diagnostics.ts

+34-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,41 @@
1+
import * as tsTypes from "typescript";
12
import { red, white, yellow } from "colors/safe";
23

34
import { tsModule } from "./tsproxy";
45
import { RollupContext } from "./context";
5-
import { IDiagnostics } from "./tscache";
6+
import { formatHost } from "./diagnostics-format-host";
7+
8+
export interface IDiagnostics
9+
{
10+
flatMessage: string;
11+
formatted: string;
12+
fileLine?: string;
13+
category: tsTypes.DiagnosticCategory;
14+
code: number;
15+
type: string;
16+
}
17+
18+
export function convertDiagnostic(type: string, data: tsTypes.Diagnostic[]): IDiagnostics[]
19+
{
20+
return data.map((diagnostic) =>
21+
{
22+
const entry: IDiagnostics = {
23+
flatMessage: tsModule.flattenDiagnosticMessageText(diagnostic.messageText, formatHost.getNewLine()),
24+
formatted: tsModule.formatDiagnosticsWithColorAndContext(data, formatHost),
25+
category: diagnostic.category,
26+
code: diagnostic.code,
27+
type,
28+
};
29+
30+
if (diagnostic.file && diagnostic.start !== undefined)
31+
{
32+
const { line, character } = diagnostic.file.getLineAndCharacterOfPosition(diagnostic.start);
33+
entry.fileLine = `${diagnostic.file.fileName}(${line + 1},${character + 1})`;
34+
}
35+
36+
return entry;
37+
});
38+
}
639

740
export function printDiagnostics(context: RollupContext, diagnostics: IDiagnostics[], pretty = true): void
841
{

src/index.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ import findCacheDir from "find-cache-dir";
77

88
import { RollupContext, VerbosityLevel } from "./context";
99
import { LanguageServiceHost } from "./host";
10-
import { TsCache, convertDiagnostic, convertEmitOutput, getAllReferences, ICode } from "./tscache";
10+
import { TsCache, convertEmitOutput, getAllReferences, ICode } from "./tscache";
1111
import { tsModule, setTypescriptModule } from "./tsproxy";
1212
import { IOptions } from "./ioptions";
1313
import { parseTsConfig } from "./parse-tsconfig";
14-
import { printDiagnostics } from "./print-diagnostics";
14+
import { convertDiagnostic, printDiagnostics } from "./diagnostics";
1515
import { TSLIB, TSLIB_VIRTUAL, tslibSource, tslibVersion } from "./tslib";
1616
import { createFilter } from "./get-options-overrides";
1717

src/parse-tsconfig.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ import * as _ from "lodash";
33

44
import { tsModule } from "./tsproxy";
55
import { RollupContext } from "./context";
6-
import { printDiagnostics } from "./print-diagnostics";
7-
import { convertDiagnostic } from "./tscache";
6+
import { convertDiagnostic, printDiagnostics } from "./diagnostics";
87
import { getOptionsOverrides } from "./get-options-overrides";
98
import { IOptions } from "./ioptions";
109

src/tscache.ts

+1-34
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { RollupContext } from "./context";
99
import { RollingCache } from "./rollingcache";
1010
import { ICache } from "./icache";
1111
import { tsModule } from "./tsproxy";
12-
import { formatHost } from "./diagnostics-format-host";
12+
import { IDiagnostics, convertDiagnostic } from "./diagnostics";
1313

1414
export interface ICode
1515
{
@@ -25,16 +25,6 @@ interface INodeLabel
2525
dirty: boolean;
2626
}
2727

28-
export interface IDiagnostics
29-
{
30-
flatMessage: string;
31-
formatted: string;
32-
fileLine?: string;
33-
category: tsTypes.DiagnosticCategory;
34-
code: number;
35-
type: string;
36-
}
37-
3828
interface ITypeSnapshot
3929
{
4030
id: string;
@@ -74,29 +64,6 @@ export function getAllReferences(importer: string, snapshot: tsTypes.IScriptSnap
7464
}));
7565
}
7666

77-
export function convertDiagnostic(type: string, data: tsTypes.Diagnostic[]): IDiagnostics[]
78-
{
79-
return data.map((diagnostic) =>
80-
{
81-
const entry: IDiagnostics =
82-
{
83-
flatMessage: tsModule.flattenDiagnosticMessageText(diagnostic.messageText, "\n"),
84-
formatted: tsModule.formatDiagnosticsWithColorAndContext(data, formatHost),
85-
category: diagnostic.category,
86-
code: diagnostic.code,
87-
type,
88-
};
89-
90-
if (diagnostic.file && diagnostic.start !== undefined)
91-
{
92-
const { line, character } = diagnostic.file.getLineAndCharacterOfPosition(diagnostic.start);
93-
entry.fileLine = `${diagnostic.file.fileName}(${line + 1},${character + 1})`;
94-
}
95-
96-
return entry;
97-
});
98-
}
99-
10067
export class TsCache
10168
{
10269
private cacheVersion = "9";

0 commit comments

Comments
 (0)