| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| |
| |
| |
| |
| 'use strict'; |
|
|
| const ESC = '\u001B'; |
| const COLOR_END = ESC + '[0m'; |
| const COLOR_RED = ESC + '[91m'; |
| const COLOR_GREEN = ESC + '[92m'; |
| const COLOR_YELLOW = ESC + '[93m'; |
| const fs = require('fs'); |
|
|
| |
| |
| |
| |
| |
| function caretPrefix(line, charNo) { |
| return line.substr(0, charNo).replace(/[^\t]/g, ' '); |
| } |
|
|
|
|
| |
| |
| |
| |
| |
| |
| module.exports = function(output, inputFiles = [], logger = console.warn) { |
| |
| function fileFor(file) { |
| if (!file) { |
| return null; |
| } |
|
|
| const originalFile = inputFiles.find(inputFile => inputFile.path === file); |
| if (originalFile) { |
| return originalFile; |
| } |
|
|
| try { |
| return { |
| path: file, |
| src: fs.readFileSync(file, 'utf8') |
| } |
| } catch (e) { } |
| return null; |
| } |
|
|
| function writemsg(color, msg) { |
| if (!msg.file && msg.lineNo < 0) { |
| logger(msg.type); |
| } else { |
| logger(`${msg.file}:${msg.lineNo} (${msg.type})`) |
| } |
| logger(msg.description); |
|
|
| const file = fileFor(msg.file); |
| if (file) { |
| const lines = file.src.split('\n'); |
| const line = lines[msg.lineNo - 1] || ''; |
| logger(color + line + COLOR_END); |
| logger(COLOR_GREEN + caretPrefix(line, msg.charNo) + '^' + COLOR_END); |
| } |
| logger(''); |
| } |
|
|
| output.warnings.forEach(writemsg.bind(null, COLOR_YELLOW)); |
| output.errors.forEach(writemsg.bind(null, COLOR_RED)); |
|
|
| return output.errors.length > 0; |
| }; |
|
|