@darksheep/eslint-formatter-github 0.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. package/README.md +6 -0
  2. package/package.json +29 -0
  3. package/src/index.js +105 -0
package/README.md ADDED
@@ -0,0 +1,6 @@
1
+ # @darksheep/eslint-formatter-github
2
+
3
+ ```
4
+ npm install --save-dev eslint @darksheep/eslint-formatter-github
5
+ npx eslint . --format @darksheep/github
6
+ ```
package/package.json ADDED
@@ -0,0 +1,29 @@
1
+ {
2
+ "name": "@darksheep/eslint-formatter-github",
3
+ "version": "0.0.0",
4
+ "description": "ESLint + Github actions",
5
+ "license": "UNLICENCED",
6
+ "type": "commonjs",
7
+ "main": "./src/index.js",
8
+ "types": "./types/index.d.ts",
9
+ "files": [
10
+ "./src"
11
+ ],
12
+ "scripts": {
13
+ "lint": "eslint .",
14
+ "lint:fix": "eslint . --fix"
15
+ },
16
+ "dependencies": {
17
+ "@darksheep/github-actions": "~0.0.0",
18
+ "@darksheep/package.json": "~0.0.1"
19
+ },
20
+ "devDependencies": {
21
+ "@darksheep/eslint": "~4.3.0",
22
+ "@types/node": "~20.12.0",
23
+ "eslint": "~8.57.0",
24
+ "typescript": "~5.4.0"
25
+ },
26
+ "engines": {
27
+ "node": "^18 || ^20 || ^21"
28
+ }
29
+ }
package/src/index.js ADDED
@@ -0,0 +1,105 @@
1
+ /* eslint jsdoc/require-property-description: 0 */
2
+ const path = require('node:path');
3
+ const { setNotice, setWarning, setError, startGroup, endGroup } = require('@darksheep/github-actions');
4
+ const { getPackageJson } = require('@darksheep/package.json/sync');
5
+
6
+ /**
7
+ * @param {string} word The word to pluralise
8
+ * @param {number} count The number of items
9
+ * @returns {string}
10
+ */
11
+ function plural(word, count) {
12
+ return (count === 1 ? word : `${word}s`);
13
+ }
14
+
15
+ const {
16
+ GITHUB_WORKSPACE = process.cwd(),
17
+ } = process.env;
18
+
19
+ /**
20
+ * @typedef {Object} LintMessage
21
+ * @property {number} column
22
+ * @property {number} line
23
+ * @property {number} [endColumn]
24
+ * @property {number} [endLine]
25
+ * @property {string | null} ruleId
26
+ * @property {string} message
27
+ * @property {string} [messageId]
28
+ * @property {string} [nodeType]
29
+ * @property {boolean} [fatal]
30
+ * @property {0 | 1 | 2} severity
31
+ */
32
+ /**
33
+ * @typedef {Object} LintResult
34
+ * @property {string} filePath
35
+ * @property {LintMessage[]} messages
36
+ * @property {number} errorCount
37
+ * @property {number} fatalErrorCount
38
+ * @property {number} warningCount
39
+ * @property {number} fixableErrorCount
40
+ * @property {number} fixableWarningCount
41
+ */
42
+
43
+ /**
44
+ * @param {LintResult[]} fileResults The eslint results
45
+ * @returns {string}
46
+ */
47
+ function formatter(fileResults) {
48
+ const lines = [];
49
+ let warningCount = 0;
50
+ let errorCount = 0;
51
+
52
+ const pkg = getPackageJson(undefined, `${process.cwd()}/package.json`);
53
+ if (pkg == null) {
54
+ throw new Error('Cannot resolve package name');
55
+ }
56
+
57
+ lines.push(startGroup(`Linting ${pkg.name}`));
58
+
59
+ for (const fileResult of fileResults) {
60
+ const filePath = path.relative(
61
+ GITHUB_WORKSPACE,
62
+ fileResult.filePath,
63
+ );
64
+
65
+ warningCount += fileResult.warningCount;
66
+ errorCount += fileResult.errorCount;
67
+
68
+ for (const result of fileResult.messages) {
69
+ const message = result.ruleId == null || result.ruleId === ''
70
+ ? `[${result.ruleId}] ${result.message}`
71
+ : result.message;
72
+
73
+ /** @type {import('@darksheep/github-actions').LineAnnotation} */
74
+ const context = {
75
+ file: filePath,
76
+ line: result.line,
77
+ col: result.column,
78
+ endLine: result.endLine,
79
+ endColumn: result.endColumn,
80
+ };
81
+
82
+ switch (result.severity) {
83
+ case 0:
84
+ lines.push(setNotice(message, context));
85
+ break;
86
+ case 1:
87
+ lines.push(setWarning(message, context));
88
+ break;
89
+ case 2:
90
+ lines.push(setError(message, context));
91
+ break;
92
+ }
93
+ }
94
+ }
95
+
96
+ lines.push(endGroup());
97
+
98
+ const total = errorCount + warningCount;
99
+ process.exitCode = errorCount;
100
+
101
+ lines.push(`${total} problem(s) (${errorCount} ${plural('error', errorCount)}, ${warningCount} ${plural('warning', warningCount)})`);
102
+ return lines.join('\n');
103
+ }
104
+
105
+ module.exports = formatter;