Skip to content

Commit 5e30bbd

Browse files
feat(lint): flag a set that fires inside a running tween on the same property (#7059)
* feat(lint): flag a set that fires inside a running tween on the same property * fix(lint): shorten a comment line in the tween overlap helper
1 parent d03bdea commit 5e30bbd

2 files changed

Lines changed: 86 additions & 7 deletions

File tree

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

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1240,6 +1240,62 @@ describe("GSAP rules", () => {
12401240
expect(finding).toBeUndefined();
12411241
});
12421242

1243+
describe("a set that fires inside a running tween on the same property", () => {
1244+
const lint = (body: string) =>
1245+
lintHyperframeHtml(`
1246+
<html><body>
1247+
<div data-composition-id="c1" data-width="1920" data-height="1080">
1248+
<h1 id="w" class="clip" data-start="0" data-duration="5" data-track-index="0">IM</h1>
1249+
</div>
1250+
<script src="https://cdn.jsdelivr.net/npm/gsap@3/dist/gsap.min.js"></script>
1251+
<script>
1252+
window.__timelines = window.__timelines || {};
1253+
const tl = gsap.timeline({ paused: true });
1254+
${body}
1255+
window.__timelines["c1"] = tl;
1256+
</script>
1257+
</body></html>`);
1258+
const overlap = (result: Awaited<ReturnType<typeof lint>>) =>
1259+
result.findings.find((f) => f.code === "overlapping_gsap_tweens");
1260+
1261+
it("reports a hide that lands in a pop-in's last frames, naming the element and both times", async () => {
1262+
const result = await lint(`
1263+
tl.fromTo("#w", { opacity: 0 }, { opacity: 1, duration: 0.16, ease: "expo.out" }, 0.54);
1264+
tl.set("#w", { opacity: 0 }, 0.69);`);
1265+
expect(overlap(result)).toMatchObject({
1266+
severity: "warning",
1267+
selector: "#w",
1268+
message: expect.stringMatching(/set on "#w" for opacity fires at 0\.69s.*0\.54s to 0\.70s/),
1269+
fixHint: expect.stringContaining("move the set to 0.70s or later"),
1270+
});
1271+
});
1272+
1273+
it.each([
1274+
[
1275+
"after the tween ends",
1276+
`tl.to("#w", { opacity: 1, duration: 0.16 }, 0.54); tl.set("#w", { opacity: 0 }, 0.7);`,
1277+
],
1278+
[
1279+
"on the tween's first frame",
1280+
`tl.to("#w", { opacity: 1, duration: 0.16 }, 0.54); tl.set("#w", { opacity: 0 }, 0.54);`,
1281+
],
1282+
[
1283+
"on another property",
1284+
`tl.to("#w", { scale: 1.2, duration: 0.16 }, 0.54); tl.set("#w", { opacity: 0 }, 0.6);`,
1285+
],
1286+
[
1287+
"on another element",
1288+
`tl.to("#w", { opacity: 1, duration: 0.16 }, 0.54); tl.set("#x", { opacity: 0 }, 0.6);`,
1289+
],
1290+
[
1291+
"with two sets and no tween",
1292+
`tl.set("#w", { opacity: 1 }, 0.5); tl.set("#w", { opacity: 0 }, 0.5);`,
1293+
],
1294+
])("does not report a set %s", async (_label, body) => {
1295+
expect(overlap(await lint(body))).toBeUndefined();
1296+
});
1297+
});
1298+
12431299
it("does NOT report overlapping_gsap_tweens when an object-target tween is interleaved (regression)", async () => {
12441300
// Regression: a non-DOM-targeting tween like `tl.to({ _: 0 }, …)` (used to
12451301
// anchor timeline duration) was matched by the regex but skipped by the

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

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,26 @@ type GsapWindow = {
6868
const SCENE_BOUNDARY_EPSILON_SECONDS = 0.05;
6969
const TWEEN_ENDPOINT_EPSILON_SECONDS = 0.001;
7070

71+
/** Where two windows overlap; a zero-length one (a `set`) only by landing inside the other (`running`). */
72+
function windowOverlap(
73+
a: GsapWindow,
74+
b: GsapWindow,
75+
): { start: number; end: number; running?: GsapWindow } | null {
76+
const aIsPoint = a.end <= a.position;
77+
const bIsPoint = b.end <= b.position;
78+
if (aIsPoint && bIsPoint) return null;
79+
if (aIsPoint || bIsPoint) {
80+
const [point, running] = aIsPoint ? [a, b] : [b, a];
81+
const inside =
82+
point.position > running.position + TWEEN_ENDPOINT_EPSILON_SECONDS &&
83+
point.position < running.end - TWEEN_ENDPOINT_EPSILON_SECONDS;
84+
return inside ? { start: point.position, end: point.position, running } : null;
85+
}
86+
const start = Math.max(a.position, b.position);
87+
const end = Math.min(a.end, b.end);
88+
return end - start > TWEEN_ENDPOINT_EPSILON_SECONDS ? { start, end } : null;
89+
}
90+
7191
// Sentinel the GSAP parser assigns to a tween whose target it cannot statically
7292
// resolve to a concrete element (a computed variable, a helper call, etc.). It is
7393
// NOT an identity: two distinct unresolved selectors are not the same element, so
@@ -1098,31 +1118,34 @@ export const gsapRules: LintRule<LintContext>[] = [
10981118
for (let i = 0; i < gsapWindows.length; i++) {
10991119
const left = gsapWindows[i];
11001120
if (!left) continue;
1101-
if (left.end <= left.position) continue;
11021121
// Unresolved targets are unknown elements: two of them are not provably
11031122
// the same element, so an overlap between them cannot be asserted.
11041123
if (targetHasNoStableIdentity(left.targetSelector, left.targetIdentity)) continue;
11051124
for (let j = i + 1; j < gsapWindows.length; j++) {
11061125
const right = gsapWindows[j];
11071126
if (!right) continue;
1108-
if (right.end <= right.position) continue;
11091127
const leftIdentity = left.targetIdentity ?? left.targetSelector;
11101128
const rightIdentity = right.targetIdentity ?? right.targetSelector;
11111129
if (leftIdentity !== rightIdentity) continue;
1112-
const overlapStart = Math.max(left.position, right.position);
1113-
const overlapEnd = Math.min(left.end, right.end);
1114-
if (overlapEnd - overlapStart <= TWEEN_ENDPOINT_EPSILON_SECONDS) continue;
1130+
const overlap = windowOverlap(left, right);
1131+
if (!overlap) continue;
11151132
if (left.overwriteAuto || right.overwriteAuto) continue;
11161133
const sharedProperties = left.properties.filter((prop) =>
11171134
right.properties.includes(prop),
11181135
);
11191136
if (sharedProperties.length === 0) continue;
1137+
const properties = sharedProperties.join(", ");
1138+
const { running } = overlap;
11201139
findings.push({
11211140
code: "overlapping_gsap_tweens",
11221141
severity: "warning",
1123-
message: `GSAP tweens overlap on "${left.targetSelector}" for ${sharedProperties.join(", ")} between ${overlapStart.toFixed(2)}s and ${overlapEnd.toFixed(2)}s.`,
1142+
message: running
1143+
? `A GSAP set on "${left.targetSelector}" for ${properties} fires at ${overlap.start.toFixed(2)}s while a tween on it is still running (${running.position.toFixed(2)}s to ${running.end.toFixed(2)}s).`
1144+
: `GSAP tweens overlap on "${left.targetSelector}" for ${properties} between ${overlap.start.toFixed(2)}s and ${overlap.end.toFixed(2)}s.`,
11241145
selector: left.targetSelector,
1125-
fixHint: 'Shorten the earlier tween, move the later tween, or add `overwrite: "auto"`.',
1146+
fixHint: running
1147+
? `Let the tween finish before the set fires: move the set to ${running.end.toFixed(2)}s or later, or shorten the tween. Played in order the set wins; seeked (scrub, export) the tween can win, so an old state stays visible.`
1148+
: 'Shorten the earlier tween, move the later tween, or add `overwrite: "auto"`.',
11261149
snippet: truncateSnippet(`${left.raw}\n${right.raw}`),
11271150
});
11281151
}

0 commit comments

Comments
 (0)