Skip to content

Commit 10be0b0

Browse files
committed
fix: cover GSAP overlay lint review cases
1 parent d01c300 commit 10be0b0

2 files changed

Lines changed: 155 additions & 18 deletions

File tree

‎packages/core/src/lint/rules/gsap.test.ts‎

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,101 @@ describe("GSAP rules", () => {
214214
expect(finding).toBeUndefined();
215215
});
216216

217+
it("reports one full-frame transition flash finding when multiple scripts touch it", async () => {
218+
const html = `
219+
<html><body data-composition-id="c1" data-width="1920" data-height="1080">
220+
<div id="tr-flash-1" style="position:fixed;inset:0;background:#fff;pointer-events:none;z-index:990"></div>
221+
<section class="clip" data-start="0" data-duration="8"><h1>Scene 1</h1></section>
222+
<section class="clip" data-start="8" data-duration="8"><h1>Scene 2</h1></section>
223+
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/gsap.min.js"></script>
224+
<script>
225+
window.__timelines = window.__timelines || {};
226+
const tl = gsap.timeline({ paused: true });
227+
tl.to("#tr-flash-1", { opacity: 1, duration: 0.08 }, 7.92);
228+
tl.to("#tr-flash-1", { opacity: 0, duration: 0.18 }, 8.00);
229+
window.__timelines["c1"] = tl;
230+
</script>
231+
<script>
232+
const tl2 = gsap.timeline({ paused: true });
233+
tl2.to("#tr-flash-1", { opacity: 1, duration: 0.08 }, 9.92);
234+
tl2.to("#tr-flash-1", { opacity: 0, duration: 0.18 }, 10.00);
235+
</script>
236+
</body></html>`;
237+
const result = await lintHyperframeHtml(html);
238+
expect(
239+
result.findings.filter((f) => f.code === "gsap_fullscreen_overlay_starts_visible"),
240+
).toHaveLength(1);
241+
});
242+
243+
it("errors when a full-frame transition flash uses a GSAP from reveal", async () => {
244+
const html = `
245+
<html><body data-composition-id="c1" data-width="1920" data-height="1080">
246+
<div id="tr-flash-1" style="position:fixed;inset:0;background:#fff;pointer-events:none;z-index:990"></div>
247+
<section class="clip" data-start="0" data-duration="8"><h1>Scene 1</h1></section>
248+
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/gsap.min.js"></script>
249+
<script>
250+
window.__timelines = window.__timelines || {};
251+
const tl = gsap.timeline({ paused: true });
252+
tl.from("#tr-flash-1", { opacity: 0, duration: 0.18 }, 7.92);
253+
window.__timelines["c1"] = tl;
254+
</script>
255+
</body></html>`;
256+
const result = await lintHyperframeHtml(html);
257+
const finding = result.findings.find(
258+
(f) => f.code === "gsap_fullscreen_overlay_starts_visible",
259+
);
260+
expect(finding).toBeDefined();
261+
expect(finding?.selector).toBe("#tr-flash-1");
262+
});
263+
264+
it("errors when a grouped GSAP selector targets a visible full-frame flash", async () => {
265+
const html = `
266+
<html><body data-composition-id="c1" data-width="1920" data-height="1080">
267+
<div id="tr-flash-1" style="position:fixed;inset:0;background:#fff;pointer-events:none;z-index:990"></div>
268+
<div id="unused"></div>
269+
<section class="clip" data-start="0" data-duration="8"><h1>Scene 1</h1></section>
270+
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/gsap.min.js"></script>
271+
<script>
272+
window.__timelines = window.__timelines || {};
273+
const tl = gsap.timeline({ paused: true });
274+
tl.to("#unused, #tr-flash-1", { opacity: 1, duration: 0.08 }, 7.92);
275+
tl.to("#unused, #tr-flash-1", { opacity: 0, duration: 0.18 }, 8.00);
276+
window.__timelines["c1"] = tl;
277+
</script>
278+
</body></html>`;
279+
const result = await lintHyperframeHtml(html);
280+
const finding = result.findings.find(
281+
(f) => f.code === "gsap_fullscreen_overlay_starts_visible",
282+
);
283+
expect(finding).toBeDefined();
284+
expect(finding?.selector).toBe("#tr-flash-1");
285+
});
286+
287+
it("errors when full-frame transition flash styles come from a style block", async () => {
288+
const html = `
289+
<html><body data-composition-id="c1" data-width="1920" data-height="1080">
290+
<style>
291+
.tr-flash { position: fixed; inset: 0; background: #fff; pointer-events: none; z-index: 990; }
292+
</style>
293+
<div id="tr-flash-1" class="tr-flash"></div>
294+
<section class="clip" data-start="0" data-duration="8"><h1>Scene 1</h1></section>
295+
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/gsap.min.js"></script>
296+
<script>
297+
window.__timelines = window.__timelines || {};
298+
const tl = gsap.timeline({ paused: true });
299+
tl.to(".tr-flash", { opacity: 1, duration: 0.08 }, 7.92);
300+
tl.to(".tr-flash", { opacity: 0, duration: 0.18 }, 8.00);
301+
window.__timelines["c1"] = tl;
302+
</script>
303+
</body></html>`;
304+
const result = await lintHyperframeHtml(html);
305+
const finding = result.findings.find(
306+
(f) => f.code === "gsap_fullscreen_overlay_starts_visible",
307+
);
308+
expect(finding).toBeDefined();
309+
expect(finding?.selector).toBe(".tr-flash");
310+
});
311+
217312
it("does NOT error when GSAP animates opacity on a clip element", async () => {
218313
const html = `
219314
<html><body>

‎packages/core/src/lint/rules/gsap.ts‎

Lines changed: 60 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,11 @@ function isVisibleGsapState(values: Record<string, string | number>): boolean {
198198
return false;
199199
}
200200

201+
function makesOverlayVisible(win: GsapWindow): boolean {
202+
if (win.method === "from" && isHiddenGsapState(win.propertyValues)) return true;
203+
return isVisibleGsapState(win.propertyValues);
204+
}
205+
201206
function isSceneBoundaryExit(win: GsapWindow): boolean {
202207
if (win.end <= win.position) return false;
203208
if (win.method !== "to" && win.method !== "fromTo") return false;
@@ -310,11 +315,6 @@ function getSingleClassSelector(selector: string): string | null {
310315
return match?.groups?.name || null;
311316
}
312317

313-
function getSingleIdSelector(selector: string): string | null {
314-
const match = selector.trim().match(/^#(?<name>[A-Za-z0-9_-]+)$/);
315-
return match?.groups?.name || null;
316-
}
317-
318318
function readStyleProperty(style: string, property: string): string | null {
319319
const escapedProperty = property.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
320320
const match = style.match(new RegExp(`(?:^|;)\\s*${escapedProperty}\\s*:\\s*([^;]+)`, "i"));
@@ -357,6 +357,39 @@ function styleLooksFullFrameOverlay(style: string): boolean {
357357
return coversFrame && styleHasOpaqueBackground(style);
358358
}
359359

360+
function collectSimpleStyleRules(styles: LintContext["styles"]): Map<string, string> {
361+
const rules = new Map<string, string>();
362+
for (const style of styles) {
363+
for (const [, selectorList, body] of style.content.matchAll(/([^{}]+)\{([^}]+)\}/g)) {
364+
if (!selectorList || !body) continue;
365+
for (const selector of selectorList.split(",")) {
366+
const token = selector.trim();
367+
if (!/^[#.][A-Za-z0-9_-]+$/.test(token)) continue;
368+
rules.set(token, `${rules.get(token) || ""};${body}`);
369+
}
370+
}
371+
}
372+
return rules;
373+
}
374+
375+
function tagSimpleSelectors(tag: OpenTag): string[] {
376+
const selectors: string[] = [];
377+
const id = readAttr(tag.raw, "id");
378+
if (id) selectors.push(`#${id}`);
379+
const classes = readAttr(tag.raw, "class")?.split(/\s+/).filter(Boolean) ?? [];
380+
for (const className of classes) selectors.push(`.${className}`);
381+
return selectors;
382+
}
383+
384+
function combinedTagStyle(tag: OpenTag, styleRules: Map<string, string>): string {
385+
const styles = [readAttr(tag.raw, "style") || ""];
386+
for (const selector of tagSimpleSelectors(tag)) {
387+
const ruleStyle = styleRules.get(selector);
388+
if (ruleStyle) styles.push(ruleStyle);
389+
}
390+
return styles.filter(Boolean).join(";");
391+
}
392+
360393
// fallow-ignore-next-line complexity
361394
function cssTransformToGsapProps(cssTransform: string): string | null {
362395
const parts: string[] = [];
@@ -474,7 +507,7 @@ function extractStandaloneGsapTransformCalls(script: string): GsapTransformCall[
474507
export const gsapRules: LintRule<LintContext>[] = [
475508
// overlapping_gsap_tweens + gsap_animates_clip_element + unscoped_gsap_selector
476509
// fallow-ignore-next-line complexity
477-
async ({ source, tags, scripts, rootCompositionId }) => {
510+
async ({ source, tags, scripts, styles, rootCompositionId }) => {
478511
const findings: HyperframeLintFinding[] = [];
479512

480513
// Build clip element selector map
@@ -499,6 +532,8 @@ export const gsapRules: LintRule<LintContext>[] = [
499532

500533
const classUsage = countClassUsage(tags);
501534
const clipStartBoundariesByComposition = collectClipStartBoundariesByComposition(source, tags);
535+
const styleRules = collectSimpleStyleRules(styles);
536+
const reportedVisibleOverlayKeys = new Set<string>();
502537

503538
for (const script of scripts) {
504539
const localTimelineCompId = readRegisteredTimelineCompositionId(script.content);
@@ -563,16 +598,18 @@ export const gsapRules: LintRule<LintContext>[] = [
563598

564599
// gsap_fullscreen_overlay_starts_visible
565600
for (const tag of tags) {
566-
const id = readAttr(tag.raw, "id");
567-
if (!id) continue;
568-
const inlineStyle = readAttr(tag.raw, "style");
569-
if (!inlineStyle || !styleLooksFullFrameOverlay(inlineStyle)) continue;
570-
if (styleHasHiddenInitialState(inlineStyle)) continue;
601+
const selectors = tagSimpleSelectors(tag);
602+
if (selectors.length === 0) continue;
603+
const overlayKey = readAttr(tag.raw, "id") || String(tag.index);
604+
if (reportedVisibleOverlayKeys.has(overlayKey)) continue;
605+
const authoredStyle = combinedTagStyle(tag, styleRules);
606+
if (!authoredStyle || !styleLooksFullFrameOverlay(authoredStyle)) continue;
607+
if (styleHasHiddenInitialState(authoredStyle)) continue;
571608

572-
const selector = `#${id}`;
573609
const visibilityWindows = gsapWindows
574610
.filter((win) => {
575-
if (getSingleIdSelector(win.targetSelector) !== id) return false;
611+
const tokens = targetedSelectorTokens(win.targetSelector);
612+
if (!selectors.some((selector) => tokens.has(selector))) return false;
576613
return win.properties.some((prop) =>
577614
["opacity", "autoAlpha", "visibility", "display"].includes(prop),
578615
);
@@ -583,23 +620,28 @@ export const gsapRules: LintRule<LintContext>[] = [
583620
win.position <= SCENE_BOUNDARY_EPSILON_SECONDS && isHiddenGsapState(win.propertyValues),
584621
);
585622
if (startsHiddenAtZero) continue;
586-
const firstVisible = visibilityWindows.find((win) =>
587-
isVisibleGsapState(win.propertyValues),
588-
);
623+
const firstVisible = visibilityWindows.find((win) => makesOverlayVisible(win));
589624
if (!firstVisible) continue;
625+
const selector =
626+
selectors.find((candidate) =>
627+
targetedSelectorTokens(firstVisible.targetSelector).has(candidate),
628+
) ||
629+
selectors[0] ||
630+
tag.name;
590631
const laterHidden = visibilityWindows.some(
591632
(win) => win.position >= firstVisible.position && isHiddenGsapState(win.propertyValues),
592633
);
593-
if (!laterHidden) continue;
634+
if (firstVisible.method !== "from" && !laterHidden) continue;
594635

636+
reportedVisibleOverlayKeys.add(overlayKey);
595637
findings.push({
596638
code: "gsap_fullscreen_overlay_starts_visible",
597639
severity: "error",
598640
message:
599641
`Full-frame overlay "${selector}" starts visible before its first GSAP opacity tween at ` +
600642
`${firstVisible.position.toFixed(2)}s. It will cover earlier render frames, often as a blank/white video.`,
601643
selector,
602-
elementId: id,
644+
elementId: readAttr(tag.raw, "id") || undefined,
603645
fixHint:
604646
`Add \`opacity: 0\` to "${selector}" in CSS/inline styles, or add ` +
605647
`\`tl.set("${selector}", { opacity: 0 }, 0)\` before the reveal tween.`,

0 commit comments

Comments
 (0)