Skip to content

Commit 3562929

Browse files
fix(parsers): resolve media source writes in their lexical scopes
1 parent 188475a commit 3562929

4 files changed

Lines changed: 367 additions & 27 deletions

File tree

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -523,6 +523,31 @@ describe("media rules", () => {
523523
expect(findings.every((finding) => finding.fixHint?.includes("data-var-src"))).toBe(true);
524524
});
525525

526+
it.each([
527+
{
528+
script: `const target = document.getElementById("clip"); { const target = document.getElementById("poster"); target.src = "b.png"; } target.src = "b.mp4";`,
529+
expected: ["clip"],
530+
},
531+
{
532+
script: `const target = document.getElementById("clip"); function update(target) { target.src = "b.png"; }`,
533+
expected: [],
534+
},
535+
{
536+
script: `function updateVideo() { const target = document.getElementById("clip"); target.src = "b.mp4"; } function updateImage() { const target = document.getElementById("poster"); target.src = "b.png"; }`,
537+
expected: ["clip"],
538+
},
539+
])("checks the media source write in its own scope ($expected)", async ({ script, expected }) => {
540+
const html = `<html><body><div data-composition-id="c1" data-width="1920" data-height="1080">
541+
<video id="clip" src="a.mp4"></video><img id="poster" src="a.png">
542+
<script>${script}</script></div></body></html>`;
543+
const result = await lintHyperframeHtml(html);
544+
expect(
545+
result.findings
546+
.filter((finding) => finding.code === "media_runtime_src_mutation")
547+
.map((finding) => finding.elementId),
548+
).toEqual(expected);
549+
});
550+
526551
it("does not flag img or script src mutations", async () => {
527552
const html = `
528553
<html><body>
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
import { describe, expect, it } from "vitest";
2+
import { extractMediaSrcMutations } from "./mediaSrcMutation.js";
3+
4+
describe("extractMediaSrcMutations", () => {
5+
it.each([
6+
{
7+
label: "block",
8+
script: `const media = document.getElementById("video"); { const media = document.getElementById("image"); media.src = "image.png"; } media.src = "new.mp4";`,
9+
},
10+
{
11+
label: "sibling functions",
12+
script: `function updateImage() { const media = document.getElementById("image"); media.src = "image.png"; } function updateVideo() { const media = document.getElementById("video"); media.src = "new.mp4"; }`,
13+
},
14+
{
15+
label: "arrow function",
16+
script: `const media = document.getElementById("video"); const updateImage = () => { const media = document.getElementById("image"); media.src = "image.png"; }; media.src = "new.mp4";`,
17+
},
18+
])("keeps $label bindings separate", ({ script }) => {
19+
expect(extractMediaSrcMutations(script).map((mutation) => mutation.selector)).toEqual([
20+
"#image",
21+
"#video",
22+
]);
23+
});
24+
25+
it.each([
26+
{
27+
label: "parameter",
28+
script: `const media = document.getElementById("video"); function update(media) { media.src = "image.png"; } media.src = "new.mp4";`,
29+
},
30+
{
31+
label: "destructured parameter",
32+
script: `const media = document.getElementById("video"); function update({ media }) { media.src = "image.png"; } media.src = "new.mp4";`,
33+
},
34+
{
35+
label: "unknown local",
36+
script: `const media = document.getElementById("video"); { const media = findImage(); media.src = "image.png"; } media.src = "new.mp4";`,
37+
},
38+
{
39+
label: "destructured local",
40+
script: `const media = document.getElementById("video"); { const { media } = assets; media.src = "image.png"; } media.src = "new.mp4";`,
41+
},
42+
{
43+
label: "catch parameter",
44+
script: `const media = document.getElementById("video"); try { update(); } catch(media) { media.src = "image.png"; } media.src = "new.mp4";`,
45+
},
46+
{
47+
label: "loop variable",
48+
script: `const media = document.getElementById("video"); for (const media of images) { media.src = "image.png"; } media.src = "new.mp4";`,
49+
},
50+
])("does not inherit an outer target through a $label", ({ script }) => {
51+
expect(extractMediaSrcMutations(script)).toEqual([
52+
{ selector: "#video", operation: "src_assignment", raw: `media.src = "new.mp4"` },
53+
]);
54+
});
55+
56+
it("resolves a closure and its scoped aliases", () => {
57+
const script = `const media = document.getElementById("video"); { const alias = media; function update() { alias.setAttribute("src", "new.mp4"); } }`;
58+
expect(extractMediaSrcMutations(script)).toEqual([
59+
{
60+
selector: "#video",
61+
operation: "set_attribute",
62+
raw: `alias.setAttribute("src", "new.mp4")`,
63+
},
64+
]);
65+
});
66+
67+
it("keeps var declarations in their enclosing function", () => {
68+
const script = `function update() { { var media = document.getElementById("video"); } media.src = "new.mp4"; }`;
69+
expect(extractMediaSrcMutations(script).map((mutation) => mutation.selector)).toEqual([
70+
"#video",
71+
]);
72+
});
73+
74+
it("retains a parameter that is redeclared and initialized with var", () => {
75+
const script = `function update(media) { var media = document.getElementById("video"); media.src = "new.mp4"; }`;
76+
expect(extractMediaSrcMutations(script).map((mutation) => mutation.selector)).toEqual([
77+
"#video",
78+
]);
79+
});
80+
81+
it("does not inherit an outer target through a named class expression", () => {
82+
const script = `const media = document.getElementById("video"); const View = class media { update() { media.src = "not-a-video"; } }; media.src = "new.mp4";`;
83+
expect(extractMediaSrcMutations(script).map((mutation) => mutation.selector)).toEqual([
84+
"#video",
85+
]);
86+
});
87+
88+
it("does not leak block declarations outside their scope", () => {
89+
const script = `{ const media = document.getElementById("video"); } media.src = "new.mp4";`;
90+
expect(extractMediaSrcMutations(script)).toEqual([]);
91+
});
92+
93+
it("retains direct and computed DOM lookups", () => {
94+
const script = `document["getElementById"]("video")["src"] = "new.mp4"; document.querySelector("#audio").setAttribute("SRC", "new.wav");`;
95+
expect(extractMediaSrcMutations(script).map((mutation) => mutation.selector)).toEqual([
96+
"#video",
97+
"#audio",
98+
]);
99+
});
100+
101+
it("keeps module parsing and rejects malformed scripts", () => {
102+
expect(
103+
extractMediaSrcMutations(
104+
`export const media = document.getElementById("video"); media.src = "new.mp4";`,
105+
),
106+
).toHaveLength(1);
107+
expect(extractMediaSrcMutations("const media = ;")).toEqual([]);
108+
});
109+
110+
it("does not recurse through circular aliases", () => {
111+
expect(extractMediaSrcMutations(`let a = b; let b = a; a.src = "new.mp4";`)).toEqual([]);
112+
});
113+
});

0 commit comments

Comments
 (0)