2023-08-28 14:58:24 -07:00
|
|
|
import { KclError as RustKclError } from '../wasm-lib/kcl/bindings/KclError'
|
2024-04-15 17:18:32 -07:00
|
|
|
import { Diagnostic as CodeMirrorDiagnostic } from '@codemirror/lint'
|
2024-06-30 14:30:44 -07:00
|
|
|
import { posToOffset } from '@kittycad/codemirror-lsp-client'
|
2024-04-15 17:18:32 -07:00
|
|
|
import { Diagnostic as LspDiagnostic } from 'vscode-languageserver-protocol'
|
|
|
|
import { Text } from '@codemirror/state'
|
2023-08-03 15:56:11 -05:00
|
|
|
|
2024-11-07 11:23:41 -05:00
|
|
|
const TOP_LEVEL_MODULE_ID = 0
|
|
|
|
|
2023-08-22 13:28:02 +10:00
|
|
|
type ExtractKind<T> = T extends { kind: infer K } ? K : never
|
2024-06-24 11:45:40 -04:00
|
|
|
export class KCLError extends Error {
|
2023-08-22 13:28:02 +10:00
|
|
|
kind: ExtractKind<RustKclError> | 'name'
|
2024-11-07 11:23:41 -05:00
|
|
|
sourceRanges: [number, number, number][]
|
2023-07-26 14:10:30 -05:00
|
|
|
msg: string
|
|
|
|
constructor(
|
2023-08-22 13:28:02 +10:00
|
|
|
kind: ExtractKind<RustKclError> | 'name',
|
2023-07-26 14:10:30 -05:00
|
|
|
msg: string,
|
2024-11-07 11:23:41 -05:00
|
|
|
sourceRanges: [number, number, number][]
|
2023-07-26 14:10:30 -05:00
|
|
|
) {
|
2024-06-24 11:45:40 -04:00
|
|
|
super()
|
2023-07-26 14:10:30 -05:00
|
|
|
this.kind = kind
|
|
|
|
this.msg = msg
|
|
|
|
this.sourceRanges = sourceRanges
|
|
|
|
Object.setPrototypeOf(this, KCLError.prototype)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-01 17:20:49 -05:00
|
|
|
export class KCLLexicalError extends KCLError {
|
2024-11-07 11:23:41 -05:00
|
|
|
constructor(msg: string, sourceRanges: [number, number, number][]) {
|
2023-11-01 17:20:49 -05:00
|
|
|
super('lexical', msg, sourceRanges)
|
|
|
|
Object.setPrototypeOf(this, KCLSyntaxError.prototype)
|
2023-11-03 13:30:19 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export class KCLInternalError extends KCLError {
|
2024-11-07 11:23:41 -05:00
|
|
|
constructor(msg: string, sourceRanges: [number, number, number][]) {
|
2023-11-03 13:30:19 -05:00
|
|
|
super('internal', msg, sourceRanges)
|
|
|
|
Object.setPrototypeOf(this, KCLSyntaxError.prototype)
|
2023-11-01 17:20:49 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-26 14:10:30 -05:00
|
|
|
export class KCLSyntaxError extends KCLError {
|
2024-11-07 11:23:41 -05:00
|
|
|
constructor(msg: string, sourceRanges: [number, number, number][]) {
|
2023-07-26 14:10:30 -05:00
|
|
|
super('syntax', msg, sourceRanges)
|
|
|
|
Object.setPrototypeOf(this, KCLSyntaxError.prototype)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export class KCLSemanticError extends KCLError {
|
2024-11-07 11:23:41 -05:00
|
|
|
constructor(msg: string, sourceRanges: [number, number, number][]) {
|
2023-07-26 14:10:30 -05:00
|
|
|
super('semantic', msg, sourceRanges)
|
|
|
|
Object.setPrototypeOf(this, KCLSemanticError.prototype)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export class KCLTypeError extends KCLError {
|
2024-11-07 11:23:41 -05:00
|
|
|
constructor(msg: string, sourceRanges: [number, number, number][]) {
|
2023-07-26 14:10:30 -05:00
|
|
|
super('type', msg, sourceRanges)
|
|
|
|
Object.setPrototypeOf(this, KCLTypeError.prototype)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export class KCLUnimplementedError extends KCLError {
|
2024-11-07 11:23:41 -05:00
|
|
|
constructor(msg: string, sourceRanges: [number, number, number][]) {
|
2023-08-22 13:28:02 +10:00
|
|
|
super('unimplemented', msg, sourceRanges)
|
2023-07-26 14:10:30 -05:00
|
|
|
Object.setPrototypeOf(this, KCLUnimplementedError.prototype)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-22 13:28:02 +10:00
|
|
|
export class KCLUnexpectedError extends KCLError {
|
2024-11-07 11:23:41 -05:00
|
|
|
constructor(msg: string, sourceRanges: [number, number, number][]) {
|
2023-08-22 13:28:02 +10:00
|
|
|
super('unexpected', msg, sourceRanges)
|
|
|
|
Object.setPrototypeOf(this, KCLUnexpectedError.prototype)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-26 14:10:30 -05:00
|
|
|
export class KCLValueAlreadyDefined extends KCLError {
|
2024-11-07 11:23:41 -05:00
|
|
|
constructor(key: string, sourceRanges: [number, number, number][]) {
|
2023-07-26 14:10:30 -05:00
|
|
|
super('name', `Key ${key} was already defined elsewhere`, sourceRanges)
|
|
|
|
Object.setPrototypeOf(this, KCLValueAlreadyDefined.prototype)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export class KCLUndefinedValueError extends KCLError {
|
2024-11-07 11:23:41 -05:00
|
|
|
constructor(key: string, sourceRanges: [number, number, number][]) {
|
2023-07-26 14:10:30 -05:00
|
|
|
super('name', `Key ${key} has not been defined`, sourceRanges)
|
|
|
|
Object.setPrototypeOf(this, KCLUndefinedValueError.prototype)
|
|
|
|
}
|
|
|
|
}
|
2023-08-03 15:56:11 -05:00
|
|
|
|
2024-04-15 17:18:32 -07:00
|
|
|
/**
|
|
|
|
* Maps the lsp diagnostic to an array of KclErrors.
|
|
|
|
* Currently the diagnostics are all errors, but in the future they could include lints.
|
|
|
|
* */
|
|
|
|
export function lspDiagnosticsToKclErrors(
|
|
|
|
doc: Text,
|
|
|
|
diagnostics: LspDiagnostic[]
|
|
|
|
): KCLError[] {
|
|
|
|
return diagnostics
|
|
|
|
.flatMap(
|
|
|
|
({ range, message }) =>
|
|
|
|
new KCLError('unexpected', message, [
|
2024-11-07 11:23:41 -05:00
|
|
|
[
|
|
|
|
posToOffset(doc, range.start)!,
|
|
|
|
posToOffset(doc, range.end)!,
|
|
|
|
TOP_LEVEL_MODULE_ID,
|
|
|
|
],
|
2024-04-15 17:18:32 -07:00
|
|
|
])
|
|
|
|
)
|
|
|
|
.filter(({ sourceRanges }) => {
|
2024-11-07 11:23:41 -05:00
|
|
|
const [from, to, moduleId] = sourceRanges[0]
|
2024-04-15 17:18:32 -07:00
|
|
|
return (
|
2024-11-07 11:23:41 -05:00
|
|
|
from !== null &&
|
|
|
|
to !== null &&
|
|
|
|
from !== undefined &&
|
|
|
|
to !== undefined &&
|
|
|
|
// Filter out errors that are not from the top-level module.
|
|
|
|
moduleId === TOP_LEVEL_MODULE_ID
|
2024-04-15 17:18:32 -07:00
|
|
|
)
|
|
|
|
})
|
|
|
|
.sort((a, b) => {
|
|
|
|
const c = a.sourceRanges[0][0]
|
|
|
|
const d = b.sourceRanges[0][0]
|
|
|
|
switch (true) {
|
|
|
|
case c < d:
|
|
|
|
return -1
|
|
|
|
case c > d:
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
return 0
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-08-03 15:56:11 -05:00
|
|
|
/**
|
|
|
|
* Maps the KCL errors to an array of CodeMirror diagnostics.
|
|
|
|
* Currently the diagnostics are all errors, but in the future they could include lints.
|
|
|
|
* */
|
2024-04-15 17:18:32 -07:00
|
|
|
export function kclErrorsToDiagnostics(
|
|
|
|
errors: KCLError[]
|
|
|
|
): CodeMirrorDiagnostic[] {
|
2023-10-11 13:36:54 +11:00
|
|
|
return errors?.flatMap((err) => {
|
2024-11-07 11:23:41 -05:00
|
|
|
const sourceRanges: CodeMirrorDiagnostic[] = err.sourceRanges
|
|
|
|
// Filter out errors that are not from the top-level module.
|
|
|
|
.filter(([_start, _end, moduleId]) => moduleId === TOP_LEVEL_MODULE_ID)
|
|
|
|
.map(([from, to]) => {
|
|
|
|
return { from, to, message: err.msg, severity: 'error' }
|
|
|
|
})
|
|
|
|
// Make sure we didn't filter out all the source ranges.
|
|
|
|
if (sourceRanges.length === 0) {
|
|
|
|
sourceRanges.push({ from: 0, to: 0, message: err.msg, severity: 'error' })
|
|
|
|
}
|
|
|
|
return sourceRanges
|
2023-08-03 15:56:11 -05:00
|
|
|
})
|
|
|
|
}
|