Skip to content

Commit aa35d0c

Browse files
committed
fix(plugin-eslint): pluralize audit display value based on count
1 parent fca87f5 commit aa35d0c

File tree

5 files changed

+53
-8
lines changed

5 files changed

+53
-8
lines changed

packages/plugin-eslint/src/lib/__snapshots__/runner.spec.ts.snap

+6-6
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ exports[`executeRunner > should execute ESLint and create audit results for Reac
8484
},
8585
],
8686
},
87-
"displayValue": "1 warnings",
87+
"displayValue": "1 warning",
8888
"score": 0,
8989
"slug": "no-unused-vars",
9090
"value": 1,
@@ -123,7 +123,7 @@ exports[`executeRunner > should execute ESLint and create audit results for Reac
123123
},
124124
],
125125
},
126-
"displayValue": "1 warnings",
126+
"displayValue": "1 warning",
127127
"score": 0,
128128
"slug": "arrow-body-style",
129129
"value": 1,
@@ -162,7 +162,7 @@ exports[`executeRunner > should execute ESLint and create audit results for Reac
162162
},
163163
],
164164
},
165-
"displayValue": "1 warnings",
165+
"displayValue": "1 warning",
166166
"score": 0,
167167
"slug": "eqeqeq",
168168
"value": 1,
@@ -185,7 +185,7 @@ exports[`executeRunner > should execute ESLint and create audit results for Reac
185185
},
186186
],
187187
},
188-
"displayValue": "1 warnings",
188+
"displayValue": "1 warning",
189189
"score": 0,
190190
"slug": "max-lines-per-function",
191191
"value": 1,
@@ -330,7 +330,7 @@ exports[`executeRunner > should execute ESLint and create audit results for Reac
330330
},
331331
],
332332
},
333-
"displayValue": "1 warnings",
333+
"displayValue": "1 warning",
334334
"score": 0,
335335
"slug": "prefer-const",
336336
"value": 1,
@@ -369,7 +369,7 @@ exports[`executeRunner > should execute ESLint and create audit results for Reac
369369
},
370370
],
371371
},
372-
"displayValue": "1 warnings",
372+
"displayValue": "1 warning",
373373
"score": 0,
374374
"slug": "react-jsx-key",
375375
"value": 1,

packages/plugin-eslint/src/lib/runner/transform.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { AuditOutput, Issue } from '@code-pushup/models';
22
import {
33
compareIssueSeverity,
44
countOccurrences,
5+
formatCount,
56
objectToEntries,
67
} from '@code-pushup/utils';
78
import type { Linter } from 'eslint';
@@ -41,7 +42,7 @@ function toAudit(slug: string, issues: LintIssue[]): AuditOutput {
4142
);
4243
const summaryText = objectToEntries(severityCounts)
4344
.sort((a, b) => -compareIssueSeverity(a[0], b[0]))
44-
.map(([severity, count]) => `${count} ${severity}s`)
45+
.map(([severity, count = 0]) => formatCount(count, severity))
4546
.join(', ');
4647
return {
4748
slug,

packages/utils/src/index.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@ export {
1717
countOccurrences,
1818
distinct,
1919
formatBytes,
20+
formatCount,
2021
objectToEntries,
22+
pluralize,
23+
readJsonFile,
2124
slugify,
2225
toArray,
23-
readJsonFile,
2426
} from './lib/utils';

packages/utils/src/lib/utils.spec.ts

+27
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import {
77
countWeightedRefs,
88
distinct,
99
formatBytes,
10+
formatCount,
11+
pluralize,
1012
slugify,
1113
sumRefs,
1214
toArray,
@@ -175,3 +177,28 @@ describe('compareIssueSeverity', () => {
175177
).toEqual(['info', 'warning', 'error'] satisfies Issue['severity'][]);
176178
});
177179
});
180+
181+
describe('formatCount', () => {
182+
it('should pluralize if count is greater than 1', () => {
183+
expect(formatCount(5, 'audit')).toBe('5 audits');
184+
});
185+
186+
it('should not pluralize if count is 1', () => {
187+
expect(formatCount(1, 'audit')).toBe('1 audit');
188+
});
189+
190+
it('should pluralize if count is 0', () => {
191+
expect(formatCount(0, 'audit')).toBe('0 audits');
192+
});
193+
});
194+
195+
describe('pluralize', () => {
196+
it.each([
197+
['warning', 'warnings'],
198+
['error', 'errors'],
199+
['category', 'categories'],
200+
['status', 'statuses'],
201+
])('should pluralize "%s" as "%s"', (singular, plural) => {
202+
expect(pluralize(singular)).toBe(plural);
203+
});
204+
});

packages/utils/src/lib/utils.ts

+15
Original file line numberDiff line numberDiff line change
@@ -77,3 +77,18 @@ export async function readJsonFile(path: string): Promise<unknown> {
7777
const buffer = await readFile(path);
7878
return JSON.parse(buffer.toString());
7979
}
80+
81+
export function formatCount(count: number, name: string) {
82+
const text = count === 1 ? name : pluralize(name);
83+
return `${count} ${text}`;
84+
}
85+
86+
export function pluralize(text: string): string {
87+
if (text.endsWith('y')) {
88+
return text.slice(0, -1) + 'ies';
89+
}
90+
if (text.endsWith('s')) {
91+
return `${text}es`;
92+
}
93+
return `${text}s`;
94+
}

0 commit comments

Comments
 (0)