Skip to content

Commit 88efad7

Browse files
committed
fix(lint): catch broken render preflight patterns
1 parent 948264d commit 88efad7

11 files changed

Lines changed: 638 additions & 3 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: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -848,6 +848,69 @@ 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(["& #scene_01 .headline", "#scene_01 .headline"])(
886+
"reports repeated ids created by nested CSS: %s",
887+
async (nestedSelector) => {
888+
const html = `<div data-composition-id="root" data-width="1920" data-height="1080">
889+
<style>#scene_01 { ${nestedSelector} { color: red; } }</style>
890+
</div>`;
891+
const result = await lintHyperframeHtml(html);
892+
const finding = result.findings.find(
893+
(candidate) => candidate.code === "repeated_id_descendant_selector",
894+
);
895+
expect(finding).toBeDefined();
896+
expect(finding?.selector).toContain("#scene_01 #scene_01");
897+
},
898+
);
899+
900+
it.each(['[data-query="#scene_01 #scene_01"]', String.raw`#scene_01 #scene_01\:child`])(
901+
"does not report non-repeated parsed ids: %s",
902+
async (selector) => {
903+
const html = `<div data-composition-id="root" data-width="1920" data-height="1080">
904+
<style>${selector} { color: red; }</style>
905+
</div>`;
906+
const result = await lintHyperframeHtml(html);
907+
expect(
908+
result.findings.find((f) => f.code === "repeated_id_descendant_selector"),
909+
).toBeUndefined();
910+
},
911+
);
912+
});
913+
851914
it("warns when a timeline-visible element has no stable id for Studio editing", async () => {
852915
const html = `
853916
<html><body>

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

Lines changed: 76 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,52 @@ 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+
try {
32+
selectorParser((root) => {
33+
root.each((selectorNode) => {
34+
const firstCompoundById = new Map<string, number>();
35+
let compound = 0;
36+
selectorNode.each((node) => {
37+
if (repeated) return;
38+
if (node.type === "combinator") {
39+
compound += 1;
40+
return;
41+
}
42+
if (node.type !== "id") return;
43+
const firstCompound = firstCompoundById.get(node.value);
44+
if (firstCompound !== undefined && firstCompound !== compound) {
45+
repeated = node.value;
46+
return;
47+
}
48+
firstCompoundById.set(node.value, compound);
49+
});
50+
});
51+
}).processSync(selector);
52+
} catch {
53+
return null;
54+
}
55+
return repeated;
56+
}
57+
58+
function resolvedRuleSelectors(rule: postcss.Rule): string[] {
59+
let ancestor: postcss.AnyNode | undefined = rule.parent;
60+
while (ancestor && ancestor.type !== "rule") ancestor = ancestor.parent;
61+
if (!ancestor || ancestor.type !== "rule") return rule.selectors;
62+
63+
const parentSelectors = resolvedRuleSelectors(ancestor);
64+
return parentSelectors.flatMap((parentSelector) =>
65+
rule.selectors.map((childSelector) => {
66+
const nestingToken = /(^|[\s>+~,(])&/g;
67+
if (nestingToken.test(childSelector)) {
68+
return childSelector.replace(nestingToken, `$1${parentSelector}`);
69+
}
70+
return `${parentSelector} ${childSelector}`;
71+
}),
72+
);
73+
}
74+
2875
function isStudioTimelineElement(tag: { raw: string; name: string }): boolean {
2976
if (["script", "style", "link", "meta", "template", "noscript"].includes(tag.name)) {
3077
return false;
@@ -321,6 +368,35 @@ export const coreRules: Array<(ctx: LintContext) => HyperframeLintFinding[]> = [
321368
return findings;
322369
},
323370

371+
// repeated_id_descendant_selector
372+
({ styles }) => {
373+
const findings: HyperframeLintFinding[] = [];
374+
const reported = new Set<string>();
375+
for (const style of styles) {
376+
let root: postcss.Root;
377+
try {
378+
root = postcss.parse(style.content);
379+
} catch {
380+
continue;
381+
}
382+
root.walkRules((rule) => {
383+
for (const selector of resolvedRuleSelectors(rule)) {
384+
const repeatedId = repeatedDescendantId(selector);
385+
if (!repeatedId || reported.has(repeatedId)) continue;
386+
reported.add(repeatedId);
387+
findings.push({
388+
code: "repeated_id_descendant_selector",
389+
severity: "error",
390+
message: `Selector "${selector}" requires #${repeatedId} to be nested inside another #${repeatedId}. IDs must be unique, so this selector cannot match a valid composition.`,
391+
selector,
392+
fixHint: `Remove the duplicate ancestor: change \`#${repeatedId} #${repeatedId}\` to \`#${repeatedId}\`.`,
393+
});
394+
}
395+
});
396+
}
397+
return findings;
398+
},
399+
324400
// invalid_inline_script_syntax (malformed close tag)
325401
({ source }) => {
326402
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)