Skip to content

Commit c686d4f

Browse files
committed
fix(lint): the transform conflict check reads every selector of a comma list too
1 parent bc69088 commit c686d4f

2 files changed

Lines changed: 49 additions & 22 deletions

File tree

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -671,6 +671,27 @@ describe("GSAP rules", () => {
671671
expect(finding?.fixHint).toMatch(/xPercent/);
672672
});
673673

674+
it("warns for every selector of a comma list with a CSS scale transform, not only the last", async () => {
675+
const html = `
676+
<html><body>
677+
<div id="root" data-composition-id="c1" data-width="1920" data-height="1080">
678+
<div id="hero"></div><div id="badge"></div>
679+
</div>
680+
<style>
681+
#hero, #badge { transform: scale(0.8); }
682+
</style>
683+
<script>
684+
window.__timelines = window.__timelines || {};
685+
const tl = gsap.timeline({ paused: true });
686+
tl.to("#hero", { scale: 1, duration: 0.5 }, 1.0);
687+
window.__timelines["c1"] = tl;
688+
</script>
689+
</body></html>`;
690+
const result = await lintHyperframeHtml(html);
691+
const finding = result.findings.find((f) => f.code === "gsap_css_transform_conflict");
692+
expect(finding?.selector).toBe("#hero");
693+
});
694+
674695
it("warns when tl.to animates scale on an element with CSS scale transform", async () => {
675696
const html = `
676697
<html><body>

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

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1018,13 +1018,10 @@ function indexInsideNonIifeRange(
10181018
// opacity to EXACTLY zero. The declaration regex is boundary-anchored so
10191019
// `opacity: 0.98` never matches; it ends at `;` or end of input, which also
10201020
// catches a final declaration without a trailing semicolon.
1021-
function collectCssOpacityZeroSelectors(
1021+
function cssRuleSubjects(
10221022
styles: LintContext["styles"],
1023-
tags: OpenTag[],
1024-
): Set<string> {
1025-
const selectors = new Set<string>();
1026-
const opacityExactlyZero = /opacity\s*:\s*0(?:\.0+)?\s*(?:;|$)/;
1027-
1023+
): Array<{ declarations: string[]; subjects: string[] }> {
1024+
const rules: Array<{ declarations: string[]; subjects: string[] }> = [];
10281025
for (const style of styles) {
10291026
let root: postcss.Root;
10301027
try {
@@ -1033,13 +1030,26 @@ function collectCssOpacityZeroSelectors(
10331030
continue;
10341031
}
10351032
root.walkRules((rule) => {
1036-
if (!rule.nodes.some((node) => opacityExactlyZero.test(node.toString()))) return;
1037-
for (const selector of rule.selectors) {
1038-
const subject = /[#.][a-zA-Z0-9_-]+$/.exec(selector.trim());
1039-
if (subject) selectors.add(subject[0]);
1040-
}
1033+
const subjects = rule.selectors.flatMap(
1034+
(selector) => /[#.][a-zA-Z0-9_-]+$/.exec(selector.trim())?.[0] ?? [],
1035+
);
1036+
if (subjects.length) rules.push({ declarations: rule.nodes.map(String), subjects });
10411037
});
10421038
}
1039+
return rules;
1040+
}
1041+
1042+
function collectCssOpacityZeroSelectors(
1043+
styles: LintContext["styles"],
1044+
tags: OpenTag[],
1045+
): Set<string> {
1046+
const selectors = new Set<string>();
1047+
const opacityExactlyZero = /opacity\s*:\s*0(?:\.0+)?\s*(?:;|$)/;
1048+
1049+
for (const { declarations, subjects } of cssRuleSubjects(styles)) {
1050+
if (!declarations.some((declaration) => opacityExactlyZero.test(declaration))) continue;
1051+
for (const subject of subjects) selectors.add(subject);
1052+
}
10431053

10441054
for (const tag of tags) {
10451055
const inlineStyle = readAttr(tag.raw, "style");
@@ -1327,17 +1337,13 @@ export const gsapRules: LintRule<LintContext>[] = [
13271337
const cssScaleSelectors = new Map<string, string>();
13281338

13291339
// Check <style> blocks for transform rules
1330-
for (const style of styles) {
1331-
for (const [, selector, body] of style.content.matchAll(
1332-
/([#.][a-zA-Z0-9_-]+)\s*\{([^}]+)\}/g,
1333-
)) {
1334-
const tMatch = body?.match(/transform\s*:\s*([^;]+)/);
1335-
if (!tMatch || !tMatch[1]) continue;
1336-
const transformVal = tMatch[1].trim();
1337-
if (/translate/i.test(transformVal))
1338-
cssTranslateSelectors.set((selector ?? "").trim(), transformVal);
1339-
if (/scale/i.test(transformVal))
1340-
cssScaleSelectors.set((selector ?? "").trim(), transformVal);
1340+
for (const { declarations, subjects } of cssRuleSubjects(styles)) {
1341+
const tMatch = declarations.join(";").match(/transform\s*:\s*([^;]+)/);
1342+
if (!tMatch || !tMatch[1]) continue;
1343+
const transformVal = tMatch[1].trim();
1344+
for (const subject of subjects) {
1345+
if (/translate/i.test(transformVal)) cssTranslateSelectors.set(subject, transformVal);
1346+
if (/scale/i.test(transformVal)) cssScaleSelectors.set(subject, transformVal);
13411347
}
13421348
}
13431349

0 commit comments

Comments
 (0)