@sigil-dev/compiler 0.9.2 → 0.9.4

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 (40) hide show
  1. package/README.md +162 -162
  2. package/index.ts +4 -4
  3. package/jsr.json +8 -8
  4. package/package.json +8 -8
  5. package/src/babel/handlers/dom/derived.ts +62 -62
  6. package/src/babel/handlers/dom/effect.ts +15 -15
  7. package/src/babel/handlers/dom/globals.ts +105 -105
  8. package/src/babel/handlers/dom/state.ts +107 -104
  9. package/src/babel/handlers/ssr/derived.ts +27 -27
  10. package/src/babel/handlers/ssr/state.ts +11 -11
  11. package/src/babel/index.ts +599 -589
  12. package/src/babel/jsx/anchor-mount.ts +385 -389
  13. package/src/babel/jsx/children.ts +124 -124
  14. package/src/babel/jsx/element.ts +850 -831
  15. package/src/babel/jsx/fragment.ts +282 -282
  16. package/src/babel/jsx/index.ts +2 -2
  17. package/src/babel/jsx/keyed-list.ts +309 -278
  18. package/src/babel/jsx/ssr.ts +324 -319
  19. package/src/babel/jsx/text-node.ts +192 -192
  20. package/src/babel/jsx/utils.ts +179 -179
  21. package/src/babel/util/bind.ts +139 -139
  22. package/src/babel/util/css.ts +151 -151
  23. package/src/babel/util/dead-code.ts +28 -28
  24. package/src/babel/util/helpers.ts +46 -46
  25. package/src/babel/util/magic.ts +11 -11
  26. package/src/bun-plugin.ts +92 -90
  27. package/src/vite/index.ts +58 -58
  28. package/test/async-components.test.ts +24 -24
  29. package/test/components.test.ts +129 -129
  30. package/test/containsSignal.test.ts +288 -288
  31. package/test/fragments.test.ts +90 -90
  32. package/test/helpers/transform.ts +45 -48
  33. package/test/hydration.test.ts +147 -147
  34. package/test/jsx.test.ts +225 -197
  35. package/test/keyed-lists.test.ts +126 -88
  36. package/test/reactivity.test.ts +225 -225
  37. package/test/scoped-css.test.ts +129 -129
  38. package/test/ssr.test.ts +130 -130
  39. package/test/tree-shaking.test.ts +78 -78
  40. package/tsconfig.json +30 -30
@@ -1,288 +1,288 @@
1
- import { describe, expect, it } from "bun:test";
2
- import { parseExpression } from "@babel/parser";
3
- import type { Expression } from "@babel/types";
4
- import { containsSignal } from "../src/babel/jsx/utils";
5
-
6
- // Our containsSignal is so jank it needs a dedicated test suite.
7
-
8
- // ── helpers ──────────────────────────────────────────────────────────────────
9
-
10
- function parse(code: string): Expression {
11
- return parseExpression(code, {
12
- plugins: ["typescript", "jsx"],
13
- strictMode: false,
14
- }) as Expression;
15
- }
16
-
17
- /** Signals in scope for all tests */
18
- const SIG = new Set(["count", "name", "items", "user", "flag"]);
19
-
20
- function yes(code: string, msg?: string) {
21
- it(msg ?? `contains signal: ${code}`, () => {
22
- expect(containsSignal(parse(code), SIG)).toBe(true);
23
- });
24
- }
25
-
26
- function no(code: string, msg?: string) {
27
- it(msg ?? `no signal: ${code}`, () => {
28
- expect(containsSignal(parse(code), SIG)).toBe(false);
29
- });
30
- }
31
-
32
- // ── Identifier ───────────────────────────────────────────────────────────────
33
-
34
- describe("Identifier", () => {
35
- yes("count");
36
- yes("name");
37
- no("foo");
38
- no("bar");
39
- no("unrelated");
40
- });
41
-
42
- // ── CallExpression ───────────────────────────────────────────────────────────
43
-
44
- describe("CallExpression — callee", () => {
45
- yes("count()");
46
- yes("name()");
47
- no("foo()");
48
- no("Math.random()");
49
- });
50
-
51
- describe("CallExpression — arguments", () => {
52
- yes("foo(count())");
53
- yes("foo(bar, count())");
54
- yes("foo(1, 2, count())");
55
- yes("foo(name(), bar)");
56
- no("foo(bar, baz)");
57
- no("foo(1, 2, 3)");
58
- });
59
-
60
- describe("CallExpression — nested callee", () => {
61
- yes("count()()"); // count is in SIG, callee of outer call
62
- yes("foo(bar(count()))"); // signal buried in nested argument
63
- no("foo(bar(baz()))");
64
- no("getSignal()()"); // getSignal not in SIG — correctly false
65
- });
66
-
67
- // ── MemberExpression ─────────────────────────────────────────────────────────
68
-
69
- describe("MemberExpression", () => {
70
- yes("count().value");
71
- yes("count().nested.deep");
72
- yes("name().length");
73
- no("foo.bar");
74
- no("obj.prop");
75
- no("Math.PI");
76
- no("foo.bar.baz");
77
- });
78
-
79
- // ── OptionalMemberExpression ─────────────────────────────────────────────────
80
-
81
- describe("OptionalMemberExpression", () => {
82
- yes("count()?.value");
83
- yes("count()?.foo?.bar");
84
- yes("name()?.length");
85
- no("foo?.bar");
86
- no("obj?.prop");
87
- no("foo?.bar?.baz");
88
- });
89
-
90
- // ── OptionalCallExpression ───────────────────────────────────────────────────
91
-
92
- describe("OptionalCallExpression", () => {
93
- yes("count?.()");
94
- yes("foo?.(count())");
95
- yes("foo?.(bar, count())");
96
- no("foo?.()");
97
- no("foo?.(bar, baz)");
98
- });
99
-
100
- // ── BinaryExpression ─────────────────────────────────────────────────────────
101
-
102
- describe("BinaryExpression", () => {
103
- yes("count() + 1");
104
- yes("1 + count()");
105
- yes("count() > 0");
106
- yes("count() === name()");
107
- yes("foo + count()");
108
- no("1 + 2");
109
- no("foo + bar");
110
- no("'a' + 'b'");
111
- });
112
-
113
- // ── LogicalExpression ────────────────────────────────────────────────────────
114
-
115
- describe("LogicalExpression", () => {
116
- yes("count() && foo");
117
- yes("foo && count()");
118
- yes("count() || name()");
119
- yes("foo ?? count()");
120
- no("foo && bar");
121
- no("a || b");
122
- no("null ?? 'default'");
123
- });
124
-
125
- // ── ConditionalExpression ────────────────────────────────────────────────────
126
-
127
- describe("ConditionalExpression", () => {
128
- yes("count() ? 'a' : 'b'", "signal in test");
129
- yes("foo ? count() : 'b'", "signal in consequent");
130
- yes("foo ? 'a' : count()", "signal in alternate");
131
- yes("count() ? name() : 'b'", "signal in test and consequent");
132
- no("foo ? 'a' : 'b'");
133
- no("true ? 1 : 0");
134
- });
135
-
136
- // ── UnaryExpression ──────────────────────────────────────────────────────────
137
-
138
- describe("UnaryExpression", () => {
139
- yes("!count()");
140
- yes("-count()");
141
- yes("typeof count()");
142
- no("!foo");
143
- no("-1");
144
- no("typeof foo");
145
- });
146
-
147
- // ── TemplateLiteral ──────────────────────────────────────────────────────────
148
-
149
- describe("TemplateLiteral", () => {
150
- yes("`${count()}`");
151
- yes("`hello ${name()}`");
152
- yes("`${foo} ${count()}`");
153
- yes("`${count()} and ${name()}`");
154
- no("`hello world`");
155
- no("`${foo}`");
156
- no("`${foo} ${bar}`");
157
- });
158
-
159
- // ── ArrayExpression ──────────────────────────────────────────────────────────
160
-
161
- describe("ArrayExpression", () => {
162
- yes("[count()]");
163
- yes("[foo, count()]");
164
- yes("[1, 2, count()]");
165
- yes("[...count()]");
166
- no("[]");
167
- no("[1, 2, 3]");
168
- no("[foo, bar]");
169
- // sparse arrays have null holes
170
- no("[,,,]", "sparse array holes");
171
- });
172
-
173
- // ── ObjectExpression ─────────────────────────────────────────────────────────
174
-
175
- describe("ObjectExpression", () => {
176
- yes("({ a: count() })");
177
- yes("({ a: foo, b: count() })");
178
- yes("({ ...count() })");
179
- yes("({ [name()]: 'val' })", "computed key with signal");
180
- no("({})");
181
- no("({ a: 1 })");
182
- no("({ a: foo, b: bar })");
183
- no("({ ...foo })");
184
- });
185
-
186
- // ── SequenceExpression ───────────────────────────────────────────────────────
187
-
188
- describe("SequenceExpression", () => {
189
- yes("(foo, count())");
190
- yes("(count(), foo)");
191
- yes("(a, b, count())");
192
- no("(a, b)");
193
- no("(1, 2, 3)");
194
- });
195
-
196
- // ── AssignmentExpression ─────────────────────────────────────────────────────
197
-
198
- describe("AssignmentExpression", () => {
199
- // only RHS matters for reactivity — LHS is where you're writing to
200
- yes("x = count()");
201
- yes("x += count()");
202
- no("x = foo");
203
- no("x = 1");
204
- });
205
-
206
- // ── SpreadElement ────────────────────────────────────────────────────────────
207
-
208
- describe("SpreadElement — inside array/call", () => {
209
- yes("[...count()]");
210
- yes("foo(...count())");
211
- no("[...foo]");
212
- no("foo(...bar)");
213
- });
214
-
215
- // ── ArrowFunctionExpression ──────────────────────────────────────────────────
216
- // signals inside a function body are captured at call time, not bind time —
217
- // making the attribute reactive would re-assign the handler on every signal change
218
-
219
- describe("ArrowFunctionExpression — must NOT traverse body", () => {
220
- no("() => count()");
221
- no("(x) => count() + x");
222
- no("() => { return count(); }");
223
- no("(e) => { count(); }");
224
- });
225
-
226
- // ── FunctionExpression ───────────────────────────────────────────────────────
227
-
228
- describe("FunctionExpression — must NOT traverse body", () => {
229
- no("function() { return count(); }");
230
- no("function handler() { count(); }");
231
- });
232
-
233
- // ── Literals (always false) ──────────────────────────────────────────────────
234
-
235
- describe("Literals", () => {
236
- no("1");
237
- no("'hello'");
238
- no("true");
239
- no("null");
240
- no("/regex/");
241
- });
242
-
243
- // ── Deep nesting ─────────────────────────────────────────────────────────────
244
-
245
- describe("Deep nesting", () => {
246
- yes("foo(bar(baz(count())))");
247
- yes("a && b ? c + count() : d");
248
- yes("`prefix_${foo ? count() : bar}`");
249
- yes("[{ a: count() }]");
250
- yes("({ arr: [1, count()] })");
251
- yes("foo?.bar?.(count())");
252
- no("foo(bar(baz(qux())))");
253
- no("a && b ? c + d : e");
254
- });
255
-
256
- // ── Multiple signals ─────────────────────────────────────────────────────────
257
-
258
- describe("Multiple signals", () => {
259
- yes("count() + name()");
260
- yes("[count(), name(), items()]");
261
- yes("user()?.profile ?? name()");
262
- yes("flag() ? count() : name()");
263
- });
264
-
265
- // ── Non-signal identifiers that look similar ─────────────────────────────────
266
-
267
- describe("Near-miss identifiers", () => {
268
- no("counter"); // not "count"
269
- no("counted");
270
- no("names"); // not "name"
271
- no("userCount");
272
- no("flagged");
273
- no("itemsList");
274
- });
275
-
276
- // ── null/undefined safety ────────────────────────────────────────────────────
277
-
278
- describe("null/undefined guard", () => {
279
- it("returns false for null input", () => {
280
- expect(containsSignal(null as any, SIG)).toBe(false);
281
- });
282
- it("returns false for undefined input", () => {
283
- expect(containsSignal(undefined as any, SIG)).toBe(false);
284
- });
285
- it("empty signal set never matches", () => {
286
- expect(containsSignal(parse("count()"), new Set())).toBe(false);
287
- });
288
- });
1
+ import { describe, expect, it } from "bun:test";
2
+ import { parseExpression } from "@babel/parser";
3
+ import type { Expression } from "@babel/types";
4
+ import { containsSignal } from "../src/babel/jsx/utils";
5
+
6
+ // Our containsSignal is so jank it needs a dedicated test suite.
7
+
8
+ // ── helpers ──────────────────────────────────────────────────────────────────
9
+
10
+ function parse(code: string): Expression {
11
+ return parseExpression(code, {
12
+ plugins: ["typescript", "jsx"],
13
+ strictMode: false,
14
+ }) as Expression;
15
+ }
16
+
17
+ /** Signals in scope for all tests */
18
+ const SIG = new Set(["count", "name", "items", "user", "flag"]);
19
+
20
+ function yes(code: string, msg?: string) {
21
+ it(msg ?? `contains signal: ${code}`, () => {
22
+ expect(containsSignal(parse(code), SIG)).toBe(true);
23
+ });
24
+ }
25
+
26
+ function no(code: string, msg?: string) {
27
+ it(msg ?? `no signal: ${code}`, () => {
28
+ expect(containsSignal(parse(code), SIG)).toBe(false);
29
+ });
30
+ }
31
+
32
+ // ── Identifier ───────────────────────────────────────────────────────────────
33
+
34
+ describe("Identifier", () => {
35
+ yes("count");
36
+ yes("name");
37
+ no("foo");
38
+ no("bar");
39
+ no("unrelated");
40
+ });
41
+
42
+ // ── CallExpression ───────────────────────────────────────────────────────────
43
+
44
+ describe("CallExpression — callee", () => {
45
+ yes("count()");
46
+ yes("name()");
47
+ no("foo()");
48
+ no("Math.random()");
49
+ });
50
+
51
+ describe("CallExpression — arguments", () => {
52
+ yes("foo(count())");
53
+ yes("foo(bar, count())");
54
+ yes("foo(1, 2, count())");
55
+ yes("foo(name(), bar)");
56
+ no("foo(bar, baz)");
57
+ no("foo(1, 2, 3)");
58
+ });
59
+
60
+ describe("CallExpression — nested callee", () => {
61
+ yes("count()()"); // count is in SIG, callee of outer call
62
+ yes("foo(bar(count()))"); // signal buried in nested argument
63
+ no("foo(bar(baz()))");
64
+ no("getSignal()()"); // getSignal not in SIG — correctly false
65
+ });
66
+
67
+ // ── MemberExpression ─────────────────────────────────────────────────────────
68
+
69
+ describe("MemberExpression", () => {
70
+ yes("count().value");
71
+ yes("count().nested.deep");
72
+ yes("name().length");
73
+ no("foo.bar");
74
+ no("obj.prop");
75
+ no("Math.PI");
76
+ no("foo.bar.baz");
77
+ });
78
+
79
+ // ── OptionalMemberExpression ─────────────────────────────────────────────────
80
+
81
+ describe("OptionalMemberExpression", () => {
82
+ yes("count()?.value");
83
+ yes("count()?.foo?.bar");
84
+ yes("name()?.length");
85
+ no("foo?.bar");
86
+ no("obj?.prop");
87
+ no("foo?.bar?.baz");
88
+ });
89
+
90
+ // ── OptionalCallExpression ───────────────────────────────────────────────────
91
+
92
+ describe("OptionalCallExpression", () => {
93
+ yes("count?.()");
94
+ yes("foo?.(count())");
95
+ yes("foo?.(bar, count())");
96
+ no("foo?.()");
97
+ no("foo?.(bar, baz)");
98
+ });
99
+
100
+ // ── BinaryExpression ─────────────────────────────────────────────────────────
101
+
102
+ describe("BinaryExpression", () => {
103
+ yes("count() + 1");
104
+ yes("1 + count()");
105
+ yes("count() > 0");
106
+ yes("count() === name()");
107
+ yes("foo + count()");
108
+ no("1 + 2");
109
+ no("foo + bar");
110
+ no("'a' + 'b'");
111
+ });
112
+
113
+ // ── LogicalExpression ────────────────────────────────────────────────────────
114
+
115
+ describe("LogicalExpression", () => {
116
+ yes("count() && foo");
117
+ yes("foo && count()");
118
+ yes("count() || name()");
119
+ yes("foo ?? count()");
120
+ no("foo && bar");
121
+ no("a || b");
122
+ no("null ?? 'default'");
123
+ });
124
+
125
+ // ── ConditionalExpression ────────────────────────────────────────────────────
126
+
127
+ describe("ConditionalExpression", () => {
128
+ yes("count() ? 'a' : 'b'", "signal in test");
129
+ yes("foo ? count() : 'b'", "signal in consequent");
130
+ yes("foo ? 'a' : count()", "signal in alternate");
131
+ yes("count() ? name() : 'b'", "signal in test and consequent");
132
+ no("foo ? 'a' : 'b'");
133
+ no("true ? 1 : 0");
134
+ });
135
+
136
+ // ── UnaryExpression ──────────────────────────────────────────────────────────
137
+
138
+ describe("UnaryExpression", () => {
139
+ yes("!count()");
140
+ yes("-count()");
141
+ yes("typeof count()");
142
+ no("!foo");
143
+ no("-1");
144
+ no("typeof foo");
145
+ });
146
+
147
+ // ── TemplateLiteral ──────────────────────────────────────────────────────────
148
+
149
+ describe("TemplateLiteral", () => {
150
+ yes("`${count()}`");
151
+ yes("`hello ${name()}`");
152
+ yes("`${foo} ${count()}`");
153
+ yes("`${count()} and ${name()}`");
154
+ no("`hello world`");
155
+ no("`${foo}`");
156
+ no("`${foo} ${bar}`");
157
+ });
158
+
159
+ // ── ArrayExpression ──────────────────────────────────────────────────────────
160
+
161
+ describe("ArrayExpression", () => {
162
+ yes("[count()]");
163
+ yes("[foo, count()]");
164
+ yes("[1, 2, count()]");
165
+ yes("[...count()]");
166
+ no("[]");
167
+ no("[1, 2, 3]");
168
+ no("[foo, bar]");
169
+ // sparse arrays have null holes
170
+ no("[,,,]", "sparse array holes");
171
+ });
172
+
173
+ // ── ObjectExpression ─────────────────────────────────────────────────────────
174
+
175
+ describe("ObjectExpression", () => {
176
+ yes("({ a: count() })");
177
+ yes("({ a: foo, b: count() })");
178
+ yes("({ ...count() })");
179
+ yes("({ [name()]: 'val' })", "computed key with signal");
180
+ no("({})");
181
+ no("({ a: 1 })");
182
+ no("({ a: foo, b: bar })");
183
+ no("({ ...foo })");
184
+ });
185
+
186
+ // ── SequenceExpression ───────────────────────────────────────────────────────
187
+
188
+ describe("SequenceExpression", () => {
189
+ yes("(foo, count())");
190
+ yes("(count(), foo)");
191
+ yes("(a, b, count())");
192
+ no("(a, b)");
193
+ no("(1, 2, 3)");
194
+ });
195
+
196
+ // ── AssignmentExpression ─────────────────────────────────────────────────────
197
+
198
+ describe("AssignmentExpression", () => {
199
+ // only RHS matters for reactivity — LHS is where you're writing to
200
+ yes("x = count()");
201
+ yes("x += count()");
202
+ no("x = foo");
203
+ no("x = 1");
204
+ });
205
+
206
+ // ── SpreadElement ────────────────────────────────────────────────────────────
207
+
208
+ describe("SpreadElement — inside array/call", () => {
209
+ yes("[...count()]");
210
+ yes("foo(...count())");
211
+ no("[...foo]");
212
+ no("foo(...bar)");
213
+ });
214
+
215
+ // ── ArrowFunctionExpression ──────────────────────────────────────────────────
216
+ // signals inside a function body are captured at call time, not bind time —
217
+ // making the attribute reactive would re-assign the handler on every signal change
218
+
219
+ describe("ArrowFunctionExpression — must NOT traverse body", () => {
220
+ no("() => count()");
221
+ no("(x) => count() + x");
222
+ no("() => { return count(); }");
223
+ no("(e) => { count(); }");
224
+ });
225
+
226
+ // ── FunctionExpression ───────────────────────────────────────────────────────
227
+
228
+ describe("FunctionExpression — must NOT traverse body", () => {
229
+ no("function() { return count(); }");
230
+ no("function handler() { count(); }");
231
+ });
232
+
233
+ // ── Literals (always false) ──────────────────────────────────────────────────
234
+
235
+ describe("Literals", () => {
236
+ no("1");
237
+ no("'hello'");
238
+ no("true");
239
+ no("null");
240
+ no("/regex/");
241
+ });
242
+
243
+ // ── Deep nesting ─────────────────────────────────────────────────────────────
244
+
245
+ describe("Deep nesting", () => {
246
+ yes("foo(bar(baz(count())))");
247
+ yes("a && b ? c + count() : d");
248
+ yes("`prefix_${foo ? count() : bar}`");
249
+ yes("[{ a: count() }]");
250
+ yes("({ arr: [1, count()] })");
251
+ yes("foo?.bar?.(count())");
252
+ no("foo(bar(baz(qux())))");
253
+ no("a && b ? c + d : e");
254
+ });
255
+
256
+ // ── Multiple signals ─────────────────────────────────────────────────────────
257
+
258
+ describe("Multiple signals", () => {
259
+ yes("count() + name()");
260
+ yes("[count(), name(), items()]");
261
+ yes("user()?.profile ?? name()");
262
+ yes("flag() ? count() : name()");
263
+ });
264
+
265
+ // ── Non-signal identifiers that look similar ─────────────────────────────────
266
+
267
+ describe("Near-miss identifiers", () => {
268
+ no("counter"); // not "count"
269
+ no("counted");
270
+ no("names"); // not "name"
271
+ no("userCount");
272
+ no("flagged");
273
+ no("itemsList");
274
+ });
275
+
276
+ // ── null/undefined safety ────────────────────────────────────────────────────
277
+
278
+ describe("null/undefined guard", () => {
279
+ it("returns false for null input", () => {
280
+ expect(containsSignal(null as any, SIG)).toBe(false);
281
+ });
282
+ it("returns false for undefined input", () => {
283
+ expect(containsSignal(undefined as any, SIG)).toBe(false);
284
+ });
285
+ it("empty signal set never matches", () => {
286
+ expect(containsSignal(parse("count()"), new Set())).toBe(false);
287
+ });
288
+ });