Skip to content

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

Skill: the validator reads strings and comments the way JavaScript does, counts only the handle's own set and destroy, and leaves a slope, a number sign and the word import alone

Lucas Marquesad07503fbba5
diff --git a/packages/hairline/test/skill/validate.test.ts b/packages/hairline/test/skill/validate.test.ts
index dd1a42d..a689818 100644
--- a/packages/hairline/test/skill/validate.test.ts
+++ b/packages/hairline/test/skill/validate.test.ts
@@ -55,6 +55,24 @@ describe("pages that pass", () => {
});
});
+/** Honest code that only looks like a broken rule: a good figure with any of these added still passes. */
+const HONEST: [why: string, code: string][] = [
+ ["an identifier named gradient, for a slope", "const gradient = 0.5;"],
+ ["a read-out with a number sign", 'const label = "pillar #101";'],
+ ["the word import in a string", 'const note = "import";'],
+ ["the word import inside a longer name", "const important = true;"],
+];
+
+describe("honest additions that pass", () => {
+ for (const [why, code] of HONEST) {
+ it(why, () => {
+ const run = check(plus(code));
+ expect(run.err).toBe("");
+ expect(run.status).toBe(0);
+ });
+ }
+});
+
const BROKEN: [id: string, why: string, figure: string, change?: (page: string) => string][] = [
["kernel", "the kernel was edited", terrain, (page) => page.replace("var HL = ", "var HL = /* mine */ ")],
["bench", "the bench was edited", terrain, (page) => page.replace("</main>", "<p>better</p></main>")],
@@ -63,16 +81,23 @@ const BROKEN: [id: string, why: string, figure: string, change?: (page: string)
["paint", "a fill of its own", plus('mk("path", { fill: "red" });')],
["paint", "a stroke width of its own", plus('mk("path", { "stroke-width": 2 });')],
["paint", "an inline style", plus('document.body.style.background = "#000";')],
+ ["paint", "a colour in hex", plus('const ink = "#a1b2c3";')],
+ ["paint", "a gradient element", plus('mk("linearGradient", {});')],
["outside", "a fetch", plus('fetch("https://example.com/data.json");')],
["outside", "a node made by hand", plus('document.createElementNS("http://www.w3.org/2000/svg", "path");')],
["outside", "a script tag in a string", plus('const s = "</script>";')],
+ ["outside", "an import statement", plus('import { x } from "./x.js";')],
+ ["outside", "a dynamic import", plus('import("./x.js");')],
+ ["outside", "a URL in a string", plus('const u = "https://example.com/a.json";')],
["clock", "its own frame", plus("requestAnimationFrame(() => {});")],
["clock", "its own timer", plus("setTimeout(() => {}, 100);")],
+ ["clock", "a timer after a // in a string", plus('const s = "a // b"; setTimeout(() => {}, 1);')],
["clock", "no kernel loop", terrain.replace("register(stage,", "((s, t) => ({ wake() {}, unregister() {} }))(stage,")],
["hit", "a box measured on screen", plus("stage.getBoundingClientRect();")],
["hit", "its own listener", plus('stage.addEventListener("pointermove", () => {});')],
["hit", "no pointer", terrain.replace("bag.add(pointer(stage,", "bag.add(((s, h) => () => {})(stage,")],
["readout", "no read-out", terrain.replace(/read\.textContent = [^;]+;/g, "")],
+ ["handle", "no set, beside a Map's set", plus("new Map().set(1, 2);").replace("set: (v) =>", "put: (v) =>")],
["handle", "no destroy", terrain.replace("destroy: bag.dispose", "stop: bag.dispose")],
["declare", "no means", terrain.replace(/\n {2}means: .*\n/, "\n")],
["declare", "a range that turns back", terrain.replace("range: [1.5, 3, 5]", "range: [1.5, 5, 3]")],
diff --git a/skills/hairline-create/validate.mjs b/skills/hairline-create/validate.mjs
index 3e2a356..6524933 100644
--- a/skills/hairline-create/validate.mjs
+++ b/skills/hairline-create/validate.mjs
@@ -25,30 +25,78 @@ import { assemble } from "./build.mjs";
const here = (p) => fileURLToPath(new URL(p, import.meta.url));
const LIMIT = 200;
const unix = (s) => s.replace(/\r\n/g, "\n");
-/** A figure's source without its comments, so its prose is never read as code. A `//` after a colon or inside a quote is kept: it is a URL. */
-const bare = (src) => src.replace(/\/\*[\s\S]*?\*\//g, "").replace(/(^|[^:\\"'`])\/\/.*$/gm, "$1");
+/**
+ * A figure's source without its comments, so its prose is never read as code.
+ * It is read the way JavaScript reads it, so a `//` inside a string, a template
+ * or a regex is not taken for a comment, and what follows it is still checked.
+ * With `words` false the strings are emptied too, for a check that must not
+ * read a string's words as code.
+ */
+function bare(src, words = true) {
+ let out = "", i = 0, prev = "", depth = 0;
+ const open = []; // for each `${` still open, the brace depth it opened at
+ /** Reads from just past an opening quote to its close, or, in a template, to a `${`. */
+ const quoted = (q) => {
+ const from = i;
+ while (i < src.length && src[i] !== q && (q === "`" ? !(src[i] === "$" && src[i + 1] === "{") : src[i] !== "\n")) {
+ i += src[i] === "\\" ? 2 : 1;
+ }
+ if (words) out += src.slice(from, i);
+ if (q === "`" && src[i] === "$") { out += "${"; i += 2; open.push(depth++); prev = "{"; return; }
+ out += src[i] ?? ""; i++; prev = q;
+ };
+ while (i < src.length) {
+ const c = src[i], d = src[i + 1];
+ if (c === "/" && d === "/") { while (i < src.length && src[i] !== "\n") i++; }
+ else if (c === "/" && d === "*") { const end = src.indexOf("*/", i + 2); i = end < 0 ? src.length : end + 2; out += " "; }
+ else if (c === "/" && /^(?:|[(,=:[!&|?{};+\-*%<>~^])$/.test(prev)) {
+ // A regex: a slash where a value starts. A quote or a `//` inside it is the regex's own.
+ let j = i + 1, cls = false;
+ for (; j < src.length && src[j] !== "\n"; j++) {
+ if (src[j] === "\\") j++;
+ else if (src[j] === "[") cls = true;
+ else if (src[j] === "]") cls = false;
+ else if (src[j] === "/" && !cls) break;
+ }
+ out += src.slice(i, j + 1); i = j + 1; prev = ")";
+ } else if (c === '"' || c === "'" || c === "`") { out += c; i++; quoted(c); }
+ else if (c === "}" && open.length && depth - 1 === open[open.length - 1]) { depth--; open.pop(); out += c; i++; quoted("`"); }
+ else {
+ if (c === "{") depth++;
+ else if (c === "}") depth--;
+ out += c; i++;
+ if (!/\s/.test(c)) prev = c;
+ }
+ }
+ return out;
+}
-/** What a figure must not contain. */
+/**
+ * What a figure must not contain: a pattern read in the code with its strings,
+ * and, for an import, one read with its strings emptied, so the word in a
+ * string is not taken for the statement.
+ */
const BAD = [
["text", /<\s*(?:text|tspan|textPath|foreignObject)\b|["'`](?:text|tspan|textPath|foreignObject)["'`]|\b(?:innerHTML|outerHTML|insertAdjacentHTML|innerText)\b/,
"rule 10. No words inside the figure, and no markup written as a string. Say it with geometry (a punch, a dot code, a bright edge); names go to read.textContent."],
- ["paint", /stroke-width|strokeWidth|stroke-dasharray|["'`](?:fill|stroke|filter|style|color|stop-color)["'`]|\b(?:fill|stroke|filter|style)\s*:|\.style\b|#[0-9a-fA-F]{3,8}\b|\b(?:rgba?|hsla?|oklch|oklab|color-mix)\(|drop-shadow|box-shadow|feDropShadow|feGaussianBlur|[Gg]radient/,
+ ["paint", /stroke-width|strokeWidth|stroke-dasharray|["'`](?:fill|stroke|filter|style|color|stop-color)["'`]|\b(?:fill|stroke|filter|style)\s*:|\.style\b|(["'`])#(?:[0-9a-fA-F]{3,4}|[0-9a-fA-F]{6}|[0-9a-fA-F]{8})\1|\b(?:rgba?|hsla?|oklch|oklab|color-mix)\(|drop-shadow|box-shadow|feDropShadow|feGaussianBlur|[Ll]inearGradient|[Rr]adialGradient|(?:linear|radial|conic)-gradient/,
"rule 04. The figure sets a stroke width, colour, fill, filter, gradient or shadow of its own. Use the kernel's classes and nothing else: sil, hi, lo, nf, fo, dash, dot, dot m, dot off."],
- ["outside", /\bfetch\s*\(|\bimport\b|XMLHttpRequest|WebSocket|EventSource|sendBeacon|https?:\/\/|\burl\(|<\/?script|<(?:link|img|iframe|style)\b|new\s+Image\b|createElement|\beval\s*\(|new\s+Function\b|localStorage|sessionStorage|\.cookie\b/,
- "the figure reaches outside the file or makes nodes by hand. One self-contained file: no fetch, import, URL, script tag or storage, and every node comes from HL.mk."],
+ ["outside", /\bfetch\s*\(|XMLHttpRequest|WebSocket|EventSource|sendBeacon|https?:\/\/|\burl\(|<\/?script|<(?:link|img|iframe|style)\b|new\s+Image\b|createElement|\beval\s*\(|new\s+Function\b|localStorage|sessionStorage|\.cookie\b/,
+ "the figure reaches outside the file or makes nodes by hand. One self-contained file: no fetch, import, URL, script tag or storage, and every node comes from HL.mk.",
+ /\bimport\s*(?:\(|["'`{*]|[\w$]+\s*(?:,|from\b))/],
["clock", /\bsetInterval\b|\bsetTimeout\b|\brequestAnimationFrame\b|\.animate\s*\(|<animate|IntersectionObserver|\bmatchMedia\b/,
"rule 07. The figure runs a clock of its own. Move inside HL.register(stage, tick), with springs (stepS) or tweens (tset, tval); a delay is a tween's delay. The loop sleeps offscreen and honours reduced motion for you."],
["hit", /getBoundingClientRect|elementFromPoint|elementsFromPoint|:hover|(?:addEventListener|\.on)\s*\(\s*(?:\w+\s*,\s*)?["'`](?:mouse|pointer|touch|click)|\bon(?:mouse|pointer|touch|click)\w*\s*=/,
"rule 01. The figure listens to the pointer itself or measures what is on screen. Take the pointer from HL.pointer(stage, { move, leave }) and test it against the rest or target pose, in world units."],
];
-/** What a figure must contain. */
+/** What a figure must contain. A call on something else, such as `map.set(k, v)`, is not the handle's `set`. */
const NEED = [
["clock", /\bregister\s*\(/, "rule 07. The figure never joins the kernel's loop. Draw inside HL.register(stage, tick), and give its unregister to destroy."],
["hit", /\bpointer\s*\(/, "the figure never listens to the pointer. Call HL.pointer(stage, { move, leave }) and answer it."],
["readout", /\bread\.textContent\s*=/, 'the figure never writes the read-out. Set read.textContent to what is under the pointer, and to "rest" when nothing is.'],
- ["handle", /\bset\s*[:(]/, "mount must return { set(value), destroy() }, and set is missing. It takes the slider's number."],
- ["handle", /\bdestroy\s*[:(]/, "mount must return { set(value), destroy() }, and destroy is missing. It undoes everything mount did: bag.dispose."],
+ ["handle", /(?<![.\w$])set\s*[:(]/, "mount must return { set(value), destroy() }, and set is missing. It takes the slider's number."],
+ ["handle", /(?<![.\w$])destroy\s*[:(]/, "mount must return { set(value), destroy() }, and destroy is missing. It undoes everything mount did: bag.dispose."],
];
/** The declaration at the end of the file, read as text. */
@@ -90,8 +138,8 @@ export function validate(input) {
out.push("bench: the page differs from bench.html outside the figure. Build again with `node build.mjs`; the bench is fixed, and a change belongs in the figure.");
}
- const src = fig[1], code = bare(src);
- for (const [id, re, say] of BAD) if (re.test(code)) out.push(`${id}: ${say}`);
+ const src = fig[1], code = bare(src), shape = bare(src, false);
+ 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));
const lines = src.split("\n").length;