Skip to content

Commit e1ff7e6

Browse files
fix(lint): flag repeated fromTo state leaks (#3699)
* fix(lint): flag repeated fromTo state leaks * fix(lint): require a timeline baseline for repeated fromTo
1 parent 131336f commit e1ff7e6

2 files changed

Lines changed: 280 additions & 3 deletions

File tree

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

Lines changed: 227 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3050,6 +3050,233 @@ describe("SVG draw-on rules", () => {
30503050
expect(finding).toBeUndefined();
30513051
});
30523052

3053+
it("gsap_repeated_fromto_without_baseline: warns for repeated future fromTo state", async () => {
3054+
const html = `
3055+
<html><body>
3056+
<style>#ring { opacity: 0; }</style>
3057+
<div data-composition-id="main" data-width="1920" data-height="1080">
3058+
<div id="ring"></div>
3059+
</div>
3060+
<script src="https://cdn.jsdelivr.net/npm/gsap@3/dist/gsap.min.js"></script>
3061+
<script>
3062+
window.__timelines = window.__timelines || {};
3063+
const tl = gsap.timeline({ paused: true });
3064+
tl.fromTo("#ring", { opacity: 0, scale: 1 }, { opacity: 1, duration: 0.5 }, 5);
3065+
tl.fromTo("#ring", { opacity: 1, scale: 0.4 }, { opacity: 0, duration: 0.5 }, 10);
3066+
window.__timelines.main = tl;
3067+
</script>
3068+
</body></html>`;
3069+
const result = await lintHyperframeHtml(html);
3070+
const finding = result.findings.find(
3071+
(candidate) => candidate.code === "gsap_repeated_fromto_without_baseline",
3072+
);
3073+
3074+
expect(finding?.severity).toBe("warning");
3075+
expect(finding?.selector).toBe("#ring");
3076+
});
3077+
3078+
it("gsap_repeated_fromto_without_baseline: accepts explicit immediateRender false", async () => {
3079+
const html = `
3080+
<html><body>
3081+
<div data-composition-id="main" data-width="1920" data-height="1080"><div id="ring"></div></div>
3082+
<script src="https://cdn.jsdelivr.net/npm/gsap@3/dist/gsap.min.js"></script>
3083+
<script>
3084+
window.__timelines = window.__timelines || {};
3085+
const tl = gsap.timeline({ paused: true });
3086+
tl.fromTo("#ring", { opacity: 0 }, { opacity: 1, duration: 0.5, immediateRender: false }, 5);
3087+
tl.fromTo("#ring", { opacity: 1 }, { opacity: 0, duration: 0.5, immediateRender: false }, 10);
3088+
window.__timelines.main = tl;
3089+
</script>
3090+
</body></html>`;
3091+
const result = await lintHyperframeHtml(html);
3092+
const finding = result.findings.find(
3093+
(candidate) => candidate.code === "gsap_repeated_fromto_without_baseline",
3094+
);
3095+
3096+
expect(finding).toBeUndefined();
3097+
});
3098+
3099+
it("gsap_repeated_fromto_without_baseline: accepts an earlier timeline set baseline", async () => {
3100+
const html = `
3101+
<html><body>
3102+
<div data-composition-id="main" data-width="1920" data-height="1080"><div id="ring"></div></div>
3103+
<script src="https://cdn.jsdelivr.net/npm/gsap@3/dist/gsap.min.js"></script>
3104+
<script>
3105+
window.__timelines = window.__timelines || {};
3106+
const tl = gsap.timeline({ paused: true });
3107+
tl.set("#ring", { opacity: 0, scale: 1 }, 0);
3108+
tl.fromTo("#ring", { opacity: 0 }, { opacity: 1, duration: 0.5 }, 5);
3109+
tl.fromTo("#ring", { opacity: 1 }, { opacity: 0, duration: 0.5 }, 10);
3110+
window.__timelines.main = tl;
3111+
</script>
3112+
</body></html>`;
3113+
const result = await lintHyperframeHtml(html);
3114+
const finding = result.findings.find(
3115+
(candidate) => candidate.code === "gsap_repeated_fromto_without_baseline",
3116+
);
3117+
3118+
expect(finding).toBeUndefined();
3119+
});
3120+
3121+
it("gsap_repeated_fromto_without_baseline: rejects an earlier standalone set", async () => {
3122+
const html = `
3123+
<html><body>
3124+
<div data-composition-id="main" data-width="1920" data-height="1080"><div id="ring"></div></div>
3125+
<script src="https://cdn.jsdelivr.net/npm/gsap@3/dist/gsap.min.js"></script>
3126+
<script>
3127+
window.__timelines = window.__timelines || {};
3128+
const tl = gsap.timeline({ paused: true });
3129+
gsap.set("#ring", { opacity: 0, scale: 1 });
3130+
tl.fromTo("#ring", { opacity: 0, scale: 1 }, { opacity: 1, duration: 0.5 }, 5);
3131+
tl.fromTo("#ring", { opacity: 1, scale: 0.4 }, { opacity: 0, duration: 0.5 }, 10);
3132+
window.__timelines.main = tl;
3133+
</script>
3134+
</body></html>`;
3135+
const result = await lintHyperframeHtml(html);
3136+
const finding = result.findings.find(
3137+
(candidate) => candidate.code === "gsap_repeated_fromto_without_baseline",
3138+
);
3139+
3140+
expect(finding?.severity).toBe("warning");
3141+
});
3142+
3143+
it("gsap_repeated_fromto_without_baseline: rejects a later incomplete standalone set", async () => {
3144+
const html = `
3145+
<html><body>
3146+
<div data-composition-id="main" data-width="1920" data-height="1080"><div id="ring"></div></div>
3147+
<script src="https://cdn.jsdelivr.net/npm/gsap@3/dist/gsap.min.js"></script>
3148+
<script>
3149+
window.__timelines = window.__timelines || {};
3150+
const tl = gsap.timeline({ paused: true });
3151+
tl.fromTo("#ring", { opacity: 0, scale: 1 }, { opacity: 1, duration: 0.5 }, 5);
3152+
tl.fromTo("#ring", { opacity: 1, scale: 0.4 }, { opacity: 0, duration: 0.5 }, 10);
3153+
gsap.set("#ring", { x: 0 });
3154+
window.__timelines.main = tl;
3155+
</script>
3156+
</body></html>`;
3157+
const result = await lintHyperframeHtml(html);
3158+
const finding = result.findings.find(
3159+
(candidate) => candidate.code === "gsap_repeated_fromto_without_baseline",
3160+
);
3161+
3162+
expect(finding?.severity).toBe("warning");
3163+
});
3164+
3165+
it("gsap_repeated_fromto_without_baseline: does not guess that a later standalone set is a timeline baseline", async () => {
3166+
const html = `
3167+
<html><body>
3168+
<div data-composition-id="main" data-width="1920" data-height="1080"><div id="ring"></div></div>
3169+
<script src="https://cdn.jsdelivr.net/npm/gsap@3/dist/gsap.min.js"></script>
3170+
<script>
3171+
window.__timelines = window.__timelines || {};
3172+
const tl = gsap.timeline({ paused: true });
3173+
tl.fromTo("#ring", { opacity: 0, scale: 1 }, { opacity: 1, duration: 0.5 }, 5);
3174+
tl.fromTo("#ring", { opacity: 1, scale: 0.4 }, { opacity: 0, duration: 0.5 }, 10);
3175+
gsap.set("#ring", { opacity: 0, scale: 1 });
3176+
window.__timelines.main = tl;
3177+
</script>
3178+
</body></html>`;
3179+
const result = await lintHyperframeHtml(html);
3180+
const finding = result.findings.find(
3181+
(candidate) => candidate.code === "gsap_repeated_fromto_without_baseline",
3182+
);
3183+
3184+
expect(finding?.severity).toBe("warning");
3185+
});
3186+
3187+
it("gsap_repeated_fromto_without_baseline: does not treat a deferred callback set as baseline", async () => {
3188+
const html = `
3189+
<html><body>
3190+
<div data-composition-id="main" data-width="1920" data-height="1080">
3191+
<div id="ring"></div><button id="reset"></button>
3192+
</div>
3193+
<script src="https://cdn.jsdelivr.net/npm/gsap@3/dist/gsap.min.js"></script>
3194+
<script>
3195+
window.__timelines = window.__timelines || {};
3196+
const tl = gsap.timeline({ paused: true });
3197+
document.getElementById("reset").addEventListener("click", () => {
3198+
gsap.set("#ring", { opacity: 0, scale: 1 });
3199+
});
3200+
tl.fromTo("#ring", { opacity: 0 }, { opacity: 1, duration: 0.5 }, 5);
3201+
tl.fromTo("#ring", { opacity: 1 }, { opacity: 0, duration: 0.5 }, 10);
3202+
window.__timelines.main = tl;
3203+
</script>
3204+
</body></html>`;
3205+
const result = await lintHyperframeHtml(html);
3206+
const finding = result.findings.find(
3207+
(candidate) => candidate.code === "gsap_repeated_fromto_without_baseline",
3208+
);
3209+
3210+
expect(finding?.severity).toBe("warning");
3211+
});
3212+
3213+
it("gsap_repeated_fromto_without_baseline: requires a timeline baseline to be authored first", async () => {
3214+
const html = `
3215+
<html><body>
3216+
<div data-composition-id="main" data-width="1920" data-height="1080"><div id="ring"></div></div>
3217+
<script src="https://cdn.jsdelivr.net/npm/gsap@3/dist/gsap.min.js"></script>
3218+
<script>
3219+
window.__timelines = window.__timelines || {};
3220+
const tl = gsap.timeline({ paused: true });
3221+
tl.fromTo("#ring", { opacity: 0 }, { opacity: 1, duration: 0.5 }, 5);
3222+
tl.fromTo("#ring", { opacity: 1 }, { opacity: 0, duration: 0.5 }, 10);
3223+
tl.set("#ring", { opacity: 0 }, 0);
3224+
window.__timelines.main = tl;
3225+
</script>
3226+
</body></html>`;
3227+
const result = await lintHyperframeHtml(html);
3228+
const finding = result.findings.find(
3229+
(candidate) => candidate.code === "gsap_repeated_fromto_without_baseline",
3230+
);
3231+
3232+
expect(finding?.severity).toBe("warning");
3233+
});
3234+
3235+
it("gsap_repeated_fromto_without_baseline: accepts one future fromTo writer", async () => {
3236+
const html = `
3237+
<html><body>
3238+
<div data-composition-id="main" data-width="1920" data-height="1080"><div id="ring"></div></div>
3239+
<script src="https://cdn.jsdelivr.net/npm/gsap@3/dist/gsap.min.js"></script>
3240+
<script>
3241+
window.__timelines = window.__timelines || {};
3242+
const tl = gsap.timeline({ paused: true });
3243+
tl.fromTo("#ring", { opacity: 0 }, { opacity: 1, duration: 0.5 }, 5);
3244+
window.__timelines.main = tl;
3245+
</script>
3246+
</body></html>`;
3247+
const result = await lintHyperframeHtml(html);
3248+
3249+
expect(
3250+
result.findings.find(
3251+
(candidate) => candidate.code === "gsap_repeated_fromto_without_baseline",
3252+
),
3253+
).toBeUndefined();
3254+
});
3255+
3256+
it("gsap_repeated_fromto_without_baseline: keeps different selectors independent", async () => {
3257+
const html = `
3258+
<html><body>
3259+
<div data-composition-id="main" data-width="1920" data-height="1080">
3260+
<div id="ring-a"></div><div id="ring-b"></div>
3261+
</div>
3262+
<script src="https://cdn.jsdelivr.net/npm/gsap@3/dist/gsap.min.js"></script>
3263+
<script>
3264+
window.__timelines = window.__timelines || {};
3265+
const tl = gsap.timeline({ paused: true });
3266+
tl.fromTo("#ring-a", { opacity: 0 }, { opacity: 1, duration: 0.5 }, 5);
3267+
tl.fromTo("#ring-b", { opacity: 1 }, { opacity: 0, duration: 0.5 }, 10);
3268+
window.__timelines.main = tl;
3269+
</script>
3270+
</body></html>`;
3271+
const result = await lintHyperframeHtml(html);
3272+
3273+
expect(
3274+
result.findings.find(
3275+
(candidate) => candidate.code === "gsap_repeated_fromto_without_baseline",
3276+
),
3277+
).toBeUndefined();
3278+
});
3279+
30533280
// ── svg_measure_before_path_d ──────────────────────────────────────────────
30543281

30553282
it("svg_measure_before_path_d: ERROR when no d assignment exists anywhere", async () => {

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

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ type GsapWindow = {
5555
propertyValues: Record<string, string | number>;
5656
fromPropertyValues?: Record<string, string | number>;
5757
overwriteAuto: boolean;
58-
immediateRender: boolean;
58+
/** Explicit immediateRender option; undefined keeps the GSAP method default. */
59+
immediateRender?: boolean;
5960
method: string;
6061
/** True for an off-timeline `gsap.set(...)` (applied once at load). */
6162
global?: boolean;
@@ -150,6 +151,7 @@ async function extractGsapWindows(script: string): Promise<GsapWindow[]> {
150151
const cycleCount = infiniteRepeat ? 1 : repeat > 0 ? repeat + 1 : 1;
151152
const effectiveDuration =
152153
animation.method === "set" ? 0 : (animation.duration ?? 0) * cycleCount;
154+
const immediateRender = unwrapRaw(animation.extras?.immediateRender);
153155
windows.push({
154156
targetSelector: animation.targetSelector,
155157
targetIdentity: animation.targetIdentity,
@@ -162,7 +164,8 @@ async function extractGsapWindows(script: string): Promise<GsapWindow[]> {
162164
propertyValues: animation.properties,
163165
fromPropertyValues: animation.fromProperties,
164166
overwriteAuto: unwrapRaw(animation.extras?.overwrite) === "auto",
165-
immediateRender: unwrapRaw(animation.extras?.immediateRender) === "true",
167+
immediateRender:
168+
immediateRender === "true" ? true : immediateRender === "false" ? false : undefined,
166169
method: animation.method,
167170
global: animation.global,
168171
raw: synthesizeWindowRaw(parsed.timelineVar, animation),
@@ -1124,6 +1127,53 @@ export const gsapRules: LintRule<LintContext>[] = [
11241127
}
11251128
}
11261129

1130+
// gsap_repeated_fromto_without_baseline
1131+
const fromToWindowsBySelector = new Map<string, GsapWindow[]>();
1132+
for (const win of gsapWindows) {
1133+
if (win.method !== "fromTo" || win.immediateRender === false) continue;
1134+
if (win.targetSelector === UNRESOLVED_TARGET) continue;
1135+
const windows = fromToWindowsBySelector.get(win.targetSelector) ?? [];
1136+
windows.push(win);
1137+
fromToWindowsBySelector.set(win.targetSelector, windows);
1138+
}
1139+
1140+
const repeatedFromToGroups = [...fromToWindowsBySelector.values()].filter(
1141+
(windows) => windows.length >= 2,
1142+
);
1143+
for (const fromToWindows of repeatedFromToGroups) {
1144+
const firstFromTo = fromToWindows[0];
1145+
if (!firstFromTo) continue;
1146+
const selector = firstFromTo.targetSelector;
1147+
const firstFromToIndex = gsapWindows.indexOf(firstFromTo);
1148+
const firstFromToPosition = Math.min(...fromToWindows.map((win) => win.position));
1149+
const hasTimelineBaseline = gsapWindows
1150+
.slice(0, firstFromToIndex)
1151+
.some(
1152+
(candidate) =>
1153+
candidate.method === "set" &&
1154+
!candidate.global &&
1155+
candidate.targetSelector === selector &&
1156+
candidate.position <= firstFromToPosition,
1157+
);
1158+
if (hasTimelineBaseline) continue;
1159+
1160+
findings.push({
1161+
code: "gsap_repeated_fromto_without_baseline",
1162+
severity: "warning",
1163+
message:
1164+
`${fromToWindows.length} tl.fromTo() calls target "${selector}" with no stable baseline. ` +
1165+
`The last-authored fromTo "from" values become the element's resting state for any seek before ` +
1166+
`the first tween actually runs, because GSAP applies fromTo from-values at authoring time ` +
1167+
`(immediateRender), not at tween position.`,
1168+
selector,
1169+
fixHint:
1170+
`Add \`immediateRender: false\` to the destination vars of each future fromTo, or set a safe ` +
1171+
`resting state with an earlier \`tl.set("${selector}", { ... }, 0)\`. Pre-first-tween seeks must not ` +
1172+
`inherit whichever fromTo call happened to author last.`,
1173+
snippet: truncateSnippet(fromToWindows.map((win) => win.raw).join("\n")),
1174+
});
1175+
}
1176+
11271177
// gsap_exit_missing_hard_kill
11281178
if (clipStartBoundaries.length > 0) {
11291179
for (const win of gsapWindows) {
@@ -2381,7 +2431,7 @@ export const gsapRules: LintRule<LintContext>[] = [
23812431
const initialHolds = firstTweenIndex < 0 ? windows : windows.slice(0, firstTweenIndex);
23822432
for (const win of initialHolds) {
23832433
if (!isInstantHold(win) || win.position !== 0) continue;
2384-
if (win.global || win.immediateRender) continue;
2434+
if (win.global || win.immediateRender === true) continue;
23852435
if (targetHasNoStableIdentity(win.targetSelector, win.targetIdentity)) continue;
23862436
const targetTokens = [...targetedSelectorTokens(win.targetSelector)];
23872437
const hiddenByToken =

0 commit comments

Comments
 (0)