@sigil-dev/compiler 0.7.5 → 0.7.7
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/package.json +6 -1
- package/src/babel/handlers/dom/derived.ts +31 -1
- package/src/babel/handlers/dom/globals.ts +102 -0
- package/src/babel/index.ts +413 -202
- package/src/babel/jsx/anchor-mount.ts +28 -5
- package/src/babel/jsx/element.ts +794 -546
- package/src/babel/jsx/ssr.ts +16 -7
- package/src/babel/jsx/utils.ts +8 -0
- package/src/babel/util/bind.ts +119 -115
- package/src/babel/util/dead-code.ts +28 -28
- package/src/babel/util/helpers.ts +16 -2
- package/src/babel/util/magic.ts +6 -0
- package/src/bun-plugin.ts +20 -3
- package/src/vite/index.ts +7 -2
- package/test/hydration.test.ts +1 -1
- package/test/jsx.test.ts +144 -142
- package/test/reactivity.test.ts +147 -1
- package/test/tree-shaking.test.ts +78 -79
package/test/jsx.test.ts
CHANGED
|
@@ -2,99 +2,99 @@ import { describe, expect, test } from "bun:test";
|
|
|
2
2
|
import { transform } from "./helpers/transform.ts";
|
|
3
3
|
|
|
4
4
|
describe("JSX", () => {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
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
35
|
let count = $state(0);
|
|
36
36
|
const el = <div id={count} />;
|
|
37
37
|
`);
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
38
|
+
expect(result).toContain("createEffect(");
|
|
39
|
+
expect(result).toContain("_el0.id = count()");
|
|
40
|
+
});
|
|
41
41
|
|
|
42
|
-
|
|
43
|
-
|
|
42
|
+
test("static expression attribute does not wrap in createEffect", () => {
|
|
43
|
+
const result = transform(`
|
|
44
44
|
const myId = "app";
|
|
45
45
|
const el = <div id={myId} />;
|
|
46
46
|
`);
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
47
|
+
expect(result).not.toContain("createEffect(");
|
|
48
|
+
expect(result).toContain("_el0.id = myId");
|
|
49
|
+
});
|
|
50
50
|
|
|
51
|
-
|
|
52
|
-
|
|
51
|
+
test("style object compiles to per-property assignment", () => {
|
|
52
|
+
const code = transform(`
|
|
53
53
|
const el = <div style={{ color: "red", fontSize: "16px" }} />;
|
|
54
54
|
`);
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
55
|
+
expect(code).toContain('.style.color = "red"');
|
|
56
|
+
expect(code).toContain('.style.fontSize = "16px"');
|
|
57
|
+
expect(code).not.toContain("[object Object]");
|
|
58
|
+
});
|
|
59
59
|
|
|
60
|
-
|
|
61
|
-
|
|
60
|
+
test("reactive style property wraps in createEffect", () => {
|
|
61
|
+
const code = transform(`
|
|
62
62
|
let size = $state(16);
|
|
63
63
|
const el = <div style={{ fontSize: size + "px", color: "red" }} />;
|
|
64
64
|
`);
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
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
80
|
let count = $state(0);
|
|
81
81
|
const el = <div>{count}</div>;
|
|
82
82
|
`);
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
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
98
|
let count = $state(0);
|
|
99
99
|
const el = (
|
|
100
100
|
<div>
|
|
@@ -103,25 +103,25 @@ describe("JSX", () => {
|
|
|
103
103
|
</div>
|
|
104
104
|
);
|
|
105
105
|
`);
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
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
125
|
let count = $state(0);
|
|
126
126
|
const el = (
|
|
127
127
|
<div>
|
|
@@ -130,66 +130,68 @@ describe("JSX", () => {
|
|
|
130
130
|
</div>
|
|
131
131
|
);
|
|
132
132
|
`);
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
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
143
|
let text = $state("");
|
|
144
144
|
const el = <input bindValue={text} />;
|
|
145
145
|
`);
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
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
156
|
let checked = $state(false);
|
|
157
157
|
const el = <input type="checkbox" bindChecked={checked} />;
|
|
158
158
|
`);
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
159
|
+
expect(code).toContain(".checked = checked()");
|
|
160
|
+
expect(code).toContain('"change"');
|
|
161
|
+
expect(code).toContain("checked.set(e.target.checked)");
|
|
162
|
+
});
|
|
163
163
|
|
|
164
|
-
|
|
165
|
-
|
|
164
|
+
test("bindThis assigns element reference", () => {
|
|
165
|
+
const code = transform(`
|
|
166
166
|
let el = $state(null);
|
|
167
167
|
const div = <div bindThis={el} />;
|
|
168
168
|
`);
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
169
|
+
expect(code).toContain("el.set(");
|
|
170
|
+
expect(code).not.toContain("addEventListener");
|
|
171
|
+
expect(code).not.toContain("createEffect(");
|
|
172
|
+
});
|
|
173
173
|
|
|
174
|
-
|
|
175
|
-
|
|
174
|
+
test("bindValue on object property uses proxy assignment", () => {
|
|
175
|
+
const code = transform(`
|
|
176
176
|
let form = $state({ name: "" });
|
|
177
177
|
const el = <input bindValue={form.name} />;
|
|
178
178
|
`);
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
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(`
|
|
191
192
|
const el = <input bindValue={"static"} />;
|
|
192
|
-
`)
|
|
193
|
-
|
|
194
|
-
|
|
193
|
+
`),
|
|
194
|
+
).not.toThrow();
|
|
195
|
+
});
|
|
196
|
+
});
|
|
195
197
|
});
|
package/test/reactivity.test.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { describe, expect, test } from "bun:test";
|
|
2
|
-
import { transform } from "./helpers/transform.ts";
|
|
2
|
+
import { transform, transformSSR } from "./helpers/transform.ts";
|
|
3
3
|
|
|
4
4
|
describe("Basic reactivity", () => {
|
|
5
5
|
describe("$state", () => {
|
|
@@ -76,4 +76,150 @@ describe("Basic reactivity", () => {
|
|
|
76
76
|
expect(result).toContain("user().name");
|
|
77
77
|
});
|
|
78
78
|
});
|
|
79
|
+
|
|
80
|
+
describe("$state.raw", () => {
|
|
81
|
+
test("compiles to createRawSignal", () => {
|
|
82
|
+
const result = transform(`let state = $state.raw({ name: "Alice" });`);
|
|
83
|
+
expect(result).toContain("createRawSignal");
|
|
84
|
+
expect(result).toContain('name: "Alice"');
|
|
85
|
+
});
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
describe("$state.snapshot", () => {
|
|
89
|
+
test("compiles to snapshot call", () => {
|
|
90
|
+
const result = transform(`let snap = $state.snapshot(state());`);
|
|
91
|
+
expect(result).toContain("snapshot");
|
|
92
|
+
});
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
describe("$effect.tracking", () => {
|
|
96
|
+
test("compiles to tracking()", () => {
|
|
97
|
+
const result = transform(`let isTracking = $effect.tracking();`);
|
|
98
|
+
expect(result).toContain("tracking()");
|
|
99
|
+
});
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
describe("$effect.pre", () => {
|
|
103
|
+
test("compiles to createEffectPre", () => {
|
|
104
|
+
const result = transform(`$effect.pre(() => {});`);
|
|
105
|
+
expect(result).toContain("createEffectPre");
|
|
106
|
+
});
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
describe("$inspect", () => {
|
|
110
|
+
test("compiles standalone $inspect to createInspectEffect", () => {
|
|
111
|
+
const result = transform(`let count = $state(0); $inspect(count);`);
|
|
112
|
+
expect(result).toContain("createInspectEffect");
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
test("signal identifier gets wrapped in call inside $inspect", () => {
|
|
116
|
+
const result = transform(`let count = $state(0); $inspect(count);`);
|
|
117
|
+
// count is a signal, so it should be wrapped as count()
|
|
118
|
+
expect(result).toContain("count()");
|
|
119
|
+
});
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
describe("$derived reassign", () => {
|
|
123
|
+
test("$derived assignment becomes .set()", () => {
|
|
124
|
+
const result = transform(`
|
|
125
|
+
let count = $state(0);
|
|
126
|
+
let doubled = $derived(count * 2);
|
|
127
|
+
doubled = 10;
|
|
128
|
+
`);
|
|
129
|
+
expect(result).toContain("doubled.set(10)");
|
|
130
|
+
});
|
|
131
|
+
});
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
describe("D1 bind:innerHTML / bind:textContent", () => {
|
|
135
|
+
test("bind:innerHTML generates createEffect and addEventListener", () => {
|
|
136
|
+
const result = transform(`
|
|
137
|
+
let text = $state("");
|
|
138
|
+
<div bind:innerHTML={text} />
|
|
139
|
+
`);
|
|
140
|
+
expect(result).toContain("innerHTML");
|
|
141
|
+
expect(result).toContain("addEventListener");
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
test("bind:textContent generates createEffect and addEventListener", () => {
|
|
145
|
+
const result = transform(`
|
|
146
|
+
let text = $state("");
|
|
147
|
+
<div bind:textContent={text} />
|
|
148
|
+
`);
|
|
149
|
+
expect(result).toContain("textContent");
|
|
150
|
+
expect(result).toContain("addEventListener");
|
|
151
|
+
});
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
describe("D2 bind:group", () => {
|
|
155
|
+
test("bind:group radio compiles to change listener with .set()", () => {
|
|
156
|
+
const result = transform(`
|
|
157
|
+
let val = $state("");
|
|
158
|
+
<input type="radio" bind:group={val} value="a" />
|
|
159
|
+
`);
|
|
160
|
+
expect(result).toContain("addEventListener");
|
|
161
|
+
expect(result).toContain('"change"');
|
|
162
|
+
expect(result).toContain("val.set(");
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
test("bind:group checkbox compiles to change listener with push/splice", () => {
|
|
166
|
+
const result = transform(`
|
|
167
|
+
let arr = $state([]);
|
|
168
|
+
<input type="checkbox" bind:group={arr} value="x" />
|
|
169
|
+
`);
|
|
170
|
+
expect(result).toContain("addEventListener");
|
|
171
|
+
expect(result).toContain('"change"');
|
|
172
|
+
expect(result).toContain("push");
|
|
173
|
+
expect(result).toContain("splice");
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
test("bind:group defaults to radio when type missing", () => {
|
|
177
|
+
const result = transform(`
|
|
178
|
+
let val = $state("");
|
|
179
|
+
<input bind:group={val} value="a" />
|
|
180
|
+
`);
|
|
181
|
+
expect(result).toContain("val.set(");
|
|
182
|
+
});
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
describe("D3 $debug", () => {
|
|
186
|
+
test("compiles $debug(expr) to DEV-guarded debugger", () => {
|
|
187
|
+
const result = transform(`$debug(count);`);
|
|
188
|
+
expect(result).toContain("import.meta.env.DEV");
|
|
189
|
+
expect(result).toContain("debugger");
|
|
190
|
+
expect(result).toContain("console.log(count)");
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
test("drops $debug in SSR", () => {
|
|
194
|
+
const result = transformSSR(`$debug(count);`);
|
|
195
|
+
expect(result).not.toContain("debugger");
|
|
196
|
+
});
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
describe("D4 Window/Document/Body globals", () => {
|
|
200
|
+
test("<Window> component generates createEffect with addEventListener", () => {
|
|
201
|
+
const result = transform(`<Window onResize={fn} />`);
|
|
202
|
+
expect(result).toContain("createEffect");
|
|
203
|
+
expect(result).toContain("window");
|
|
204
|
+
expect(result).toContain("addEventListener");
|
|
205
|
+
});
|
|
206
|
+
|
|
207
|
+
test("<Document> component generates createEffect with addEventListener", () => {
|
|
208
|
+
const result = transform(`<Document onClick={fn} />`);
|
|
209
|
+
expect(result).toContain("createEffect");
|
|
210
|
+
expect(result).toContain("document");
|
|
211
|
+
expect(result).toContain("addEventListener");
|
|
212
|
+
});
|
|
213
|
+
|
|
214
|
+
test("<Body> component generates createEffect with addEventListener", () => {
|
|
215
|
+
const result = transform(`<Body onScroll={fn} />`);
|
|
216
|
+
expect(result).toContain("createEffect");
|
|
217
|
+
expect(result).toContain("document.body");
|
|
218
|
+
expect(result).toContain("addEventListener");
|
|
219
|
+
});
|
|
220
|
+
|
|
221
|
+
test("global components return empty in SSR", () => {
|
|
222
|
+
const result = transformSSR(`<Window onResize={fn} />`);
|
|
223
|
+
expect(result).not.toContain("createEffect");
|
|
224
|
+
});
|
|
79
225
|
});
|
|
@@ -1,79 +1,78 @@
|
|
|
1
|
-
import { describe, expect, spyOn, test } from "bun:test";
|
|
2
|
-
import { transform } from "./helpers/transform.ts";
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
let
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
export let
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
});
|
|
1
|
+
import { describe, expect, spyOn, test } from "bun:test";
|
|
2
|
+
import { transform } from "./helpers/transform.ts";
|
|
3
|
+
|
|
4
|
+
describe("Dead code warnings", () => {
|
|
5
|
+
test("warns on unused $state", () => {
|
|
6
|
+
const warn = spyOn(console, "warn");
|
|
7
|
+
transform(`
|
|
8
|
+
let unused = $state(0);
|
|
9
|
+
const el = <div />;
|
|
10
|
+
`);
|
|
11
|
+
expect(warn).toHaveBeenCalledWith(
|
|
12
|
+
expect.stringContaining('unused reactive declaration: "unused"'),
|
|
13
|
+
);
|
|
14
|
+
warn.mockRestore();
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
test("warns on unused $derived", () => {
|
|
18
|
+
const warn = spyOn(console, "warn");
|
|
19
|
+
transform(`
|
|
20
|
+
let count = $state(0);
|
|
21
|
+
let doubled = $derived(count * 2);
|
|
22
|
+
const el = <div>{count}</div>;
|
|
23
|
+
`);
|
|
24
|
+
expect(warn).toHaveBeenCalledWith(
|
|
25
|
+
expect.stringContaining('unused reactive declaration: "doubled"'),
|
|
26
|
+
);
|
|
27
|
+
warn.mockRestore();
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
test("no warning when $state is used in JSX", () => {
|
|
31
|
+
const warn = spyOn(console, "warn");
|
|
32
|
+
transform(`
|
|
33
|
+
let count = $state(0);
|
|
34
|
+
const el = <div>{count}</div>;
|
|
35
|
+
`);
|
|
36
|
+
expect(warn).not.toHaveBeenCalledWith(expect.stringContaining('"count"'));
|
|
37
|
+
warn.mockRestore();
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
test("no warning when $state is used in effect", () => {
|
|
41
|
+
const warn = spyOn(console, "warn");
|
|
42
|
+
transform(`
|
|
43
|
+
let count = $state(0);
|
|
44
|
+
$effect(() => { console.log(count); });
|
|
45
|
+
`);
|
|
46
|
+
expect(warn).not.toHaveBeenCalledWith(expect.stringContaining('"count"'));
|
|
47
|
+
warn.mockRestore();
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
test("$store does not warn even if not used in same file", () => {
|
|
51
|
+
const warn = spyOn(console, "warn");
|
|
52
|
+
transform(`
|
|
53
|
+
export let count = $store(0);
|
|
54
|
+
export let user = $store({ name: "" });
|
|
55
|
+
`);
|
|
56
|
+
expect(warn).not.toHaveBeenCalledWith(
|
|
57
|
+
expect.stringContaining("unused reactive declaration"),
|
|
58
|
+
);
|
|
59
|
+
warn.mockRestore();
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
test("$store compiles to createSignal like $state", () => {
|
|
63
|
+
const code = transform(`
|
|
64
|
+
export let count = $store(0);
|
|
65
|
+
`);
|
|
66
|
+
expect(code).toContain("createSignal(0)");
|
|
67
|
+
expect(code).not.toContain("$store");
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
test("$store in JSX works like $state", () => {
|
|
71
|
+
const code = transform(`
|
|
72
|
+
export let count = $store(0);
|
|
73
|
+
const el = <div>{count}</div>;
|
|
74
|
+
`);
|
|
75
|
+
expect(code).toContain("count()");
|
|
76
|
+
expect(code).not.toContain("$store");
|
|
77
|
+
});
|
|
78
|
+
});
|