Skip to content

Commit 563c8ec

Browse files
committed
refactor: use copy code
1 parent 37ea315 commit 563c8ec

File tree

2 files changed

+2
-188
lines changed

2 files changed

+2
-188
lines changed

code-pushup.preset.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ export const eslintCoreConfigNx = async (
180180
: await eslintPlugin(await eslintConfigFromAllNxProjects(), {
181181
artifacts: {
182182
// We leverage Nx dependsOn to only run all lint targets before we run code-pushup
183-
// generateArtifactsCommand: 'npx nx run-many -t lint-report',
183+
// generateArtifactsCommand: 'npx nx run-many -t lint',
184184
artifactsPaths: ['packages/**/.eslint/eslint-report.json'],
185185
},
186186
}),

tools/eslint-formatter-multiple-formats/src/lib/utils.ts

Lines changed: 1 addition & 187 deletions
Original file line numberDiff line numberDiff line change
@@ -1,195 +1,9 @@
1-
// Import ansis for colors (similar to chalk)
2-
import { bold, dim, red, reset, underline, yellow } from 'ansis';
31
import type { ESLint } from 'eslint';
42
import { mkdir, writeFile } from 'node:fs/promises';
53
import path from 'node:path';
4+
import stylishFormatter from './stylish.js';
65
import type { FormatterConfig } from './types.js';
76

8-
// Helper function to pluralize words
9-
function pluralize(word: string, count: number): string {
10-
return count === 1 ? word : `${word}s`;
11-
}
12-
13-
// Simple function to strip ANSI codes for length calculation
14-
function stripAnsi(str: string): string {
15-
// eslint-disable-next-line no-control-regex
16-
return str.replace(/\u001B\[[\d;]*m/g, '');
17-
}
18-
19-
// Simple table formatting function
20-
function createTable(
21-
data: (string | number)[][],
22-
options: { align?: string[]; stringLength?: (str: string) => number } = {},
23-
): string {
24-
const { align = [], stringLength = s => s.length } = options;
25-
26-
if (data.length === 0) {
27-
return '';
28-
}
29-
30-
// Calculate column widths
31-
const colWidths: number[] = data.reduce<number[]>(
32-
(widths, row) =>
33-
row.reduce<number[]>((acc, cell, colIndex) => {
34-
const cellStr = String(cell);
35-
const width = stringLength(cellStr);
36-
const currentWidth = acc[colIndex] || 0;
37-
const maxWidth = Math.max(currentWidth, width);
38-
return [
39-
...acc.slice(0, colIndex),
40-
maxWidth,
41-
...acc.slice(colIndex + 1),
42-
];
43-
}, widths),
44-
[],
45-
);
46-
47-
// Format rows
48-
return data
49-
.map(row =>
50-
row
51-
.map((cell, colIndex) => {
52-
const cellStr = String(cell);
53-
const width = colWidths[colIndex] || 0;
54-
const padding = width - stringLength(cellStr);
55-
56-
if (align[colIndex] === 'r') {
57-
return ' '.repeat(padding) + cellStr;
58-
}
59-
return cellStr + ' '.repeat(padding);
60-
})
61-
.join(' '),
62-
)
63-
.join('\n');
64-
}
65-
66-
// Summary statistics for lint results
67-
type LintSummary = {
68-
errorCount: number;
69-
warningCount: number;
70-
fixableErrorCount: number;
71-
fixableWarningCount: number;
72-
summaryColor: 'yellow' | 'red';
73-
};
74-
75-
// Calculate summary statistics from results
76-
function calculateSummary(results: ESLint.LintResult[]): LintSummary {
77-
return results.reduce<LintSummary>(
78-
(summary, result) => ({
79-
errorCount: summary.errorCount + result.errorCount,
80-
warningCount: summary.warningCount + result.warningCount,
81-
fixableErrorCount: summary.fixableErrorCount + result.fixableErrorCount,
82-
fixableWarningCount:
83-
summary.fixableWarningCount + result.fixableWarningCount,
84-
summaryColor:
85-
result.errorCount > 0 ? ('red' as const) : summary.summaryColor,
86-
}),
87-
{
88-
errorCount: 0,
89-
warningCount: 0,
90-
fixableErrorCount: 0,
91-
fixableWarningCount: 0,
92-
summaryColor: 'yellow',
93-
},
94-
);
95-
}
96-
97-
// Format a single result file
98-
function formatResultFile(result: ESLint.LintResult): string {
99-
if (result.messages.length === 0) {
100-
return '';
101-
}
102-
103-
const header = `${underline(result.filePath)}\n`;
104-
105-
const tableData = result.messages.map(message => {
106-
const messageType =
107-
message.fatal || message.severity === 2
108-
? red('error')
109-
: yellow('warning');
110-
111-
return [
112-
'',
113-
message.line || 0,
114-
message.column || 0,
115-
messageType,
116-
message.message.replace(/([^ ])\.$/u, '$1'),
117-
dim(message.ruleId || ''),
118-
];
119-
});
120-
121-
const table = createTable(tableData, {
122-
align: ['', 'r', 'l'],
123-
stringLength: (str: string) => stripAnsi(str).length,
124-
});
125-
126-
const formattedTable = table
127-
.split('\n')
128-
.map(line =>
129-
line.replace(/(\d+)\s+(\d+)/u, (m, p1, p2) => dim(`${p1}:${p2}`)),
130-
)
131-
.join('\n');
132-
133-
return `${header}${formattedTable}\n\n`;
134-
}
135-
136-
// Format summary section
137-
function formatSummary(summary: LintSummary): string {
138-
const {
139-
errorCount,
140-
warningCount,
141-
fixableErrorCount,
142-
fixableWarningCount,
143-
summaryColor,
144-
} = summary;
145-
const total = errorCount + warningCount;
146-
147-
if (total === 0) {
148-
return '';
149-
}
150-
151-
const colorFn = summaryColor === 'red' ? red : yellow;
152-
const problemText = [
153-
'\u2716 ',
154-
total,
155-
pluralize(' problem', total),
156-
' (',
157-
errorCount,
158-
pluralize(' error', errorCount),
159-
', ',
160-
warningCount,
161-
pluralize(' warning', warningCount),
162-
')\n',
163-
].join('');
164-
165-
const problemOutput = bold(colorFn(problemText));
166-
167-
if (fixableErrorCount > 0 || fixableWarningCount > 0) {
168-
const fixableText = [
169-
' ',
170-
fixableErrorCount,
171-
pluralize(' error', fixableErrorCount),
172-
' and ',
173-
fixableWarningCount,
174-
pluralize(' warning', fixableWarningCount),
175-
' potentially fixable with the `--fix` option.\n',
176-
].join('');
177-
178-
return problemOutput + bold(colorFn(fixableText));
179-
}
180-
181-
return problemOutput;
182-
}
183-
184-
function stylishFormatter(results: ESLint.LintResult[]): string {
185-
const summary = calculateSummary(results);
186-
const filesOutput = results.map(formatResultFile).join('');
187-
const summaryOutput = formatSummary(summary);
188-
189-
const total = summary.errorCount + summary.warningCount;
190-
return total > 0 ? reset(`\n${filesOutput}${summaryOutput}`) : '';
191-
}
192-
1937
export function stringifyError(error: unknown): string {
1948
if (error instanceof Error) {
1959
if (error.name === 'Error' || error.message.startsWith(error.name)) {

0 commit comments

Comments
 (0)