UNPKG

eslint-formatter-summary

Version:

ESLint summary formatter aggregating results by rule

51 lines (50 loc) 2.17 kB
"use strict"; import chalk from "chalk-template"; import { aggregate } from "./aggregator.js"; import { lengthOfLongest, padNumber, sortBy, sum } from "./utils.js"; const maxErrorLen = (rules) => lengthOfLongest("errors", rules); const maxWarningLen = (rules) => lengthOfLongest("warnings", rules); const maxRuleLen = (rules) => lengthOfLongest("ruleId", rules); const totalErrors = (rules) => sum("errors", rules); const totalWarnings = (rules) => sum("warnings", rules); const totalProblems = (rules) => totalErrors(rules) + totalWarnings(rules); const sparkles = String.fromCodePoint(10024); const flames = String.fromCodePoint(128293); const constructHeader = (rules) => { const errors = "0".repeat(maxErrorLen(rules)); const warnings = "0".repeat(maxWarningLen(rules)); const longestRule = "0".repeat(maxRuleLen(rules)); const len = `errors ${errors} warnings ${warnings} rule: ${longestRule}`.length; const header = ` ${sparkles} Summary of failing ESLint rules `.padEnd(len); return chalk`{bgBlue ${header}}`; }; const constructSummary = (rules) => rules.map((rule, i) => { const errors = padNumber(rule.errors, maxErrorLen(rules)); const warnings = padNumber(rule.warnings, maxWarningLen(rules)); const { ruleId } = rule; const line = chalk`errors {red ${errors}} warnings {yellow ${warnings}} rule: {gray ${ruleId}}`; return i < rules.length - 1 ? `${line} ` : line; }).join(""); const constructTotal = (rules) => chalk`${flames} {red ${totalProblems( rules )} problems in total} (${totalErrors(rules)} {red errors}, ${totalWarnings( rules )} {yellow warnings})`; export const format = (results, { SORT_BY = "rule", DESC }) => { const rules = aggregate(results); if (["rule", "errors", "warnings"].includes(SORT_BY)) { const prop = SORT_BY === "rule" ? "ruleId" : SORT_BY; const direction = DESC === "true" ? "desc" : "asc"; sortBy(prop, rules, direction); } else { sortBy("ruleId", rules, "asc"); } const header = constructHeader(rules); const summary = constructSummary(rules); const total = constructTotal(rules); return `${header} ${summary} ${total}`; }; //# sourceMappingURL=format-results.js.map