Initial js validation
This commit is contained in:
parent
72074dfd60
commit
7bac376599
|
@ -49,6 +49,7 @@
|
|||
import type { EditorMode } from "@budibase/types"
|
||||
import type { BindingCompletion, CodeValidator } from "@/types"
|
||||
import { validateHbsTemplate } from "./validator/hbs"
|
||||
import { validateJsTemplate } from "./validator/js"
|
||||
|
||||
export let label: string | undefined = undefined
|
||||
export let completions: BindingCompletion[] = []
|
||||
|
@ -356,6 +357,9 @@
|
|||
if (mode === EditorModes.Handlebars) {
|
||||
const diagnostics = validateHbsTemplate(value, validations)
|
||||
editor.dispatch(setDiagnostics(editor.state, diagnostics))
|
||||
} else if (mode === EditorModes.JS) {
|
||||
const diagnostics = validateJsTemplate(value, validations)
|
||||
editor.dispatch(setDiagnostics(editor.state, diagnostics))
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,80 @@
|
|||
import { Parser } from "acorn"
|
||||
import { simple as walk } from "acorn-walk"
|
||||
|
||||
import { iifeWrapper } from "@budibase/string-templates"
|
||||
import type { Diagnostic } from "@codemirror/lint"
|
||||
import { CodeValidator } from "@/types"
|
||||
|
||||
export function validateJsTemplate(
|
||||
code: string,
|
||||
validations: CodeValidator
|
||||
): Diagnostic[] {
|
||||
const diagnostics: Diagnostic[] = []
|
||||
|
||||
try {
|
||||
// const helperUsages = new RegExp(/\bhelpers\.(\w)+\b/).exec(code)
|
||||
const ast = Parser.parse(iifeWrapper(code), {
|
||||
ecmaVersion: "latest",
|
||||
locations: true,
|
||||
})
|
||||
|
||||
const lineOffsets: number[] = [0]
|
||||
let offset = 0
|
||||
for (const line of code.split("\n")) {
|
||||
lineOffsets.push(offset)
|
||||
offset += line.length + 1 // +1 for newline character
|
||||
}
|
||||
|
||||
walk(ast, {
|
||||
CallExpression(node) {
|
||||
const callee: any = node.callee
|
||||
if (
|
||||
node.type === "CallExpression" &&
|
||||
callee.object?.name === "helpers" &&
|
||||
node.loc
|
||||
) {
|
||||
const functionName = callee.property.name
|
||||
const from =
|
||||
lineOffsets[node.loc.start.line - 1] + node.loc.start.column
|
||||
const to = lineOffsets[node.loc.end.line - 1] + node.loc.end.column
|
||||
|
||||
if (!(functionName in validations)) {
|
||||
diagnostics.push({
|
||||
from,
|
||||
to,
|
||||
severity: "warning",
|
||||
message: `"${functionName}" function does not exist.`,
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
const { arguments: expectedArguments } = validations[functionName]
|
||||
if (
|
||||
expectedArguments &&
|
||||
node.arguments.length !== expectedArguments.length
|
||||
) {
|
||||
diagnostics.push({
|
||||
from,
|
||||
to,
|
||||
severity: "error",
|
||||
message: `Function "${functionName}" expects ${
|
||||
expectedArguments.length
|
||||
} parameters (${expectedArguments.join(", ")}), but got ${
|
||||
node.arguments.length
|
||||
}.`,
|
||||
})
|
||||
}
|
||||
}
|
||||
},
|
||||
})
|
||||
} catch (e: any) {
|
||||
diagnostics.push({
|
||||
from: 0,
|
||||
to: code.length,
|
||||
severity: "error",
|
||||
message: `Syntax error: ${e.message}`,
|
||||
})
|
||||
}
|
||||
|
||||
return diagnostics
|
||||
}
|
|
@ -377,6 +377,7 @@
|
|||
value={jsValue ? decodeJSBinding(jsValue) : jsValue}
|
||||
on:change={onChangeJSValue}
|
||||
{completions}
|
||||
{validations}
|
||||
mode={EditorModes.JS}
|
||||
bind:getCaretPosition
|
||||
bind:insertAtPos
|
||||
|
|
Loading…
Reference in New Issue