UNPKG

2.39 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.default = prettyDiagnostic;
7
8var _codeframe = _interopRequireDefault(require("@parcel/codeframe"));
9
10var _markdownAnsi = _interopRequireDefault(require("@parcel/markdown-ansi"));
11
12var _chalk = _interopRequireDefault(require("chalk"));
13
14var _path = _interopRequireDefault(require("path"));
15
16var _nullthrows = _interopRequireDefault(require("nullthrows"));
17
18function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
19
20async function prettyDiagnostic(diagnostic, options, terminalWidth) {
21 let {
22 origin,
23 message,
24 stack,
25 codeFrame,
26 hints,
27 filePath,
28 language,
29 skipFormatting
30 } = diagnostic;
31
32 if (filePath != null && options && !_path.default.isAbsolute(filePath)) {
33 filePath = _path.default.join(options.projectRoot, filePath);
34 }
35
36 let result = {
37 message: (0, _markdownAnsi.default)(`**${origin !== null && origin !== void 0 ? origin : 'unknown'}**: `) + (skipFormatting ? message : (0, _markdownAnsi.default)(message)),
38 stack: '',
39 codeframe: '',
40 hints: []
41 };
42
43 if (codeFrame !== undefined) {
44 var _codeFrame$code;
45
46 let highlights = Array.isArray(codeFrame.codeHighlights) ? codeFrame.codeHighlights : [codeFrame.codeHighlights];
47 let code = (_codeFrame$code = codeFrame.code) !== null && _codeFrame$code !== void 0 ? _codeFrame$code : options && (await options.inputFS.readFile((0, _nullthrows.default)(filePath), 'utf8'));
48
49 if (code != null) {
50 let formattedCodeFrame = (0, _codeframe.default)(code, highlights, {
51 useColor: true,
52 syntaxHighlighting: true,
53 language: // $FlowFixMe sketchy null checks do not matter here...
54 language || (filePath ? _path.default.extname(filePath).substr(1) : undefined),
55 terminalWidth
56 });
57 result.codeframe += typeof filePath !== 'string' ? '' : _chalk.default.underline(`${filePath}:${highlights[0].start.line}:${highlights[0].start.column}\n`);
58 result.codeframe += formattedCodeFrame;
59 }
60 }
61
62 if (stack != null) {
63 result.stack = stack;
64 } else if (filePath != null && result.codeframe == null) {
65 result.stack = filePath;
66 }
67
68 if (Array.isArray(hints) && hints.length) {
69 result.hints = hints.map(h => {
70 return (0, _markdownAnsi.default)(h);
71 });
72 }
73
74 return result;
75}
\No newline at end of file