/
formatDiagnosticMessage.ts
101 lines (98 loc) 路 3.61 KB
/
formatDiagnosticMessage.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import { inlineCodeBlock, unStyledCodeBlock } from "../components";
import { formatTypeBlock } from "./formatTypeBlock";
const formatTypeScriptBlock = (_: string, code: string) =>
inlineCodeBlock(code, "typescript");
const formatSimpleTypeBlock = (_: string, code: string) =>
inlineCodeBlock(code, "type");
export const formatDiagnosticMessage = (
message: string,
format: (type: string) => string
) =>
message
.replaceAll(/(?:\s)'"(.*?)(?<!\\)"'(?:\s|\:|.|$)/g, (_, p1: string) =>
formatTypeBlock("", `"${p1}"`, format)
)
// format declare module snippet
.replaceAll(
/['鈥淽(declare module )['鈥漖(.*)['鈥淽;['鈥漖/g,
(_: string, p1: string, p2: string) =>
formatTypeScriptBlock(_, `${p1} "${p2}"`)
)
// format missing props error
.replaceAll(
/(is missing the following properties from type\s?)'(.*)': ((?:#?\w+, )*(?:(?!and)\w+)?)/g,
(_, pre, type, post) =>
`${pre}${formatTypeBlock("", type, format)}: <ul>${post
.split(", ")
.filter(Boolean)
.map((prop: string) => `<li>${prop}</li>`)
.join("")}</ul>`
)
// Format type pairs
.replaceAll(
/(types) ['鈥淽(.*?)['鈥漖 and ['鈥淽(.*?)['鈥漖[\.]?/gi,
(_: string, p1: string, p2: string, p3: string) =>
`${formatTypeBlock(p1, p2, format)} and ${formatTypeBlock(
"",
p3,
format
)}`
)
// Format type annotation options
.replaceAll(
/type annotation must be ['鈥淽(.*?)['鈥漖 or ['鈥淽(.*?)['鈥漖[\.]?/gi,
(_: string, p1: string, p2: string, p3: string) =>
`${formatTypeBlock(p1, p2, format)} or ${formatTypeBlock(
"",
p3,
format
)}`
)
.replaceAll(
/(Overload \d of \d), ['鈥淽(.*?)['鈥漖, /gi,
(_, p1: string, p2: string) => `${p1}${formatTypeBlock("", p2, format)}`
)
// format simple strings
.replaceAll(/^['鈥淽"[^"]*"['鈥漖$/g, formatTypeScriptBlock)
// Replace module 'x' by module "x" for ts error #2307
.replaceAll(
/(module )'([^"]*?)'/gi,
(_, p1: string, p2: string) => `${p1}"${p2}"`
)
// Format string types
.replaceAll(
/(module|file|file name) "(.*?)"(?=[\s(.|,)])/gi,
(_, p1: string, p2: string) => formatTypeBlock(p1, `"${p2}"`, format)
)
// Format types
.replaceAll(
/(type|type alias|interface|module|file|file name|method's|subtype of constraint) ['鈥淽(.*?)['鈥淽(?=[\s(.|,)]|$)/gi,
(_, p1: string, p2: string) => formatTypeBlock(p1, p2, format)
)
// Format reversed types
.replaceAll(
/(.*)['鈥淽([^>]*)['鈥漖 (type|interface|return type|file|module|is (not )?assignable)/gi,
(_: string, p1: string, p2: string, p3: string) =>
`${p1}${formatTypeBlock("", p2, format)} ${p3}`
)
// Format simple types that didn't captured before
.replaceAll(
/['鈥淽((void|null|undefined|any|boolean|string|number|bigint|symbol)(\[\])?)['鈥漖/g,
formatSimpleTypeBlock
)
// Format some typescript key words
.replaceAll(
/['鈥淽(import|export|require|in|continue|break|let|false|true|const|new|throw|await|for await|[0-9]+)( ?.*?)['鈥漖/g,
(_: string, p1: string, p2: string) =>
formatTypeScriptBlock(_, `${p1}${p2}`)
)
// Format return values
.replaceAll(
/(return|operator) ['鈥淽(.*?)['鈥漖/gi,
(_, p1: string, p2: string) => `${p1} ${formatTypeScriptBlock("", p2)}`
)
// Format regular code blocks
.replaceAll(
/(?<!.*?")(?:^|\s)['鈥淽((?:(?!:\s*}).)*?)['鈥淽(?!\s*:)(?!.*?")/g,
(_: string, p1: string) => ` ${unStyledCodeBlock(p1)} `
);