statigen
Version:
A static site generator that supports html, ejs, and markdown source files
106 lines • 5.27 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.getDiagnosticSquigglyText = exports.printDiagnostic = exports.printDiagnostics = void 0;
const chalk = require("chalk");
const interfaces_1 = require("./interfaces");
function printDiagnostics(diagnostics) {
if ((diagnostics === null || diagnostics === void 0 ? void 0 : diagnostics.length) > 0) {
for (const diagnostic of diagnostics) {
printDiagnostic(diagnostic);
//separate each diagnostic with an additional blank line
console.log('');
}
}
}
exports.printDiagnostics = printDiagnostics;
function printDiagnostic(diagnostic) {
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l;
let typeColor = {
[interfaces_1.DiagnosticSeverity.Information]: chalk.blue,
[interfaces_1.DiagnosticSeverity.Hint]: chalk.green,
[interfaces_1.DiagnosticSeverity.Warning]: chalk.yellow,
[interfaces_1.DiagnosticSeverity.Error]: chalk.red
};
const typeColorFunc = (_a = typeColor[diagnostic.severity]) !== null && _a !== void 0 ? _a : ((text) => text);
console.log('');
console.log(chalk.cyan((_c = (_b = diagnostic.file) === null || _b === void 0 ? void 0 : _b.srcPath) !== null && _c !== void 0 ? _c : '<unknown file>') +
':' +
chalk.yellow(diagnostic.range
? (diagnostic.range.start.line + 1) + ':' + (diagnostic.range.start.character + 1)
: 'line?:col?') +
' - ' +
typeColorFunc(diagnostic.severity) +
' ' +
chalk.grey(diagnostic.code) +
': ' +
chalk.white(diagnostic.message));
console.log('');
//Get the line referenced by the diagnostic. if we couldn't find a line,
// default to an empty string so it doesn't crash the error printing below
let diagnosticLine = (_h = (_d = diagnostic.file) === null || _d === void 0 ? void 0 : _d.getLine((_g = (_f = (_e = diagnostic.range) === null || _e === void 0 ? void 0 : _e.start) === null || _f === void 0 ? void 0 : _f.line) !== null && _g !== void 0 ? _g : -1)) !== null && _h !== void 0 ? _h : '';
let squigglyText = getDiagnosticSquigglyText(diagnostic, diagnosticLine);
//only print the line information if we have some
if (diagnostic.range && diagnosticLine) {
let lineNumberText = chalk.bgWhite(' ' + chalk.black((diagnostic.range.start.line + 1).toString()) + ' ') + ' ';
let blankLineNumberText = chalk.bgWhite(' ' + chalk.bgWhite((diagnostic.range.start.line + 1).toString()) + ' ') + ' ';
console.log(lineNumberText + diagnosticLine);
console.log(blankLineNumberText + typeColor[diagnostic.severity](squigglyText));
}
for (const info of (_j = diagnostic.relatedInformation) !== null && _j !== void 0 ? _j : []) {
console.log('');
console.log(' ' + chalk.yellow(info.message));
console.log(' ' +
chalk.cyan((_l = (_k = info.file) === null || _k === void 0 ? void 0 : _k.srcPath) !== null && _l !== void 0 ? _l : '<unknown file>') +
chalk.yellow(info.range ? `:${info.range.start.line + 1}:${info.range.start.character + 1}` : ''));
}
}
exports.printDiagnostic = printDiagnostic;
/**
* Given a diagnostic, compute the range for the squiggly
*/
function getDiagnosticSquigglyText(diagnostic, line) {
var _a, _b;
let squiggle;
//fill the entire line
if (
//there is no range
!diagnostic.range ||
//there is no line
!line ||
//both positions point to same location
diagnostic.range.start.character === diagnostic.range.end.character ||
//the diagnostic starts after the end of the line
diagnostic.range.start.character >= line.length) {
squiggle = ''.padStart((_a = line === null || line === void 0 ? void 0 : line.length) !== null && _a !== void 0 ? _a : 0, '~');
}
else {
let endIndex = Math.max((_b = diagnostic.range) === null || _b === void 0 ? void 0 : _b.end.character, line.length);
endIndex = endIndex > 0 ? endIndex : 0;
if ((line === null || line === void 0 ? void 0 : line.length) < endIndex) {
endIndex = line.length;
}
let leadingWhitespaceLength = diagnostic.range.start.character;
let squiggleLength;
if (diagnostic.range.end.character === Number.MAX_VALUE) {
squiggleLength = line.length - leadingWhitespaceLength;
}
else {
squiggleLength = diagnostic.range.end.character - diagnostic.range.start.character;
}
let trailingWhitespaceLength = endIndex - diagnostic.range.end.character;
//opening whitespace
squiggle =
''.padStart(leadingWhitespaceLength, ' ') +
//squiggle
''.padStart(squiggleLength, '~') +
//trailing whitespace
''.padStart(trailingWhitespaceLength, ' ');
//trim the end of the squiggle so it doesn't go longer than the end of the line
if (squiggle.length > endIndex) {
squiggle = squiggle.slice(0, endIndex);
}
}
return squiggle;
}
exports.getDiagnosticSquigglyText = getDiagnosticSquigglyText;
//# sourceMappingURL=diagnosticUtils.js.map
;