import type { Plugin, TransformPluginContext, SourceDescription } from "rollup"; import { jsxSource } from "../../src/jsx-source"; const ROOT = "/project/src"; const callTransform = async ( plugin: Plugin, code: string, id: string, ): Promise => { const ctx = {} as TransformPluginContext; let result; if (typeof plugin.transform === "function") { result = await plugin.transform.call(ctx, code, id); } else if ( plugin.transform && typeof plugin.transform === "object" && "handler" in plugin.transform ) { result = await (plugin.transform as any).handler.call(ctx, code, id); } else { return null; } if (typeof result === "string") return { code: result }; return result as SourceDescription | null; }; describe("jsxSource plugin", () => { describe("attribute injection", () => { test("simple element", async () => { const plugin = jsxSource({ rootDir: ROOT }) as Plugin; const result = await callTransform( plugin, `export const A = () =>
hello
;`, `${ROOT}/App.tsx`, ); expect(result!.code).toMatchSnapshot(); }); test("self-closing element", async () => { const plugin = jsxSource({ rootDir: ROOT }) as Plugin; const result = await callTransform( plugin, `export const A = () => ;`, `${ROOT}/App.tsx`, ); expect(result!.code).toMatchSnapshot(); }); test("nested elements — all get data-source", async () => { const plugin = jsxSource({ rootDir: ROOT }) as Plugin; const code = [ `export const A = () => (`, `
`, ` foo`, ` `, `
`, `);`, ].join("\n"); const result = await callTransform(plugin, code, `${ROOT}/App.tsx`); expect(result!.code).toMatchSnapshot(); }); test("element on line 3 — correct line number", async () => { const plugin = jsxSource({ rootDir: ROOT }) as Plugin; const code = `const x = 1;\nconst y = 2;\nconst A = () => ;`; const result = await callTransform(plugin, code, `${ROOT}/App.tsx`); expect(result!.code).toMatchSnapshot(); }); test("data-source-loc appears before existing attributes", async () => { const plugin = jsxSource({ rootDir: ROOT }) as Plugin; const result = await callTransform( plugin, `export const A = () =>
;`, `${ROOT}/App.tsx`, ); expect(result!.code).toMatchSnapshot(); }); test("nested path — relative path in attribute", async () => { const plugin = jsxSource({ rootDir: ROOT }) as Plugin; const result = await callTransform( plugin, `export const A = () =>
;`, `${ROOT}/components/Button.tsx`, ); expect(result!.code).toMatchSnapshot(); }); }); describe("JSX support", () => { test(".jsx file", async () => { const plugin = jsxSource({ rootDir: ROOT }) as Plugin; const result = await callTransform( plugin, `export const A = () =>
;`, `${ROOT}/App.jsx`, ); expect(result!.code).toMatchSnapshot(); }); test(".tsx file with type annotation", async () => { const plugin = jsxSource({ rootDir: ROOT }) as Plugin; const result = await callTransform( plugin, `export const A = (): JSX.Element =>
;`, `${ROOT}/App.tsx`, ); expect(result!.code).toMatchSnapshot(); }); }); describe("source maps", () => { test("returns a source map", async () => { const plugin = jsxSource({ rootDir: ROOT }) as Plugin; const result = await callTransform( plugin, `export const A = () =>
;`, `${ROOT}/App.tsx`, ); const map = typeof result!.map === "string" ? JSON.parse(result!.map) : result!.map; expect(map).toHaveProperty("mappings"); expect(map).toMatchSnapshot(); }); }); describe("file filtering", () => { test("skips .ts files by default", async () => { const plugin = jsxSource({ rootDir: ROOT }) as Plugin; const result = await callTransform( plugin, `export const x = 1;`, `${ROOT}/util.ts`, ); expect(result).toBeNull(); }); test("skips .js files by default", async () => { const plugin = jsxSource({ rootDir: ROOT }) as Plugin; const result = await callTransform( plugin, `export const x = 1;`, `${ROOT}/util.js`, ); expect(result).toBeNull(); }); test("custom include — processes .jsx, skips .tsx", async () => { const plugin = jsxSource({ rootDir: ROOT, include: "**/*.jsx", }) as Plugin; const jsxResult = await callTransform( plugin, `export const A = () =>
;`, `${ROOT}/App.jsx`, ); const tsxResult = await callTransform( plugin, `export const A = () =>
;`, `${ROOT}/App.tsx`, ); expect(jsxResult!.code).toMatchSnapshot(); expect(tsxResult).toBeNull(); }); test("exclude — skips *.stories.* files", async () => { const plugin = jsxSource({ rootDir: ROOT, exclude: "**/*.stories.*", }) as Plugin; const normalResult = await callTransform( plugin, `export const A = () =>
;`, `${ROOT}/App.tsx`, ); const storiesResult = await callTransform( plugin, `export const A = () =>
;`, `${ROOT}/App.stories.tsx`, ); expect(normalResult!.code).toMatchSnapshot(); expect(storiesResult).toBeNull(); }); }); describe("path escaping", () => { test("ampersand in path is escaped to &", async () => { const plugin = jsxSource({ rootDir: ROOT }) as Plugin; const result = await callTransform( plugin, `export const A = () =>
;`, `${ROOT}/src&utils/App.tsx`, ); expect(result).not.toBeNull(); const match = result!.code.match(/data-source-loc="([^"]+)"/); expect(match).not.toBeNull(); expect(match![1]).toContain("&"); expect(match![1]).not.toContain("src&utils"); }); test("double-quote in path is escaped to "", async () => { const plugin = jsxSource({ rootDir: ROOT }) as Plugin; const result = await callTransform( plugin, `export const A = () =>
;`, `${ROOT}/src"weird"/App.tsx`, ); expect(result).not.toBeNull(); const match = result!.code.match(/data-source-loc="([^"]+)"/); expect(match).not.toBeNull(); expect(match![1]).toContain("""); }); test("less-than and greater-than in path are escaped", async () => { const plugin = jsxSource({ rootDir: ROOT }) as Plugin; const result = await callTransform( plugin, `export const A = () =>
;`, `${ROOT}/src/App.tsx`, ); 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 = jsxSource({ rootDir: ROOT }) as Plugin; const result = await callTransform( plugin, `export const A = () =>
;`, `${ROOT}/components/App.tsx`, ); expect(result).not.toBeNull(); const match = result!.code.match(/data-source-loc="([^"]+)"/); expect(match).not.toBeNull(); expect(match![1]).toMatch(/^components\/App\.tsx:\d+:\d+$/); }); }); describe("rootDir option", () => { test("defaults to process.cwd()", async () => { const plugin = jsxSource() as Plugin; const result = await callTransform( plugin, `export const A = () =>
;`, `${process.cwd()}/App.tsx`, ); expect(result!.code).toMatchSnapshot(); }); test("strips rootDir prefix from attribute path", async () => { const plugin = jsxSource({ rootDir: ROOT }) as Plugin; const result = await callTransform( plugin, `export const A = () =>
;`, `${ROOT}/components/Button.tsx`, ); expect(result!.code).not.toContain(ROOT); expect(result!.code).toMatchSnapshot(); }); }); });