Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 25 additions & 2 deletions packages/lint/src/rules/composition.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -912,18 +912,41 @@ describe("composition rules", () => {
expect(result.findings.find((f) => f.code === "missing_data_no_timeline")).toBeUndefined();
});

it("does not warn when a script registers window.__timelines[id]", async () => {
it.each([
'window.__timelines["c1"] = gsap.timeline({ paused: true });',
"window.__timelines.c1 = gsap.timeline({ paused: true });",
"window.__timelines = { c1: gsap.timeline({ paused: true }) };",
'window.__timelines = { "c1": gsap.timeline({ paused: true }) };',
'const spec = { id: "c1" }; window.__timelines[spec.id] = gsap.timeline({ paused: true });',
'window.__timelines["c1"] ??= gsap.timeline({ paused: true });',
'window.__timelines["c1"] ||= gsap.timeline({ paused: true });',
'const ids = ["c1"]; window.__timelines[ids[0]] = gsap.timeline({ paused: true });',
])("does not warn when a script registers a timeline: %s", async (registration) => {
const html = `<!DOCTYPE html><html><body>
<div data-composition-id="c1" data-width="320" data-height="180" data-duration="5"></div>
<script>
window.__timelines = window.__timelines || {};
window.__timelines["c1"] = gsap.timeline({ paused: true });
${registration}
</script>
</body></html>`;
const result = await lintHyperframeHtml(html);
expect(result.findings.find((f) => f.code === "missing_data_no_timeline")).toBeUndefined();
});

it.each([
"window.__timelines = {};",
'// window.__timelines["c1"] = gsap.timeline({ paused: true });',
])("still warns when a script does not register a timeline: %s", async (script) => {
const html = `<!DOCTYPE html><html><body>
<div data-composition-id="c1" data-width="320" data-height="180" data-duration="5"></div>
<script>${script}</script>
</body></html>`;
const result = await lintHyperframeHtml(html);
expect(result.findings.find((f) => f.code === "missing_data_no_timeline")).toMatchObject({
severity: "warning",
});
});

it("does not warn when there is no root composition-id", async () => {
const html = `<!DOCTYPE html><html><body><p>hello</p></body></html>`;
const result = await lintHyperframeHtml(html);
Expand Down
12 changes: 11 additions & 1 deletion packages/lint/src/rules/composition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import {
stripJsComments,
stripJsCode,
truncateSnippet,
TIMELINE_REGISTRY_ASSIGN_PATTERN,
TIMELINE_REGISTRY_OBJECT_LITERAL_PATTERN,
WINDOW_TIMELINE_ASSIGN_PATTERN,
} from "../utils";
import { COMPOSITION_VARIABLE_TYPES, isSafeMediaUrl } from "@hyperframes/parsers/composition";
Expand Down Expand Up @@ -631,7 +633,15 @@ export const compositionRules: Array<(ctx: LintContext) => HyperframeLintFinding
// Can't scan external script files for timeline registration; skip to avoid
// false positives on compositions that register via a bundled JS file.
if (/<script\b[^>]*\bsrc\s*=/i.test(rawSource)) return [];
const registersTimeline = scripts.some((s) => s.content.includes("window.__timelines["));
const registersTimeline = scripts.some((script) => {
const source = stripJsComments(script.content);
return (
// Computed keys and logical assignments may not match the shared patterns.
source.includes("window.__timelines[") ||
TIMELINE_REGISTRY_ASSIGN_PATTERN.test(source) ||
TIMELINE_REGISTRY_OBJECT_LITERAL_PATTERN.test(source)
);
});
if (registersTimeline) return [];
return [
{
Expand Down