Skip to content

Commit 09a5ef7

Browse files
fix(lint): stop duplicate_composition_id firing on repeated sub-composition mounts (#3404)
sub-compositions.md documents mounting one sub-composition several times with different data-variable-values to get per-instance variations. That necessarily repeats data-composition-id, so the rule reported our own documented pattern as an error and blocked check with no correct way to satisfy it. The rule bucketed every element by id with no awareness of data-composition-src, so it could not tell a composition root from a mount. The runtime already distinguishes them: repeated mounts are rewritten to id__hf1, id__hf2 so they coexist, and render, validate, inspect and snapshot all handle the pattern. Skip mounts, the same way the rule already skips tags inside an inert template. The collision it exists for is unaffected: its own fixHint names a <meta> tag carrying the root's id, and that tag has no data-composition-src. Closes #3403
1 parent e1191ed commit 09a5ef7

2 files changed

Lines changed: 54 additions & 0 deletions

File tree

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

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,50 @@ describe("composition rules", () => {
315315
expect(finding).toBeUndefined();
316316
});
317317

318+
it("does not flag one sub-composition mounted repeatedly with per-instance values", async () => {
319+
// Regression: sub-compositions.md "Per-Instance Variables" documents
320+
// mounting one source several times with different data-variable-values.
321+
// That necessarily repeats the id, and the runtime rewrites repeated
322+
// mounts to `id__hf1`/`id__hf2` so they coexist. Flagging it made the
323+
// documented pattern an error with no correct way to satisfy it.
324+
const html = `<!DOCTYPE html>
325+
<html>
326+
<body>
327+
<div data-composition-id="main" data-width="1920" data-height="1080" data-start="0" data-duration="6" data-no-timeline>
328+
<div data-composition-id="word" data-composition-src="compositions/word-caption.html" data-variable-values='{"text":"one"}' data-start="0" data-duration="2"></div>
329+
<div data-composition-id="word" data-composition-src="compositions/word-caption.html" data-variable-values='{"text":"two"}' data-start="2" data-duration="2"></div>
330+
<div data-composition-id="word" data-composition-src="compositions/word-caption.html" data-variable-values='{"text":"three"}' data-start="4" data-duration="2"></div>
331+
</div>
332+
</body>
333+
</html>`;
334+
335+
const result = await lintHyperframeHtml(html);
336+
expect(result.findings.find((f) => f.code === "duplicate_composition_id")).toBeUndefined();
337+
});
338+
339+
it("still flags a real collision between a root and a non-mount element", async () => {
340+
// The guard that keeps the exemption honest: skipping mounts must not
341+
// blind the rule to the meta-versus-root collision it exists for, even
342+
// when a legitimately repeated mount is present in the same file.
343+
const html = `<!DOCTYPE html>
344+
<html>
345+
<head>
346+
<meta name="composition-id" data-composition-id="main">
347+
</head>
348+
<body>
349+
<div data-composition-id="main" data-width="1920" data-height="1080" data-start="0" data-duration="4" data-no-timeline>
350+
<div data-composition-id="word" data-composition-src="compositions/word-caption.html" data-start="0" data-duration="2"></div>
351+
<div data-composition-id="word" data-composition-src="compositions/word-caption.html" data-start="2" data-duration="2"></div>
352+
</div>
353+
</body>
354+
</html>`;
355+
356+
const result = await lintHyperframeHtml(html);
357+
const finding = result.findings.find((f) => f.code === "duplicate_composition_id");
358+
expect(finding).toBeDefined();
359+
expect(finding?.message).toContain("main");
360+
});
361+
318362
it("ignores composition ids inside inert template content", async () => {
319363
const html = `<!DOCTYPE html>
320364
<html><body>

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,16 @@ export const compositionRules: Array<(ctx: LintContext) => HyperframeLintFinding
262262
const tagsByCompositionId = new Map<string, string[]>();
263263
for (const tag of tags) {
264264
if (isInsideInertTemplate(tag, tags)) continue;
265+
// A `data-composition-src` element is a MOUNT of a sub-composition, not a
266+
// composition root, and sub-compositions.md documents mounting one source
267+
// repeatedly with different `data-variable-values` to get per-instance
268+
// variations. Those mounts legitimately share an id: the runtime rewrites
269+
// repeated ones to `id__hf1`, `id__hf2` so they coexist. Counting them
270+
// here made the documented pattern an error with no correct way to
271+
// satisfy it. The collision this rule exists for -- a <meta> tag carrying
272+
// the root's id, per its own fixHint -- is unaffected, since that tag has
273+
// no `data-composition-src`.
274+
if (readAttr(tag.raw, "data-composition-src")) continue;
265275
const compositionId = readDecodedAttr(tag.raw, "data-composition-id");
266276
if (!compositionId || compositionId.trim().length === 0) continue;
267277

0 commit comments

Comments
 (0)