import type { Plugin, TransformPluginContext, SourceDescription } from "rollup"; import litTemplateInjection from "../../src/lit-source"; import { buildSourceLoc } from "../../src/utils/source-loc"; describe("Lit template injection plugin", () => { const ROOT_DIR = "/project"; const callTransform = async ( plugin: Plugin, code: string, id: string = "/project/app.ts", ): Promise => { const context = { warn: jest.fn() } as unknown as TransformPluginContext; if (typeof plugin.transform === "function") { const result = await plugin.transform.call(context, code, id); if (typeof result === "string") return { code: result }; return result as SourceDescription | null; } return null; }; /** Prepend the lit import so the plugin recognizes `html` as coming from lit. */ const lit = (code: string) => `import { html } from 'lit';\n${code}`; const DEFAULT_ID = "/project/app.ts"; const DEFAULT_RELATIVE_ID = "app.ts"; describe("basic functionality", () => { test("should inject data-source-loc with file:line:col location", async () => { const plugin = litTemplateInjection({ rootDir: ROOT_DIR }) as Plugin; const code = lit("const t = html`
hello
`;"); const result = await callTransform(plugin, code); // import line is line 1;
is on line 2 const expected = buildSourceLoc(DEFAULT_RELATIVE_ID, 2, 16); expect(result).not.toBeNull(); expect(result!.code).toContain(`
`); }); test("should produce values that decode to file:line:col", async () => { const plugin = litTemplateInjection({ rootDir: ROOT_DIR }) as Plugin; const code = lit("const t = html`
hello
`;"); const result = await callTransform(plugin, code); expect(result).not.toBeNull(); const match = result!.code.match(/data-source-loc="([^"]+)"/); expect(match).not.toBeNull(); const decoded = match![1]; expect(decoded).toMatch(/^app\.ts:\d+:\d+$/); }); test("should inject on multiple elements with distinct locations", async () => { const plugin = litTemplateInjection() as Plugin; const code = lit("const t = html`
text
`;"); const result = await callTransform(plugin, code); expect(result).not.toBeNull(); const matches = [...result!.code.matchAll(/data-source-loc="([^"]+)"/g)]; expect(matches).toHaveLength(2); // Each element should have a unique encoded location expect(matches[0][1]).not.toBe(matches[1][1]); // Both should be valid locations expect(matches[0][1]).toMatch(/:\d+:\d+$/); expect(matches[1][1]).toMatch(/:\d+:\d+$/); }); test("should inject on custom elements", async () => { const plugin = litTemplateInjection() as Plugin; const code = lit("const t = html``;"); const result = await callTransform(plugin, code); expect(result).not.toBeNull(); expect(result!.code).toMatch(/ { const plugin = litTemplateInjection() as Plugin; const code = lit("const t = html`
`;"); const result = await callTransform(plugin, code); expect(result).not.toBeNull(); expect(result!.code).toMatch(//); expect(result!.code).toMatch(/
/); }); test("should inject on elements with existing attributes", async () => { const plugin = litTemplateInjection() as Plugin; const code = lit('const t = html`
text
`;'); const result = await callTransform(plugin, code); expect(result).not.toBeNull(); expect(result!.code).toMatch( /
/, ); }); test("should generate a source map", async () => { const plugin = litTemplateInjection() as Plugin; const code = lit("const t = html`
hello
`;"); const result = await callTransform(plugin, code); expect(result).not.toBeNull(); expect(result!.map).toBeDefined(); expect(result!.map).toHaveProperty("mappings"); }); }); describe("location encoding", () => { test("should encode correct line numbers for multiline templates", async () => { const plugin = litTemplateInjection() as Plugin; const code = lit( [ "const t = html`", "
", " text", "
", "`;", ].join("\n"), ); const result = await callTransform(plugin, code); expect(result).not.toBeNull(); const matches = [...result!.code.matchAll(/data-source-loc="([^"]+)"/g)]; expect(matches).toHaveLength(2); const divLocation = matches[0][1]; const spanLocation = matches[1][1]; // import is line 1, `const t = html\`` is line 2,
is on line 3, is on line 4 expect(divLocation).toContain(":3:"); expect(spanLocation).toContain(":4:"); }); test("should include the relative file id in encoded value", async () => { const plugin = litTemplateInjection({ rootDir: ROOT_DIR }) as Plugin; const code = lit("const t = html`
hello
`;"); const customId = "/project/src/components/my-widget.ts"; const result = await callTransform(plugin, code, customId); expect(result).not.toBeNull(); const match = result!.code.match(/data-source-loc="([^"]+)"/); const decoded = match![1]; expect(decoded).toContain("src/components/my-widget.ts:"); }); }); describe("nested templates", () => { test("should inject in nested html templates", async () => { const plugin = litTemplateInjection() as Plugin; const code = lit( "const t = html`
${html`inner`}
`;", ); const result = await callTransform(plugin, code); expect(result).not.toBeNull(); const matches = [...result!.code.matchAll(/data-source-loc="([^"]+)"/g)]; expect(matches).toHaveLength(2); const divDecoded = matches[0][1]; const spanDecoded = matches[1][1]; expect(divDecoded).toMatch(/:\d+:\d+$/); expect(spanDecoded).toMatch(/:\d+:\d+$/); expect(divDecoded).not.toBe(spanDecoded); }); }); describe("no injection cases", () => { test("should not inject on closing tags", async () => { const plugin = litTemplateInjection() as Plugin; const code = lit("const t = html`
hello
`;"); const result = await callTransform(plugin, code); expect(result).not.toBeNull(); expect(result!.code).toContain("
"); expect(result!.code).not.toContain("
{ const plugin = litTemplateInjection() as Plugin; const code = "const x = 42;"; const result = await callTransform(plugin, code); expect(result).toBeNull(); }); test("should return null for filtered-out files", async () => { const plugin = litTemplateInjection() as Plugin; const code = lit("const t = html`
hello
`;"); const result = await callTransform(plugin, code, "/project/style.css"); expect(result).toBeNull(); }); test("should not inject on non-html tagged templates", async () => { const plugin = litTemplateInjection() as Plugin; const code = "import { css } from 'lit';\nconst t = css`div { color: red; }`;"; const result = await callTransform(plugin, code); expect(result).toBeNull(); }); test("should skip elements that already have data-source-loc", async () => { const plugin = litTemplateInjection() as Plugin; const code = lit( 'const t = html`
hello
`;', ); const result = await callTransform(plugin, code); expect(result).toBeNull(); }); test("should return null when html is not imported from lit", async () => { const plugin = litTemplateInjection() as Plugin; const code = "import { html } from 'other-lib';\nconst t = html`
hello
`;"; const result = await callTransform(plugin, code); expect(result).toBeNull(); }); test("should return null when html is used without any import", async () => { const plugin = litTemplateInjection() as Plugin; const code = "const html = (s) => s;\nconst t = html`
hello
`;"; const result = await callTransform(plugin, code); expect(result).toBeNull(); }); }); describe("import variations", () => { test("should return null when html is aliased (local name is not html)", async () => { const plugin = litTemplateInjection() as Plugin; const code = "import { html as myHtml } from 'lit';\nconst t = myHtml`
hello
`;"; const result = await callTransform(plugin, code); expect(result).toBeNull(); }); test("should not inject for aliased non-html import", async () => { const plugin = litTemplateInjection() as Plugin; const code = "import { css as html } from 'lit';\nconst t = html`div { color: red; }`;"; const result = await callTransform(plugin, code); expect(result).toBeNull(); }); }); describe("templates with expressions", () => { test("should inject on tags with expression attributes", async () => { const plugin = litTemplateInjection() as Plugin; const code = lit("const t = html`
text
`;"); const result = await callTransform(plugin, code); expect(result).not.toBeNull(); expect(result!.code).toMatch(/
{ const plugin = litTemplateInjection() as Plugin; const code = lit("const t = html`
${content}
`;"); const result = await callTransform(plugin, code); expect(result).not.toBeNull(); expect(result!.code).toMatch(/
/); }); }); describe("options", () => { test("should support custom attributeName", async () => { const plugin = litTemplateInjection({ attributeName: "data-testid", }) as Plugin; const code = lit("const t = html`
hello
`;"); const result = await callTransform(plugin, code); expect(result).not.toBeNull(); expect(result!.code).toMatch(/
/); expect(result!.code).not.toContain("data-source-loc"); }); test("should support skipTags", async () => { const plugin = litTemplateInjection({ skipTags: ["svg", "path"], }) as Plugin; const code = lit( 'const t = html`
`;', ); const result = await callTransform(plugin, code); expect(result).not.toBeNull(); expect(result!.code).toMatch(/
/); expect(result!.code).not.toContain("svg data-source-loc"); expect(result!.code).not.toContain("path data-source-loc"); }); test("should respect custom include filter", async () => { const plugin = litTemplateInjection({ include: "**/*.lit.ts", }) as Plugin; const code = lit("const t = html`
hello
`;"); const included = await callTransform(plugin, code, "/project/app.lit.ts"); const excluded = await callTransform(plugin, code, "/project/app.ts"); expect(included).not.toBeNull(); expect(excluded).toBeNull(); }); test("should use rootDir to produce relative paths in data-source-loc", async () => { const plugin = litTemplateInjection({ rootDir: "/workspace/my-app", }) as Plugin; const code = lit("const t = html`
hello
`;"); const result = await callTransform( plugin, code, "/workspace/my-app/src/views/home.ts", ); expect(result).not.toBeNull(); const match = result!.code.match(/data-source-loc="([^"]+)"/); const decoded = match![1]; expect(decoded).toMatch(/^src\/views\/home\.ts:\d+:\d+$/); }); test("should respect exclude filter", async () => { const plugin = litTemplateInjection({ exclude: "**/vendor/**", }) as Plugin; const code = lit("const t = html`
hello
`;"); const included = await callTransform(plugin, code, "/project/src/app.ts"); const excluded = await callTransform( plugin, code, "/project/vendor/lib.ts", ); expect(included).not.toBeNull(); expect(excluded).toBeNull(); }); }); describe("edge cases", () => { test("should handle empty template", async () => { const plugin = litTemplateInjection() as Plugin; const code = lit("const t = html``;"); const result = await callTransform(plugin, code); expect(result).toBeNull(); }); test("should handle template with only text", async () => { const plugin = litTemplateInjection() as Plugin; const code = lit("const t = html`hello world`;"); const result = await callTransform(plugin, code); expect(result).toBeNull(); }); test("should handle multiple templates in one file", async () => { const plugin = litTemplateInjection() as Plugin; const code = lit( [ "const a = html`
first
`;", "const b = html`second`;", ].join("\n"), ); const result = await callTransform(plugin, code); expect(result).not.toBeNull(); const matches = [...result!.code.matchAll(/data-source-loc="([^"]+)"/g)]; expect(matches).toHaveLength(2); // import is line 1, first template on line 2, second on line 3 expect(matches[0][1]).toContain(":2:"); expect(matches[1][1]).toContain(":3:"); }); test("should handle attribute values containing >", async () => { const plugin = litTemplateInjection() as Plugin; const code = lit('const t = html`
text
`;'); const result = await callTransform(plugin, code); expect(result).not.toBeNull(); expect(result!.code).toMatch( /
/, ); }); test("should handle single-quoted attribute values containing >", async () => { const plugin = litTemplateInjection() as Plugin; const code = lit("const t = html`
text
`;"); const result = await callTransform(plugin, code); expect(result).not.toBeNull(); expect(result!.code).toMatch( /
/, ); }); test("should not double-inject when > appears in attribute before another tag", async () => { const plugin = litTemplateInjection() as Plugin; const code = lit( 'const t = html`
inner
`;', ); const result = await callTransform(plugin, code); expect(result).not.toBeNull(); const matches = [...result!.code.matchAll(/data-source-loc="([^"]+)"/g)]; expect(matches).toHaveLength(2); // one for div, one for span expect(result!.code).toMatch( /
/, ); expect(result!.code).toMatch(//); }); test("should not modify non-lit tagged template usage", async () => { const plugin = litTemplateInjection() as Plugin; const code = lit( "const x = html`
yes
`; const y = other`no`;", ); const result = await callTransform(plugin, code); expect(result).not.toBeNull(); expect(result!.code).toMatch(/
/); expect(result!.code).toContain("no"); expect(result!.code).not.toContain("span data-source-loc"); }); }); describe("path escaping", () => { test("ampersand in path is escaped to &", async () => { const plugin = litTemplateInjection({ rootDir: "/root" }) as Plugin; const code = lit("const t = html`
hello
`;"); const result = await callTransform( plugin, code, "/root/src&utils/app.ts", ); expect(result).not.toBeNull(); const match = result!.code.match(/data-source-loc="([^"]+)"/); expect(match).not.toBeNull(); expect(match![1]).toContain("src&utils"); expect(match![1]).not.toContain("src&utils"); }); test("double-quote in path is escaped to "", async () => { const plugin = litTemplateInjection({ rootDir: "/root" }) as Plugin; const code = lit("const t = html`
hello
`;"); const result = await callTransform( plugin, code, '/root/src"weird"/app.ts', ); expect(result).not.toBeNull(); const match = result!.code.match(/data-source-loc="([^"]+)"/); expect(match).not.toBeNull(); expect(match![1]).toContain("""); }); test("less-than in path is escaped to <", async () => { const plugin = litTemplateInjection({ rootDir: "/root" }) as Plugin; const code = lit("const t = html`
hello
`;"); const result = await callTransform(plugin, code, "/root/src/app.ts"); expect(result).not.toBeNull(); const match = result!.code.match(/data-source-loc="([^"]+)"/); expect(match).not.toBeNull(); expect(match![1]).toContain("<"); expect(match![1]).toContain(">"); }); test("normal path is not modified", async () => { const plugin = litTemplateInjection({ rootDir: "/root" }) as Plugin; const code = lit("const t = html`
hello
`;"); const result = await callTransform( plugin, code, "/root/src/components/app.ts", ); expect(result).not.toBeNull(); const match = result!.code.match(/data-source-loc="([^"]+)"/); expect(match).not.toBeNull(); expect(match![1]).toMatch(/^src\/components\/app\.ts:\d+:\d+$/); }); }); describe("realistic LitElement component", () => { const COMPONENT_ID = "/project/src/components/user-profile.ts"; const COMPONENT_RELATIVE_ID = "src/components/user-profile.ts"; const componentSource = `\ import { LitElement, html, css } from 'lit'; import { customElement, property } from 'lit/decorators.js'; @customElement('user-profile') export class UserProfile extends LitElement { @property() name = ''; @property() role = ''; @property({ type: Boolean }) showDetails = false; @property({ type: Array }) badges = []; static styles = css\` :host { display: block; } .avatar { border-radius: 50%; } \`; render() { return html\`

\${this.name}

\${this.role} \${this.showDetails ? this._renderDetails() : html\`\`}
\`; } _renderDetails() { return html\`

Details

Email
\${this.email}
Joined
\${this.joinDate}
\${this._renderBadges()}
\`; } _renderBadges() { return html\`
    \${this.badges.map( (badge) => html\`
  • \${badge.label}
  • \` )}
\`; } _renderEmpty() { return html\`

No data available

\`; } _onToggle() { this.showDetails = !this.showDetails; } }`; test("should inject into all templates across render and _render* methods", async () => { const plugin = litTemplateInjection() as Plugin; const result = await callTransform(plugin, componentSource, COMPONENT_ID); expect(result).not.toBeNull(); const matches = [...result!.code.matchAll(/data-source-loc="([^"]+)"/g)]; // Count expected elements across all templates: // render(): div, img, h2, span, button, footer, slot = 7 // _renderDetails(): section, h3, dl, dt, dd, dt, dd = 7 // _renderBadges(): ul, li = 2 // _renderEmpty(): p = 1 // Total = 17 expect(matches).toHaveLength(17); }); test("should assign unique locations to every element", async () => { const plugin = litTemplateInjection() as Plugin; const result = await callTransform(plugin, componentSource, COMPONENT_ID); const matches = [...result!.code.matchAll(/data-source-loc="([^"]+)"/g)]; const decoded = matches.map((m) => m[1]); // Every element should have a distinct file:line:col const unique = new Set(decoded); expect(unique.size).toBe(matches.length); }); test("should encode the correct relative file id for all elements", async () => { const plugin = litTemplateInjection({ rootDir: ROOT_DIR }) as Plugin; const result = await callTransform(plugin, componentSource, COMPONENT_ID); const matches = [...result!.code.matchAll(/data-source-loc="([^"]+)"/g)]; for (const match of matches) { const decoded = match[1]; expect(decoded).toMatch( new RegExp( `^${COMPONENT_RELATIVE_ID.replace(/[/.]/g, "\\$&")}:\\d+:\\d+$`, ), ); } }); test("should not touch the css template literal", async () => { const plugin = litTemplateInjection() as Plugin; const result = await callTransform(plugin, componentSource, COMPONENT_ID); expect(result).not.toBeNull(); // The css block should be unchanged expect(result!.code).toContain( "css`\n :host { display: block; }\n .avatar { border-radius: 50%; }\n `", ); }); test("should preserve template expressions and event bindings", async () => { const plugin = litTemplateInjection() as Plugin; const result = await callTransform(plugin, componentSource, COMPONENT_ID); expect(result).not.toBeNull(); // Expressions should be preserved expect(result!.code).toContain("${this.name}"); expect(result!.code).toContain("${this.role}"); expect(result!.code).toContain("${this.email}"); expect(result!.code).toContain("${this.badges.map("); // Event binding should be preserved expect(result!.code).toContain("@click=${this._onToggle}"); }); test("should inject into inline conditional template (ternary)", async () => { const plugin = litTemplateInjection() as Plugin; const result = await callTransform(plugin, componentSource, COMPONENT_ID); expect(result).not.toBeNull(); // The inline html`