Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions packages/lint/src/rules/adapters.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,20 @@ describe("adapter rules", () => {
expect(finding?.message).toContain("Three.js");
});

it.each([
["missing_three_script", "const scene = new THREE.Scene();"],
["missing_lottie_script", "lottie.loadAnimation({ container: document.body });"],
])(
"does not report %s in a scene, since the parent document loads the library",
async (code, usage) => {
const html = `<template id="scene-template"><div data-composition-id="scene" data-width="1920" data-height="1080"><script>${usage}</script></div></template>`;
const sub = await lintHyperframeHtml(html, { isSubComposition: true });
const bare = await lintHyperframeHtml(html);
expect(sub.findings.map((f) => f.code)).not.toContain(code);
expect(bare.findings.map((f) => f.code)).not.toContain(code);
},
);

it("does not report missing_three_script when Three.js CDN script is present", async () => {
const html = `
<html><body>
Expand Down
14 changes: 12 additions & 2 deletions packages/lint/src/rules/adapters.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
import type { LintContext, HyperframeLintFinding } from "../context";
import { readAttr, extractScriptTextsAndSrcs } from "../utils";

function inheritsLibrariesFromHost({ options, rawSource }: LintContext): boolean {
return (
options.isSubComposition === true || rawSource.trimStart().toLowerCase().startsWith("<template")
);
}

export const adapterRules: Array<(ctx: LintContext) => HyperframeLintFinding[]> = [
// missing_lottie_script
({ tags, scripts }) => {
(ctx) => {
const { tags, scripts } = ctx;
if (inheritsLibrariesFromHost(ctx)) return [];
const { texts, srcs } = extractScriptTextsAndSrcs(scripts);

const hasLottieAttr = tags.some((t) => readAttr(t.raw, "data-lottie-src") !== null);
Expand All @@ -26,7 +34,9 @@ export const adapterRules: Array<(ctx: LintContext) => HyperframeLintFinding[]>
},

// missing_three_script
({ scripts }) => {
(ctx) => {
const { scripts } = ctx;
if (inheritsLibrariesFromHost(ctx)) return [];
const { texts, srcs } = extractScriptTextsAndSrcs(scripts);

const usesThree = texts.some((t) => /\bTHREE\./.test(t));
Expand Down
Loading