From ebd07f01813ec720dd899c2f81dabe48fde54144 Mon Sep 17 00:00:00 2001 From: undergroundwires Date: Sat, 14 Sep 2024 11:27:22 +0200 Subject: [PATCH] Improve compiler output for line validation 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 --- .../Analyzers/AnalyzeTooLongLines.ts | 2 +- .../Script/Validation/CodeValidator.ts | 32 ++++++++++++++++--- .../Analyzers/AnalyzeTooLongLines.spec.ts | 2 +- 3 files changed, 30 insertions(+), 6 deletions(-) diff --git a/src/application/Parser/Executable/Script/Validation/Analyzers/AnalyzeTooLongLines.ts b/src/application/Parser/Executable/Script/Validation/Analyzers/AnalyzeTooLongLines.ts index fc8c5da1..7ff237a8 100644 --- a/src/application/Parser/Executable/Script/Validation/Analyzers/AnalyzeTooLongLines.ts +++ b/src/application/Parser/Executable/Script/Validation/Analyzers/AnalyzeTooLongLines.ts @@ -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(' '), })); diff --git a/src/application/Parser/Executable/Script/Validation/CodeValidator.ts b/src/application/Parser/Executable/Script/Validation/CodeValidator.ts index f89e5c8a..1bc3826a 100644 --- a/src/application/Parser/Executable/Script/Validation/CodeValidator.ts +++ b/src/application/Parser/Executable/Script/Validation/CodeValidator.ts @@ -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; +} diff --git a/tests/unit/application/Parser/Executable/Script/Validation/Analyzers/AnalyzeTooLongLines.spec.ts b/tests/unit/application/Parser/Executable/Script/Validation/Analyzers/AnalyzeTooLongLines.spec.ts index ac4cd120..b77b34cf 100644 --- a/tests/unit/application/Parser/Executable/Script/Validation/Analyzers/AnalyzeTooLongLines.spec.ts +++ b/tests/unit/application/Parser/Executable/Script/Validation/Analyzers/AnalyzeTooLongLines.spec.ts @@ -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(' '); }