2023-11-21 15:56:21 +00:00
|
|
|
import { YAML } from "$sb/syscalls.ts";
|
2023-12-21 17:37:50 +00:00
|
|
|
import { LintDiagnostic, QueryExpression } from "$sb/types.ts";
|
2023-11-21 15:24:20 +00:00
|
|
|
import {
|
|
|
|
findNodeOfType,
|
|
|
|
renderToText,
|
|
|
|
traverseTreeAsync,
|
|
|
|
} from "$sb/lib/tree.ts";
|
2023-11-21 15:56:21 +00:00
|
|
|
import { LintEvent } from "$sb/app_event.ts";
|
2023-12-21 17:37:50 +00:00
|
|
|
import { queryObjects } from "./api.ts";
|
|
|
|
import { AttributeObject } from "./attributes.ts";
|
|
|
|
import { extractFrontmatter } from "$sb/lib/frontmatter.ts";
|
2023-11-21 15:24:20 +00:00
|
|
|
|
2023-11-21 15:56:21 +00:00
|
|
|
export async function lintYAML({ tree }: LintEvent): Promise<LintDiagnostic[]> {
|
2023-11-21 15:24:20 +00:00
|
|
|
const diagnostics: LintDiagnostic[] = [];
|
2023-12-21 17:37:50 +00:00
|
|
|
const frontmatter = await extractFrontmatter(tree);
|
2023-12-22 10:27:07 +00:00
|
|
|
const tags = ["page", ...frontmatter.tags || []];
|
2023-11-21 15:24:20 +00:00
|
|
|
await traverseTreeAsync(tree, async (node) => {
|
|
|
|
if (node.type === "FrontMatterCode") {
|
2024-01-13 16:30:15 +00:00
|
|
|
// Query all readOnly attributes for pages with this tag set
|
|
|
|
const readOnlyAttributes = await queryObjects<AttributeObject>(
|
|
|
|
"attribute",
|
|
|
|
{
|
|
|
|
filter: ["and", ["=", ["attr", "tagName"], [
|
|
|
|
"array",
|
|
|
|
tags.map((tag): QueryExpression => ["string", tag]),
|
|
|
|
]], [
|
|
|
|
"=",
|
|
|
|
["attr", "readOnly"],
|
|
|
|
["boolean", true],
|
|
|
|
]],
|
|
|
|
distinct: true,
|
|
|
|
select: [{ name: "name" }],
|
|
|
|
},
|
|
|
|
);
|
2023-11-21 15:24:20 +00:00
|
|
|
const lintResult = await lintYaml(
|
|
|
|
renderToText(node),
|
|
|
|
node.from!,
|
2023-12-21 17:37:50 +00:00
|
|
|
readOnlyAttributes.map((a) => a.name),
|
2023-11-21 15:24:20 +00:00
|
|
|
);
|
|
|
|
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 (
|
|
|
|
codeLang === "template" || codeLang === "yaml" ||
|
|
|
|
codeLang.startsWith("#")
|
|
|
|
) {
|
|
|
|
const codeText = findNodeOfType(node, "CodeText");
|
|
|
|
if (!codeText) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
const yamlCode = renderToText(codeText);
|
|
|
|
const lintResult = await lintYaml(
|
|
|
|
yamlCode,
|
|
|
|
codeText.from!,
|
|
|
|
);
|
|
|
|
if (lintResult) {
|
|
|
|
diagnostics.push(lintResult);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
return diagnostics;
|
|
|
|
}
|
|
|
|
|
2023-11-29 15:51:28 +00:00
|
|
|
const errorRegex = /\((\d+):(\d+)\)/;
|
2023-11-21 15:24:20 +00:00
|
|
|
|
|
|
|
async function lintYaml(
|
|
|
|
yamlText: string,
|
|
|
|
from: number,
|
2023-12-22 10:27:07 +00:00
|
|
|
readOnlyKeys: string[] = [],
|
2023-11-21 15:24:20 +00:00
|
|
|
): Promise<LintDiagnostic | undefined> {
|
|
|
|
try {
|
2023-12-21 17:37:50 +00:00
|
|
|
const parsed = await YAML.parse(yamlText);
|
2023-12-22 10:27:07 +00:00
|
|
|
for (const key of readOnlyKeys) {
|
2023-12-21 17:37:50 +00:00
|
|
|
if (parsed[key]) {
|
|
|
|
return {
|
|
|
|
from,
|
|
|
|
to: from + yamlText.length,
|
|
|
|
severity: "error",
|
2023-12-22 10:27:07 +00:00
|
|
|
message: `Cannot set read-only attribute "${key}"`,
|
2023-12-21 17:37:50 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
2023-11-21 15:24:20 +00:00
|
|
|
} catch (e) {
|
|
|
|
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");
|
|
|
|
let pos = from;
|
|
|
|
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,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|