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
6 changes: 5 additions & 1 deletion src/vite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,16 @@ import type { SolidGrabPluginOptions } from "./types.js";
* <div | <MyComponent | <ns:tag
* Does NOT match:
* </div | <> | </ | <= | <<
* Accessor<boolean> | createContext<Type> (TypeScript generics)
*
* The negative lookbehind (?<!\w) ensures the `<` is not preceded by a
* word character, which distinguishes JSX tags from TypeScript generics.
*
* Capture group 1 = everything before we inject the attribute
* We track line numbers ourselves for accuracy.
*/
const JSX_OPEN_TAG_RE =
/(<\s*)([A-Z_a-z][\w.:-]*)(\s|\/?>)/g;
/(?<!\w)(<\s*)([A-Z_a-z][\w.:-]*)(\s|\/?>)/g;

/**
* Matches component-style names: PascalCase or contains a dot (Foo.Bar).
Expand Down
32 changes: 32 additions & 0 deletions tests/vite-plugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,38 @@ describe("transform", () => {
expect(result).toBeNull();
});

test("does not inject into TypeScript generics", () => {
const plugin = createPlugin();
const code = [
`const x: Accessor<boolean> = () => true;`,
`const ctx = createContext<ModalContextType | null>(null);`,
`const [store, setStore] = createStore<SubjectViewerStore>({});`,
`function useFoo(a: string, b: Accessor<boolean>) {}`,
`return useDittoQuery<typeof Schema, Response | null>({});`,
].join("\n");
const result = (plugin as any).transform(code, "/project/src/hooks.tsx");

// No JSX in this code — should return null (no changes)
expect(result).toBeNull();
});

test("injects into JSX but not generics in the same file", () => {
const plugin = createPlugin();
const code = [
`function App() {`,
` const x: Accessor<boolean> = () => true;`,
` return <div>hello</div>;`,
`}`,
].join("\n");
const result = (plugin as any).transform(code, "/project/src/App.tsx");

expect(result).not.toBeNull();
expect(result.code).toContain('data-solid-source=');
// The generic should be untouched
expect(result.code).toContain("Accessor<boolean>");
expect(result.code).not.toContain("Accessor<boolean data-solid");
});

test("respects jsxLocation: false", () => {
const plugin = createPlugin({ jsxLocation: false });
const code = `function App() {\n return <div>hello</div>;\n}`;
Expand Down