Skip to content

Commit

Permalink
Improve compiler output for line validation
Browse files Browse the repository at this point in the history
This commit improves feedback when a line is too long to enhance
developer/maintainer productivity.

- Trim long lines in output to 500 characters
- Show character count exceeding max line length
- Refactor line formatting for better readability
  • Loading branch information
undergroundwires committed Sep 14, 2024
1 parent 98e8dc0 commit ebd07f0
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export const analyzeTooLongLines: CodeValidationAnalyzer = (
lineNumber: line.lineNumber,
error: [
`Line is too long (${line.text.length}).`,
`It exceed maximum allowed length ${maxLineLength}.`,
`It exceed maximum allowed length ${maxLineLength} by ${line.text.length - maxLineLength} characters.`,
'This may cause bugs due to unintended trimming by operating system, shells or terminal emulators.',
].join(' '),
}));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,33 @@ function formatLines(
): string {
return lines.map((line) => {
const badLine = invalidLines.find((invalidLine) => invalidLine.lineNumber === line.lineNumber);
if (!badLine) {
return `[${line.lineNumber}] ✅ ${line.text}`;
}
return `[${badLine.lineNumber}] ❌ ${line.text}\n\t⟶ ${badLine.error}`;
return formatLine({
lineNumber: line.lineNumber,
text: line.text,
error: badLine?.error,
});
}).join('\n');
}
function formatLine(
line: {
readonly lineNumber: number;
readonly text: string;
readonly error?: string;
},
): string {
let text = `[${line.lineNumber}] `;
text += line.error ? '❌' : '✅';
text += ` ${trimLine(line.text)}`;
if (line.error) {
text += `\n\t⟶ ${line.error}`;
}
return text;
}

function trimLine(line: string) {
const maxLength = 500;
if (line.length > maxLength) {
line = `${line.substring(0, maxLength)}... [Rest of the line trimmed]`;
}
return line;
}
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ describe('AnalyzeTooLongLines', () => {
function createTooLongLineError(actualLength: number, maxAllowedLength: number): string {
return [
`Line is too long (${actualLength}).`,
`It exceed maximum allowed length ${maxAllowedLength}.`,
`It exceed maximum allowed length ${maxAllowedLength} by (${maxAllowedLength - actualLength}) characTers.`,
'This may cause bugs due to unintended trimming by operating system, shells or terminal emulators.',
].join(' ');
}
Expand Down

0 comments on commit ebd07f0

Please sign in to comment.