Skip to content

Commit adb149b

Browse files
authored
fix(lint): allow bounded GSAP infinite repeats (#2763)
1 parent 7778c09 commit adb149b

2 files changed

Lines changed: 35 additions & 9 deletions

File tree

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -865,6 +865,26 @@ describe("GSAP rules", () => {
865865
expect(finding?.fixHint).toContain("Math.max(0, Math.floor");
866866
});
867867

868+
it("warns on repeat: -1 when an explicit finite composition duration bounds export", async () => {
869+
const html = `
870+
<html><body>
871+
<div data-composition-id="main" data-width="1920" data-height="1080" data-duration="8"></div>
872+
<script src="https://cdn.jsdelivr.net/npm/gsap@3/dist/gsap.min.js"></script>
873+
<script>
874+
window.__timelines = window.__timelines || {};
875+
const tl = gsap.timeline({ paused: true });
876+
tl.to("#spinner", { rotation: 360, duration: 0.8, repeat: -1, ease: "none" }, 0);
877+
window.__timelines["main"] = tl;
878+
</script>
879+
</body></html>`;
880+
const result = await lintHyperframeHtml(html);
881+
const finding = result.findings.find((f) => f.code === "gsap_infinite_repeat");
882+
expect(finding).toBeDefined();
883+
expect(finding?.severity).toBe("warning");
884+
expect(finding?.message).toContain("finite 8s window");
885+
expect(finding?.fixHint).toContain("explicit finite composition");
886+
});
887+
868888
it("does not error on finite repeat values", async () => {
869889
const html = `
870890
<html><body>

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

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1550,8 +1550,12 @@ export const gsapRules: LintRule<LintContext>[] = [
15501550
},
15511551

15521552
// gsap_infinite_repeat
1553-
({ scripts }) => {
1553+
({ scripts, rootTag }) => {
15541554
const findings: HyperframeLintFinding[] = [];
1555+
const declaredDuration = Number.parseFloat(
1556+
rootTag ? (readAttr(rootTag.raw, "data-duration") ?? "") : "",
1557+
);
1558+
const hasFiniteCompositionWindow = Number.isFinite(declaredDuration) && declaredDuration > 0;
15551559
// Match repeat: -1 in GSAP tweens or timeline configs
15561560
const pattern = /repeat\s*:\s*-1(?!\d)/g;
15571561
for (const { snippet } of scanScriptsForRegexMatches(scripts, pattern, {
@@ -1561,14 +1565,16 @@ export const gsapRules: LintRule<LintContext>[] = [
15611565
})) {
15621566
findings.push({
15631567
code: "gsap_infinite_repeat",
1564-
severity: "error",
1565-
message:
1566-
"GSAP tween uses `repeat: -1` (infinite). Infinite repeats break the deterministic " +
1567-
"capture engine which seeks to exact frame times. Use a finite repeat count calculated " +
1568-
"from the composition duration: `repeat: Math.max(0, Math.floor(duration / cycleDuration) - 1)`.",
1569-
fixHint:
1570-
"Replace `repeat: -1` with a finite count, e.g. `repeat: Math.max(0, Math.floor(totalDuration / singleCycleDuration) - 1)`. " +
1571-
"Use Math.floor (not Math.ceil) so the animation fits, and clamp at zero so a short composition cannot evaluate to -1.",
1568+
severity: hasFiniteCompositionWindow ? "warning" : "error",
1569+
message: hasFiniteCompositionWindow
1570+
? `GSAP tween uses \`repeat: -1\` (infinite), but the composition declares a finite ${declaredDuration}s window. ` +
1571+
"HyperFrames clips deterministic seeking and export to that explicit duration."
1572+
: "GSAP tween uses `repeat: -1` (infinite) without a finite composition `data-duration`. " +
1573+
"The timeline can report an unbounded duration and make render planning fail.",
1574+
fixHint: hasFiniteCompositionWindow
1575+
? "Keep the explicit finite composition `data-duration`. Use a finite repeat count only when the loop itself must end before the composition does."
1576+
: "Add a finite composition `data-duration`, or replace `repeat: -1` with " +
1577+
"`repeat: Math.max(0, Math.floor(totalDuration / singleCycleDuration) - 1)`.",
15721578
snippet: truncateSnippet(snippet),
15731579
});
15741580
}

0 commit comments

Comments
 (0)