Files
hyperframes/packages/lint/src/browser.test.ts
T
Miguel Ángel 5ec2dd2e12 fix(lint): check passes valid sub-compositions and scene-relative asset paths (#5337)
* fix(lint): check passes valid sub-compositions and scene-relative asset paths

* fix(lint): keep the browser lint build free of node modules

* refactor(lint): tidy sub-composition lint helpers and guard bare node imports

* fix(lint): resolve stylesheet asset paths from the stylesheet's own folder

* fix(lint): catch every written form of a path that climbs out of the project

* fix(parsers): sub-composition asset paths written with backslashes load in preview

* fix(parsers): an encoded backslash stays in the file name and a leading backslash stays absolute

* fix(lint): an encoded %2f counts as a separator when checking for a climb out of the project
2026-10-09 19:54:58 +00:00

62 lines
2.8 KiB
TypeScript

import { existsSync, readFileSync } from "node:fs";
import { builtinModules } from "node:module";
import { dirname, resolve } from "node:path";
import { fileURLToPath } from "node:url";
import { describe, expect, it } from "vitest";
import { lintHyperframeHtml, shouldBlockRender } from "./browser.js";
// Guards that @hyperframes/lint/browser exposes a working, node-free rule engine.
// (The platform:"browser" tsup build is the compile-time node-free guarantee;
// this verifies the API actually runs.)
const IMPORT_SPECIFIER =
/^(?:import|export)\s[^"';]*?\bfrom\s+["']([^"']+)["']|^import\s+["']([^"']+)["']|\bimport\(\s*["']([^"']+)["']\s*\)/gm;
const isNodeBuiltin = (spec: string) => spec.startsWith("node:") || builtinModules.includes(spec);
const resolveSource = (path: string): string => {
const base = path.replace(/\.js$/, "");
return [`${base}.ts`, `${base}/index.ts`].find((candidate) => existsSync(candidate)) ?? path;
};
describe("@hyperframes/lint/browser", () => {
it("lints an HTML string with no filesystem access", async () => {
const html = `<html><body>
<div data-composition-id="main" data-width="1920" data-height="1080"></div>
</body></html>`;
const result = await lintHyperframeHtml(html, { filePath: "index.html" });
expect(typeof result.ok).toBe("boolean");
expect(Array.isArray(result.findings)).toBe(true);
});
it("reaches no node: module, including through @hyperframes/parsers", () => {
const parsersDir = resolve(fileURLToPath(import.meta.url), "../../../parsers");
const parsersExports = JSON.parse(readFileSync(`${parsersDir}/package.json`, "utf8")).exports;
const seen = new Set<string>();
const nodeImports: string[] = [];
const importedFile = (file: string, spec: string): string | null => {
if (spec.startsWith(".")) return resolveSource(resolve(dirname(file), spec));
if (!spec.startsWith("@hyperframes/parsers")) return null;
return resolve(parsersDir, parsersExports[spec.replace("@hyperframes/parsers", ".")].bun);
};
const visit = (file: string) => {
if (seen.has(file)) return;
seen.add(file);
for (const [, from, bare, dynamic] of readFileSync(file, "utf8").matchAll(IMPORT_SPECIFIER)) {
const spec = from ?? bare ?? dynamic ?? "";
if (isNodeBuiltin(spec)) nodeImports.push(`${file}: ${spec}`);
const next = importedFile(file, spec);
if (next) visit(next);
}
};
visit(resolve(fileURLToPath(import.meta.url), "../browser.ts"));
expect(seen.size).toBeGreaterThan(10);
expect(nodeImports).toEqual([]);
});
it("exposes the pure shouldBlockRender gate", () => {
expect(shouldBlockRender(true, false, 1, 0)).toBe(true);
expect(shouldBlockRender(true, false, 0, 3)).toBe(false);
expect(shouldBlockRender(false, true, 0, 1)).toBe(true);
});
});