2023-08-28 14:58:24 -07:00
|
|
|
import { KclError as RustKclError } from '../wasm-lib/kcl/bindings/KclError'
|
2024-12-06 13:57:31 +13:00
|
|
|
import { CompilationError } from 'wasm-lib/kcl/bindings/CompilationError'
|
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'
|
2024-12-06 13:57:31 +13:00
|
|
|
import { EditorView } from 'codemirror'
|
2025-01-08 20:02:30 -05:00
|
|
|
import { ArtifactCommand, SourceRange } from 'lang/wasm'
|
2024-12-16 13:10:31 -05:00
|
|
|
import { Operation } from 'wasm-lib/kcl/bindings/Operation'
|
2024-11-07 11:23:41 -05:00
|
|
|
|
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-12-06 13:57:31 +13:00
|
|
|
sourceRange: SourceRange
|
2023-07-26 14:10:30 -05:00
|
|
|
msg: string
|
2024-12-16 13:10:31 -05:00
|
|
|
operations: Operation[]
|
2025-01-08 20:02:30 -05:00
|
|
|
artifactCommands: ArtifactCommand[]
|
2024-12-06 13:57:31 +13:00
|
|
|
|
2023-07-26 14:10:30 -05:00
|
|
|
constructor(
|
2023-08-22 13:28:02 +10:00
|
|
|
kind: ExtractKind<RustKclError> | 'name',
|
2023-07-26 14:10:30 -05:00
|
|
|
msg: string,
|
2024-12-16 13:10:31 -05:00
|
|
|
sourceRange: SourceRange,
|
2025-01-08 20:02:30 -05:00
|
|
|
operations: Operation[],
|
|
|
|
artifactCommands: ArtifactCommand[]
|
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
|
2024-12-06 13:57:31 +13:00
|
|
|
this.sourceRange = sourceRange
|
2024-12-16 13:10:31 -05:00
|
|
|
this.operations = operations
|
2025-01-08 20:02:30 -05:00
|
|
|
this.artifactCommands = artifactCommands
|
2023-07-26 14:10:30 -05:00
|
|
|
Object.setPrototypeOf(this, KCLError.prototype)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-01 17:20:49 -05:00
|
|
|
export class KCLLexicalError extends KCLError {
|
2025-01-08 20:02:30 -05:00
|
|
|
constructor(
|
|
|
|
msg: string,
|
|
|
|
sourceRange: SourceRange,
|
|
|
|
operations: Operation[],
|
|
|
|
artifactCommands: ArtifactCommand[]
|
|
|
|
) {
|
|
|
|
super('lexical', msg, sourceRange, operations, artifactCommands)
|
2023-11-01 17:20:49 -05:00
|
|
|
Object.setPrototypeOf(this, KCLSyntaxError.prototype)
|
2023-11-03 13:30:19 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export class KCLInternalError extends KCLError {
|
2025-01-08 20:02:30 -05:00
|
|
|
constructor(
|
|
|
|
msg: string,
|
|
|
|
sourceRange: SourceRange,
|
|
|
|
operations: Operation[],
|
|
|
|
artifactCommands: ArtifactCommand[]
|
|
|
|
) {
|
|
|
|
super('internal', msg, sourceRange, operations, artifactCommands)
|
2023-11-03 13:30:19 -05:00
|
|
|
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 {
|
2025-01-08 20:02:30 -05:00
|
|
|
constructor(
|
|
|
|
msg: string,
|
|
|
|
sourceRange: SourceRange,
|
|
|
|
operations: Operation[],
|
|
|
|
artifactCommands: ArtifactCommand[]
|
|
|
|
) {
|
|
|
|
super('syntax', msg, sourceRange, operations, artifactCommands)
|
2023-07-26 14:10:30 -05:00
|
|
|
Object.setPrototypeOf(this, KCLSyntaxError.prototype)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export class KCLSemanticError extends KCLError {
|
2025-01-08 20:02:30 -05:00
|
|
|
constructor(
|
|
|
|
msg: string,
|
|
|
|
sourceRange: SourceRange,
|
|
|
|
operations: Operation[],
|
|
|
|
artifactCommands: ArtifactCommand[]
|
|
|
|
) {
|
|
|
|
super('semantic', msg, sourceRange, operations, artifactCommands)
|
2023-07-26 14:10:30 -05:00
|
|
|
Object.setPrototypeOf(this, KCLSemanticError.prototype)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export class KCLTypeError extends KCLError {
|
2025-01-08 20:02:30 -05:00
|
|
|
constructor(
|
|
|
|
msg: string,
|
|
|
|
sourceRange: SourceRange,
|
|
|
|
operations: Operation[],
|
|
|
|
artifactCommands: ArtifactCommand[]
|
|
|
|
) {
|
|
|
|
super('type', msg, sourceRange, operations, artifactCommands)
|
2023-07-26 14:10:30 -05:00
|
|
|
Object.setPrototypeOf(this, KCLTypeError.prototype)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export class KCLUnimplementedError extends KCLError {
|
2025-01-08 20:02:30 -05:00
|
|
|
constructor(
|
|
|
|
msg: string,
|
|
|
|
sourceRange: SourceRange,
|
|
|
|
operations: Operation[],
|
|
|
|
artifactCommands: ArtifactCommand[]
|
|
|
|
) {
|
|
|
|
super('unimplemented', msg, sourceRange, operations, artifactCommands)
|
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 {
|
2025-01-08 20:02:30 -05:00
|
|
|
constructor(
|
|
|
|
msg: string,
|
|
|
|
sourceRange: SourceRange,
|
|
|
|
operations: Operation[],
|
|
|
|
artifactCommands: ArtifactCommand[]
|
|
|
|
) {
|
|
|
|
super('unexpected', msg, sourceRange, operations, artifactCommands)
|
2023-08-22 13:28:02 +10:00
|
|
|
Object.setPrototypeOf(this, KCLUnexpectedError.prototype)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-26 14:10:30 -05:00
|
|
|
export class KCLValueAlreadyDefined extends KCLError {
|
2025-01-08 20:02:30 -05:00
|
|
|
constructor(
|
|
|
|
key: string,
|
|
|
|
sourceRange: SourceRange,
|
|
|
|
operations: Operation[],
|
|
|
|
artifactCommands: ArtifactCommand[]
|
|
|
|
) {
|
2024-12-16 13:10:31 -05:00
|
|
|
super(
|
|
|
|
'name',
|
|
|
|
`Key ${key} was already defined elsewhere`,
|
|
|
|
sourceRange,
|
2025-01-08 20:02:30 -05:00
|
|
|
operations,
|
|
|
|
artifactCommands
|
2024-12-16 13:10:31 -05:00
|
|
|
)
|
2023-07-26 14:10:30 -05:00
|
|
|
Object.setPrototypeOf(this, KCLValueAlreadyDefined.prototype)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export class KCLUndefinedValueError extends KCLError {
|
2025-01-08 20:02:30 -05:00
|
|
|
constructor(
|
|
|
|
key: string,
|
|
|
|
sourceRange: SourceRange,
|
|
|
|
operations: Operation[],
|
|
|
|
artifactCommands: ArtifactCommand[]
|
|
|
|
) {
|
|
|
|
super(
|
|
|
|
'name',
|
|
|
|
`Key ${key} has not been defined`,
|
|
|
|
sourceRange,
|
|
|
|
operations,
|
|
|
|
artifactCommands
|
|
|
|
)
|
2023-07-26 14:10:30 -05:00
|
|
|
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 }) =>
|
2024-12-16 13:10:31 -05:00
|
|
|
new KCLError(
|
|
|
|
'unexpected',
|
|
|
|
message,
|
|
|
|
[posToOffset(doc, range.start)!, posToOffset(doc, range.end)!, true],
|
2025-01-08 20:02:30 -05:00
|
|
|
[],
|
2024-12-16 13:10:31 -05:00
|
|
|
[]
|
|
|
|
)
|
2024-04-15 17:18:32 -07:00
|
|
|
)
|
|
|
|
.sort((a, b) => {
|
2024-12-06 13:57:31 +13:00
|
|
|
const c = a.sourceRange[0]
|
|
|
|
const d = b.sourceRange[0]
|
2024-04-15 17:18:32 -07:00
|
|
|
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[] {
|
2024-12-06 13:57:31 +13:00
|
|
|
return errors
|
|
|
|
?.filter((err) => err.sourceRange[2])
|
|
|
|
.map((err) => {
|
|
|
|
return {
|
|
|
|
from: err.sourceRange[0],
|
|
|
|
to: err.sourceRange[1],
|
|
|
|
message: err.msg,
|
|
|
|
severity: 'error',
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
export function complilationErrorsToDiagnostics(
|
|
|
|
errors: CompilationError[]
|
|
|
|
): CodeMirrorDiagnostic[] {
|
|
|
|
return errors
|
|
|
|
?.filter((err) => err.sourceRange[2] === 0)
|
|
|
|
.map((err) => {
|
|
|
|
let severity: any = 'error'
|
|
|
|
if (err.severity === 'Warning') {
|
|
|
|
severity = 'warning'
|
|
|
|
}
|
|
|
|
let actions
|
|
|
|
const suggestion = err.suggestion
|
|
|
|
if (suggestion) {
|
|
|
|
actions = [
|
|
|
|
{
|
|
|
|
name: suggestion.title,
|
|
|
|
apply: (view: EditorView, from: number, to: number) => {
|
|
|
|
view.dispatch({
|
|
|
|
changes: { from, to, insert: suggestion.insert },
|
|
|
|
})
|
|
|
|
},
|
|
|
|
},
|
|
|
|
]
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
from: err.sourceRange[0],
|
|
|
|
to: err.sourceRange[1],
|
|
|
|
message: err.message,
|
|
|
|
severity,
|
|
|
|
actions,
|
|
|
|
}
|
|
|
|
})
|
2023-08-03 15:56:11 -05:00
|
|
|
}
|