basant307/AI_Governance_Project
048
1//------------------------------------------------------------------------------2// Requirements3//------------------------------------------------------------------------------4import chalk from "chalk";5import { table } from "table";6import stripAnsi from "strip-ansi";7import pluralize from "pluralize";8//------------------------------------------------------------------------------9// Helpers10//------------------------------------------------------------------------------11/**12 * Draws text table.13 * @param {Array<Object>} messages Error messages relating to a specific file.14 * @returns {string} A text table.15 */16function drawTable(messages) {17 let rows = [];18 if (messages.length === 0) {19 return "";20 }21 rows.push([22 chalk.bold("Line"),23 chalk.bold("Column"),24 chalk.bold("Type"),25 chalk.bold("Message"),26 chalk.bold("Rule ID"),27 ]);28 messages.forEach(function (message) {29 let messageType;30 if (message.severity === "error") {31 messageType = chalk.red("error");32 }33 else {34 messageType = chalk.yellow("warning");35 }36 rows.push([message.loc.start.line, message.loc.start.column, messageType, message.message, message.ruleId]);37 });38 const output = table(rows, {39 columns: {40 0: {41 width: 8,42 wrapWord: true,43 },44 1: {45 width: 8,46 wrapWord: true,47 },48 2: {49 width: 8,50 wrapWord: true,51 },52 3: {53 paddingRight: 5,54 width: 50,55 wrapWord: true,56 },57 4: {58 width: 20,59 wrapWord: true,60 },61 },62 drawHorizontalLine: function (index) {63 return index === 1;64 },65 });66 return output;67}68/**69 * Draws a report (multiple tables).70 * @param {Array} results Report results for every file.71 * @returns {string} A column of text tables.72 */73function drawReport(results) {74 let files;75 files = results.map(function (result) {76 if (!result.messages.length) {77 return "";78 }79 return "\n" + result.filePath + "\n\n" + drawTable(result.messages);80 });81 files = files.filter(function (content) {82 return content.trim();83 });84 return files.join("");85}86//------------------------------------------------------------------------------87// Public Interface88//------------------------------------------------------------------------------89const formatter = (results, options) => {90 const useColor = options.color ?? true;91 let output = "";92 let errorCount = 0;93 let warningCount = 0;94 results.forEach((fileReport) => {95 fileReport.messages.forEach((message) => {96 if (message.severity === "warning") {97 warningCount += 1;98 }99 else if (message.severity === "error") {100 errorCount += 1;101 }102 });103 });104 if (errorCount || warningCount) {105 output = drawReport(results);106 }107 output +=108 "\n" +109 table([110 [chalk.red(pluralize("Error", errorCount, true))],111 [chalk.yellow(pluralize("Warning", warningCount, true))],112 ], {113 columns: {114 0: {115 width: 110,116 wrapWord: true,117 },118 },119 drawHorizontalLine: function () {120 return true;121 },122 });123 if (!useColor) {124 return stripAnsi(output);125 }126 return output;127};128export default formatter;129//# sourceMappingURL=table.js.map