silverbullet/plugs/index/lint.ts

261 lines
7.5 KiB
TypeScript
Raw Permalink Normal View History

2024-08-15 22:39:06 +08:00
import {
jsonschema,
lua,
2024-08-15 22:39:06 +08:00
system,
YAML,
} from "@silverbulletmd/silverbullet/syscalls";
2024-07-30 23:33:33 +08:00
import type { LintDiagnostic, QueryExpression } from "../../plug-api/types.ts";
2024-02-29 22:23:05 +08:00
import {
findNodeOfType,
renderToText,
traverseTreeAsync,
} from "@silverbulletmd/silverbullet/lib/tree";
2024-07-30 23:33:33 +08:00
import type { LintEvent } from "../../plug-api/types.ts";
2023-12-22 01:37:50 +08:00
import { queryObjects } from "./api.ts";
import type { AdhocAttributeObject } from "./attributes.ts";
import { extractFrontmatter } from "@silverbulletmd/silverbullet/lib/frontmatter";
2024-08-15 22:39:06 +08:00
import {
cleanupJSON,
deepObjectMerge,
} from "@silverbulletmd/silverbullet/lib/json";
2023-11-21 23:56:21 +08:00
export async function lintYAML({ tree }: LintEvent): Promise<LintDiagnostic[]> {
const diagnostics: LintDiagnostic[] = [];
2023-12-22 01:37:50 +08:00
const frontmatter = await extractFrontmatter(tree);
const tags = ["page", ...frontmatter.tags || []];
const schemaConfig = await system.getSpaceConfig("schema", {});
await traverseTreeAsync(tree, async (node) => {
if (node.type === "FrontMatterCode") {
// Query all readOnly attributes for pages with this tag set
const readOnlyAttributes = await queryObjects<AdhocAttributeObject>(
"ah-attr",
{
filter: ["and", ["=", ["attr", "tagName"], [
"array",
tags.map((tag): QueryExpression => ["string", tag]),
]], [
"=",
["attr", "readOnly"],
["boolean", true],
]],
distinct: true,
select: [{ name: "name" }],
},
);
2024-08-15 22:39:06 +08:00
// Check if we have schema for this
let schema = {
type: "object",
additionalProperties: true,
};
for (const tag of tags) {
if (schemaConfig.tag[tag]) {
schema = deepObjectMerge(schema, schemaConfig.tag[tag]);
}
}
const lintResult = await lintYaml(
renderToText(node),
node.from!,
2023-12-22 01:37:50 +08:00
readOnlyAttributes.map((a) => a.name),
2024-08-15 22:39:06 +08:00
schema,
);
if (lintResult) {
diagnostics.push(lintResult);
}
return true;
}
if (node.type === "FencedCode") {
const codeInfo = findNodeOfType(node, "CodeInfo")!;
if (!codeInfo) {
return true;
}
const codeLang = codeInfo.children![0].text!;
// All known YAML formats
if (
["include", "embed", "yaml", "space-config"].includes(codeLang) ||
codeLang.startsWith("#")
) {
const codeText = findNodeOfType(node, "CodeText");
if (!codeText) {
return true;
}
const yamlCode = renderToText(codeText);
2024-08-15 22:39:06 +08:00
let lintResult: LintDiagnostic | undefined;
if (codeLang === "space-config") {
// First validate that config schema itself is valid
let schemaResult = await jsonschema.validateSchema(
schemaConfig.config,
);
if (schemaResult) {
lintResult = {
from: codeText.from!,
to: codeText.to!,
severity: "error",
message: "[CONFIG SCHEMA ERROR]: " + schemaResult,
};
}
// Lint the actual YAML
if (!lintResult) {
// First do a regular YAML lint based on the schema
lintResult = await lintYaml(
yamlCode,
codeText.from!,
[],
schemaConfig.config,
);
}
// Then check the tag schemas
if (!lintResult) {
// Quickly parse YAML again
let parsed = await YAML.parse(yamlCode);
parsed = cleanupJSON(parsed);
// If tag schemas are defined, validate them
if (parsed?.schema?.tag) {
2024-08-15 22:39:06 +08:00
for (
let [tagName, tagSchema] of Object.entries(parsed.schema.tag)
) {
tagSchema = deepObjectMerge({ type: "object" }, tagSchema);
schemaResult = await jsonschema.validateSchema(tagSchema);
if (schemaResult) {
lintResult = {
from: codeText.from!,
to: codeText.to!,
severity: "error",
message: `[TAG ${tagName} SCHEMA ERROR]: ${schemaResult}`,
};
break;
}
}
}
}
} else {
// Regular YAML lint
lintResult = await lintYaml(
yamlCode,
codeText.from!,
[],
);
}
if (lintResult) {
diagnostics.push(lintResult);
}
return true;
}
}
return false;
});
return diagnostics;
}
2023-11-29 23:51:28 +08:00
const errorRegex = /\((\d+):(\d+)\)/;
async function lintYaml(
yamlText: string,
2024-08-15 22:39:06 +08:00
startPos: number,
readOnlyKeys: string[] = [],
2024-08-15 22:39:06 +08:00
schema?: any,
): Promise<LintDiagnostic | undefined> {
try {
2024-08-15 22:39:06 +08:00
let parsed = await YAML.parse(yamlText);
parsed = cleanupJSON(parsed);
for (const key of readOnlyKeys) {
2023-12-22 01:37:50 +08:00
if (parsed[key]) {
return {
2024-08-15 22:39:06 +08:00
from: startPos,
to: startPos + yamlText.length,
2023-12-22 01:37:50 +08:00
severity: "error",
message: `Cannot set read-only attribute "${key}"`,
2023-12-22 01:37:50 +08:00
};
}
}
2024-08-15 22:39:06 +08:00
if (schema) {
// First validate the schema itself
const schemaResult = await jsonschema.validateSchema(schema);
if (schemaResult) {
return {
from: startPos,
to: startPos + yamlText.length,
severity: "error",
message: "[SCHEMA ERROR]: " + schemaResult,
};
}
// Then validate the object
const result = await jsonschema.validateObject(schema, parsed);
if (result) {
return {
from: startPos,
to: startPos + yamlText.length,
severity: "error",
message: result,
};
}
}
2024-10-10 18:52:28 +08:00
} catch (e: any) {
const errorMatch = errorRegex.exec(e.message);
if (errorMatch) {
console.log("YAML error", e.message);
const line = parseInt(errorMatch[1], 10) - 1;
const yamlLines = yamlText.split("\n");
2024-08-15 22:39:06 +08:00
let pos = startPos;
for (let i = 0; i < line; i++) {
pos += yamlLines[i].length + 1;
}
const endPos = pos + yamlLines[line]?.length || pos;
return {
from: pos,
to: endPos,
severity: "error",
message: e.message,
};
}
}
}
export async function lintLua({ tree }: LintEvent): Promise<LintDiagnostic[]> {
const diagnostics: LintDiagnostic[] = [];
await traverseTreeAsync(tree, async (node) => {
if (node.type === "FencedCode") {
const codeInfo = findNodeOfType(node, "CodeInfo")!;
if (!codeInfo) {
return true;
}
const codeLang = codeInfo.children![0].text!;
if (codeLang !== "space-lua") {
return true;
}
const codeText = findNodeOfType(node, "CodeText");
if (!codeText) {
return true;
}
const luaCode = renderToText(codeText);
try {
await lua.parse(luaCode);
} catch (e: any) {
2024-10-07 15:08:36 +08:00
const offset = codeText.from!;
let from = codeText.from!;
let to = codeText.to!;
if (e.message.includes("Parse error (")) {
const errorMatch = errorRegex.exec(e.message);
if (errorMatch) {
from = offset + parseInt(errorMatch[1], 10);
to = offset + parseInt(errorMatch[2], 10);
}
}
diagnostics.push({
2024-10-07 15:08:36 +08:00
from,
to,
severity: "error",
message: e.message,
});
console.log("Lua error", e);
}
return true;
}
return false;
});
return diagnostics;
}