Skip to content

Commit 7a294f1

Browse files
authored
fix(lint): address render preflight review feedback (#2739)
1 parent 948264d commit 7a294f1

10 files changed

Lines changed: 788 additions & 8 deletions

File tree

‎bun.lock‎

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎packages/lint/package.json‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@
5656
"@hyperframes/parsers": "workspace:*",
5757
"htmlparser2": "^10.1.0",
5858
"linkedom": "^0.18.12",
59-
"postcss": "^8.5.8"
59+
"postcss": "^8.5.8",
60+
"postcss-selector-parser": "^7.1.4"
6061
},
6162
"devDependencies": {
6263
"@types/node": "^25.0.10",

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

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -848,6 +848,108 @@ body {
848848
});
849849
});
850850

851+
describe("repeated_id_descendant_selector", () => {
852+
it("reports a selector that nests the same id inside itself", async () => {
853+
const html = `<div id="scene_01" data-composition-id="root" data-width="1920" data-height="1080">
854+
<style>#scene_01 #scene_01 .headline { color: red; }</style>
855+
</div>`;
856+
const result = await lintHyperframeHtml(html);
857+
const finding = result.findings.find((f) => f.code === "repeated_id_descendant_selector");
858+
expect(finding?.severity).toBe("error");
859+
expect(finding?.selector).toBe("#scene_01 #scene_01 .headline");
860+
});
861+
862+
it("does not report distinct descendant ids", async () => {
863+
const html = `<div id="scene_01" data-composition-id="root" data-width="1920" data-height="1080">
864+
<style>#scene_01 #headline { color: red; }</style>
865+
</div>`;
866+
const result = await lintHyperframeHtml(html);
867+
expect(
868+
result.findings.find((f) => f.code === "repeated_id_descendant_selector"),
869+
).toBeUndefined();
870+
});
871+
872+
it.each(["#scene_01 > #scene_01", "#scene_01 .wrapper #scene_01"])(
873+
"reports repeated ids across descendant combinators: %s",
874+
async (selector) => {
875+
const html = `<div data-composition-id="root" data-width="1920" data-height="1080">
876+
<style>${selector} { color: red; }</style>
877+
</div>`;
878+
const result = await lintHyperframeHtml(html);
879+
expect(
880+
result.findings.find((f) => f.code === "repeated_id_descendant_selector"),
881+
).toBeDefined();
882+
},
883+
);
884+
885+
it.each([
886+
":is(#scene_01) #scene_01",
887+
":where(#scene_01) #scene_01",
888+
"#scene_01 :is(#scene_01)",
889+
])("reports repeated ids required by selector pseudos: %s", async (selector) => {
890+
const html = `<div data-composition-id="root" data-width="1920" data-height="1080">
891+
<style>${selector} { color: red; }</style>
892+
</div>`;
893+
const result = await lintHyperframeHtml(html);
894+
expect(
895+
result.findings.find((f) => f.code === "repeated_id_descendant_selector"),
896+
).toBeDefined();
897+
});
898+
899+
it.each([
900+
":is(#scene_01, .scene) #scene_01",
901+
":not(#scene_01) #scene_01",
902+
":has(#scene_01) #scene_01",
903+
])("does not report ids that are not required by a selector pseudo: %s", async (selector) => {
904+
const html = `<div data-composition-id="root" data-width="1920" data-height="1080">
905+
<style>${selector} { color: red; }</style>
906+
</div>`;
907+
const result = await lintHyperframeHtml(html);
908+
expect(
909+
result.findings.find((f) => f.code === "repeated_id_descendant_selector"),
910+
).toBeUndefined();
911+
});
912+
913+
it.each(["& #scene_01 .headline", "#scene_01 .headline"])(
914+
"reports repeated ids created by nested CSS: %s",
915+
async (nestedSelector) => {
916+
const html = `<div data-composition-id="root" data-width="1920" data-height="1080">
917+
<style>#scene_01 { ${nestedSelector} { color: red; } }</style>
918+
</div>`;
919+
const result = await lintHyperframeHtml(html);
920+
const finding = result.findings.find(
921+
(candidate) => candidate.code === "repeated_id_descendant_selector",
922+
);
923+
expect(finding).toBeDefined();
924+
expect(finding?.selector).toContain("#scene_01 #scene_01");
925+
},
926+
);
927+
928+
it("preserves dollar sequences while resolving nested selectors", async () => {
929+
const html = `<div data-composition-id="root" data-width="1920" data-height="1080">
930+
<style>#scene_01[data-query="$1"] { & #scene_01 { color: red; } }</style>
931+
</div>`;
932+
const result = await lintHyperframeHtml(html);
933+
const finding = result.findings.find(
934+
(candidate) => candidate.code === "repeated_id_descendant_selector",
935+
);
936+
expect(finding?.selector).toBe('#scene_01[data-query="$1"] #scene_01');
937+
});
938+
939+
it.each(['[data-query="#scene_01 #scene_01"]', String.raw`#scene_01 #scene_01\:child`])(
940+
"does not report non-repeated parsed ids: %s",
941+
async (selector) => {
942+
const html = `<div data-composition-id="root" data-width="1920" data-height="1080">
943+
<style>${selector} { color: red; }</style>
944+
</div>`;
945+
const result = await lintHyperframeHtml(html);
946+
expect(
947+
result.findings.find((f) => f.code === "repeated_id_descendant_selector"),
948+
).toBeUndefined();
949+
},
950+
);
951+
});
952+
851953
it("warns when a timeline-visible element has no stable id for Studio editing", async () => {
852954
const html = `
853955
<html><body>

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

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { LintContext, HyperframeLintFinding } from "../context";
22
import postcss from "postcss";
3+
import selectorParser from "postcss-selector-parser";
34
import {
45
readAttr,
56
readDecodedAttr,
@@ -25,6 +26,87 @@ function selectorTargetsCompositionId(selector: string, compositionId: string):
2526
).test(selector);
2627
}
2728

29+
function repeatedDescendantId(selector: string): string | null {
30+
let repeated: string | null = null;
31+
32+
const requiredPseudoIds = (pseudo: selectorParser.Pseudo): Set<string> => {
33+
if (![":is", ":where"].includes(pseudo.value.toLowerCase()) || pseudo.nodes.length === 0) {
34+
return new Set<string>();
35+
}
36+
37+
const optionIdSets: Set<string>[] = [];
38+
for (const option of pseudo.nodes) {
39+
// Only promote ids from a single compound. For selector-list branches with
40+
// combinators, determining which compound is the subject requires fuller
41+
// selector semantics; skipping them avoids false positives.
42+
if (option.nodes.some((node) => node.type === "combinator")) return new Set<string>();
43+
const optionIds = new Set<string>(
44+
option.nodes.filter((node) => node.type === "id").map((node) => node.value),
45+
);
46+
optionIdSets.push(optionIds);
47+
}
48+
const [firstOptionIds, ...remainingOptionIds] = optionIdSets;
49+
return new Set<string>(
50+
[...(firstOptionIds ?? [])].filter((id) =>
51+
remainingOptionIds.every((optionIds) => optionIds.has(id)),
52+
),
53+
);
54+
};
55+
56+
try {
57+
selectorParser((root) => {
58+
root.each((selectorNode) => {
59+
const firstCompoundById = new Map<string, number>();
60+
let compound = 0;
61+
selectorNode.each((node) => {
62+
if (repeated) return;
63+
if (node.type === "combinator") {
64+
compound += 1;
65+
return;
66+
}
67+
const requiredIds =
68+
node.type === "id"
69+
? [node.value]
70+
: node.type === "pseudo"
71+
? [...requiredPseudoIds(node)]
72+
: [];
73+
for (const id of requiredIds) {
74+
const firstCompound = firstCompoundById.get(id);
75+
if (firstCompound !== undefined && firstCompound !== compound) {
76+
repeated = id;
77+
return;
78+
}
79+
firstCompoundById.set(id, compound);
80+
}
81+
});
82+
});
83+
}).processSync(selector);
84+
} catch {
85+
return null;
86+
}
87+
return repeated;
88+
}
89+
90+
function resolvedRuleSelectors(rule: postcss.Rule): string[] {
91+
let ancestor: postcss.AnyNode | undefined = rule.parent;
92+
while (ancestor && ancestor.type !== "rule") ancestor = ancestor.parent;
93+
if (!ancestor || ancestor.type !== "rule") return rule.selectors;
94+
95+
const parentSelectors = resolvedRuleSelectors(ancestor);
96+
return parentSelectors.flatMap((parentSelector) =>
97+
rule.selectors.map((childSelector) => {
98+
const nestingToken = /(^|[\s>+~,(])&/g;
99+
if (nestingToken.test(childSelector)) {
100+
return childSelector.replace(
101+
nestingToken,
102+
(_, separator: string) => separator + parentSelector,
103+
);
104+
}
105+
return `${parentSelector} ${childSelector}`;
106+
}),
107+
);
108+
}
109+
28110
function isStudioTimelineElement(tag: { raw: string; name: string }): boolean {
29111
if (["script", "style", "link", "meta", "template", "noscript"].includes(tag.name)) {
30112
return false;
@@ -321,6 +403,35 @@ export const coreRules: Array<(ctx: LintContext) => HyperframeLintFinding[]> = [
321403
return findings;
322404
},
323405

406+
// repeated_id_descendant_selector
407+
({ styles }) => {
408+
const findings: HyperframeLintFinding[] = [];
409+
const reported = new Set<string>();
410+
for (const style of styles) {
411+
let root: postcss.Root;
412+
try {
413+
root = postcss.parse(style.content);
414+
} catch {
415+
continue;
416+
}
417+
root.walkRules((rule) => {
418+
for (const selector of resolvedRuleSelectors(rule)) {
419+
const repeatedId = repeatedDescendantId(selector);
420+
if (!repeatedId || reported.has(repeatedId)) continue;
421+
reported.add(repeatedId);
422+
findings.push({
423+
code: "repeated_id_descendant_selector",
424+
severity: "error",
425+
message: `Selector "${selector}" requires #${repeatedId} to be nested inside another #${repeatedId}. IDs must be unique, so this selector cannot match a valid composition.`,
426+
selector,
427+
fixHint: `Remove the duplicate ancestor: change \`#${repeatedId} #${repeatedId}\` to \`#${repeatedId}\`.`,
428+
});
429+
}
430+
});
431+
}
432+
return findings;
433+
},
434+
324435
// invalid_inline_script_syntax (malformed close tag)
325436
({ source }) => {
326437
if (!INVALID_SCRIPT_CLOSE_PATTERN.test(source)) return [];

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,34 @@ describe("font rules", () => {
262262
expect(result.errorCount).toBe(0);
263263
});
264264

265+
it("accepts a URL-style plus alias used literally as the CSS family", async () => {
266+
const html = `<div data-composition-id="test" data-width="1920" data-height="1080">
267+
<style>
268+
@import url("https://fonts.googleapis.com/css2?family=DM+Mono&family=IBM+Plex+Mono&display=swap");
269+
h1 { font-family: 'DM+Mono', monospace; }
270+
code { font-family: 'IBM+Plex+Mono', monospace; }
271+
</style>
272+
</div>`;
273+
const result = await lintHyperframeHtml(html, { isSubComposition: true });
274+
expect(
275+
result.findings.filter((f) => f.code === "font_family_without_font_face"),
276+
).toHaveLength(0);
277+
expect(result.errorCount).toBe(0);
278+
});
279+
280+
it("does not decode percent escapes in a literal CSS family", async () => {
281+
const html = `<div data-composition-id="test" data-width="1920" data-height="1080">
282+
<style>
283+
@import url("https://fonts.googleapis.com/css2?family=DM+Mono&display=swap");
284+
h1 { font-family: 'DM%20Mono', monospace; }
285+
</style>
286+
</div>`;
287+
const result = await lintHyperframeHtml(html, { isSubComposition: true });
288+
expect(
289+
result.findings.filter((f) => f.code === "font_family_without_font_face"),
290+
).toHaveLength(1);
291+
});
292+
265293
it("still flags non-bundled families not covered by the Google Fonts URL", async () => {
266294
const html = `<div data-composition-id="test" data-width="1920" data-height="1080">
267295
<link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Inter">

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,10 @@ export const fontRules: Array<(ctx: LintContext) => HyperframeLintFinding[]> = [
220220
const googleFonts = collectGoogleFontFamilies(source, styles);
221221

222222
const undeclared = used.filter(
223-
(name) => !declared.has(name) && !FONT_ALIAS_KEYS.has(name) && !googleFonts.has(name),
223+
(name) =>
224+
!declared.has(name) &&
225+
!FONT_ALIAS_KEYS.has(name) &&
226+
!googleFonts.has(name.replace(/\+/g, " ")),
224227
);
225228
if (undeclared.length === 0) return findings;
226229

0 commit comments

Comments
 (0)