-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
146 lines (123 loc) Β· 5.09 KB
/
Copy pathindex.js
File metadata and controls
146 lines (123 loc) Β· 5.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import fs from "node:fs";
import path from "node:path";
import chalk from "chalk";
const EMOJIS = {
error: "π΄",
warning: "π‘",
};
const CLEAN_ASCII = `
βββββββ βββ ββββββ βββ βββββββββββββββββββ ββββββββββββββ
βββββββββββ ββββββ βββ ββββββββββββββββββββ βββββββββββββββ
βββββββββββ ββββββ βββ ββββββββββββββ βββββββ ββββββ βββ
βββββββββββ ββββββ βββ ββββββββββββββ βββββ ββββββ βββ
βββββββββββββββββββββββββββββββββββββββββββββββββ βββ βββββββββββ
βββββββ βββββββ ββββββββββββββββββββββββββββββββ βββ βββββββββββ
`;
// Cache for file contents to prevent multiple reads
const fileCache = new Map();
function getLineFromFile(filePath, lineNumber) {
try {
if (!fileCache.has(filePath)) {
const fileContent = fs.readFileSync(filePath, "utf8");
fileCache.set(filePath, fileContent.split("\n"));
}
const lines = fileCache.get(filePath);
return lines[lineNumber - 1] || "";
} catch {
return "";
}
}
function getRelativePath(filePath) {
const currentModulePath = new URL(import.meta.url).pathname;
const currentDir = path.dirname(currentModulePath);
return path.relative(currentDir, filePath);
}
function stripAnsi(string_) {
// ANSI color codes are control characters that don't affect string length
// String.trim() only removes whitespace, not control characters
return string_.replaceAll(/\[(?:\d{1,2}(?:;\d{1,2})*)?m/g, "");
}
function createCaretLine(prefix, originalLine, trimmedLine, column) {
const leadingSpaces = originalLine.length - originalLine.trimStart().length;
const strippedPrefix = stripAnsi(prefix);
const lineContentStart = strippedPrefix.length + 3; // Account for " | " separator
return (
" ".repeat(lineContentStart) +
" ".repeat(column - leadingSpaces - 1) +
chalk.red("^")
);
}
function createHackerHeader(text, color) {
const strippedText = stripAnsi(text);
const border = "β".repeat(strippedText.length + 4);
const coloredBorder = chalk[color].bold;
return `${coloredBorder(`β${border}β`)}
${coloredBorder(`β ${chalk.bold(text)} β`)}
${coloredBorder(`β${border}β`)}`;
}
function formatMessage(filePath, message) {
const { line, column, ruleId, severity } = message;
const originalLine = getLineFromFile(filePath, line);
const trimmedLine = originalLine.trim();
const emoji = severity === 2 ? EMOJIS.error : EMOJIS.warning;
const prefix = `${emoji} ${chalk.cyan(filePath)}${chalk.yellow(`:${line}:${column}`)}`;
const parts = [prefix, chalk.green(trimmedLine), chalk.red(ruleId)];
let output = parts.join(" | ") + "\n";
output += createCaretLine(prefix, originalLine, trimmedLine, column) + "\n";
return output;
}
export default function eslintFormatterBullseye(results) {
let output = "\n";
let errorCount = 0;
let warningCount = 0;
// Clear the cache before processing new results
fileCache.clear();
// Group messages by severity and file
const warnings = new Map();
const errors = new Map();
for (const result of results) {
const filePath = getRelativePath(result.filePath);
for (const message of result.messages) {
if (message.severity === 1) {
warningCount++;
if (!warnings.has(filePath)) {
warnings.set(filePath, []);
}
warnings.get(filePath).push(message);
} else {
errorCount++;
if (!errors.has(filePath)) {
errors.set(filePath, []);
}
errors.get(filePath).push(message);
}
}
}
// If no issues, show the success ASCII art
if (errorCount === 0 && warningCount === 0) {
return `\n${chalk.green(CLEAN_ASCII)}\n${chalk.cyan("β¨ All systems operational! Code is clean. β¨")}\n`;
}
// Warnings Section
if (warningCount > 0) {
output += createHackerHeader(" WARNINGS ", "yellow") + "\n";
for (const [filePath, messages] of warnings) {
for (const message of messages) {
output += formatMessage(filePath, message);
}
}
}
// Errors Section
if (errorCount > 0) {
output += createHackerHeader(" ERRORS ", "red") + "\n";
for (const [filePath, messages] of errors) {
for (const message of messages) {
output += formatMessage(filePath, message);
}
}
}
// Summary
output += createHackerHeader(" SUMMARY ", "cyan") + "\n";
output += `${EMOJIS.error} ${chalk.red("Errors detected:")} ${errorCount}\n`;
output += `${EMOJIS.warning} ${chalk.yellow("Warnings identified:")} ${warningCount}\n`;
return output;
}