@sigil-dev/compiler 0.9.20 → 0.9.22
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.
- package/README.md +162 -162
- package/package.json +3 -2
- package/src/analysis/hydration.ts +899 -0
- package/src/babel/handlers/dom/state.ts +106 -106
- package/src/babel/index.ts +685 -667
- package/src/babel/jsx/anchor-mount.ts +426 -426
- package/src/babel/jsx/children.ts +116 -116
- package/src/babel/jsx/element.ts +1027 -960
- package/src/babel/jsx/fragment.ts +271 -271
- package/src/babel/jsx/keyed-list.ts +317 -317
- package/src/babel/jsx/ssr.ts +307 -297
- package/src/babel/jsx/utils.ts +355 -333
- package/src/babel/util/bind.ts +143 -143
- package/src/babel/util/compile-state.ts +5 -0
- package/src/babel/util/css.ts +208 -208
- package/src/babel/util/props.ts +256 -256
- package/src/babel/util/reactive-fns.ts +110 -0
- package/src/babel/util/static-assertion.ts +51 -0
- package/src/bun-plugin.ts +103 -103
- package/src/vite/index.ts +61 -61
- package/test/bun-plugin.test.ts +45 -45
- package/test/hydration-analysis.test.ts +133 -0
- package/test/hydration.test.ts +277 -277
- package/test/jsx.test.ts +254 -254
- package/test/reactive-helpers.test.ts +135 -0
- package/test/render-parity.test.ts +827 -827
- package/test/scoped-css.test.ts +166 -166
- package/test/ssr-boolean-attrs.test.ts +56 -56
- package/test/ssr-raw-text.test.ts +68 -68
- package/test/ssr.test.ts +130 -130
- package/test/static-assertion.test.ts +123 -0
- package/test/static-skip.test.ts +292 -0
- package/test/style-object.test.ts +36 -36
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,135 @@
|
|
|
1
|
+
import { describe, expect, test } from "bun:test";
|
|
2
|
+
import * as runtime from "../../runtime/index.ts";
|
|
3
|
+
import { transformDOM } from "./helpers/transform.ts";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* `{label()}` where `label` reads a signal in its body used to compile to a
|
|
7
|
+
* one-time insert: the read is real, but nothing in the expression names a
|
|
8
|
+
* signal, so the compiler saw nothing to track and the text never updated.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
function run(src: string, exports: string[]) {
|
|
12
|
+
const stripped = transformDOM(src).replace(
|
|
13
|
+
/import\s*\{[^}]*\}\s*from\s*["']@sigil-dev\/runtime["'];?/g,
|
|
14
|
+
"",
|
|
15
|
+
);
|
|
16
|
+
const factory = new Function(
|
|
17
|
+
"document",
|
|
18
|
+
...Object.keys(runtime),
|
|
19
|
+
`${stripped};\nreturn { ${exports.join(", ")} };`,
|
|
20
|
+
);
|
|
21
|
+
return factory(
|
|
22
|
+
(globalThis as { document: unknown }).document,
|
|
23
|
+
...Object.values(runtime),
|
|
24
|
+
) as Record<string, any>;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const effects = (src: string) =>
|
|
28
|
+
(transformDOM(src).match(/createEffect\(/g) ?? []).length;
|
|
29
|
+
|
|
30
|
+
describe("helpers that read signals", () => {
|
|
31
|
+
test("a same-file helper called in JSX updates", async () => {
|
|
32
|
+
const { el, count } = run(
|
|
33
|
+
`let count = $state(0);
|
|
34
|
+
const label = () => "n=" + count;
|
|
35
|
+
const el = <p>{label()}</p>;`,
|
|
36
|
+
["el", "count"],
|
|
37
|
+
);
|
|
38
|
+
expect(el.textContent).toBe("n=0");
|
|
39
|
+
count.set(5);
|
|
40
|
+
await runtime.tick();
|
|
41
|
+
expect(el.textContent).toBe("n=5");
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
test("through another helper, declared after the one that calls it", async () => {
|
|
45
|
+
const { el, count } = run(
|
|
46
|
+
`let count = $state(1);
|
|
47
|
+
const outer = () => inner() + "!";
|
|
48
|
+
const inner = () => "n=" + count;
|
|
49
|
+
const el = <p>{outer()}</p>;`,
|
|
50
|
+
["el", "count"],
|
|
51
|
+
);
|
|
52
|
+
expect(el.textContent).toBe("n=1!");
|
|
53
|
+
count.set(2);
|
|
54
|
+
await runtime.tick();
|
|
55
|
+
expect(el.textContent).toBe("n=2!");
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
test("a function declaration, and a derived read inside it", async () => {
|
|
59
|
+
const { el, count } = run(
|
|
60
|
+
`let count = $state(1);
|
|
61
|
+
let double = $derived(count * 2);
|
|
62
|
+
function show() { return "d=" + double; }
|
|
63
|
+
const el = <p>{show()}</p>;`,
|
|
64
|
+
["el", "count"],
|
|
65
|
+
);
|
|
66
|
+
count.set(4);
|
|
67
|
+
await runtime.tick();
|
|
68
|
+
expect(el.textContent).toBe("d=8");
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
test("in an attribute", async () => {
|
|
72
|
+
const { el, count } = run(
|
|
73
|
+
`let count = $state(0);
|
|
74
|
+
const cls = () => (count > 1 ? "big" : "small");
|
|
75
|
+
const el = <p className={cls()}>x</p>;`,
|
|
76
|
+
["el", "count"],
|
|
77
|
+
);
|
|
78
|
+
expect(el.className).toBe("small");
|
|
79
|
+
count.set(2);
|
|
80
|
+
await runtime.tick();
|
|
81
|
+
expect(el.className).toBe("big");
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
test("props read through a local helper", async () => {
|
|
85
|
+
const out = transformDOM(
|
|
86
|
+
`export const A = (props) => {
|
|
87
|
+
const title = () => props.title.toUpperCase();
|
|
88
|
+
return <h1>{title()}</h1>;
|
|
89
|
+
};`,
|
|
90
|
+
);
|
|
91
|
+
expect(out).toContain("createEffect(");
|
|
92
|
+
});
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
describe("what stays static", () => {
|
|
96
|
+
test("a pure helper gets no effect", () => {
|
|
97
|
+
expect(
|
|
98
|
+
effects(
|
|
99
|
+
`let count = $state(0);
|
|
100
|
+
const fmt = (n) => "n=" + n;
|
|
101
|
+
export const A = () => <p>{fmt(1)}</p>;`,
|
|
102
|
+
),
|
|
103
|
+
).toBe(0);
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
test("a helper that only writes is not a read", () => {
|
|
107
|
+
expect(
|
|
108
|
+
effects(
|
|
109
|
+
`let count = $state(0);
|
|
110
|
+
const reset = () => { count = 0; };
|
|
111
|
+
export const A = () => <p>{reset()}</p>;`,
|
|
112
|
+
),
|
|
113
|
+
).toBe(0);
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
test("a helper that builds JSX is inserted, not stringified", () => {
|
|
117
|
+
const out = transformDOM(
|
|
118
|
+
`let count = $state(0);
|
|
119
|
+
const badge = () => <b>{count}</b>;
|
|
120
|
+
export const A = () => <p>{badge()}</p>;`,
|
|
121
|
+
);
|
|
122
|
+
expect(out).toContain("insert(_el0, badge())");
|
|
123
|
+
expect(out).not.toContain("String(badge())");
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
test("a call inside an event handler is left alone", () => {
|
|
127
|
+
const out = transformDOM(
|
|
128
|
+
`let count = $state(0);
|
|
129
|
+
const inc = () => count++;
|
|
130
|
+
export const A = () => <button onClick={() => inc()}>x</button>;`,
|
|
131
|
+
);
|
|
132
|
+
expect(out).toContain('addEventListener("click", () => inc())');
|
|
133
|
+
expect(out).not.toContain("createEffect(");
|
|
134
|
+
});
|
|
135
|
+
});
|