@darksheep/eslint-formatter-github 0.0.0 → 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,24 +1,26 @@
1
1
  {
2
2
  "name": "@darksheep/eslint-formatter-github",
3
- "version": "0.0.0",
3
+ "version": "0.1.0",
4
4
  "description": "ESLint + Github actions",
5
5
  "license": "UNLICENCED",
6
6
  "type": "commonjs",
7
7
  "main": "./src/index.js",
8
8
  "types": "./types/index.d.ts",
9
9
  "files": [
10
- "./src"
10
+ "./src",
11
+ "./types"
11
12
  ],
12
13
  "scripts": {
13
14
  "lint": "eslint .",
14
- "lint:fix": "eslint . --fix"
15
+ "lint:fix": "eslint . --fix",
16
+ "prepack": "tsc"
15
17
  },
16
18
  "dependencies": {
17
19
  "@darksheep/github-actions": "~0.0.0",
18
20
  "@darksheep/package.json": "~0.0.1"
19
21
  },
20
22
  "devDependencies": {
21
- "@darksheep/eslint": "~4.3.0",
23
+ "@darksheep/eslint": "~4.4.0",
22
24
  "@types/node": "~20.12.0",
23
25
  "eslint": "~8.57.0",
24
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
 
@@ -0,0 +1,30 @@
1
+ export = formatter;
2
+ /**
3
+ * @param {LintResult[]} fileResults The eslint results
4
+ * @returns {string}
5
+ */
6
+ declare function formatter(fileResults: LintResult[]): string;
7
+ declare namespace formatter {
8
+ export { LintMessage, LintResult };
9
+ }
10
+ type LintResult = {
11
+ filePath: string;
12
+ messages: LintMessage[];
13
+ errorCount: number;
14
+ fatalErrorCount: number;
15
+ warningCount: number;
16
+ fixableErrorCount: number;
17
+ fixableWarningCount: number;
18
+ };
19
+ type LintMessage = {
20
+ column: number;
21
+ line: number;
22
+ endColumn?: number | undefined;
23
+ endLine?: number | undefined;
24
+ ruleId: string | null;
25
+ message: string;
26
+ messageId?: string | undefined;
27
+ nodeType?: string | undefined;
28
+ fatal?: boolean | undefined;
29
+ severity: 0 | 1 | 2;
30
+ };