From 2df0031d8f71c4677908f451d34282fc0c369c7d Mon Sep 17 00:00:00 2001 From: qcqcqc <1220204124@zust.edu.cn> Date: Mon, 5 Jan 2026 11:06:16 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E9=99=90=E5=88=B6=E9=94=99=E8=AF=AF?= =?UTF-8?q?=E4=BF=A1=E6=81=AF=E7=9A=84=E6=9C=80=E5=A4=A7=E8=BE=93=E5=87=BA?= =?UTF-8?q?=E9=95=BF=E5=BA=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/compiler/tscBuilder.js | 14 ++++++++++++-- src/compiler/tscBuilder.ts | 16 ++++++++++++++-- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/lib/compiler/tscBuilder.js b/lib/compiler/tscBuilder.js index b106508..a2cf043 100644 --- a/lib/compiler/tscBuilder.js +++ b/lib/compiler/tscBuilder.js @@ -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; diff --git a/src/compiler/tscBuilder.ts b/src/compiler/tscBuilder.ts index 0f3a623..06c7460 100644 --- a/src/compiler/tscBuilder.ts +++ b/src/compiler/tscBuilder.ts @@ -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) {