1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | const os = require("os");
|
4 | const codeFrame = require("babel-code-frame");
|
5 | const chalk_1 = require("chalk");
|
6 | const fs = require("fs");
|
7 | const NormalizedMessage_1 = require("../NormalizedMessage");
|
8 | const FsHelper_1 = require("../FsHelper");
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 | function createCodeframeFormatter(options) {
|
16 | return function codeframeFormatter(message, useColors) {
|
17 | const colors = new chalk_1.default.constructor({ enabled: useColors });
|
18 | const messageColor = message.isWarningSeverity()
|
19 | ? colors.bold.yellow
|
20 | : colors.bold.red;
|
21 | const positionColor = colors.dim;
|
22 | if (message.code === NormalizedMessage_1.NormalizedMessage.ERROR_CODE_INTERNAL) {
|
23 | return (messageColor(`INTERNAL ${message.severity.toUpperCase()}: `) +
|
24 | message.content +
|
25 | (message.stack
|
26 | ? os.EOL + 'stack trace:' + os.EOL + colors.gray(message.stack)
|
27 | : ''));
|
28 | }
|
29 | const file = message.file;
|
30 | const source = file && FsHelper_1.FsHelper.existsSync(file) && fs.readFileSync(file, 'utf-8');
|
31 | let frame = '';
|
32 | if (source) {
|
33 | frame = codeFrame(source, message.line,
|
34 | message.character, Object.assign({}, (options || {}), { highlightCode: useColors }))
|
35 | .split('\n')
|
36 | .map(str => ' ' + str)
|
37 | .join(os.EOL);
|
38 | }
|
39 | return (messageColor(message.severity.toUpperCase() + ' in ' + message.file) +
|
40 | os.EOL +
|
41 | positionColor(message.line + ':' + message.character) +
|
42 | ' ' +
|
43 | message.content +
|
44 | (frame ? os.EOL + frame : ''));
|
45 | };
|
46 | }
|
47 | exports.createCodeframeFormatter = createCodeframeFormatter;
|
48 |
|
\ | No newline at end of file |