2023-08-03 15:56:11 -05:00
|
|
|
import { Diagnostic } from '@codemirror/lint'
|
2023-08-28 14:58:24 -07:00
|
|
|
import { KclError as RustKclError } from '../wasm-lib/kcl/bindings/KclError'
|
2023-08-03 15:56:11 -05:00
|
|
|
|
2023-08-22 13:28:02 +10:00
|
|
|
type ExtractKind<T> = T extends { kind: infer K } ? K : never
|
2023-07-26 14:10:30 -05:00
|
|
|
export class KCLError {
|
2023-08-22 13:28:02 +10:00
|
|
|
kind: ExtractKind<RustKclError> | 'name'
|
2023-07-26 14:10:30 -05:00
|
|
|
sourceRanges: [number, number][]
|
|
|
|
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,
|
|
|
|
sourceRanges: [number, number][]
|
|
|
|
) {
|
|
|
|
this.kind = kind
|
|
|
|
this.msg = msg
|
|
|
|
this.sourceRanges = sourceRanges
|
|
|
|
Object.setPrototypeOf(this, KCLError.prototype)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export class KCLSyntaxError extends KCLError {
|
|
|
|
constructor(msg: string, sourceRanges: [number, number][]) {
|
|
|
|
super('syntax', msg, sourceRanges)
|
|
|
|
Object.setPrototypeOf(this, KCLSyntaxError.prototype)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export class KCLSemanticError extends KCLError {
|
|
|
|
constructor(msg: string, sourceRanges: [number, number][]) {
|
|
|
|
super('semantic', msg, sourceRanges)
|
|
|
|
Object.setPrototypeOf(this, KCLSemanticError.prototype)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export class KCLTypeError extends KCLError {
|
|
|
|
constructor(msg: string, sourceRanges: [number, number][]) {
|
|
|
|
super('type', msg, sourceRanges)
|
|
|
|
Object.setPrototypeOf(this, KCLTypeError.prototype)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export class KCLUnimplementedError extends KCLError {
|
|
|
|
constructor(msg: string, sourceRanges: [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 {
|
|
|
|
constructor(msg: string, sourceRanges: [number, number][]) {
|
|
|
|
super('unexpected', msg, sourceRanges)
|
|
|
|
Object.setPrototypeOf(this, KCLUnexpectedError.prototype)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-26 14:10:30 -05:00
|
|
|
export class KCLValueAlreadyDefined extends KCLError {
|
|
|
|
constructor(key: string, sourceRanges: [number, number][]) {
|
|
|
|
super('name', `Key ${key} was already defined elsewhere`, sourceRanges)
|
|
|
|
Object.setPrototypeOf(this, KCLValueAlreadyDefined.prototype)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export class KCLUndefinedValueError extends KCLError {
|
|
|
|
constructor(key: string, sourceRanges: [number, number][]) {
|
|
|
|
super('name', `Key ${key} has not been defined`, sourceRanges)
|
|
|
|
Object.setPrototypeOf(this, KCLUndefinedValueError.prototype)
|
|
|
|
}
|
|
|
|
}
|
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.
|
|
|
|
* */
|
|
|
|
export function kclErrToDiagnostic(errors: KCLError[]): Diagnostic[] {
|
2023-10-11 13:36:54 +11:00
|
|
|
return errors?.flatMap((err) => {
|
2023-08-03 15:56:11 -05:00
|
|
|
return err.sourceRanges.map(([from, to]) => {
|
|
|
|
return { from, to, message: err.msg, severity: 'error' }
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|