/
diagnostic.js
337 lines (299 loc) Β· 9.34 KB
/
diagnostic.js
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
// @flow strict-local
import invariant from 'assert';
import nullthrows from 'nullthrows';
import jsonMap, {type Mapping} from 'json-source-map';
/** These positions are 1-based (so <code>1</code> is the first line/column) */
export type DiagnosticHighlightLocation = {|
+line: number,
+column: number,
|};
export type DiagnosticSeverity = 'error' | 'warn' | 'info';
/**
* Note: A tab character is always counted as a single character
* This is to prevent any mismatch of highlighting across machines
*/
export type DiagnosticCodeHighlight = {|
/** Location of the first character that should get highlighted for this highlight. */
start: DiagnosticHighlightLocation,
/** Location of the last character that should get highlighted for this highlight. */
end: DiagnosticHighlightLocation,
/** A message that should be displayed at this location in the code (optional). */
message?: string,
|};
/**
* Describes how to format a code frame.
* A code frame is a visualization of a piece of code with a certain amount of
* code highlights that point to certain chunk(s) inside the code.
*/
export type DiagnosticCodeFrame = {|
/**
* The contents of the source file.
*
* If no code is passed, it will be read in from filePath, remember that
* the asset's current code could be different from the input contents.
*
*/
code?: string,
/** Path to the file this code frame is about (optional, absolute or relative to the project root) */
filePath?: string,
/** Language of the file this code frame is about (optional) */
language?: string,
codeHighlights: Array<DiagnosticCodeHighlight>,
|};
/**
* A style agnostic way of emitting errors, warnings and info.
* Reporters are responsible for rendering the message, codeframes, hints, ...
*/
export type Diagnostic = {|
/** This is the message you want to log. */
message: string,
/** Name of plugin or file that threw this error */
origin?: string,
/** A stacktrace of the error (optional) */
stack?: string,
/** Name of the error (optional) */
name?: string,
/** A code frame points to a certain location(s) in the file this diagnostic is linked to (optional) */
codeFrames?: ?Array<DiagnosticCodeFrame>,
/** An optional list of strings that suggest ways to resolve this issue */
hints?: Array<string>,
/** @private */
skipFormatting?: boolean,
/** A URL to documentation to learn more about the diagnostic. */
documentationURL?: string,
|};
// This type should represent all error formats Parcel can encounter...
export interface PrintableError extends Error {
fileName?: string;
filePath?: string;
codeFrame?: string;
highlightedCodeFrame?: string;
loc?: ?{
column: number,
line: number,
...
};
source?: string;
}
export type DiagnosticWithoutOrigin = {|
...Diagnostic,
origin?: string,
|};
/** Something that can be turned into a diagnostic. */
export type Diagnostifiable =
| Diagnostic
| Array<Diagnostic>
| ThrowableDiagnostic
| PrintableError
| Error
| string;
/** Normalize the given value into a diagnostic. */
export function anyToDiagnostic(input: Diagnostifiable): Array<Diagnostic> {
if (Array.isArray(input)) {
return input;
} else if (input instanceof ThrowableDiagnostic) {
return input.diagnostics;
} else if (input instanceof Error) {
return errorToDiagnostic(input);
} else if (typeof input === 'string') {
return [{message: input}];
} else if (typeof input === 'object') {
return [input];
} else {
return errorToDiagnostic(input);
}
}
/** Normalize the given error into a diagnostic. */
export function errorToDiagnostic(
error: ThrowableDiagnostic | PrintableError | string,
defaultValues?: {|
origin?: ?string,
filePath?: ?string,
|},
): Array<Diagnostic> {
let codeFrames: ?Array<DiagnosticCodeFrame> = undefined;
if (typeof error === 'string') {
return [
{
origin: defaultValues?.origin ?? 'Error',
message: escapeMarkdown(error),
},
];
}
if (error instanceof ThrowableDiagnostic) {
return error.diagnostics.map(d => {
return {
...d,
origin: d.origin ?? defaultValues?.origin ?? 'unknown',
};
});
}
if (error.loc && error.source != null) {
codeFrames = [
{
filePath:
error.filePath ??
error.fileName ??
defaultValues?.filePath ??
undefined,
code: error.source,
codeHighlights: [
{
start: {
line: error.loc.line,
column: error.loc.column,
},
end: {
line: error.loc.line,
column: error.loc.column,
},
},
],
},
];
}
return [
{
origin: defaultValues?.origin ?? 'Error',
message: escapeMarkdown(error.message),
name: error.name,
stack:
codeFrames == null
? error.highlightedCodeFrame ?? error.codeFrame ?? error.stack
: undefined,
codeFrames,
},
];
}
type ThrowableDiagnosticOpts = {
diagnostic: Diagnostic | Array<Diagnostic>,
...
};
/**
* An error wrapper around a diagnostic that can be <code>throw</code>n (e.g. to signal a
* build error).
*/
export default class ThrowableDiagnostic extends Error {
diagnostics: Array<Diagnostic>;
constructor(opts: ThrowableDiagnosticOpts) {
let diagnostics = Array.isArray(opts.diagnostic)
? opts.diagnostic
: [opts.diagnostic];
// Construct error from diagnostics
super(diagnostics[0].message);
// @ts-ignore
this.stack = diagnostics[0].stack ?? super.stack;
// @ts-ignore
this.name = diagnostics[0].name ?? super.name;
this.diagnostics = diagnostics;
}
}
/**
* Turns a list of positions in a JSON file with messages into a list of diagnostics.
* Uses <a href="https://github.com/epoberezkin/json-source-map">epoberezkin/json-source-map</a>.
*
* @param code the JSON code
* @param ids A list of JSON keypaths (<code>key: "/some/parent/child"</code>) with corresponding messages, \
* <code>type</code> signifies whether the key of the value in a JSON object should be highlighted.
*/
export function generateJSONCodeHighlights(
data:
| string
| {|
data: mixed,
pointers: {|[key: string]: Mapping|},
|},
ids: Array<{|key: string, type?: ?'key' | 'value', message?: string|}>,
): Array<DiagnosticCodeHighlight> {
// json-source-map doesn't support a tabWidth option (yet)
let map =
typeof data == 'string' ? jsonMap.parse(data.replace(/\t/g, ' ')) : data;
return ids.map(({key, type, message}) => {
let pos = nullthrows(map.pointers[key]);
return {
...getJSONSourceLocation(pos, type),
message,
};
});
}
/**
* Converts entries in <a href="https://github.com/epoberezkin/json-source-map">epoberezkin/json-source-map</a>'s
* <code>result.pointers</code> array.
*/
export function getJSONSourceLocation(
pos: Mapping,
type?: ?'key' | 'value',
): {|
start: DiagnosticHighlightLocation,
end: DiagnosticHighlightLocation,
|} {
if (!type && pos.key && pos.value) {
// key and value
return {
start: {line: pos.key.line + 1, column: pos.key.column + 1},
end: {line: pos.valueEnd.line + 1, column: pos.valueEnd.column},
};
} else if (type == 'key' || !pos.value) {
invariant(pos.key && pos.keyEnd);
return {
start: {line: pos.key.line + 1, column: pos.key.column + 1},
end: {line: pos.keyEnd.line + 1, column: pos.keyEnd.column},
};
} else {
return {
start: {line: pos.value.line + 1, column: pos.value.column + 1},
end: {line: pos.valueEnd.line + 1, column: pos.valueEnd.column},
};
}
}
/** Sanitizes object keys before using them as <code>key</code> in generateJSONCodeHighlights */
export function encodeJSONKeyComponent(component: string): string {
return component.replace(/\//g, '~1');
}
const escapeCharacters = ['\\', '*', '_', '~'];
export function escapeMarkdown(s: string): string {
let result = s;
for (const char of escapeCharacters) {
result = result.replace(new RegExp(`\\${char}`, 'g'), `\\${char}`);
}
return result;
}
type TemplateInput = $FlowFixMe;
const mdVerbatim = Symbol();
export function md(
strings: Array<string>,
...params: Array<TemplateInput>
): string {
let result = [];
for (let i = 0; i < params.length; i++) {
result.push(strings[i]);
let param = params[i];
if (Array.isArray(param)) {
for (let j = 0; j < param.length; j++) {
result.push(param[j]?.[mdVerbatim] ?? escapeMarkdown(`${param[j]}`));
if (j < param.length - 1) {
result.push(', ');
}
}
} else {
result.push(param?.[mdVerbatim] ?? escapeMarkdown(`${param}`));
}
}
return result.join('') + strings[strings.length - 1];
}
md.bold = function (s: TemplateInput): TemplateInput {
// $FlowFixMe[invalid-computed-prop]
return {[mdVerbatim]: '**' + escapeMarkdown(`${s}`) + '**'};
};
md.italic = function (s: TemplateInput): TemplateInput {
// $FlowFixMe[invalid-computed-prop]
return {[mdVerbatim]: '_' + escapeMarkdown(`${s}`) + '_'};
};
md.underline = function (s: TemplateInput): TemplateInput {
// $FlowFixMe[invalid-computed-prop]
return {[mdVerbatim]: '__' + escapeMarkdown(`${s}`) + '__'};
};
md.strikethrough = function (s: TemplateInput): TemplateInput {
// $FlowFixMe[invalid-computed-prop]
return {[mdVerbatim]: '~~' + escapeMarkdown(`${s}`) + '~~'};
};