@sigil-dev/compiler 0.9.29 → 0.9.31

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.
Files changed (38) hide show
  1. package/index.ts +6 -4
  2. package/package.json +1 -1
  3. package/src/babel/index.ts +763 -750
  4. package/src/babel/jsx/anchor-mount.ts +338 -333
  5. package/src/babel/jsx/children.ts +116 -116
  6. package/src/babel/jsx/element.ts +1040 -1034
  7. package/src/babel/jsx/fragment.ts +271 -271
  8. package/src/babel/jsx/keyed-list.ts +339 -339
  9. package/src/babel/jsx/utils.ts +356 -356
  10. package/src/babel/util/bind.ts +143 -143
  11. package/src/babel/util/css.ts +208 -208
  12. package/src/babel/util/dead-code.ts +94 -93
  13. package/src/babel/util/links.ts +101 -0
  14. package/src/babel/util/reactive-fns.ts +110 -110
  15. package/src/babel/util/runtime-names.ts +88 -0
  16. package/src/babel/util/state-imports.ts +122 -121
  17. package/src/babel/util/static-assertion.ts +51 -51
  18. package/src/babel/util/static-env.ts +84 -0
  19. package/src/vite/index.ts +61 -61
  20. package/test/bun-plugin.test.ts +45 -45
  21. package/test/context.test.ts +110 -102
  22. package/test/fixtures/stores/index.ts +2 -2
  23. package/test/hydration-analysis.test.ts +133 -133
  24. package/test/hydration.test.ts +315 -313
  25. package/test/jsx.test.ts +254 -254
  26. package/test/links.test.ts +57 -0
  27. package/test/reactive-helpers.test.ts +135 -135
  28. package/test/render-parity.test.ts +909 -907
  29. package/test/runtime-names.test.ts +60 -0
  30. package/test/scoped-css.test.ts +166 -166
  31. package/test/ssr-boolean-attrs.test.ts +56 -56
  32. package/test/ssr-raw-text.test.ts +68 -68
  33. package/test/ssr.test.ts +30 -0
  34. package/test/static-assertion.test.ts +123 -123
  35. package/test/static-env.test.ts +85 -0
  36. package/test/static-skip.test.ts +292 -292
  37. package/test/style-object.test.ts +36 -36
  38. package/test/tree-shaking.test.ts +122 -120
package/test/jsx.test.ts CHANGED
@@ -1,254 +1,254 @@
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('setProp(_el0, "className", "app")');
20
- });
21
-
22
- test("attribute without mapping", () => {
23
- const result = transform(`const el = <input type="text" />;`);
24
- expect(result).toContain('setProp(_el0, "type", "text")');
25
- });
26
-
27
- test("bare boolean attribute goes through setProp", () => {
28
- const result = transform(`const el = <input disabled />;`);
29
- expect(result).toContain('setProp(_el0, "disabled", true)');
30
- });
31
-
32
- test("expression attribute", () => {
33
- const result = transform(`const el = <div id={myId} />;`);
34
- expect(result).toContain('setProp(_el0, "id", myId)');
35
- });
36
-
37
- describe("Reactivity", () => {
38
- test("dynamic attribute wraps in createEffect", () => {
39
- const result = transform(`
40
- let count = $state(0);
41
- const el = <div id={count} />;
42
- `);
43
- expect(result).toContain("createEffect(");
44
- expect(result).toContain('setProp(_el0, "id", count())');
45
- });
46
-
47
- test("static expression attribute does not wrap in createEffect", () => {
48
- const result = transform(`
49
- const myId = "app";
50
- const el = <div id={myId} />;
51
- `);
52
- expect(result).not.toContain("createEffect(");
53
- expect(result).toContain('setProp(_el0, "id", myId)');
54
- });
55
-
56
- test("style object compiles to per-property assignment", () => {
57
- const code = transform(`
58
- const el = <div style={{ color: "red", fontSize: "16px" }} />;
59
- `);
60
- expect(code).toContain('setStyleProp(_el0, "color", "red")');
61
- expect(code).toContain('setStyleProp(_el0, "fontSize", "16px")');
62
- expect(code).not.toContain("[object Object]");
63
- });
64
-
65
- test("reactive style property wraps in createEffect", () => {
66
- const code = transform(`
67
- let size = $state(16);
68
- const el = <div style={{ fontSize: size + "px", color: "red" }} />;
69
- `);
70
- expect(code).toContain("createEffect");
71
- expect(code).toMatch(
72
- /createEffect\(\(\) => \{\s*setStyleProp\(_el0, "fontSize"/,
73
- );
74
- expect(code).toContain('setStyleProp(_el0, "color", "red")');
75
- });
76
- });
77
-
78
- describe("Children", () => {
79
- // text children
80
- test("static text child", () => {
81
- const result = transform(`const el = <div>hello</div>;`);
82
- expect(result).toContain('createTextNode("hello")');
83
- });
84
-
85
- test("dynamic text child", () => {
86
- const result = transform(`
87
- let count = $state(0);
88
- const el = <div>{count}</div>;
89
- `);
90
- expect(result).toContain("createEffect(");
91
- expect(result).toContain("count()");
92
- });
93
-
94
- // event handlers
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
- // combined
103
- test("counter shape", () => {
104
- const result = transform(`
105
- let count = $state(0);
106
- const el = (
107
- <div>
108
- <span>{count}</span>
109
- <button onClick={() => count++}>+</button>
110
- </div>
111
- );
112
- `);
113
- expect(result).toContain("createSignal(0)");
114
- expect(result).toContain('addEventListener("click"');
115
- expect(result).toContain("createEffect(");
116
- expect(result).toContain("createTextNode");
117
- });
118
- });
119
-
120
- describe("Events", () => {
121
- test("onClick handler", () => {
122
- const result = transform(
123
- `const el = <button onClick={() => {}}>click</button>;`,
124
- );
125
- expect(result).toContain('addEventListener("click"');
126
- });
127
- });
128
-
129
- describe("Integration", () => {
130
- test("counter shape", () => {
131
- const result = transform(`
132
- let count = $state(0);
133
- const el = (
134
- <div>
135
- <span>{count}</span>
136
- <button onClick={() => count++}>+</button>
137
- </div>
138
- );
139
- `);
140
- expect(result).toContain("createSignal(0)");
141
- expect(result).toContain('addEventListener("click"');
142
- expect(result).toContain("createEffect(");
143
- expect(result).toContain("createTextNode");
144
- });
145
- });
146
-
147
- describe("Bind", () => {
148
- test("bindValue compiles to effect + event listener", () => {
149
- const code = transform(`
150
- let text = $state("");
151
- const el = <input bindValue={text} />;
152
- `);
153
- // getter: createEffect(() => el.value = text())
154
- expect(code).toContain("createEffect");
155
- expect(code).toContain(".value = text()");
156
- // setter: addEventListener("input", e => text.set(e.target.value))
157
- expect(code).toContain('"input"');
158
- expect(code).toContain("text.set(e.target.value)");
159
- });
160
-
161
- test("camelCase bind keeps the property's casing", () => {
162
- const code = transform(`
163
- let top = $state(0);
164
- const el = <div bindScrollTop={top} />;
165
- `);
166
- expect(code).toContain(".scrollTop = top()");
167
- expect(code).not.toContain("scrolltop");
168
- });
169
-
170
- test("scroll and details bindings listen for their own events", () => {
171
- const code = transform(`
172
- let top = $state(0);
173
- let open = $state(false);
174
- const a = <div bind:scrollTop={top} />;
175
- const b = <details bind:open={open} />;
176
- `);
177
- expect(code).toContain('"scroll"');
178
- expect(code).toContain('"toggle"');
179
- });
180
-
181
- test("bindChecked compiles to change event", () => {
182
- const code = transform(`
183
- let checked = $state(false);
184
- const el = <input type="checkbox" bindChecked={checked} />;
185
- `);
186
- expect(code).toContain(".checked = checked()");
187
- expect(code).toContain('"change"');
188
- expect(code).toContain("checked.set(e.target.checked)");
189
- });
190
-
191
- test("bindThis assigns element reference", () => {
192
- const code = transform(`
193
- let el = $state(null);
194
- const div = <div bindThis={el} />;
195
- `);
196
- expect(code).toContain("el.set(");
197
- expect(code).not.toContain("addEventListener");
198
- expect(code).not.toContain("createEffect(");
199
- });
200
-
201
- test("bindValue on object property uses proxy assignment", () => {
202
- const code = transform(`
203
- let form = $state({ name: "" });
204
- const el = <input bindValue={form.name} />;
205
- `);
206
- // getter: createEffect(() => el.value = form().name)
207
- expect(code).toContain("form().name");
208
- expect(code).toContain("createEffect(");
209
- // setter: proxy assignment, not .set()
210
- expect(code).toContain("form().name = e.target.value");
211
- expect(code).not.toContain("form.name.set");
212
- });
213
-
214
- test("bindValue does not affect non-signal expressions", () => {
215
- // static string should not compile — but if it does, at least no crash
216
- // this is a misuse case, just verify it does not throw
217
- expect(() =>
218
- transform(`
219
- const el = <input bindValue={"static"} />;
220
- `),
221
- ).not.toThrow();
222
- });
223
- });
224
- });
225
- describe("transition directives", () => {
226
- test("transition: emits mountTransition with transition kind", () => {
227
- const result = transform(`
228
- const fade = (el, p, d) => {};
229
- const el = <div transition:fade={fade}>hi</div>;
230
- `);
231
- expect(result).toContain("mountTransition(");
232
- expect(result).toMatch(/mountTransition\(_el\d+, fade, "transition"\)/);
233
- });
234
-
235
- test("in: and out: emit their kinds", () => {
236
- const result = transform(`
237
- const appear = () => {};
238
- const leave = () => {};
239
- const el = <div in:appear={appear} out:leave={leave}>hi</div>;
240
- `);
241
- expect(result).toMatch(/mountTransition\(_el\d+, appear, "in"\)/);
242
- expect(result).toMatch(/mountTransition\(_el\d+, leave, "out"\)/);
243
- });
244
-
245
- test("tuple params pass through", () => {
246
- const result = transform(`
247
- const fly = () => {};
248
- const el = <div in:fly={[fly, { y: 20 }]}>hi</div>;
249
- `);
250
- expect(result).toMatch(
251
- /mountTransition\(_el\d+, \[fly, \{\s*y: 20\s*\}\], "in"\)/,
252
- );
253
- });
254
- });
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('setProp(_el0, "className", "app")');
20
+ });
21
+
22
+ test("attribute without mapping", () => {
23
+ const result = transform(`const el = <input type="text" />;`);
24
+ expect(result).toContain('setProp(_el0, "type", "text")');
25
+ });
26
+
27
+ test("bare boolean attribute goes through setProp", () => {
28
+ const result = transform(`const el = <input disabled />;`);
29
+ expect(result).toContain('setProp(_el0, "disabled", true)');
30
+ });
31
+
32
+ test("expression attribute", () => {
33
+ const result = transform(`const el = <div id={myId} />;`);
34
+ expect(result).toContain('setProp(_el0, "id", myId)');
35
+ });
36
+
37
+ describe("Reactivity", () => {
38
+ test("dynamic attribute wraps in createEffect", () => {
39
+ const result = transform(`
40
+ let count = $state(0);
41
+ const el = <div id={count} />;
42
+ `);
43
+ expect(result).toContain("createEffect(");
44
+ expect(result).toContain('setProp(_el0, "id", count())');
45
+ });
46
+
47
+ test("static expression attribute does not wrap in createEffect", () => {
48
+ const result = transform(`
49
+ const myId = "app";
50
+ const el = <div id={myId} />;
51
+ `);
52
+ expect(result).not.toContain("createEffect(");
53
+ expect(result).toContain('setProp(_el0, "id", myId)');
54
+ });
55
+
56
+ test("style object compiles to per-property assignment", () => {
57
+ const code = transform(`
58
+ const el = <div style={{ color: "red", fontSize: "16px" }} />;
59
+ `);
60
+ expect(code).toContain('setStyleProp(_el0, "color", "red")');
61
+ expect(code).toContain('setStyleProp(_el0, "fontSize", "16px")');
62
+ expect(code).not.toContain("[object Object]");
63
+ });
64
+
65
+ test("reactive style property wraps in createEffect", () => {
66
+ const code = transform(`
67
+ let size = $state(16);
68
+ const el = <div style={{ fontSize: size + "px", color: "red" }} />;
69
+ `);
70
+ expect(code).toContain("createEffect");
71
+ expect(code).toMatch(
72
+ /createEffect\(\(\) => \{\s*setStyleProp\(_el0, "fontSize"/,
73
+ );
74
+ expect(code).toContain('setStyleProp(_el0, "color", "red")');
75
+ });
76
+ });
77
+
78
+ describe("Children", () => {
79
+ // text children
80
+ test("static text child", () => {
81
+ const result = transform(`const el = <div>hello</div>;`);
82
+ expect(result).toContain('createTextNode("hello")');
83
+ });
84
+
85
+ test("dynamic text child", () => {
86
+ const result = transform(`
87
+ let count = $state(0);
88
+ const el = <div>{count}</div>;
89
+ `);
90
+ expect(result).toContain("createEffect(");
91
+ expect(result).toContain("count()");
92
+ });
93
+
94
+ // event handlers
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
+ // combined
103
+ test("counter shape", () => {
104
+ const result = transform(`
105
+ let count = $state(0);
106
+ const el = (
107
+ <div>
108
+ <span>{count}</span>
109
+ <button onClick={() => count++}>+</button>
110
+ </div>
111
+ );
112
+ `);
113
+ expect(result).toContain("createSignal(0)");
114
+ expect(result).toContain('addEventListener("click"');
115
+ expect(result).toContain("createEffect(");
116
+ expect(result).toContain("createTextNode");
117
+ });
118
+ });
119
+
120
+ describe("Events", () => {
121
+ test("onClick handler", () => {
122
+ const result = transform(
123
+ `const el = <button onClick={() => {}}>click</button>;`,
124
+ );
125
+ expect(result).toContain('addEventListener("click"');
126
+ });
127
+ });
128
+
129
+ describe("Integration", () => {
130
+ test("counter shape", () => {
131
+ const result = transform(`
132
+ let count = $state(0);
133
+ const el = (
134
+ <div>
135
+ <span>{count}</span>
136
+ <button onClick={() => count++}>+</button>
137
+ </div>
138
+ );
139
+ `);
140
+ expect(result).toContain("createSignal(0)");
141
+ expect(result).toContain('addEventListener("click"');
142
+ expect(result).toContain("createEffect(");
143
+ expect(result).toContain("createTextNode");
144
+ });
145
+ });
146
+
147
+ describe("Bind", () => {
148
+ test("bindValue compiles to effect + event listener", () => {
149
+ const code = transform(`
150
+ let text = $state("");
151
+ const el = <input bindValue={text} />;
152
+ `);
153
+ // getter: createEffect(() => el.value = text())
154
+ expect(code).toContain("createEffect");
155
+ expect(code).toContain(".value = text()");
156
+ // setter: addEventListener("input", e => text.set(e.target.value))
157
+ expect(code).toContain('"input"');
158
+ expect(code).toContain("text.set(e.target.value)");
159
+ });
160
+
161
+ test("camelCase bind keeps the property's casing", () => {
162
+ const code = transform(`
163
+ let top = $state(0);
164
+ const el = <div bindScrollTop={top} />;
165
+ `);
166
+ expect(code).toContain(".scrollTop = top()");
167
+ expect(code).not.toContain("scrolltop");
168
+ });
169
+
170
+ test("scroll and details bindings listen for their own events", () => {
171
+ const code = transform(`
172
+ let top = $state(0);
173
+ let open = $state(false);
174
+ const a = <div bind:scrollTop={top} />;
175
+ const b = <details bind:open={open} />;
176
+ `);
177
+ expect(code).toContain('"scroll"');
178
+ expect(code).toContain('"toggle"');
179
+ });
180
+
181
+ test("bindChecked compiles to change event", () => {
182
+ const code = transform(`
183
+ let checked = $state(false);
184
+ const el = <input type="checkbox" bindChecked={checked} />;
185
+ `);
186
+ expect(code).toContain(".checked = checked()");
187
+ expect(code).toContain('"change"');
188
+ expect(code).toContain("checked.set(e.target.checked)");
189
+ });
190
+
191
+ test("bindThis assigns element reference", () => {
192
+ const code = transform(`
193
+ let el = $state(null);
194
+ const div = <div bindThis={el} />;
195
+ `);
196
+ expect(code).toContain("el.set(");
197
+ expect(code).not.toContain("addEventListener");
198
+ expect(code).not.toContain("createEffect(");
199
+ });
200
+
201
+ test("bindValue on object property uses proxy assignment", () => {
202
+ const code = transform(`
203
+ let form = $state({ name: "" });
204
+ const el = <input bindValue={form.name} />;
205
+ `);
206
+ // getter: createEffect(() => el.value = form().name)
207
+ expect(code).toContain("form().name");
208
+ expect(code).toContain("createEffect(");
209
+ // setter: proxy assignment, not .set()
210
+ expect(code).toContain("form().name = e.target.value");
211
+ expect(code).not.toContain("form.name.set");
212
+ });
213
+
214
+ test("bindValue does not affect non-signal expressions", () => {
215
+ // static string should not compile — but if it does, at least no crash
216
+ // this is a misuse case, just verify it does not throw
217
+ expect(() =>
218
+ transform(`
219
+ const el = <input bindValue={"static"} />;
220
+ `),
221
+ ).not.toThrow();
222
+ });
223
+ });
224
+ });
225
+ describe("transition directives", () => {
226
+ test("transition: emits mountTransition with transition kind", () => {
227
+ const result = transform(`
228
+ const fade = (el, p, d) => {};
229
+ const el = <div transition:fade={fade}>hi</div>;
230
+ `);
231
+ expect(result).toContain("mountTransition(");
232
+ expect(result).toMatch(/mountTransition\(_el\d+, fade, "transition"\)/);
233
+ });
234
+
235
+ test("in: and out: emit their kinds", () => {
236
+ const result = transform(`
237
+ const appear = () => {};
238
+ const leave = () => {};
239
+ const el = <div in:appear={appear} out:leave={leave}>hi</div>;
240
+ `);
241
+ expect(result).toMatch(/mountTransition\(_el\d+, appear, "in"\)/);
242
+ expect(result).toMatch(/mountTransition\(_el\d+, leave, "out"\)/);
243
+ });
244
+
245
+ test("tuple params pass through", () => {
246
+ const result = transform(`
247
+ const fly = () => {};
248
+ const el = <div in:fly={[fly, { y: 20 }]}>hi</div>;
249
+ `);
250
+ expect(result).toMatch(
251
+ /mountTransition\(_el\d+, \[fly, \{\s*y: 20\s*\}\], "in"\)/,
252
+ );
253
+ });
254
+ });
@@ -0,0 +1,57 @@
1
+ import { afterEach, describe, expect, spyOn, test } from "bun:test";
2
+ import { routeRegExp, setKnownRoutes } from "../src/babel/util/links";
3
+ import { transformDOM } from "./helpers/transform";
4
+
5
+ afterEach(() => setKnownRoutes(undefined));
6
+
7
+ function warnings(src: string): string[] {
8
+ const spy = spyOn(console, "warn").mockImplementation(() => {});
9
+ try {
10
+ transformDOM(src);
11
+ return spy.mock.calls.map((c) => String(c[0]));
12
+ } finally {
13
+ spy.mockRestore();
14
+ }
15
+ }
16
+
17
+ describe("route ids as patterns", () => {
18
+ test("params, optional params and rest", () => {
19
+ expect(routeRegExp("/").test("/")).toBe(true);
20
+ expect(routeRegExp("/blog/[slug]").test("/blog/hello")).toBe(true);
21
+ expect(routeRegExp("/blog/[slug]").test("/blog")).toBe(false);
22
+ expect(routeRegExp("/[[lang]]/about").test("/about")).toBe(true);
23
+ expect(routeRegExp("/[[lang]]/about").test("/sr/about")).toBe(true);
24
+ expect(routeRegExp("/docs/[...rest]").test("/docs/a/b")).toBe(true);
25
+ expect(routeRegExp("/docs/[...rest]").test("/docs")).toBe(true);
26
+ expect(routeRegExp("/about").test("/about/")).toBe(true);
27
+ });
28
+ });
29
+
30
+ describe("literal links", () => {
31
+ test("a link no route matches warns, with the nearest route", () => {
32
+ setKnownRoutes(["/", "/blog", "/blog/[slug]"]);
33
+ const out = warnings(
34
+ 'export default function N() { return <a href="/blgo">Blog</a>; }',
35
+ );
36
+ expect(out.join("\n")).toContain(
37
+ '<a href="/blgo"> matches no route in src/routes. Did you mean "/blog"?',
38
+ );
39
+ });
40
+
41
+ test("routes, params, queries, hashes, files and other sites are fine", () => {
42
+ setKnownRoutes(["/", "/blog", "/blog/[slug]"]);
43
+ const out = warnings(`export default function N() { return <nav>
44
+ <a href="/">Home</a><a href="/blog/hello?x=1#top">Post</a><a href={"/blog"}>Blog</a>
45
+ <a href="/style.css">css</a><a href="https://example.com/nope">ext</a><a href="#top">top</a>
46
+ <a href={"/" + 1}>dynamic</a>
47
+ </nav>; }`);
48
+ expect(out.filter((w) => w.includes("matches no route"))).toEqual([]);
49
+ });
50
+
51
+ test("nothing is checked outside an app", () => {
52
+ const out = warnings(
53
+ 'export default function N() { return <a href="/anything">x</a>; }',
54
+ );
55
+ expect(out.filter((w) => w.includes("matches no route"))).toEqual([]);
56
+ });
57
+ });