UNPKG

@textlint/fixer-formatter

Version:

textlint output formatter for fixer

113 lines 4.12 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = default_1; const chalk_1 = __importDefault(require("chalk")); // @ts-expect-error no types const text_table_1 = __importDefault(require("text-table")); const string_width_1 = __importDefault(require("string-width")); const strip_ansi_1 = __importDefault(require("strip-ansi")); /** * Given a word and a count, append an s if count is not one. * @param {string} word A word in its singular form. * @param {number} count A number controlling whether word should be pluralized. * @returns {string} The original word with an s on the end if count is not one. */ function pluralize(word, count) { return count === 1 ? word : `${word}s`; } function default_1(results, options) { // default: true const useColor = options.color !== undefined ? options.color : true; let output = "\n"; let totalFixed = 0; let errors = 0; let warnings = 0; let infos = 0; const summaryColor = "yellow"; const greenColor = "green"; results.forEach(function (result) { if (!result.applyingMessages || !result.remainingMessages) { return; } const messages = result.applyingMessages; // still error count const remainingMessages = result.remainingMessages; // Count remaining messages by severity remainingMessages.forEach(function (message) { const fatal = message.fatal; if (fatal || message.severity === 2) { errors++; } else if (message.severity === 1) { warnings++; } else if (message.severity === 3) { infos++; } }); if (messages.length === 0) { return; } output += `${chalk_1.default.underline(result.filePath)}\n`; output += `${(0, text_table_1.default)(messages.map(function (message) { // fixable totalFixed++; const messageType = chalk_1.default[greenColor].bold("\u2714 "); return [ "", message.line || 0, message.column || 0, messageType, message.message.replace(/\.$/, ""), chalk_1.default.gray(message.ruleId || "") ]; }), { align: ["", "r", "l"], stringLength: (str) => { const lines = (0, strip_ansi_1.default)(str).split("\n"); return Math.max.apply(null, lines.map(function (line) { return (0, string_width_1.default)(line); })); } }) .split("\n") .map(function (el) { return el.replace(/(\d+)\s+(\d+)/, function (_m, p1, p2) { return chalk_1.default.gray(`${p1}:${p2}`); }); }) .join("\n")}\n\n`; }); if (totalFixed > 0) { output += chalk_1.default[greenColor].bold([ // http://www.fileformat.info/info/unicode/char/2714/index.htm "\u2714 Fixed ", totalFixed, pluralize(" problem", totalFixed), "\n" ].join("")); } const totalRemaining = errors + warnings + infos; if (totalRemaining > 0) { const summaryParts = [ `${errors} ${pluralize("error", errors)}`, `${warnings} ${pluralize("warning", warnings)}`, `${infos} ${pluralize("info", infos)}` ]; output += chalk_1.default[summaryColor].bold([ // http://www.fileformat.info/info/unicode/char/2716/index.htm "\u2716 Remaining ", summaryParts.join(", "), "\n" ].join("")); } const finalOutput = totalFixed > 0 ? output : ""; if (!useColor) { return (0, strip_ansi_1.default)(finalOutput); } return finalOutput; } //# sourceMappingURL=stylish.js.map