Remove editor from validator

This commit is contained in:
Adria Navarro 2025-02-17 10:35:29 +01:00
parent 77d2ac0629
commit e266c1b8cf
2 changed files with 16 additions and 13 deletions

View File

@ -345,17 +345,17 @@
function validate( function validate(
value: string | null, value: string | null,
editor: EditorView, editor: EditorView | undefined,
mode: EditorMode, mode: EditorMode,
validations: CodeValidator | null validations: CodeValidator | null
) { ) {
if (!value || !validations) { if (!value || !validations || !editor) {
return return
} }
if (mode === EditorModes.Handlebars) { if (mode === EditorModes.Handlebars) {
const diagnostics = validateHbsTemplate(editor, value, validations) const diagnostics = validateHbsTemplate(value, validations)
editor?.dispatch(setDiagnostics(editor.state, diagnostics)) editor.dispatch(setDiagnostics(editor.state, diagnostics))
} }
} }

View File

@ -1,6 +1,5 @@
/* global hbs */ /* global hbs */
import Handlebars from "handlebars" import Handlebars from "handlebars"
import { EditorView } from "@codemirror/view"
import type { Diagnostic } from "@codemirror/lint" import type { Diagnostic } from "@codemirror/lint"
import { CodeValidator } from "@/types" import { CodeValidator } from "@/types"
@ -23,14 +22,20 @@ function isPathExpression(
} }
export function validateHbsTemplate( export function validateHbsTemplate(
editor: EditorView, text: string,
template: string,
validations: CodeValidator validations: CodeValidator
): Diagnostic[] { ): Diagnostic[] {
const diagnostics: Diagnostic[] = [] const diagnostics: Diagnostic[] = []
try { try {
const ast = Handlebars.parse(template, {}) const ast = Handlebars.parse(text, {})
const lineOffsets: number[] = []
let offset = 0
for (const line of text.split("\n")) {
lineOffsets.push(offset)
offset += line.length + 1 // +1 for newline character
}
function traverseNodes( function traverseNodes(
nodes: hbs.AST.Statement[], nodes: hbs.AST.Statement[],
@ -44,10 +49,8 @@ export function validateHbsTemplate(
const helperName = node.path.original const helperName = node.path.original
const from = const from =
editor.state.doc.line(node.loc.start.line).from + lineOffsets[node.loc.start.line - 1] + node.loc.start.column
node.loc.start.column const to = lineOffsets[node.loc.end.line - 1] + node.loc.end.column
const to =
editor.state.doc.line(node.loc.end.line).from + node.loc.end.column
if (!(helperName in validations)) { if (!(helperName in validations)) {
if (!ignoreMissing) { if (!ignoreMissing) {
@ -100,7 +103,7 @@ export function validateHbsTemplate(
} catch (e: any) { } catch (e: any) {
diagnostics.push({ diagnostics.push({
from: 0, from: 0,
to: template.length, to: text.length,
severity: "error", severity: "error",
message: `Syntax error: ${e.message}`, message: `Syntax error: ${e.message}`,
}) })