UNPKG

897 BJavaScriptView Raw
1const fs = require('fs')
2const os = require('os')
3const chalk = require('chalk')
4const { codeFrameColumns } = require('@babel/code-frame')
5
6module.exports = codeFrameOptions => (error, colors) => {
7 const { file } = error
8 const source = file && fs.existsSync(file) && fs.readFileSync(file, 'utf-8')
9
10 let frame = ''
11
12 if (source) {
13 frame = codeFrameColumns(
14 source,
15 {
16 start: {
17 line: error.line,
18 column: error.character,
19 },
20 },
21 Object.assign({}, codeFrameOptions || {}, {
22 highlightCode: chalk.supportsColor,
23 })
24 )
25 }
26
27 return (
28 colors.dim(`${error.line}:${error.character} `) +
29 error.content +
30 colors.cyan(` (TS${error.code})`) +
31 os.EOL +
32 (frame ? os.EOL + frame : '')
33 )
34}