]>
git.proxmox.com Git - pve-eslint.git/blob - eslint/lib/cli-engine/formatters/codeframe.js
41e3ab7b0afe813f1199bd1cfc4421646bf6710b
2 * @fileoverview Codeframe reporter
3 * @author Vitor Balocco
7 const chalk
= require("chalk");
8 const { codeFrameColumns
} = require("@babel/code-frame");
9 const path
= require("path");
11 //------------------------------------------------------------------------------
13 //------------------------------------------------------------------------------
16 * Given a word and a count, append an s if count is not one.
17 * @param {string} word A word in its singular form.
18 * @param {number} count A number controlling whether word should be pluralized.
19 * @returns {string} The original word with an s on the end if count is not one.
21 function pluralize(word
, count
) {
22 return (count
=== 1 ? word
: `${word}s`);
26 * Gets a formatted relative file path from an absolute path and a line/column in the file.
27 * @param {string} filePath The absolute file path to format.
28 * @param {number} line The line from the file to use for formatting.
29 * @param {number} column The column from the file to use for formatting.
30 * @returns {string} The formatted file path.
32 function formatFilePath(filePath
, line
, column
) {
33 let relPath
= path
.relative(process
.cwd(), filePath
);
36 relPath
+= `:${line}:${column}`;
39 return chalk
.green(relPath
);
43 * Gets the formatted output for a given message.
44 * @param {Object} message The object that represents this message.
45 * @param {Object} parentResult The result object that this message belongs to.
46 * @returns {string} The formatted output.
48 function formatMessage(message
, parentResult
) {
49 const type
= (message
.fatal
|| message
.severity
=== 2) ? chalk
.red("error") : chalk
.yellow("warning");
50 const msg
= `${chalk.bold(message.message.replace(/([^ ])\.$/u, "$1"))}`;
51 const ruleId
= message
.fatal
? "" : chalk
.dim(`(${message.ruleId})`);
52 const filePath
= formatFilePath(parentResult
.filePath
, message
.line
, message
.column
);
53 const sourceCode
= parentResult
.output
? parentResult
.output
: parentResult
.source
;
58 ruleId
? `${ruleId}` : "",
59 sourceCode
? `at ${filePath}:` : `at ${filePath}`
60 ].filter(String
).join(" ");
62 const result
= [firstLine
];
66 codeFrameColumns(sourceCode
, { start
: { line
: message
.line
, column
: message
.column
} }, { highlightCode
: false })
70 return result
.join("\n");
74 * Gets the formatted output summary for a given number of errors and warnings.
75 * @param {number} errors The number of errors.
76 * @param {number} warnings The number of warnings.
77 * @param {number} fixableErrors The number of fixable errors.
78 * @param {number} fixableWarnings The number of fixable warnings.
79 * @returns {string} The formatted output summary.
81 function formatSummary(errors
, warnings
, fixableErrors
, fixableWarnings
) {
82 const summaryColor
= errors
> 0 ? "red" : "yellow";
84 const fixablesSummary
= [];
87 summary
.push(`${errors} ${pluralize("error", errors)}`);
91 summary
.push(`${warnings} ${pluralize("warning", warnings)}`);
94 if (fixableErrors
> 0) {
95 fixablesSummary
.push(`${fixableErrors} ${pluralize("error", fixableErrors)}`);
98 if (fixableWarnings
> 0) {
99 fixablesSummary
.push(`${fixableWarnings} ${pluralize("warning", fixableWarnings)}`);
102 let output
= chalk
[summaryColor
].bold(`${summary.join(" and ")} found.`);
104 if (fixableErrors
|| fixableWarnings
) {
105 output
+= chalk
[summaryColor
].bold(`\n${fixablesSummary.join(" and ")} potentially fixable with the \`--fix
\` option.`);
111 //------------------------------------------------------------------------------
113 //------------------------------------------------------------------------------
115 module
.exports = function(results
) {
118 let fixableErrors
= 0;
119 let fixableWarnings
= 0;
121 const resultsWithMessages
= results
.filter(result
=> result
.messages
.length
> 0);
123 let output
= resultsWithMessages
.reduce((resultsOutput
, result
) => {
124 const messages
= result
.messages
.map(message
=> `${formatMessage(message, result)}\n\n`);
126 errors
+= result
.errorCount
;
127 warnings
+= result
.warningCount
;
128 fixableErrors
+= result
.fixableErrorCount
;
129 fixableWarnings
+= result
.fixableWarningCount
;
131 return resultsOutput
.concat(messages
);
135 output
+= formatSummary(errors
, warnings
, fixableErrors
, fixableWarnings
);
137 return (errors
+ warnings
) > 0 ? output
: "";