gherkin-lint-ts
Version:
Gherkin features linter written in Typescript
81 lines • 3.64 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.printResults = void 0;
const chalk_1 = __importDefault(require("chalk"));
const types_1 = require("../types");
const _ = require("lodash");
const terminal_link_1 = __importDefault(require("terminal-link"));
const style = {
gray: function (text) {
return chalk_1.default.gray(text);
},
underline: function (text) {
return chalk_1.default.underline(text);
},
};
function stylizeFilePath(filePath) {
return style.underline(filePath);
}
function stylizeError(filePath, error, maxLineLength, maxMessageLength, addColors) {
var _a;
const indent = " "; // indent 2 spaces so it looks pretty
const errorLinePadded = (_a = error.line) === null || _a === void 0 ? void 0 : _a.toString().padEnd(maxLineLength);
const errorLineStylized = addColors ? style.gray(errorLinePadded) : errorLinePadded;
const errorRuleStylized = addColors ? style.gray(error.rule) : error.rule;
const errorLevel = error.errorLevel === types_1.ErrorLevel.Error ? chalk_1.default.red("error") : chalk_1.default.yellow("warning");
const link = terminal_link_1.default(errorRuleStylized, `${filePath}:${error.line || 0}`);
return [indent, errorLineStylized, errorLevel, error.message.padEnd(maxMessageLength), link].join(indent);
}
function getMaxLineLength(result) {
var _a;
let length = 0;
(_a = result.errors) === null || _a === void 0 ? void 0 : _a.forEach(function (error) {
var _a;
const errorLength = ((_a = error.line) === null || _a === void 0 ? void 0 : _a.toString().length) || 0;
if (errorLength > length) {
length = errorLength;
}
});
return length;
}
function getMaxMessageLength(result, maxLineLength, consoleWidth) {
var _a;
let length = 0;
(_a = result.errors) === null || _a === void 0 ? void 0 : _a.forEach(function (error) {
const errorStr = error.message.toString();
// Get the length of the formatted error message when no extra padding is applied
// If the formatted message is longer than the console width, we will ignore its length
const expandedErrorStrLength = stylizeError(result.filePath, error, maxLineLength, 0, false).length;
if (errorStr.length > length && expandedErrorStrLength < consoleWidth) {
length = errorStr.length;
}
});
return length;
}
function printResults(results) {
// If the console is tty, get its width and use it to ensure we don't try to write messages longer
// than the console width when possible
let consoleWidth = Infinity;
if (process.stdout.isTTY) {
consoleWidth = process.stdout.columns;
}
const messages = [];
results.forEach(function (result) {
var _a;
if ((_a = result.errors) === null || _a === void 0 ? void 0 : _a.length) {
const maxLineLength = getMaxLineLength(result);
const maxMessageLength = getMaxMessageLength(result, maxLineLength, consoleWidth);
messages.push(stylizeFilePath(result.filePath));
_(result.errors).sortBy("errorLevel", "line").forEach(function (error) {
messages.push((stylizeError(result.filePath, error, maxLineLength, maxMessageLength, true)));
});
messages.push("\n");
}
});
process.stdout.write(messages.join("\n"));
}
exports.printResults = printResults;
//# sourceMappingURL=stylish.js.map