Files
hyperframes/packages/lint/src/rules/slideshow.ts
T
Miguel Ángel 6aaab32ccb refactor: make @hyperframes/lint depend only on parsers (#1773)
* refactor: make @hyperframes/lint depend only on parsers, not core

Relocates the leaf utilities lint pulled from core — URL/asset-path helpers,
font aliases, and the slideshow manifest parser — into the standalone
@hyperframes/parsers base, and drops @hyperframes/core from lint's
dependencies. Core keeps back-compat re-export stubs at the old paths, so
producer/studio/cli are unchanged.

Why: lint was the lightweight validator from #1749, but depending on core
transitively pulled studio-server (hono) and bpm-detective — irrelevant to
linting. Now installing @hyperframes/lint pulls only parsers + postcss, and
the core<->lint dependency cycle is gone.

- parsers main entry stays browser-safe (pure utils only); the node:path
  asset helpers live behind the new @hyperframes/parsers/asset-paths subpath
- slideshow parser exposed via @hyperframes/parsers/slideshow

* feat(lint): add browser entry; harden CSS url() regex (ReDoS)

@hyperframes/lint/browser — a fully client-side rule engine (lintHyperframeHtml,
lintMediaUrls, shouldBlockRender) with zero node: builtins, so browser-only
editors can validate compositions with no Node.js and no server round-trip.
Closes the browser-validation ask on #1749.

- shouldBlockRender extracted from the fs-bound project.ts into its own pure
  module so the browser entry stays node-free
- pure composition primitives (data types, font aliases, URL helper) exposed via
  a new recast-free @hyperframes/parsers/composition subpath, so the browser
  bundle tree-shakes out the GSAP/recast machinery (verified: esbuild
  platform=browser bundles with 0 node builtins)
- lint built with a platform:browser tsup pass — compile-time guarantee the
  browser entry never pulls a node builtin
- harden CSS_URL_RE against polynomial ReDoS (CodeQL js/polynomial-redos);
  behavior-preserving, verified against existing tests + an old/new parity check
- parsers/lint marked sideEffects:false
2026-06-27 13:51:21 -04:00

86 lines
2.8 KiB
TypeScript

import type { LintContext, HyperframeLintFinding } from "../context";
import type { LintRule } from "../types";
import { readAttr } from "../utils";
import {
parseSlideshowManifest,
resolveSlideshow,
isSceneLikeCompositionId,
} from "@hyperframes/parsers/slideshow";
type Scene = { id: string; start: number; duration: number };
function parseTiming(raw: string): { start: number; duration: number } | null {
const startStr = readAttr(raw, "data-start");
if (startStr === null) return null;
const start = Number(startStr);
if (!Number.isFinite(start)) return null;
const durationStr = readAttr(raw, "data-duration");
if (durationStr !== null) {
const duration = Number(durationStr);
if (Number.isFinite(duration)) return { start, duration };
}
const endStr = readAttr(raw, "data-end") ?? readAttr(raw, "data-hf-authored-end");
if (endStr !== null) {
const end = Number(endStr);
if (Number.isFinite(end) && end > start) return { start, duration: end - start };
}
return null;
}
function collectCompositionIdScenes(ctx: LintContext, seen: Set<string>, out: Scene[]): void {
for (const tag of ctx.tags) {
const compositionId = readAttr(tag.raw, "data-composition-id");
if (!compositionId || !isSceneLikeCompositionId(compositionId) || seen.has(compositionId))
continue;
const timing = parseTiming(tag.raw);
if (!timing || timing.duration <= 0) continue;
seen.add(compositionId);
out.push({ id: compositionId, ...timing });
}
}
function extractScenesFromClips(ctx: LintContext): Scene[] {
const seen = new Set<string>();
const scenes: Scene[] = [];
collectCompositionIdScenes(ctx, seen, scenes);
return scenes;
}
export const slideshowRules: LintRule<LintContext>[] = [
(ctx) => {
const findings: HyperframeLintFinding[] = [];
let manifest;
try {
manifest = parseSlideshowManifest(ctx.source);
} catch (e) {
findings.push({
code: "slideshow_invalid",
severity: "error",
message: `Slideshow island contains invalid JSON or structure: ${e instanceof Error ? e.message : String(e)}`,
fixHint:
'Ensure the <script type="application/hyperframes-slideshow+json"> block contains valid JSON matching the SlideshowManifest schema.',
});
return findings;
}
if (!manifest) return findings;
const scenes = extractScenesFromClips(ctx);
const { errors } = resolveSlideshow(manifest, scenes);
for (const error of errors) {
findings.push({
code: "slideshow_unresolved_ref",
severity: "error",
message: `Slideshow manifest error: ${error}`,
fixHint:
"Ensure every sceneId in the slideshow island matches the data-composition-id of a scene element in the composition, or provide explicit startTime/endTime.",
});
}
return findings;
},
];