Skip to content

Isometric line figures that answer the pointer. For React and for anything with a DOM.

Skill: validate.mjs fails a figure that does not parse as a module, and names the syntax error and its line, instead of printing ok for a page that draws nothing.

Lucas Marques5c6bd402008f
diff --git a/packages/hairline/test/skill/shape.test.ts b/packages/hairline/test/skill/shape.test.ts
index 81c81a5..bc7e23e 100644
--- a/packages/hairline/test/skill/shape.test.ts
+++ b/packages/hairline/test/skill/shape.test.ts
@@ -43,7 +43,7 @@ it("look.md and SKILL.md name the validator's checks and the bench's parameters
expect(text("look.md")).toContain("at=");
for (const param of ["?intensity=0", "?intensity=1", "?theme=dark", "?theme=light"]) expect(text("look.md"), param).toContain(param);
for (const param of ["intensity", "theme"]) expect(text("bench.html"), param).toContain(`params.get("${param}")`);
- for (const id of ["kernel", "bench", "text", "paint", "outside", "clock", "hit", "readout", "handle", "declare", "length"]) {
+ for (const id of ["kernel", "parse", "bench", "text", "paint", "outside", "clock", "hit", "readout", "handle", "declare", "length"]) {
expect(text("validate.mjs"), id).toMatch(new RegExp(`^ \\* {3}${id} `, "m"));
}
});
diff --git a/packages/hairline/test/skill/validate.test.ts b/packages/hairline/test/skill/validate.test.ts
index 3e672f9..1ea58c8 100644
--- a/packages/hairline/test/skill/validate.test.ts
+++ b/packages/hairline/test/skill/validate.test.ts
@@ -122,7 +122,7 @@ const BROKEN: [id: string, why: string, figure: string, change?: (page: string)
["declare", "a range that turns back", terrain.replace("range: [1.5, 3, 5]", "range: [1.5, 5, 3]")],
["declare", "a rule that does not exist", terrain.replace("rules: [1, 3, 5, 9]", "rules: [1, 11]")],
["declare", "no declaration", terrain.replace(/hairline\(\{[\s\S]*$/, "")],
- ["length", "over 200 lines", plus(Array.from({ length: 200 }, (_, i) => `const pad${i} = ${i};`).join("\n"))],
+ ["length", "over 200 lines", plus(Array.from({ length: 200 }, (_, i) => `const pad${i} = ${i};`).join("\n"))], ["parse", "a const declared twice, so the module never loads", plus("const twice = 1;\nconst twice = 2;")],
];
describe("pages that fail, each for its own reason", () => {
@@ -153,3 +153,11 @@ it("knows a path that differs only in the case of its drive letter is the same p
expect(same("/x/validate.mjs", "/x/validate.mjs", "darwin")).toBe(true);
expect(same("C:\\x\\validate.mjs", "C:\\x\\validate.mjs", "win32")).toBe(true);
});
+
+it("names the syntax error that stops the figure loading, and its line", () => {
+ const run = check(plus("const twice = 1;\nconst twice = 2;"));
+ expect(run.status).toBe(1);
+ expect(run.err).toContain("Identifier 'twice' has already been declared");
+ const line = plus("const twice = 1;\nconst twice = 2;").trim().split("\n").indexOf("const twice = 2;") + 1;
+ expect(run.err).toContain(`line ${line}`);
+});
diff --git a/skills/hairline-create/validate.mjs b/skills/hairline-create/validate.mjs
index 2238c32..c38c189 100644
--- a/skills/hairline-create/validate.mjs
+++ b/skills/hairline-create/validate.mjs
@@ -6,6 +6,7 @@
* (look.md) catches the rest. Each line it prints starts with the check's name:
*
* kernel the kernel in the page is this folder's kernel.js, untouched
+ * parse the figure loads as a module: no syntax error stops it before it draws
* bench nothing but the figure differs from bench.html
* text no words inside the figure (rule 10)
* paint no stroke width, colour, fill, opacity, filter or shadow of its own (rule 04)
@@ -17,8 +18,11 @@
* declare the file ends with hairline({ name, means, rules, range, mount })
* length at most 200 lines
*/
+import { spawnSync } from "node:child_process";
import { createHash } from "node:crypto";
-import { readFileSync, realpathSync } from "node:fs";
+import { mkdtempSync, readFileSync, realpathSync, rmSync, writeFileSync } from "node:fs";
+import { tmpdir } from "node:os";
+import { join } from "node:path";
import { fileURLToPath } from "node:url";
import { assemble } from "./build.mjs";
@@ -125,6 +129,25 @@ function declared(code) {
return wrong.length ? [say(`Wrong or missing: ${wrong.join("; ")}.`)] : [];
}
+/**
+ * The syntax error that keeps the figure from loading, or nothing. The page
+ * loads the figure as a module, so it is checked as one, by `node --check`:
+ * the browser would stop at the same error and draw nothing.
+ */
+function parse(src) {
+ const dir = mkdtempSync(join(tmpdir(), "hl-parse-")), file = join(dir, "figure.mjs");
+ try {
+ writeFileSync(file, src);
+ const run = spawnSync(process.execPath, ["--check", file], { encoding: "utf8" });
+ if (run.status === 0) return [];
+ const line = /figure\.mjs:(\d+)/.exec(run.stderr)?.[1];
+ const error = /^\w*Error: .*$/m.exec(run.stderr)?.[0] ?? run.stderr.trim().split("\n")[0];
+ return [`parse: the figure does not load${line ? `, line ${line}` : ""}: ${error}. The browser stops there and draws nothing. Fix it in the figure and build again.`];
+ } finally {
+ rmSync(dir, { recursive: true, force: true });
+ }
+}
+
/** Everything wrong with a page, one line each; empty when it passes. */
export function validate(input) {
const page = unix(input), out = [];
@@ -145,6 +168,7 @@ export function validate(input) {
}
const src = fig[1], code = bare(src), shape = bare(src, false);
+ out.push(...parse(src));
for (const [id, re, say, empty] of BAD) if (re.test(code) || empty?.test(shape)) out.push(`${id}: ${say}`);
for (const [id, re, say] of NEED) if (!re.test(code)) out.push(`${id}: ${say}`);
out.push(...declared(code));