@darksheep/eslint-formatter-github 0.0.1 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@darksheep/eslint-formatter-github",
3
- "version": "0.0.1",
3
+ "version": "0.1.0",
4
4
  "description": "ESLint + Github actions",
5
5
  "license": "UNLICENCED",
6
6
  "type": "commonjs",
@@ -20,7 +20,7 @@
20
20
  "@darksheep/package.json": "~0.0.1"
21
21
  },
22
22
  "devDependencies": {
23
- "@darksheep/eslint": "~4.3.0",
23
+ "@darksheep/eslint": "~4.4.0",
24
24
  "@types/node": "~20.12.0",
25
25
  "eslint": "~8.57.0",
26
26
  "typescript": "~5.4.0"
package/src/index.js CHANGED
@@ -40,65 +40,96 @@ const {
40
40
  * @property {number} fixableWarningCount
41
41
  */
42
42
 
43
+ /**
44
+ * @param {LintResult[]} fileResults The eslint results
45
+ * @returns {Record<'warningCount' | 'errorCount', number>}
46
+ */
47
+ function counters(fileResults) {
48
+ let warningCount = 0;
49
+ let errorCount = 0;
50
+
51
+ for (const fileResult of fileResults) {
52
+ warningCount += fileResult.warningCount;
53
+ errorCount += fileResult.errorCount;
54
+ }
55
+
56
+ return {
57
+ warningCount,
58
+ errorCount,
59
+ };
60
+ }
61
+
62
+ /**
63
+ * @param {LintResult} fileResult The eslint single file result
64
+ * @returns {string[]}
65
+ */
66
+ function formatFileResult(fileResult) {
67
+ /** @type {string[]} */
68
+ const lines = [];
69
+ const filePath = path.relative(
70
+ GITHUB_WORKSPACE,
71
+ fileResult.filePath,
72
+ );
73
+
74
+ for (const result of fileResult.messages) {
75
+ const message = result.ruleId == null || result.ruleId === ''
76
+ ? `[${result.ruleId}] ${result.message}`
77
+ : result.message;
78
+
79
+ /** @type {import('@darksheep/github-actions').LineAnnotation} */
80
+ const context = {
81
+ file: filePath,
82
+ line: result.line,
83
+ col: result.column,
84
+ endLine: result.endLine,
85
+ endColumn: result.endColumn,
86
+ };
87
+
88
+ switch (result.severity) {
89
+ /* eslint-disable style/max-statements-per-line */
90
+ case 0: lines.push(setNotice(message, context)); break;
91
+ case 1: lines.push(setWarning(message, context)); break;
92
+ case 2: lines.push(setError(message, context)); break;
93
+ /* eslint-enable style/max-statements-per-line */
94
+ }
95
+ }
96
+
97
+ return lines;
98
+ }
99
+
43
100
  /**
44
101
  * @param {LintResult[]} fileResults The eslint results
45
102
  * @returns {string}
46
103
  */
47
104
  function formatter(fileResults) {
48
105
  const lines = [];
49
- let warningCount = 0;
50
- let errorCount = 0;
51
106
 
52
107
  const pkg = getPackageJson(undefined, `${process.cwd()}/package.json`);
53
108
  if (pkg == null) {
54
109
  throw new Error('Cannot resolve package name');
55
110
  }
56
111
 
57
- lines.push(startGroup(`Linting ${pkg.name}`));
112
+ const { warningCount, errorCount } = counters(fileResults);
58
113
 
59
- for (const fileResult of fileResults) {
60
- const filePath = path.relative(
61
- GITHUB_WORKSPACE,
62
- fileResult.filePath,
63
- );
114
+ if (warningCount + errorCount > 0) {
115
+ lines.push(startGroup([
116
+ `Linted ${pkg.name} (`,
117
+ `${errorCount} ${plural('error', errorCount)}, `,
118
+ `${warningCount} ${plural('warning', warningCount)})`,
119
+ ].join('')));
120
+ } else {
121
+ lines.push(`Linted ${pkg.name} (0 errors, 0 warnings)`);
122
+ }
64
123
 
65
- warningCount += fileResult.warningCount;
66
- errorCount += fileResult.errorCount;
124
+ lines.push(...fileResults.flatMap(formatFileResult));
67
125
 
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
- }
126
+ if (warningCount + errorCount > 0) {
127
+ lines.push(endGroup());
94
128
  }
95
129
 
96
- lines.push(endGroup());
97
-
98
- const total = errorCount + warningCount;
99
- process.exitCode = errorCount;
130
+ process.exitCode ??= 0;
131
+ process.exitCode += errorCount;
100
132
 
101
- lines.push(`${total} problem(s) (${errorCount} ${plural('error', errorCount)}, ${warningCount} ${plural('warning', warningCount)})`);
102
133
  return lines.join('\n');
103
134
  }
104
135
 
package/types/index.d.ts CHANGED
@@ -1,27 +1,4 @@
1
1
  export = formatter;
2
- /**
3
- * @typedef {Object} LintMessage
4
- * @property {number} column
5
- * @property {number} line
6
- * @property {number} [endColumn]
7
- * @property {number} [endLine]
8
- * @property {string | null} ruleId
9
- * @property {string} message
10
- * @property {string} [messageId]
11
- * @property {string} [nodeType]
12
- * @property {boolean} [fatal]
13
- * @property {0 | 1 | 2} severity
14
- */
15
- /**
16
- * @typedef {Object} LintResult
17
- * @property {string} filePath
18
- * @property {LintMessage[]} messages
19
- * @property {number} errorCount
20
- * @property {number} fatalErrorCount
21
- * @property {number} warningCount
22
- * @property {number} fixableErrorCount
23
- * @property {number} fixableWarningCount
24
- */
25
2
  /**
26
3
  * @param {LintResult[]} fileResults The eslint results
27
4
  * @returns {string}