@sigil-dev/compiler 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,120 @@
1
+ import { describe, expect, test } from "bun:test";
2
+ import { transform } from "./helpers/transform.ts";
3
+
4
+ describe("JSX", () => {
5
+ test("single static element", () => {
6
+ const result = transform(`const el = <div />;`);
7
+ expect(result).toContain('document.createElement("div")');
8
+ });
9
+
10
+ test("nested static elements", () => {
11
+ const result = transform(`const el = <div><span /></div>;`);
12
+ expect(result).toContain('document.createElement("div")');
13
+ expect(result).toContain('document.createElement("span")');
14
+ expect(result).toContain(".append(");
15
+ });
16
+
17
+ test("static string attribute", () => {
18
+ const result = transform(`const el = <div class="app" />;`);
19
+ expect(result).toContain('_el0.className = "app"');
20
+ });
21
+
22
+ test("attribute without mapping", () => {
23
+ const result = transform(`const el = <input type="text" />;`);
24
+ expect(result).toContain('_el0.type = "text"');
25
+ });
26
+
27
+ test("expression attribute", () => {
28
+ const result = transform(`const el = <div id={myId} />;`);
29
+ expect(result).toContain("_el0.id = myId");
30
+ });
31
+
32
+ describe("Reactivity", () => {
33
+ test("dynamic attribute wraps in createEffect", () => {
34
+ const result = transform(`
35
+ let count = $state(0);
36
+ const el = <div id={count} />;
37
+ `);
38
+ expect(result).toContain("createEffect(");
39
+ expect(result).toContain("_el0.id = count()");
40
+ });
41
+
42
+ test("static expression attribute does not wrap in createEffect", () => {
43
+ const result = transform(`
44
+ const myId = "app";
45
+ const el = <div id={myId} />;
46
+ `);
47
+ expect(result).not.toContain("createEffect(");
48
+ expect(result).toContain("_el0.id = myId");
49
+ });
50
+ });
51
+
52
+ describe("Children", () => {
53
+ // text children
54
+ test("static text child", () => {
55
+ const result = transform(`const el = <div>hello</div>;`);
56
+ expect(result).toContain('createTextNode("hello")');
57
+ });
58
+
59
+ test("dynamic text child", () => {
60
+ const result = transform(`
61
+ let count = $state(0);
62
+ const el = <div>{count}</div>;
63
+ `);
64
+ expect(result).toContain("createEffect(");
65
+ expect(result).toContain("count()");
66
+ });
67
+
68
+ // event handlers
69
+ test("onClick handler", () => {
70
+ const result = transform(
71
+ `const el = <button onClick={() => {}}>click</button>;`,
72
+ );
73
+ expect(result).toContain('addEventListener("click"');
74
+ });
75
+
76
+ // combined
77
+ test("counter shape", () => {
78
+ const result = transform(`
79
+ let count = $state(0);
80
+ const el = (
81
+ <div>
82
+ <span>{count}</span>
83
+ <button onClick={() => count++}>+</button>
84
+ </div>
85
+ );
86
+ `);
87
+ expect(result).toContain("createSignal(0)");
88
+ expect(result).toContain('addEventListener("click"');
89
+ expect(result).toContain("createEffect(");
90
+ expect(result).toContain("createTextNode");
91
+ });
92
+ });
93
+
94
+ describe("Events", () => {
95
+ test("onClick handler", () => {
96
+ const result = transform(
97
+ `const el = <button onClick={() => {}}>click</button>;`,
98
+ );
99
+ expect(result).toContain('addEventListener("click"');
100
+ });
101
+ });
102
+
103
+ describe("Integration", () => {
104
+ test("counter shape", () => {
105
+ const result = transform(`
106
+ let count = $state(0);
107
+ const el = (
108
+ <div>
109
+ <span>{count}</span>
110
+ <button onClick={() => count++}>+</button>
111
+ </div>
112
+ );
113
+ `);
114
+ expect(result).toContain("createSignal(0)");
115
+ expect(result).toContain('addEventListener("click"');
116
+ expect(result).toContain("createEffect(");
117
+ expect(result).toContain("createTextNode");
118
+ });
119
+ });
120
+ });
@@ -0,0 +1,88 @@
1
+ import { describe, expect, test } from "bun:test";
2
+ import { transform } from "./helpers/transform.ts";
3
+
4
+ describe("Keyed Lists", () => {
5
+ test("detects .map() with key prop", () => {
6
+ const result = transform(`
7
+ let items = $state([{ id: 1 }]);
8
+ const el = <ul>{items.map(i => <li key={i.id}>{i.text}</li>)}</ul>;
9
+ `);
10
+ expect(result).toContain("reconcile(");
11
+ expect(result).toContain("new Map()");
12
+ expect(result).not.toContain("_mountFn");
13
+ });
14
+
15
+ test("key prop is stripped from DOM output", () => {
16
+ const result = transform(`
17
+ let items = $state([{ id: 1 }]);
18
+ const el = <ul>{items.map(i => <li key={i.id}>{i.text}</li>)}</ul>;
19
+ `);
20
+ expect(result).not.toContain(".key =");
21
+ });
22
+
23
+ test("key extractor is arrow function of key prop", () => {
24
+ const result = transform(`
25
+ let items = $state([{ id: 1 }]);
26
+ const el = <ul>{items.map(i => <li key={i.id}>{i.text}</li>)}</ul>;
27
+ `);
28
+ // The key extractor should be: item => item.id
29
+ expect(result).toMatch(/i => i\.id/);
30
+ });
31
+
32
+ test("createNode factory contains JSX element creation", () => {
33
+ const result = transform(`
34
+ let items = $state([{ id: 1, text: "a" }]);
35
+ const el = <ul>{items.map(i => <li key={i.id}>{i.text}</li>)}</ul>;
36
+ `);
37
+ expect(result).toContain('document.createElement("li")');
38
+ expect(result).toContain("i.text");
39
+ });
40
+
41
+ test("signal expression is passed to reconcile", () => {
42
+ const result = transform(`
43
+ let items = $state([1, 2, 3]);
44
+ const el = <ul>{items.map(i => <li key={i}>{i}</li>)}</ul>;
45
+ `);
46
+ // The signal accessor should be passed as the items argument
47
+ expect(result).toMatch(/reconcile\([^,]+, [^,]+, items\(\)/);
48
+ });
49
+
50
+ test("anchor and keyMap are created", () => {
51
+ const result = transform(`
52
+ let items = $state([{ id: 1 }]);
53
+ const el = <ul>{items.map(i => <li key={i.id}>{i.text}</li>)}</ul>;
54
+ `);
55
+ expect(result).toContain("document.createComment");
56
+ expect(result).toContain("new Map()");
57
+ });
58
+
59
+ test("effect wraps reconcile call", () => {
60
+ const result = transform(`
61
+ let items = $state([{ id: 1 }]);
62
+ const el = <ul>{items.map(i => <li key={i.id}>{i.text}</li>)}</ul>;
63
+ `);
64
+ expect(result).toContain("createEffect(");
65
+ expect(result).toContain("reconcile(");
66
+ });
67
+
68
+ test("unkeyed .map() falls back to buildAnchorMount", () => {
69
+ const result = transform(`
70
+ let items = $state([1, 2, 3]);
71
+ const el = <ul>{items.map(i => <li>{i}</li>)}</ul>;
72
+ `);
73
+ // No key prop → old behavior (anchor mount, not reconcile)
74
+ expect(result).not.toContain("reconcile(");
75
+ // Should contain the old mount pattern: remove all + recreate
76
+ expect(result).toContain("_n.remove()");
77
+ expect(result).toContain("Array.isArray");
78
+ });
79
+
80
+ test("complex key expression", () => {
81
+ const result = transform(`
82
+ let items = $state([{ id: 1, meta: { version: 2 } }]);
83
+ const el = <ul>{items.map(i => <li key={i.meta.version}>{i.id}</li>)}</ul>
84
+ `);
85
+ expect(result).toContain("reconcile(");
86
+ expect(result).toMatch(/i => i\.meta\.version/);
87
+ });
88
+ });
@@ -0,0 +1,79 @@
1
+ import { describe, expect, test } from "bun:test";
2
+ import { transform } from "./helpers/transform.ts";
3
+
4
+ describe("Basic reactivity", () => {
5
+ describe("$state", () => {
6
+ test("rewrites count++", () => {
7
+ const result = transform(`let count = $state(0); count++;`);
8
+ expect(result).toContain("count.set(count() + 1)");
9
+ });
10
+
11
+ test("rewrites count = 5", () => {
12
+ const result = transform(`let count = $state(0); count = 5;`);
13
+ expect(result).toContain("count.set(5)");
14
+ });
15
+
16
+ test("rewrites count += 2", () => {
17
+ const result = transform(`let count = $state(0); count += 2;`);
18
+ expect(result).toContain("count.set(count() + 2)");
19
+ });
20
+ });
21
+
22
+ describe("$derived", () => {
23
+ test("rewrites count*2", () => {
24
+ const result = transform(`let doubled = $derived(count * 2);`);
25
+ expect(result).toContain("createMemo");
26
+ expect(result).toContain("count * 2");
27
+ });
28
+ });
29
+
30
+ describe("Object state (proxy-compatible)", () => {
31
+ test("$state({...}) compiles to createSignal({...})", () => {
32
+ const result = transform(`let user = $state({ name: "Alice" });`);
33
+ expect(result).toContain("createSignal({");
34
+ expect(result).toContain('name: "Alice"');
35
+ });
36
+
37
+ test("root reassignment becomes .set()", () => {
38
+ const result = transform(`
39
+ let user = $state({ name: "Alice" });
40
+ user = { name: "Bob" };
41
+ `);
42
+ expect(result).toContain("user.set(");
43
+ });
44
+
45
+ test("member assignment is left alone (proxy handles it)", () => {
46
+ const result = transform(`
47
+ let user = $state({ name: "Alice" });
48
+ user.name = "Bob";
49
+ `);
50
+ // user() returns the proxy, then .name = "Bob" sets through proxy trap
51
+ expect(result).toContain('user().name = "Bob"');
52
+ expect(result).not.toContain("user.set");
53
+ });
54
+
55
+ test("nested member assignment is left alone", () => {
56
+ const result = transform(`
57
+ let state = $state({ user: { name: "Alice" } });
58
+ state.user.name = "Bob";
59
+ `);
60
+ // state() returns the proxy, then .user.name = "Bob" sets through proxy trap
61
+ expect(result).toContain('state().user.name = "Bob"');
62
+ expect(result).not.toContain("state.set");
63
+ });
64
+
65
+ test("$state with array compiles to createSignal([...])", () => {
66
+ const result = transform(`let items = $state([1, 2, 3]);`);
67
+ expect(result).toContain("createSignal([1, 2, 3])");
68
+ });
69
+
70
+ test("signal read in JSX text becomes reactive text node", () => {
71
+ const result = transform(`
72
+ let user = $state({ name: "Alice" });
73
+ const el = <span>{user.name}</span>;
74
+ `);
75
+ expect(result).toContain("createEffect");
76
+ expect(result).toContain("user().name");
77
+ });
78
+ });
79
+ });
@@ -0,0 +1,129 @@
1
+ import { describe, expect, test } from "bun:test";
2
+ import { computeHash, scopeCSS } from "../src/babel/util/css.ts";
3
+ import { transform } from "./helpers/transform.ts";
4
+
5
+ describe("Scoped CSS", () => {
6
+ describe("computeHash", () => {
7
+ test("deterministic from file path", () => {
8
+ const hash1 = computeHash("/src/components/App.tsx");
9
+ const hash2 = computeHash("/src/components/App.tsx");
10
+ expect(hash1).toBe(hash2);
11
+ });
12
+
13
+ test("different files get different hashes", () => {
14
+ const hash1 = computeHash("/src/components/App.tsx");
15
+ const hash2 = computeHash("/src/components/Button.tsx");
16
+ expect(hash1).not.toBe(hash2);
17
+ });
18
+
19
+ test("starts with 's' prefix", () => {
20
+ const hash = computeHash("/src/App.tsx");
21
+ expect(hash.startsWith("s")).toBe(true);
22
+ });
23
+
24
+ test("8 characters after prefix", () => {
25
+ const hash = computeHash("/src/App.tsx");
26
+ expect(hash.length).toBe(9); // 's' + 8 hex chars
27
+ });
28
+ });
29
+
30
+ describe("scopeCSS", () => {
31
+ test("appends hash to simple selector", () => {
32
+ const result = scopeCSS(".card { color: red; }", "abc12345");
33
+ expect(result).toContain(".card.abc12345");
34
+ });
35
+
36
+ test("handles multiple selectors", () => {
37
+ const result = scopeCSS(".card, .button { color: red; }", "abc12345");
38
+ expect(result).toContain(".card.abc12345");
39
+ expect(result).toContain(".button.abc12345");
40
+ });
41
+
42
+ test(":global() escapes scoping", () => {
43
+ const result = scopeCSS(":global(.external) { color: red; }", "abc12345");
44
+ expect(result).toContain(".external");
45
+ expect(result).not.toContain(".external.abc12345");
46
+ });
47
+
48
+ test(":global() with multiple selectors", () => {
49
+ const result = scopeCSS(":global(.a, .b) { color: red; }", "abc12345");
50
+ expect(result).toContain(".a");
51
+ expect(result).toContain(".b");
52
+ expect(result).not.toContain(".a.abc12345");
53
+ expect(result).not.toContain(".b.abc12345");
54
+ });
55
+
56
+ test("handles nested braces", () => {
57
+ const result = scopeCSS(".card { .nested { color: red; } }", "abc12345");
58
+ expect(result).toContain(".card.abc12345");
59
+ expect(result).toContain(".nested.abc12345");
60
+ });
61
+
62
+ test("preserves comments", () => {
63
+ const result = scopeCSS(
64
+ "/* comment */ .card { color: red; }",
65
+ "abc12345",
66
+ );
67
+ expect(result).toContain("/* comment */");
68
+ expect(result).toContain(".card.abc12345");
69
+ });
70
+
71
+ test("handles string literals", () => {
72
+ const result = scopeCSS(
73
+ '.card::before { content: ".card"; }',
74
+ "abc12345",
75
+ );
76
+ expect(result).toContain(".card::before.abc12345");
77
+ expect(result).toContain('".card"'); // String literal preserved
78
+ });
79
+ });
80
+
81
+ describe("Babel plugin integration", () => {
82
+ test("classList.add(hash) on native elements", () => {
83
+ const result = transform(`<div />`, "abc12345");
84
+ expect(result).toContain('classList.add("abc12345")');
85
+ });
86
+
87
+ test("classList.add on nested elements", () => {
88
+ const result = transform(`<div><span /></div>`, "abc12345");
89
+ // Both div and span should get the hash
90
+ const matches = result.match(/classList\.add\("abc12345"\)/g);
91
+ expect(matches?.length).toBe(2);
92
+ });
93
+
94
+ test("components do NOT get classList.add", () => {
95
+ const result = transform(`<Card />`, "abc12345");
96
+ expect(result).not.toContain("classList.add");
97
+ });
98
+
99
+ test("no hash option = no classList.add", () => {
100
+ const result = transform(`<div />`);
101
+ expect(result).not.toContain("classList.add");
102
+ });
103
+
104
+ test("classList.add after createElement and attributes", () => {
105
+ const result = transform(`<div class="foo" />`, "abc12345");
106
+ // className = "foo" should come before classList.add
107
+ const classNameIdx = result.indexOf('.className = "foo"');
108
+ const classListIdx = result.indexOf('classList.add("abc12345")');
109
+ expect(classNameIdx).toBeGreaterThan(0);
110
+ expect(classListIdx).toBeGreaterThan(classNameIdx);
111
+ });
112
+
113
+ test("dynamic class preserves scope hash after update", () => {
114
+ const result = transform(
115
+ `
116
+ let dynamicClass = $state("foo");
117
+ const el = <div class={dynamicClass} />;
118
+ `,
119
+ "abc12345",
120
+ );
121
+ // Should use classList.value instead of className
122
+ expect(result).toContain("classList.value = dynamicClass()");
123
+ // Should re-add hash inside the effect
124
+ expect(result).toContain('classList.add("abc12345")');
125
+ // Should NOT use className assignment (which would wipe hash)
126
+ expect(result).not.toContain("className = dynamicClass()");
127
+ });
128
+ });
129
+ });
@@ -0,0 +1,130 @@
1
+ import { describe, expect, test } from "bun:test";
2
+ import { transformSSR } from "./helpers/transform.ts";
3
+
4
+ describe("SSR", () => {
5
+ describe("$state", () => {
6
+ test("becomes plain let", () => {
7
+ const result = transformSSR(`let count = $state(0);`);
8
+ expect(result).toContain("let count = 0");
9
+ expect(result).not.toContain("createSignal");
10
+ });
11
+
12
+ test("mutations stay as mutations", () => {
13
+ const result = transformSSR(`let count = $state(0); count++;`);
14
+ expect(result).toContain("let count = 0");
15
+ expect(result).toContain("count++");
16
+ });
17
+ });
18
+
19
+ describe("$derived", () => {
20
+ test("becomes arrow function", () => {
21
+ const result = transformSSR(
22
+ `let count = $state(0); let doubled = $derived(count * 2);`,
23
+ );
24
+ expect(result).toContain("const doubled = () => count * 2");
25
+ });
26
+ });
27
+
28
+ describe("$effect", () => {
29
+ test("is dropped entirely", () => {
30
+ const result = transformSSR(`$effect(() => { console.log("hi"); });`);
31
+ expect(result).not.toContain("createEffect");
32
+ expect(result).not.toContain("console.log");
33
+ });
34
+ });
35
+
36
+ describe("JSX", () => {
37
+ test("static element", () => {
38
+ const result = transformSSR(`const el = <div />;`);
39
+ expect(result).toContain("<div>");
40
+ expect(result).toContain("</div>");
41
+ });
42
+
43
+ test("static element with class", () => {
44
+ const result = transformSSR(`const el = <div class="app" />;`);
45
+ expect(result).toContain('class="app"');
46
+ expect(result).not.toContain("className");
47
+ });
48
+
49
+ test("void element self closes", () => {
50
+ const result = transformSSR(`const el = <input type="text" />;`);
51
+ expect(result).toContain("<input");
52
+ expect(result).not.toContain("</input>");
53
+ });
54
+
55
+ test("static text child", () => {
56
+ const result = transformSSR(`const el = <div>hello</div>;`);
57
+ expect(result).toContain("<div>");
58
+ expect(result).toContain("hello");
59
+ expect(result).toContain("</div>");
60
+ });
61
+
62
+ test("dynamic child is escaped", () => {
63
+ const result = transformSSR(`
64
+ let count = $state(0);
65
+ const el = <div>{count}</div>;
66
+ `);
67
+ expect(result).toContain("__e(");
68
+ expect(result).toContain("</div>");
69
+ });
70
+
71
+ test("event handlers dropped", () => {
72
+ const result = transformSSR(
73
+ `const el = <button onClick={() => {}}>click</button>;`,
74
+ );
75
+ expect(result).not.toContain("addEventListener");
76
+ expect(result).not.toContain("onClick");
77
+ });
78
+
79
+ test("nested elements", () => {
80
+ const result = transformSSR(`const el = <div><span>hi</span></div>;`);
81
+ expect(result).toContain("<div>");
82
+ expect(result).toContain("<span>");
83
+ expect(result).toContain("hi");
84
+ expect(result).toContain("</span>");
85
+ expect(result).toContain("</div>");
86
+ });
87
+
88
+ test("component called as function returning string", () => {
89
+ const result = transformSSR(`const el = <Card title="hi" />;`);
90
+ expect(result).toContain("Card(");
91
+ expect(result).toContain('title: "hi"');
92
+ expect(result).not.toContain("createElement");
93
+ });
94
+
95
+ test("component children passed as string", () => {
96
+ const result = transformSSR(`const el = <Card><p>body</p></Card>;`);
97
+ expect(result).toContain("Card(");
98
+ expect(result).toContain("children:");
99
+ expect(result).toContain("<p>");
100
+ });
101
+
102
+ test("runtime import is escape only", () => {
103
+ const result = transformSSR(
104
+ `const name = $state("John"); const el = <div>{name}</div>;`,
105
+ );
106
+ expect(result).toContain("__e(");
107
+ expect(result).not.toContain("createSignal");
108
+ expect(result).not.toContain("createEffect");
109
+ });
110
+
111
+ test("innerHTML passes through unsanitized", () => {
112
+ const result = transformSSR(
113
+ `const html = "<b>bold</b>"; const el = <div innerHTML={html}></div>;`,
114
+ );
115
+ expect(result).toContain("html");
116
+ // innerHTML wraps in __h() for SafeHtml, not __e() for escaping
117
+ expect(result).toContain("__h(");
118
+ expect(result).not.toMatch(/__e\([^)]*html/);
119
+ });
120
+
121
+ test("keyed list converts key to data-key", () => {
122
+ const result = transformSSR(
123
+ `let items = $state([{id: 1, text: "A"}]);
124
+ const el = <ul>{items.map(i => <li key={i.id}>{i.text}</li>)}</ul>;`,
125
+ );
126
+ expect(result).toContain("data-key");
127
+ expect(result).not.toContain('"key"');
128
+ });
129
+ });
130
+ });
package/tsconfig.json ADDED
@@ -0,0 +1,30 @@
1
+ {
2
+ "compilerOptions": {
3
+ // Environment setup & latest features
4
+ "lib": ["ESNext"],
5
+ "target": "ESNext",
6
+ "module": "Preserve",
7
+ "moduleDetection": "force",
8
+ "jsx": "react-jsx",
9
+ "allowJs": true,
10
+ "types": ["bun"],
11
+
12
+ // Bundler mode
13
+ "moduleResolution": "bundler",
14
+ "allowImportingTsExtensions": true,
15
+ "verbatimModuleSyntax": true,
16
+ "noEmit": true,
17
+
18
+ // Best practices
19
+ "strict": true,
20
+ "skipLibCheck": true,
21
+ "noFallthroughCasesInSwitch": true,
22
+ "noUncheckedIndexedAccess": true,
23
+ "noImplicitOverride": true,
24
+
25
+ // Some stricter flags (disabled by default)
26
+ "noUnusedLocals": false,
27
+ "noUnusedParameters": false,
28
+ "noPropertyAccessFromIndexSignature": false
29
+ }
30
+ }