Format code

This commit is contained in:
Jonathan Tran
2025-07-01 23:27:21 -04:00
parent ab4ebbe78a
commit 538f3a322f

View File

@ -371,54 +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) => {
.flatMap((err) => { const diagnostics: CodeMirrorDiagnostic[] = []
const diagnostics: CodeMirrorDiagnostic[] = [] let message = err.msg
let message = err.msg if (err.kclBacktrace.length > 0) {
if (err.kclBacktrace.length > 0) { // Show the backtrace in the error message.
// Show the backtrace in the error message. const backtraceLines: Array<string> = []
const backtraceLines: Array<string> = [] for (let i = 0; i < err.kclBacktrace.length; i++) {
for (let i = 0; i < err.kclBacktrace.length; i++) { const item = err.kclBacktrace[i]
const item = err.kclBacktrace[i] if (
if ( i > 0 &&
i > 0 && isTopLevelModule(item.sourceRange) &&
isTopLevelModule(item.sourceRange) && !sourceRangeContains(item.sourceRange, err.sourceRange)
!sourceRangeContains(item.sourceRange, err.sourceRange) ) {
) { diagnostics.push({
diagnostics.push({ from: toUtf16(item.sourceRange[0], sourceCode),
from: toUtf16(item.sourceRange[0], sourceCode), to: toUtf16(item.sourceRange[1], sourceCode),
to: toUtf16(item.sourceRange[1], sourceCode), message: 'Part of the error backtrace',
message: 'Part of the error backtrace', severity: 'hint',
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 (isTopLevelModule(err.sourceRange)) { }
diagnostics.push({ if (err.nonFatal.length > 0) {
from: toUtf16(err.sourceRange[0], sourceCode), nonFatal = nonFatal.concat(
to: toUtf16(err.sourceRange[1], sourceCode), compilationErrorsToDiagnostics(err.nonFatal, sourceCode)
message, )
severity: 'error', }
}) if (isTopLevelModule(err.sourceRange)) {
} diagnostics.push({
return diagnostics from: toUtf16(err.sourceRange[0], sourceCode),
}) to: toUtf16(err.sourceRange[1], sourceCode),
message,
severity: 'error',
})
}
return diagnostics
})
return errs.concat(nonFatal) return errs.concat(nonFatal)
} }