Compare commits

...

2 Commits

Author SHA1 Message Date
538f3a322f Format code 2025-07-01 23:27:21 -04:00
ab4ebbe78a Fix to show backtraces of errors in other files 2025-07-01 23:27:00 -04:00

View File

@ -371,53 +371,53 @@ export function kclErrorsToDiagnostics(
sourceCode: string sourceCode: string
): CodeMirrorDiagnostic[] { ): CodeMirrorDiagnostic[] {
let nonFatal: CodeMirrorDiagnostic[] = [] let nonFatal: CodeMirrorDiagnostic[] = []
const errs = errors const errs = errors.flatMap((err) => {
?.filter((err) => isTopLevelModule(err.sourceRange)) const diagnostics: CodeMirrorDiagnostic[] = []
.flatMap((err) => { let message = err.msg
const diagnostics: CodeMirrorDiagnostic[] = [] if (err.kclBacktrace.length > 0) {
let message = err.msg // Show the backtrace in the error message.
if (err.kclBacktrace.length > 0) { const backtraceLines: Array<string> = []
// Show the backtrace in the error message. for (let i = 0; i < err.kclBacktrace.length; i++) {
const backtraceLines: Array<string> = [] const item = err.kclBacktrace[i]
for (let i = 0; i < err.kclBacktrace.length; i++) { if (
const item = err.kclBacktrace[i] i > 0 &&
if ( isTopLevelModule(item.sourceRange) &&
i > 0 && !sourceRangeContains(item.sourceRange, err.sourceRange)
isTopLevelModule(item.sourceRange) && ) {
!sourceRangeContains(item.sourceRange, err.sourceRange) diagnostics.push({
) { from: toUtf16(item.sourceRange[0], sourceCode),
diagnostics.push({ to: toUtf16(item.sourceRange[1], sourceCode),
from: toUtf16(item.sourceRange[0], sourceCode), message: 'Part of the error backtrace',
to: toUtf16(item.sourceRange[1], sourceCode), severity: 'hint',
message: 'Part of the error backtrace', })
severity: 'hint',
})
}
if (i === err.kclBacktrace.length - 1 && !item.fnName) {
// The top-level doesn't have a name.
break
}
const name = item.fnName ? `${item.fnName}()` : '(anonymous)'
backtraceLines.push(name)
} }
// If the backtrace is only one line, it's not helpful to show. if (i === err.kclBacktrace.length - 1 && !item.fnName) {
if (backtraceLines.length > 1) { // The top-level doesn't have a name.
message += `\n\nBacktrace:\n${backtraceLines.join('\n')}` break
} }
const name = item.fnName ? `${item.fnName}()` : '(anonymous)'
backtraceLines.push(name)
} }
if (err.nonFatal.length > 0) { // If the backtrace is only one line, it's not helpful to show.
nonFatal = nonFatal.concat( if (backtraceLines.length > 1) {
compilationErrorsToDiagnostics(err.nonFatal, sourceCode) message += `\n\nBacktrace:\n${backtraceLines.join('\n')}`
)
} }
}
if (err.nonFatal.length > 0) {
nonFatal = nonFatal.concat(
compilationErrorsToDiagnostics(err.nonFatal, sourceCode)
)
}
if (isTopLevelModule(err.sourceRange)) {
diagnostics.push({ diagnostics.push({
from: toUtf16(err.sourceRange[0], sourceCode), from: toUtf16(err.sourceRange[0], sourceCode),
to: toUtf16(err.sourceRange[1], sourceCode), to: toUtf16(err.sourceRange[1], sourceCode),
message, message,
severity: 'error', severity: 'error',
}) })
return diagnostics }
}) return diagnostics
})
return errs.concat(nonFatal) return errs.concat(nonFatal)
} }