-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
60 lines (47 loc) · 1.76 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/**
* @fileoverview GitHub checks annotations formatter
*/
"use strict";
const path = require("path");
const ESCAPE_MAP = { "%": "", "\n": "%0A", "\r": "%0D" };
const ESCAPE_REGEXP = new RegExp(`${Object.keys(ESCAPE_MAP).join("|")}`);
//------------------------------------------------------------------------------
// Helper Functions
//------------------------------------------------------------------------------
/**
* Returns a escaped string for GitHub checks annotations
* @param {string} string to escape
* @returns {string} escaped string
* @private
*/
function githubEscape(string) {
return string.replace(ESCAPE_REGEXP, (match) => ESCAPE_MAP[match]);
}
//------------------------------------------------------------------------------
// Public Interface
//------------------------------------------------------------------------------
module.exports = function (results) {
let output = "";
let total = 0;
results.forEach((result) => {
if (result.messages.length === 0) return;
const relPath = path.relative(process.cwd(), result.filePath);
total += result.messages.length;
output += `::group::EsLint ${relPath}: errors=${result.errorCount} warnings=${result.warningCount}\n`;
result.messages.forEach((message) => {
output += `::${message.severity === 1 ? "warning" : "error"} `;
output += `file=${relPath},line=${message.line || 0}`;
output += message.column ? `,col=${message.column}` : "";
output += message.ruleId ? `::[${message.ruleId}]` : "";
output += ` ${githubEscape(message.message)}`;
output += "\n";
});
output += "::endgroup::\n";
});
if (total === 0) {
output += "no problems";
} else {
output += `\n${total} problem${total !== 1 ? "s" : ""}`;
}
return output;
};