fix: 限制错误信息的最大输出长度

This commit is contained in:
Pan Qiancheng 2026-01-05 11:06:16 +08:00
parent 86b9f217af
commit 2df0031d8f
2 changed files with 26 additions and 4 deletions

View File

@ -74,9 +74,19 @@ function printDiagnostic(diagnostic, index) {
const lineStart = sourceFile.getPositionOfLineAndCharacter(line, 0);
const lineEnd = sourceFile.getPositionOfLineAndCharacter(line + 1, 0);
const lineText = sourceFile.text.substring(lineStart, lineEnd).trimEnd();
// 限制显示的最大字符数,避免输出过长
const maxDisplayLength = 100;
const displayText = lineText.length > maxDisplayLength
? lineText.substring(0, maxDisplayLength) + colors.gray + '...' + colors.reset
: lineText;
// 如果代码被截断,调整错误标记的显示位置
const effectiveCharacter = character < maxDisplayLength ? character : maxDisplayLength - 1;
const effectiveLength = character + (diagnostic.length || 0) <= maxDisplayLength
? Math.max(1, diagnostic.length || 0)
: Math.max(1, maxDisplayLength - character);
console.log(`${colors.cyan}${colors.reset}`);
console.log(`${colors.cyan}${colors.reset} ${colors.gray}${line + 1}${colors.reset}${lineText}`);
console.log(`${colors.cyan}${colors.reset} ${' '.repeat(String(line + 1).length)}${' '.repeat(character)}${colors.red}${'~'.repeat(Math.max(1, diagnostic.length || 0))}${colors.reset}`);
console.log(`${colors.cyan}${colors.reset} ${colors.gray}${line + 1}${colors.reset}${displayText}`);
console.log(`${colors.cyan}${colors.reset} ${' '.repeat(String(line + 1).length)}${' '.repeat(effectiveCharacter)}${colors.red}${'~'.repeat(effectiveLength)}${colors.reset}`);
// 如果是自定义诊断,显示额外信息
if (isCustom) {
const customDiag = diagnostic;

View File

@ -132,9 +132,21 @@ function printDiagnostic(diagnostic: ts.Diagnostic | CustomDiagnostic, index: nu
const lineEnd = sourceFile.getPositionOfLineAndCharacter(line + 1, 0);
const lineText = sourceFile.text.substring(lineStart, lineEnd).trimEnd();
// 限制显示的最大字符数,避免输出过长
const maxDisplayLength = 100;
const displayText = lineText.length > maxDisplayLength
? lineText.substring(0, maxDisplayLength) + colors.gray + '...' + colors.reset
: lineText;
// 如果代码被截断,调整错误标记的显示位置
const effectiveCharacter = character < maxDisplayLength ? character : maxDisplayLength - 1;
const effectiveLength = character + (diagnostic.length || 0) <= maxDisplayLength
? Math.max(1, diagnostic.length || 0)
: Math.max(1, maxDisplayLength - character);
console.log(`${colors.cyan}${colors.reset}`);
console.log(`${colors.cyan}${colors.reset} ${colors.gray}${line + 1}${colors.reset}${lineText}`);
console.log(`${colors.cyan}${colors.reset} ${' '.repeat(String(line + 1).length)}${' '.repeat(character)}${colors.red}${'~'.repeat(Math.max(1, diagnostic.length || 0))}${colors.reset}`);
console.log(`${colors.cyan}${colors.reset} ${colors.gray}${line + 1}${colors.reset}${displayText}`);
console.log(`${colors.cyan}${colors.reset} ${' '.repeat(String(line + 1).length)}${' '.repeat(effectiveCharacter)}${colors.red}${'~'.repeat(effectiveLength)}${colors.reset}`);
// 如果是自定义诊断,显示额外信息
if (isCustom) {