@sigil-dev/compiler 0.9.2 → 0.9.3
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/index.ts +4 -4
- package/jsr.json +8 -8
- package/package.json +1 -1
- package/src/babel/handlers/dom/derived.ts +62 -62
- package/src/babel/handlers/dom/effect.ts +15 -15
- package/src/babel/handlers/dom/globals.ts +105 -105
- package/src/babel/handlers/dom/state.ts +107 -104
- package/src/babel/handlers/ssr/derived.ts +27 -27
- package/src/babel/handlers/ssr/state.ts +11 -11
- package/src/babel/index.ts +594 -589
- package/src/babel/jsx/anchor-mount.ts +389 -389
- package/src/babel/jsx/children.ts +124 -124
- package/src/babel/jsx/element.ts +831 -831
- package/src/babel/jsx/fragment.ts +282 -282
- package/src/babel/jsx/index.ts +2 -2
- package/src/babel/jsx/keyed-list.ts +278 -278
- package/src/babel/jsx/ssr.ts +319 -319
- package/src/babel/jsx/text-node.ts +192 -192
- package/src/babel/jsx/utils.ts +179 -179
- package/src/babel/util/bind.ts +139 -139
- package/src/babel/util/css.ts +151 -151
- package/src/babel/util/dead-code.ts +28 -28
- package/src/babel/util/helpers.ts +46 -46
- package/src/babel/util/magic.ts +11 -11
- package/src/bun-plugin.ts +92 -90
- package/src/vite/index.ts +58 -58
- package/test/async-components.test.ts +24 -24
- package/test/components.test.ts +129 -129
- package/test/containsSignal.test.ts +288 -288
- package/test/fragments.test.ts +90 -90
- package/test/helpers/transform.ts +45 -48
- package/test/hydration.test.ts +147 -147
- package/test/jsx.test.ts +197 -197
- package/test/keyed-lists.test.ts +88 -88
- package/test/reactivity.test.ts +225 -225
- package/test/scoped-css.test.ts +129 -129
- package/test/ssr.test.ts +130 -130
- package/test/tree-shaking.test.ts +78 -78
- package/tsconfig.json +30 -30
package/test/jsx.test.ts
CHANGED
|
@@ -1,197 +1,197 @@
|
|
|
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
|
-
test("style object compiles to per-property assignment", () => {
|
|
52
|
-
const code = transform(`
|
|
53
|
-
const el = <div style={{ color: "red", fontSize: "16px" }} />;
|
|
54
|
-
`);
|
|
55
|
-
expect(code).toContain('.style.color = "red"');
|
|
56
|
-
expect(code).toContain('.style.fontSize = "16px"');
|
|
57
|
-
expect(code).not.toContain("[object Object]");
|
|
58
|
-
});
|
|
59
|
-
|
|
60
|
-
test("reactive style property wraps in createEffect", () => {
|
|
61
|
-
const code = transform(`
|
|
62
|
-
let size = $state(16);
|
|
63
|
-
const el = <div style={{ fontSize: size + "px", color: "red" }} />;
|
|
64
|
-
`);
|
|
65
|
-
expect(code).toContain("createEffect");
|
|
66
|
-
expect(code).toContain(".style.fontSize");
|
|
67
|
-
expect(code).toContain('.style.color = "red"');
|
|
68
|
-
});
|
|
69
|
-
});
|
|
70
|
-
|
|
71
|
-
describe("Children", () => {
|
|
72
|
-
// text children
|
|
73
|
-
test("static text child", () => {
|
|
74
|
-
const result = transform(`const el = <div>hello</div>;`);
|
|
75
|
-
expect(result).toContain('createTextNode("hello")');
|
|
76
|
-
});
|
|
77
|
-
|
|
78
|
-
test("dynamic text child", () => {
|
|
79
|
-
const result = transform(`
|
|
80
|
-
let count = $state(0);
|
|
81
|
-
const el = <div>{count}</div>;
|
|
82
|
-
`);
|
|
83
|
-
expect(result).toContain("createEffect(");
|
|
84
|
-
expect(result).toContain("count()");
|
|
85
|
-
});
|
|
86
|
-
|
|
87
|
-
// event handlers
|
|
88
|
-
test("onClick handler", () => {
|
|
89
|
-
const result = transform(
|
|
90
|
-
`const el = <button onClick={() => {}}>click</button>;`,
|
|
91
|
-
);
|
|
92
|
-
expect(result).toContain('addEventListener("click"');
|
|
93
|
-
});
|
|
94
|
-
|
|
95
|
-
// combined
|
|
96
|
-
test("counter shape", () => {
|
|
97
|
-
const result = transform(`
|
|
98
|
-
let count = $state(0);
|
|
99
|
-
const el = (
|
|
100
|
-
<div>
|
|
101
|
-
<span>{count}</span>
|
|
102
|
-
<button onClick={() => count++}>+</button>
|
|
103
|
-
</div>
|
|
104
|
-
);
|
|
105
|
-
`);
|
|
106
|
-
expect(result).toContain("createSignal(0)");
|
|
107
|
-
expect(result).toContain('addEventListener("click"');
|
|
108
|
-
expect(result).toContain("createEffect(");
|
|
109
|
-
expect(result).toContain("createTextNode");
|
|
110
|
-
});
|
|
111
|
-
});
|
|
112
|
-
|
|
113
|
-
describe("Events", () => {
|
|
114
|
-
test("onClick handler", () => {
|
|
115
|
-
const result = transform(
|
|
116
|
-
`const el = <button onClick={() => {}}>click</button>;`,
|
|
117
|
-
);
|
|
118
|
-
expect(result).toContain('addEventListener("click"');
|
|
119
|
-
});
|
|
120
|
-
});
|
|
121
|
-
|
|
122
|
-
describe("Integration", () => {
|
|
123
|
-
test("counter shape", () => {
|
|
124
|
-
const result = transform(`
|
|
125
|
-
let count = $state(0);
|
|
126
|
-
const el = (
|
|
127
|
-
<div>
|
|
128
|
-
<span>{count}</span>
|
|
129
|
-
<button onClick={() => count++}>+</button>
|
|
130
|
-
</div>
|
|
131
|
-
);
|
|
132
|
-
`);
|
|
133
|
-
expect(result).toContain("createSignal(0)");
|
|
134
|
-
expect(result).toContain('addEventListener("click"');
|
|
135
|
-
expect(result).toContain("createEffect(");
|
|
136
|
-
expect(result).toContain("createTextNode");
|
|
137
|
-
});
|
|
138
|
-
});
|
|
139
|
-
|
|
140
|
-
describe("Bind", () => {
|
|
141
|
-
test("bindValue compiles to effect + event listener", () => {
|
|
142
|
-
const code = transform(`
|
|
143
|
-
let text = $state("");
|
|
144
|
-
const el = <input bindValue={text} />;
|
|
145
|
-
`);
|
|
146
|
-
// getter: createEffect(() => el.value = text())
|
|
147
|
-
expect(code).toContain("createEffect");
|
|
148
|
-
expect(code).toContain(".value = text()");
|
|
149
|
-
// setter: addEventListener("input", e => text.set(e.target.value))
|
|
150
|
-
expect(code).toContain('"input"');
|
|
151
|
-
expect(code).toContain("text.set(e.target.value)");
|
|
152
|
-
});
|
|
153
|
-
|
|
154
|
-
test("bindChecked compiles to change event", () => {
|
|
155
|
-
const code = transform(`
|
|
156
|
-
let checked = $state(false);
|
|
157
|
-
const el = <input type="checkbox" bindChecked={checked} />;
|
|
158
|
-
`);
|
|
159
|
-
expect(code).toContain(".checked = checked()");
|
|
160
|
-
expect(code).toContain('"change"');
|
|
161
|
-
expect(code).toContain("checked.set(e.target.checked)");
|
|
162
|
-
});
|
|
163
|
-
|
|
164
|
-
test("bindThis assigns element reference", () => {
|
|
165
|
-
const code = transform(`
|
|
166
|
-
let el = $state(null);
|
|
167
|
-
const div = <div bindThis={el} />;
|
|
168
|
-
`);
|
|
169
|
-
expect(code).toContain("el.set(");
|
|
170
|
-
expect(code).not.toContain("addEventListener");
|
|
171
|
-
expect(code).not.toContain("createEffect(");
|
|
172
|
-
});
|
|
173
|
-
|
|
174
|
-
test("bindValue on object property uses proxy assignment", () => {
|
|
175
|
-
const code = transform(`
|
|
176
|
-
let form = $state({ name: "" });
|
|
177
|
-
const el = <input bindValue={form.name} />;
|
|
178
|
-
`);
|
|
179
|
-
// getter: createEffect(() => el.value = form().name)
|
|
180
|
-
expect(code).toContain("form().name");
|
|
181
|
-
expect(code).toContain("createEffect(");
|
|
182
|
-
// setter: proxy assignment, not .set()
|
|
183
|
-
expect(code).toContain("form().name = e.target.value");
|
|
184
|
-
expect(code).not.toContain("form.name.set");
|
|
185
|
-
});
|
|
186
|
-
|
|
187
|
-
test("bindValue does not affect non-signal expressions", () => {
|
|
188
|
-
// static string should not compile — but if it does, at least no crash
|
|
189
|
-
// this is a misuse case, just verify it does not throw
|
|
190
|
-
expect(() =>
|
|
191
|
-
transform(`
|
|
192
|
-
const el = <input bindValue={"static"} />;
|
|
193
|
-
`),
|
|
194
|
-
).not.toThrow();
|
|
195
|
-
});
|
|
196
|
-
});
|
|
197
|
-
});
|
|
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
|
+
test("style object compiles to per-property assignment", () => {
|
|
52
|
+
const code = transform(`
|
|
53
|
+
const el = <div style={{ color: "red", fontSize: "16px" }} />;
|
|
54
|
+
`);
|
|
55
|
+
expect(code).toContain('.style.color = "red"');
|
|
56
|
+
expect(code).toContain('.style.fontSize = "16px"');
|
|
57
|
+
expect(code).not.toContain("[object Object]");
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
test("reactive style property wraps in createEffect", () => {
|
|
61
|
+
const code = transform(`
|
|
62
|
+
let size = $state(16);
|
|
63
|
+
const el = <div style={{ fontSize: size + "px", color: "red" }} />;
|
|
64
|
+
`);
|
|
65
|
+
expect(code).toContain("createEffect");
|
|
66
|
+
expect(code).toContain(".style.fontSize");
|
|
67
|
+
expect(code).toContain('.style.color = "red"');
|
|
68
|
+
});
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
describe("Children", () => {
|
|
72
|
+
// text children
|
|
73
|
+
test("static text child", () => {
|
|
74
|
+
const result = transform(`const el = <div>hello</div>;`);
|
|
75
|
+
expect(result).toContain('createTextNode("hello")');
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
test("dynamic text child", () => {
|
|
79
|
+
const result = transform(`
|
|
80
|
+
let count = $state(0);
|
|
81
|
+
const el = <div>{count}</div>;
|
|
82
|
+
`);
|
|
83
|
+
expect(result).toContain("createEffect(");
|
|
84
|
+
expect(result).toContain("count()");
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
// event handlers
|
|
88
|
+
test("onClick handler", () => {
|
|
89
|
+
const result = transform(
|
|
90
|
+
`const el = <button onClick={() => {}}>click</button>;`,
|
|
91
|
+
);
|
|
92
|
+
expect(result).toContain('addEventListener("click"');
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
// combined
|
|
96
|
+
test("counter shape", () => {
|
|
97
|
+
const result = transform(`
|
|
98
|
+
let count = $state(0);
|
|
99
|
+
const el = (
|
|
100
|
+
<div>
|
|
101
|
+
<span>{count}</span>
|
|
102
|
+
<button onClick={() => count++}>+</button>
|
|
103
|
+
</div>
|
|
104
|
+
);
|
|
105
|
+
`);
|
|
106
|
+
expect(result).toContain("createSignal(0)");
|
|
107
|
+
expect(result).toContain('addEventListener("click"');
|
|
108
|
+
expect(result).toContain("createEffect(");
|
|
109
|
+
expect(result).toContain("createTextNode");
|
|
110
|
+
});
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
describe("Events", () => {
|
|
114
|
+
test("onClick handler", () => {
|
|
115
|
+
const result = transform(
|
|
116
|
+
`const el = <button onClick={() => {}}>click</button>;`,
|
|
117
|
+
);
|
|
118
|
+
expect(result).toContain('addEventListener("click"');
|
|
119
|
+
});
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
describe("Integration", () => {
|
|
123
|
+
test("counter shape", () => {
|
|
124
|
+
const result = transform(`
|
|
125
|
+
let count = $state(0);
|
|
126
|
+
const el = (
|
|
127
|
+
<div>
|
|
128
|
+
<span>{count}</span>
|
|
129
|
+
<button onClick={() => count++}>+</button>
|
|
130
|
+
</div>
|
|
131
|
+
);
|
|
132
|
+
`);
|
|
133
|
+
expect(result).toContain("createSignal(0)");
|
|
134
|
+
expect(result).toContain('addEventListener("click"');
|
|
135
|
+
expect(result).toContain("createEffect(");
|
|
136
|
+
expect(result).toContain("createTextNode");
|
|
137
|
+
});
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
describe("Bind", () => {
|
|
141
|
+
test("bindValue compiles to effect + event listener", () => {
|
|
142
|
+
const code = transform(`
|
|
143
|
+
let text = $state("");
|
|
144
|
+
const el = <input bindValue={text} />;
|
|
145
|
+
`);
|
|
146
|
+
// getter: createEffect(() => el.value = text())
|
|
147
|
+
expect(code).toContain("createEffect");
|
|
148
|
+
expect(code).toContain(".value = text()");
|
|
149
|
+
// setter: addEventListener("input", e => text.set(e.target.value))
|
|
150
|
+
expect(code).toContain('"input"');
|
|
151
|
+
expect(code).toContain("text.set(e.target.value)");
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
test("bindChecked compiles to change event", () => {
|
|
155
|
+
const code = transform(`
|
|
156
|
+
let checked = $state(false);
|
|
157
|
+
const el = <input type="checkbox" bindChecked={checked} />;
|
|
158
|
+
`);
|
|
159
|
+
expect(code).toContain(".checked = checked()");
|
|
160
|
+
expect(code).toContain('"change"');
|
|
161
|
+
expect(code).toContain("checked.set(e.target.checked)");
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
test("bindThis assigns element reference", () => {
|
|
165
|
+
const code = transform(`
|
|
166
|
+
let el = $state(null);
|
|
167
|
+
const div = <div bindThis={el} />;
|
|
168
|
+
`);
|
|
169
|
+
expect(code).toContain("el.set(");
|
|
170
|
+
expect(code).not.toContain("addEventListener");
|
|
171
|
+
expect(code).not.toContain("createEffect(");
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
test("bindValue on object property uses proxy assignment", () => {
|
|
175
|
+
const code = transform(`
|
|
176
|
+
let form = $state({ name: "" });
|
|
177
|
+
const el = <input bindValue={form.name} />;
|
|
178
|
+
`);
|
|
179
|
+
// getter: createEffect(() => el.value = form().name)
|
|
180
|
+
expect(code).toContain("form().name");
|
|
181
|
+
expect(code).toContain("createEffect(");
|
|
182
|
+
// setter: proxy assignment, not .set()
|
|
183
|
+
expect(code).toContain("form().name = e.target.value");
|
|
184
|
+
expect(code).not.toContain("form.name.set");
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
test("bindValue does not affect non-signal expressions", () => {
|
|
188
|
+
// static string should not compile — but if it does, at least no crash
|
|
189
|
+
// this is a misuse case, just verify it does not throw
|
|
190
|
+
expect(() =>
|
|
191
|
+
transform(`
|
|
192
|
+
const el = <input bindValue={"static"} />;
|
|
193
|
+
`),
|
|
194
|
+
).not.toThrow();
|
|
195
|
+
});
|
|
196
|
+
});
|
|
197
|
+
});
|
package/test/keyed-lists.test.ts
CHANGED
|
@@ -1,88 +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
|
-
});
|
|
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
|
+
});
|