@sigil-dev/compiler 0.7.4 → 0.7.6

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.
@@ -1,582 +1,803 @@
1
- import { types as t } from "@babel/core";
2
- import {generate} from "@babel/generator"
3
- import { buildBind, buildProxyBind } from "../util/bind";
4
- import { buildAnchorMount } from "./anchor-mount";
5
- import { collectChildren } from "./children";
6
- import { processFragment } from "./fragment";
7
- import { buildKeyedList, findKeyedMapExpr } from "./keyed-list";
8
- import { buildTextNode } from "./text-node";
9
- import {
10
- ATTR_MAP,
11
- buildHydrationScope,
12
- containsSignal,
13
- getCreateElement,
14
- isPrimitive,
15
- } from "./utils";
16
-
17
- /**
18
- * Add classList.add(hash) statement for scoped CSS.
19
- */
20
- function addScopedClass(
21
- varName: string,
22
- hash: string,
23
- statements: t.Statement[],
24
- ): void {
25
- statements.push(
26
- t.expressionStatement(
27
- t.callExpression(
28
- t.memberExpression(
29
- t.memberExpression(t.identifier(varName), t.identifier("classList")),
30
- t.identifier("add"),
31
- ),
32
- [t.stringLiteral(hash)],
33
- ),
34
- ),
35
- );
36
- }
37
-
38
- export function processElement(
39
- node: t.JSXElement,
40
- statements: t.Statement[],
41
- genId: () => string,
42
- signals: Set<string>,
43
- hash?: string,
44
- hydrate?: boolean,
45
- nodesVar?: string,
46
- parentVar?: string,
47
- ): string {
48
- const varName = genId();
49
- const tag = (node.openingElement.name as t.JSXIdentifier).name;
50
- const isComponent = /^[A-Z]/.test(tag);
51
-
52
- if (isComponent) {
53
- // Component: call the function with a props object
54
- const propsObj = t.objectExpression([]);
55
-
56
- // Add attributes to props (including spread)
57
- for (const attr of node.openingElement.attributes) {
58
- if (t.isJSXSpreadAttribute(attr)) {
59
- // {…obj} — spread into props
60
- propsObj.properties.push(t.spreadElement(attr.argument));
61
- continue;
62
- }
63
- if (!t.isJSXAttribute(attr)) continue;
64
- const attrName = (attr.name as t.JSXIdentifier).name;
65
-
66
- if (t.isStringLiteral(attr.value)) {
67
- propsObj.properties.push(
68
- t.objectProperty(t.identifier(attrName), attr.value),
69
- );
70
- } else if (t.isJSXExpressionContainer(attr.value)) {
71
- const expr = attr.value.expression as t.Expression;
72
- propsObj.properties.push(
73
- t.objectProperty(t.identifier(attrName), expr),
74
- );
75
- }
76
- }
77
-
78
- // Add children to props
79
- const childrenExpr = collectChildren(
80
- node,
81
- statements,
82
- genId,
83
- signals,
84
- hash,
85
- hydrate,
86
- nodesVar,
87
- );
88
- if (childrenExpr) {
89
- propsObj.properties.push(
90
- t.objectProperty(t.identifier("children"), childrenExpr),
91
- );
92
- }
93
-
94
- // const _el0 = TagName({ ...props })
95
- statements.push(
96
- t.variableDeclaration("const", [
97
- t.variableDeclarator(
98
- t.identifier(varName),
99
- t.callExpression(t.identifier(tag), [propsObj]),
100
- ),
101
- ]),
102
- );
103
-
104
- return varName;
105
- }
106
-
107
- // Native element: claim (hydrate) or create (dom)
108
- const currentNodesVar = nodesVar ?? "__nodes";
109
- statements.push(
110
- t.variableDeclaration("const", [
111
- t.variableDeclarator(
112
- t.identifier(varName),
113
- getCreateElement(
114
- tag,
115
- !!hydrate,
116
- currentNodesVar,
117
- hydrate ? parentVar : undefined,
118
- ),
119
- ),
120
- ]),
121
- );
122
-
123
- // In hydrate mode, scope to this element's children for descendants
124
- let childNodesVar: string | undefined;
125
- if (hydrate) {
126
- childNodesVar = genId();
127
- buildHydrationScope(varName, statements, childNodesVar);
128
- }
129
-
130
- // attributes
131
- const seenAttrs = new Set<string>();
132
- for (const attr of node.openingElement.attributes) {
133
- if (t.isJSXSpreadAttribute(attr)) {
134
- // {…obj} — spread onto native element
135
- statements.push(
136
- t.expressionStatement(
137
- t.callExpression(t.identifier("Object.assign"), [
138
- t.identifier(varName),
139
- attr.argument as t.Expression,
140
- ]),
141
- ),
142
- );
143
- continue;
144
- }
145
- if (!t.isJSXAttribute(attr)) continue;
146
- const attrName = (attr.name as t.JSXIdentifier).name;
147
- const realAttr = ATTR_MAP[attrName] ?? attrName;
148
-
149
- if (seenAttrs.has(realAttr)) {
150
- throw new TypeError(
151
- `Duplicate attribute "${realAttr}" (via "${attrName}"). Use class or className, not both.`,
152
- );
153
- }
154
- seenAttrs.add(realAttr);
155
- if (attrName === "use") {
156
- // use={directive} or use={[directive, params]}
157
- const expr = (attr.value as t.JSXExpressionContainer)
158
- .expression as t.Expression;
159
- statements.push(
160
- t.expressionStatement(
161
- t.callExpression(t.identifier("applyDirective"), [
162
- t.identifier(varName),
163
- expr,
164
- ]),
165
- ),
166
- );
167
- } else if (/^on[A-Z]/.test(attrName)) {
168
- // Standard JSX: onClick, onSubmit, onMouseenter, etc.
169
- // Convert camelCase to lowercase: onClick -> click, onMouseenter -> mouseenter
170
- const event = attrName.slice(2).toLowerCase();
171
- const handler = (attr.value as t.JSXExpressionContainer)
172
- .expression as t.Expression;
173
- statements.push(
174
- t.expressionStatement(
175
- t.callExpression(
176
- t.memberExpression(
177
- t.identifier(varName),
178
- t.identifier("addEventListener"),
179
- ),
180
- [t.stringLiteral(event), handler],
181
- ),
182
- ),
183
- );
184
- } else if (attrName.startsWith("bind")) {
185
- const signal = (attr.value as t.JSXExpressionContainer)
186
- .expression as t.Expression;
187
-
188
- const isTopLevelSignal = t.isCallExpression(signal) &&
189
- t.isIdentifier((signal as t.CallExpression).callee) &&
190
- signals.has(((signal as t.CallExpression).callee as t.Identifier).name);
191
-
192
- const isMemberOfSignal = t.isMemberExpression(signal) &&
193
- t.isCallExpression((signal as t.MemberExpression).object) &&
194
- t.isIdentifier(((signal as t.MemberExpression).object as t.CallExpression).callee) &&
195
- signals.has((((signal as t.MemberExpression).object as t.CallExpression).callee as t.Identifier).name);
196
-
197
- if (isTopLevelSignal) {
198
- buildBind(varName, attrName, attr, statements);
199
- } else if (isMemberOfSignal) {
200
- buildProxyBind(varName, attrName, attr, statements, signal);
201
- } else {
202
- console.warn(
203
- `[sigil] ${attrName}={...} requires a $state signal or $state object property. ` +
204
- `Got: ${generate(signal).code}. ` +
205
- `Hint: change to a $state() call.`
206
- );
207
- }
208
- } else if (t.isStringLiteral(attr.value)) {
209
- statements.push(
210
- t.expressionStatement(
211
- t.assignmentExpression(
212
- "=",
213
- t.memberExpression(t.identifier(varName), t.identifier(realAttr)),
214
- attr.value,
215
- ),
216
- ),
217
- );
218
- } else if (t.isJSXExpressionContainer(attr.value)) {
219
- const expr = attr.value.expression as t.Expression;
220
- // style={{ color: "red", fontSize: val }} — per-property assignment
221
- if (attrName === "style" && t.isObjectExpression(expr)) {
222
- for (const prop of expr.properties) {
223
- if (!t.isObjectProperty(prop)) continue;
224
- const key = t.isIdentifier(prop.key)
225
- ? prop.key.name
226
- : t.isStringLiteral(prop.key)
227
- ? prop.key.value
228
- : null;
229
- if (!key) continue;
230
- const val = prop.value as t.Expression;
231
- const styleProp = t.memberExpression(
232
- t.memberExpression(t.identifier(varName), t.identifier("style")),
233
- t.identifier(key),
234
- );
235
- if (containsSignal(val, signals)) {
236
- statements.push(
237
- t.expressionStatement(
238
- t.callExpression(t.identifier("createEffect"), [
239
- t.arrowFunctionExpression(
240
- [],
241
- t.blockStatement([
242
- t.expressionStatement(
243
- t.assignmentExpression("=", styleProp, val),
244
- ),
245
- ]),
246
- ),
247
- ]),
248
- ),
249
- );
250
- } else {
251
- statements.push(
252
- t.expressionStatement(
253
- t.assignmentExpression("=", styleProp, val),
254
- ),
255
- );
256
- }
257
- }
258
- // done with this attribute
259
- } else if (containsSignal(expr, signals)) {
260
- // Dynamic class with scoped CSS: use classList.value to preserve hash
261
- if (attrName === "class" && hash) {
262
- statements.push(
263
- t.expressionStatement(
264
- t.callExpression(t.identifier("createEffect"), [
265
- t.arrowFunctionExpression(
266
- [],
267
- t.blockStatement([
268
- t.expressionStatement(
269
- t.assignmentExpression(
270
- "=",
271
- t.memberExpression(
272
- t.memberExpression(
273
- t.identifier(varName),
274
- t.identifier("classList"),
275
- ),
276
- t.identifier("value"),
277
- ),
278
- expr,
279
- ),
280
- ),
281
- t.expressionStatement(
282
- t.callExpression(
283
- t.memberExpression(
284
- t.memberExpression(
285
- t.identifier(varName),
286
- t.identifier("classList"),
287
- ),
288
- t.identifier("add"),
289
- ),
290
- [t.stringLiteral(hash)],
291
- ),
292
- ),
293
- ]),
294
- ),
295
- ]),
296
- ),
297
- );
298
- } else {
299
- // createEffect(() => varName.realAttr = expr)
300
- statements.push(
301
- t.expressionStatement(
302
- t.callExpression(t.identifier("createEffect"), [
303
- t.arrowFunctionExpression(
304
- [],
305
- t.blockStatement([
306
- t.expressionStatement(
307
- t.assignmentExpression(
308
- "=",
309
- t.memberExpression(
310
- t.identifier(varName),
311
- t.identifier(realAttr),
312
- ),
313
- expr,
314
- ),
315
- ),
316
- ]),
317
- ),
318
- ]),
319
- ),
320
- );
321
- }
322
- } else {
323
- // varName.realAttr = expr
324
- statements.push(
325
- t.expressionStatement(
326
- t.assignmentExpression(
327
- "=",
328
- t.memberExpression(t.identifier(varName), t.identifier(realAttr)),
329
- expr,
330
- ),
331
- ),
332
- );
333
- }
334
- }
335
- }
336
-
337
- // Add scoped CSS hash to classList (if hash is present)
338
- if (hash) {
339
- addScopedClass(varName, hash, statements);
340
- }
341
-
342
- // children
343
- for (const child of node.children) {
344
- if (t.isJSXElement(child)) {
345
- const childVar = processElement(
346
- child,
347
- statements,
348
- genId,
349
- signals,
350
- hash,
351
- hydrate,
352
- childNodesVar,
353
- hydrate ? varName : undefined,
354
- );
355
- if (!hydrate) {
356
- statements.push(
357
- t.expressionStatement(
358
- t.callExpression(
359
- t.memberExpression(t.identifier(varName), t.identifier("append")),
360
- [t.identifier(childVar)],
361
- ),
362
- ),
363
- );
364
- }
365
- } else if (t.isJSXFragment(child)) {
366
- const childVar = processFragment(
367
- child,
368
- statements,
369
- genId,
370
- signals,
371
- hash,
372
- hydrate,
373
- childNodesVar,
374
- hydrate ? varName : undefined,
375
- );
376
- if (!hydrate) {
377
- statements.push(
378
- t.expressionStatement(
379
- t.callExpression(
380
- t.memberExpression(t.identifier(varName), t.identifier("append")),
381
- [t.identifier(childVar)],
382
- ),
383
- ),
384
- );
385
- }
386
- } else if (t.isJSXText(child)) {
387
- const text = child.value;
388
- if (!text.trim()) continue;
389
- if (hydrate) {
390
- // SSR hydration: text already in DOM from SSR, do nothing.
391
- // SPA navigation: childNodesVar pool is empty, create text node.
392
- const poolLen = t.memberExpression(
393
- t.identifier(childNodesVar!),
394
- t.identifier("length"),
395
- );
396
- statements.push(
397
- t.ifStatement(
398
- t.binaryExpression("===", poolLen, t.numericLiteral(0)),
399
- t.expressionStatement(
400
- t.callExpression(
401
- t.memberExpression(
402
- t.identifier(varName),
403
- t.identifier("append"),
404
- ),
405
- [
406
- t.callExpression(
407
- t.memberExpression(
408
- t.identifier("document"),
409
- t.identifier("createTextNode"),
410
- ),
411
- [t.stringLiteral(text.replace(/\s*\n\s*/g, " "))],
412
- ),
413
- ],
414
- ),
415
- ),
416
- ),
417
- );
418
- } else {
419
- statements.push(
420
- t.expressionStatement(
421
- t.callExpression(
422
- t.memberExpression(t.identifier(varName), t.identifier("append")),
423
- [
424
- t.callExpression(
425
- t.memberExpression(
426
- t.identifier("document"),
427
- t.identifier("createTextNode"),
428
- ),
429
- [t.stringLiteral(text)],
430
- ),
431
- ],
432
- ),
433
- ),
434
- );
435
- }
436
- } else if (t.isJSXExpressionContainer(child)) {
437
- if (t.isJSXEmptyExpression(child.expression)) continue;
438
- const expr = child.expression as t.Expression;
439
- if (t.isJSXElement(expr)) {
440
- const childVar = processElement(
441
- expr,
442
- statements,
443
- genId,
444
- signals,
445
- hash,
446
- hydrate,
447
- childNodesVar,
448
- hydrate ? varName : undefined,
449
- );
450
- if (!hydrate) {
451
- statements.push(
452
- t.expressionStatement(
453
- t.callExpression(
454
- t.memberExpression(
455
- t.identifier(varName),
456
- t.identifier("append"),
457
- ),
458
- [t.identifier(childVar)],
459
- ),
460
- ),
461
- );
462
- }
463
- } else if (t.isJSXFragment(expr)) {
464
- const childVar = processFragment(
465
- expr,
466
- statements,
467
- genId,
468
- signals,
469
- hash,
470
- hydrate,
471
- childNodesVar,
472
- hydrate ? varName : undefined,
473
- );
474
- if (!hydrate) {
475
- statements.push(
476
- t.expressionStatement(
477
- t.callExpression(
478
- t.memberExpression(
479
- t.identifier(varName),
480
- t.identifier("append"),
481
- ),
482
- [t.identifier(childVar)],
483
- ),
484
- ),
485
- );
486
- }
487
- } else if (containsSignal(expr, signals)) {
488
- if (isPrimitive(expr)) {
489
- // fast path: text node + textContent
490
- buildTextNode(
491
- varName,
492
- expr,
493
- statements,
494
- genId,
495
- hydrate,
496
- childNodesVar,
497
- hydrate ? varName : undefined,
498
- );
499
- } else {
500
- // Check for keyed list pattern
501
- const keyed = findKeyedMapExpr(expr, signals);
502
- if (keyed) {
503
- buildKeyedList(
504
- varName,
505
- keyed,
506
- statements,
507
- genId,
508
- signals,
509
- processElement,
510
- hash,
511
- hydrate,
512
- childNodesVar,
513
- hydrate ? varName : undefined,
514
- );
515
- } else {
516
- buildAnchorMount(
517
- varName,
518
- expr,
519
- statements,
520
- genId,
521
- hydrate,
522
- childNodesVar,
523
- hydrate ? varName : undefined,
524
- );
525
- }
526
- }
527
- } else {
528
- // non-reactive: set once
529
- if (hydrate) {
530
- const claimCommentArgs: t.Expression[] = [
531
- t.identifier(childNodesVar!),
532
- t.stringLiteral("g"),
533
- ];
534
- if (varName) claimCommentArgs.push(t.identifier(varName));
535
- const poolLen = t.memberExpression(
536
- t.identifier(childNodesVar!),
537
- t.identifier("length"),
538
- );
539
- // Pool has elements (initial load): SSR content already in DOM, just consume delimiters
540
- const claimG = t.expressionStatement(
541
- t.callExpression(t.identifier("claimComment"), claimCommentArgs),
542
- );
543
- const claimSlashG = (() => {
544
- const args: t.Expression[] = [
545
- t.identifier(childNodesVar!),
546
- t.stringLiteral("/g"),
547
- ];
548
- if (varName) args.push(t.identifier(varName));
549
- return t.expressionStatement(
550
- t.callExpression(t.identifier("claimComment"), args),
551
- );
552
- })();
553
- // Pool empty (SPA nav): create and insert fresh elements
554
- const insertCall = t.expressionStatement(
555
- t.callExpression(t.identifier("insert"), [
556
- t.identifier(varName),
557
- expr,
558
- ]),
559
- );
560
- statements.push(
561
- t.ifStatement(
562
- t.binaryExpression(">", poolLen, t.numericLiteral(0)),
563
- t.blockStatement([claimG, claimSlashG]),
564
- t.blockStatement([insertCall]),
565
- ),
566
- );
567
- } else {
568
- statements.push(
569
- t.expressionStatement(
570
- t.callExpression(t.identifier("insert"), [
571
- t.identifier(varName),
572
- expr,
573
- ]),
574
- ),
575
- );
576
- }
577
- }
578
- }
579
- }
580
-
581
- return varName;
582
- }
1
+ import { types as t } from "@babel/core";
2
+ import {generate} from "@babel/generator"
3
+ import { buildBind, buildProxyBind } from "../util/bind";
4
+ import { handleGlobalComponent, isGlobalComponent } from "../handlers/dom/globals";
5
+ import { buildAnchorMount } from "./anchor-mount";
6
+ import { collectChildren } from "./children";
7
+ import { processFragment } from "./fragment";
8
+ import { buildKeyedList, findKeyedMapExpr } from "./keyed-list";
9
+ import { buildTextNode } from "./text-node";
10
+ import {
11
+ ATTR_MAP,
12
+ buildHydrationScope,
13
+ containsSignal,
14
+ getCreateElement,
15
+ isPrimitive,
16
+ } from "./utils";
17
+
18
+ /**
19
+ * Build a property-access expression for a JSX attribute name.
20
+ * Dashed names (data-*, aria-*) require computed access (e.g. el["data-id"])
21
+ * because t.identifier() rejects names with dashes.
22
+ */
23
+ function attrAccess(varName: string, attrName: string): t.MemberExpression {
24
+ if (/^[A-Za-z_$][A-Za-z0-9_$]*$/.test(attrName)) {
25
+ return t.memberExpression(t.identifier(varName), t.identifier(attrName));
26
+ }
27
+ return t.memberExpression(
28
+ t.identifier(varName),
29
+ t.stringLiteral(attrName),
30
+ true,
31
+ );
32
+ }
33
+
34
+ /**
35
+ * Build an object property key (identifier for valid names, string literal for dashed).
36
+ */
37
+ function attrKey(attrName: string): t.Identifier | t.StringLiteral {
38
+ if (/^[A-Za-z_$][A-Za-z0-9_$]*$/.test(attrName)) {
39
+ return t.identifier(attrName);
40
+ }
41
+ return t.stringLiteral(attrName);
42
+ }
43
+
44
+ /**
45
+ * Add classList.add(hash) statement for scoped CSS.
46
+ */
47
+ function addScopedClass(
48
+ varName: string,
49
+ hash: string,
50
+ statements: t.Statement[],
51
+ ): void {
52
+ statements.push(
53
+ t.expressionStatement(
54
+ t.callExpression(
55
+ t.memberExpression(
56
+ t.memberExpression(t.identifier(varName), t.identifier("classList")),
57
+ t.identifier("add"),
58
+ ),
59
+ [t.stringLiteral(hash)],
60
+ ),
61
+ ),
62
+ );
63
+ }
64
+
65
+ export function processElement(
66
+ node: t.JSXElement,
67
+ statements: t.Statement[],
68
+ genId: () => string,
69
+ signals: Set<string>,
70
+ hash?: string,
71
+ hydrate?: boolean,
72
+ nodesVar?: string,
73
+ parentVar?: string,
74
+ ): string {
75
+ const varName = genId();
76
+ const tag = (node.openingElement.name as t.JSXIdentifier).name;
77
+ const isComponent = /^[A-Z]/.test(tag);
78
+
79
+ if (isComponent) {
80
+ // Global components: Window, Document, Body
81
+ if (isGlobalComponent(tag)) {
82
+ return handleGlobalComponent(tag, node, statements, genId);
83
+ }
84
+
85
+ // Component: call the function with a props object
86
+ const propsObj = t.objectExpression([]);
87
+
88
+ // Add attributes to props (including spread)
89
+ for (const attr of node.openingElement.attributes) {
90
+ if (t.isJSXSpreadAttribute(attr)) {
91
+ // {…obj} — spread into props
92
+ propsObj.properties.push(t.spreadElement(attr.argument));
93
+ continue;
94
+ }
95
+ if (!t.isJSXAttribute(attr)) continue;
96
+ const attrName = (attr.name as t.JSXIdentifier).name;
97
+
98
+ if (t.isStringLiteral(attr.value)) {
99
+ propsObj.properties.push(
100
+ t.objectProperty(attrKey(attrName), attr.value),
101
+ );
102
+ } else if (t.isJSXExpressionContainer(attr.value)) {
103
+ const expr = attr.value.expression as t.Expression;
104
+ propsObj.properties.push(
105
+ t.objectProperty(attrKey(attrName), expr),
106
+ );
107
+ }
108
+ }
109
+
110
+ // Add children to props
111
+ const childrenExpr = collectChildren(
112
+ node,
113
+ statements,
114
+ genId,
115
+ signals,
116
+ hash,
117
+ hydrate,
118
+ nodesVar,
119
+ );
120
+ if (childrenExpr) {
121
+ propsObj.properties.push(
122
+ t.objectProperty(t.identifier("children"), childrenExpr),
123
+ );
124
+ }
125
+
126
+ const componentCall = t.callExpression(t.identifier(tag), [propsObj]);
127
+
128
+ if (hydrate && nodesVar) {
129
+ // Push the parent's child pool before calling the component so that
130
+ // getHydrationNodes() inside the component returns the right scope.
131
+ // In SSR hydration the component claims its root node from that pool
132
+ // (it's already in the DOM). In empty-pool / SPA mode the component
133
+ // creates a fresh node, so we explicitly insert it when the pool is empty.
134
+ statements.push(
135
+ t.expressionStatement(
136
+ t.callExpression(t.identifier("pushHydrationNodes"), [
137
+ t.identifier(nodesVar),
138
+ ]),
139
+ ),
140
+ );
141
+ statements.push(
142
+ t.variableDeclaration("const", [
143
+ t.variableDeclarator(t.identifier(varName), componentCall),
144
+ ]),
145
+ );
146
+ statements.push(
147
+ t.expressionStatement(
148
+ t.callExpression(t.identifier("popHydrationNodes"), []),
149
+ ),
150
+ );
151
+ if (parentVar) {
152
+ // Insert only when the pool was empty (fresh DOM); if the pool had
153
+ // nodes the component already claimed its SSR element (already in DOM).
154
+ statements.push(
155
+ t.ifStatement(
156
+ t.binaryExpression(
157
+ "===",
158
+ t.memberExpression(
159
+ t.identifier(nodesVar),
160
+ t.identifier("length"),
161
+ ),
162
+ t.numericLiteral(0),
163
+ ),
164
+ t.expressionStatement(
165
+ t.callExpression(t.identifier("insert"), [
166
+ t.identifier(parentVar),
167
+ t.identifier(varName),
168
+ ]),
169
+ ),
170
+ ),
171
+ );
172
+ }
173
+ } else {
174
+ statements.push(
175
+ t.variableDeclaration("const", [
176
+ t.variableDeclarator(t.identifier(varName), componentCall),
177
+ ]),
178
+ );
179
+ }
180
+
181
+ return varName;
182
+ }
183
+
184
+ // Native element: claim (hydrate) or create (dom)
185
+ const currentNodesVar = nodesVar ?? "__nodes";
186
+ statements.push(
187
+ t.variableDeclaration("const", [
188
+ t.variableDeclarator(
189
+ t.identifier(varName),
190
+ getCreateElement(
191
+ tag,
192
+ !!hydrate,
193
+ currentNodesVar,
194
+ hydrate ? parentVar : undefined,
195
+ ),
196
+ ),
197
+ ]),
198
+ );
199
+
200
+ // In hydrate mode, scope to this element's children for descendants
201
+ let childNodesVar: string | undefined;
202
+ if (hydrate) {
203
+ childNodesVar = genId();
204
+ buildHydrationScope(varName, statements, childNodesVar);
205
+ }
206
+
207
+ // attributes
208
+ const seenAttrs = new Set<string>();
209
+ for (const attr of node.openingElement.attributes) {
210
+ if (t.isJSXSpreadAttribute(attr)) {
211
+ // {…obj} — spread onto native element
212
+ statements.push(
213
+ t.expressionStatement(
214
+ t.callExpression(t.identifier("Object.assign"), [
215
+ t.identifier(varName),
216
+ attr.argument as t.Expression,
217
+ ]),
218
+ ),
219
+ );
220
+ continue;
221
+ }
222
+ if (!t.isJSXAttribute(attr)) continue;
223
+ let attrName: string;
224
+ let attrNamespace: string | undefined;
225
+ if (t.isJSXNamespacedName(attr.name)) {
226
+ attrNamespace = attr.name.namespace.name;
227
+ attrName = attr.name.name.name;
228
+ } else {
229
+ attrName = (attr.name as t.JSXIdentifier).name;
230
+ }
231
+ const realAttr = ATTR_MAP[attrName] ?? attrName;
232
+
233
+ if (seenAttrs.has(realAttr)) {
234
+ throw new TypeError(
235
+ `Duplicate attribute "${realAttr}" (via "${attrName}"). Use class or className, not both.`,
236
+ );
237
+ }
238
+ seenAttrs.add(realAttr);
239
+ if (attrName === "use") {
240
+ // use={directive} or use={[directive, params]}
241
+ const expr = (attr.value as t.JSXExpressionContainer)
242
+ .expression as t.Expression;
243
+ statements.push(
244
+ t.expressionStatement(
245
+ t.callExpression(t.identifier("applyDirective"), [
246
+ t.identifier(varName),
247
+ expr,
248
+ ]),
249
+ ),
250
+ );
251
+ } else if (/^on[A-Z]/.test(attrName)) {
252
+ // Standard JSX: onClick, onSubmit, onMouseenter, etc.
253
+ // Convert camelCase to lowercase: onClick -> click, onMouseenter -> mouseenter
254
+ const event = attrName.slice(2).toLowerCase();
255
+ const handler = (attr.value as t.JSXExpressionContainer)
256
+ .expression as t.Expression;
257
+ statements.push(
258
+ t.expressionStatement(
259
+ t.callExpression(
260
+ t.memberExpression(
261
+ t.identifier(varName),
262
+ t.identifier("addEventListener"),
263
+ ),
264
+ [t.stringLiteral(event), handler],
265
+ ),
266
+ ),
267
+ );
268
+ } else if (attrNamespace === "bind") {
269
+ const bindProp = attrName;
270
+ const signal = (attr.value as t.JSXExpressionContainer)
271
+ .expression as t.Expression;
272
+
273
+ if (bindProp === "this") {
274
+ statements.push(
275
+ t.expressionStatement(
276
+ t.callExpression(
277
+ t.memberExpression(
278
+ (signal as t.CallExpression).callee as t.Expression,
279
+ t.identifier("set"),
280
+ ),
281
+ [t.identifier(varName)],
282
+ ),
283
+ ),
284
+ );
285
+ continue;
286
+ }
287
+
288
+ if (bindProp === "group") {
289
+ // Scan element attributes for type and value
290
+ let inputType = "";
291
+ let inputValue: t.StringLiteral | undefined;
292
+ for (const a of node.openingElement.attributes) {
293
+ if (t.isJSXAttribute(a) && t.isJSXIdentifier(a.name)) {
294
+ if (a.name.name === "type" && t.isStringLiteral(a.value)) {
295
+ inputType = a.value.value;
296
+ }
297
+ if (a.name.name === "value" && t.isStringLiteral(a.value)) {
298
+ inputValue = a.value;
299
+ }
300
+ }
301
+ }
302
+
303
+ if (inputType === "radio" || inputType === "") {
304
+ const isTopLevel = t.isCallExpression(signal) &&
305
+ t.isIdentifier((signal as t.CallExpression).callee) &&
306
+ signals.has(((signal as t.CallExpression).callee as t.Identifier).name);
307
+
308
+ if (isTopLevel) {
309
+ const signalId = (signal as t.CallExpression).callee as t.Identifier;
310
+ statements.push(
311
+ t.expressionStatement(
312
+ t.callExpression(
313
+ t.memberExpression(t.identifier(varName), t.identifier("addEventListener")),
314
+ [
315
+ t.stringLiteral("change"),
316
+ t.arrowFunctionExpression(
317
+ [],
318
+ t.callExpression(
319
+ t.memberExpression(signalId, t.identifier("set")),
320
+ [
321
+ t.memberExpression(
322
+ t.identifier(varName),
323
+ t.identifier("value"),
324
+ ),
325
+ ],
326
+ ),
327
+ ),
328
+ ],
329
+ ),
330
+ ),
331
+ );
332
+ }
333
+ } else if (inputType === "checkbox") {
334
+ const isTopLevel = t.isCallExpression(signal) &&
335
+ t.isIdentifier((signal as t.CallExpression).callee) &&
336
+ signals.has(((signal as t.CallExpression).callee as t.Identifier).name);
337
+
338
+ if (isTopLevel) {
339
+ const callExpr = signal as t.CallExpression;
340
+ statements.push(
341
+ t.expressionStatement(
342
+ t.callExpression(
343
+ t.memberExpression(t.identifier(varName), t.identifier("addEventListener")),
344
+ [
345
+ t.stringLiteral("change"),
346
+ t.arrowFunctionExpression(
347
+ [],
348
+ t.blockStatement([
349
+ t.ifStatement(
350
+ t.memberExpression(t.identifier(varName), t.identifier("checked")),
351
+ // checked: push value into array
352
+ t.blockStatement([
353
+ t.expressionStatement(
354
+ t.callExpression(
355
+ t.memberExpression(
356
+ t.callExpression(callExpr, []),
357
+ t.identifier("push"),
358
+ ),
359
+ [
360
+ t.memberExpression(
361
+ t.identifier(varName),
362
+ t.identifier("value"),
363
+ ),
364
+ ],
365
+ ),
366
+ ),
367
+ ]),
368
+ // unchecked: splice value out of array
369
+ t.blockStatement([
370
+ t.expressionStatement(
371
+ t.callExpression(
372
+ t.memberExpression(
373
+ t.callExpression(callExpr, []),
374
+ t.identifier("splice"),
375
+ ),
376
+ [
377
+ t.callExpression(
378
+ t.memberExpression(
379
+ t.callExpression(callExpr, []),
380
+ t.identifier("indexOf"),
381
+ ),
382
+ [
383
+ t.memberExpression(
384
+ t.identifier(varName),
385
+ t.identifier("value"),
386
+ ),
387
+ ],
388
+ ),
389
+ t.numericLiteral(1),
390
+ ],
391
+ ),
392
+ ),
393
+ ]),
394
+ ),
395
+ ]),
396
+ ),
397
+ ],
398
+ ),
399
+ ),
400
+ );
401
+ }
402
+ } else {
403
+ console.warn(
404
+ `[sigil] bind:group requires type="radio" or type="checkbox" on the element. ` +
405
+ `Got: type="${inputType}".`
406
+ );
407
+ }
408
+ continue;
409
+ }
410
+
411
+ // Generic bind:innerHTML, bind:textContent, bind:value, bind:checked, etc.
412
+ const isTopLevelSignal = t.isCallExpression(signal) &&
413
+ t.isIdentifier((signal as t.CallExpression).callee) &&
414
+ signals.has(((signal as t.CallExpression).callee as t.Identifier).name);
415
+
416
+ const isMemberOfSignal = t.isMemberExpression(signal) &&
417
+ t.isCallExpression((signal as t.MemberExpression).object) &&
418
+ t.isIdentifier(((signal as t.MemberExpression).object as t.CallExpression).callee) &&
419
+ signals.has((((signal as t.MemberExpression).object as t.CallExpression).callee as t.Identifier).name);
420
+
421
+ if (isTopLevelSignal) {
422
+ buildBind(varName, bindProp, attr, statements);
423
+ } else if (isMemberOfSignal) {
424
+ buildProxyBind(varName, bindProp, attr, statements, signal);
425
+ } else {
426
+ console.warn(
427
+ `[sigil] bind:${bindProp}={...} requires a $state signal or $state object property. ` +
428
+ `Got: ${generate(signal).code}. ` +
429
+ `Hint: change to a $state() call.`
430
+ );
431
+ }
432
+ } else if (t.isStringLiteral(attr.value)) {
433
+ statements.push(
434
+ t.expressionStatement(
435
+ t.assignmentExpression(
436
+ "=",
437
+ attrAccess(varName, realAttr),
438
+ attr.value,
439
+ ),
440
+ ),
441
+ );
442
+ } else if (t.isJSXExpressionContainer(attr.value)) {
443
+ const expr = attr.value.expression as t.Expression;
444
+ // style={{ color: "red", fontSize: val }} — per-property assignment
445
+ if (attrName === "style" && t.isObjectExpression(expr)) {
446
+ for (const prop of expr.properties) {
447
+ if (!t.isObjectProperty(prop)) continue;
448
+ const key = t.isIdentifier(prop.key)
449
+ ? prop.key.name
450
+ : t.isStringLiteral(prop.key)
451
+ ? prop.key.value
452
+ : null;
453
+ if (!key) continue;
454
+ const val = prop.value as t.Expression;
455
+ const styleProp = t.memberExpression(
456
+ t.memberExpression(t.identifier(varName), t.identifier("style")),
457
+ t.identifier(key),
458
+ );
459
+ if (containsSignal(val, signals)) {
460
+ statements.push(
461
+ t.expressionStatement(
462
+ t.callExpression(t.identifier("createEffect"), [
463
+ t.arrowFunctionExpression(
464
+ [],
465
+ t.blockStatement([
466
+ t.expressionStatement(
467
+ t.assignmentExpression("=", styleProp, val),
468
+ ),
469
+ ]),
470
+ ),
471
+ ]),
472
+ ),
473
+ );
474
+ } else {
475
+ statements.push(
476
+ t.expressionStatement(
477
+ t.assignmentExpression("=", styleProp, val),
478
+ ),
479
+ );
480
+ }
481
+ }
482
+ // done with this attribute
483
+ } else if (containsSignal(expr, signals)) {
484
+ // Dynamic class with scoped CSS: use classList.value to preserve hash
485
+ if (attrName === "class" && hash) {
486
+ statements.push(
487
+ t.expressionStatement(
488
+ t.callExpression(t.identifier("createEffect"), [
489
+ t.arrowFunctionExpression(
490
+ [],
491
+ t.blockStatement([
492
+ t.expressionStatement(
493
+ t.assignmentExpression(
494
+ "=",
495
+ t.memberExpression(
496
+ t.memberExpression(
497
+ t.identifier(varName),
498
+ t.identifier("classList"),
499
+ ),
500
+ t.identifier("value"),
501
+ ),
502
+ expr,
503
+ ),
504
+ ),
505
+ t.expressionStatement(
506
+ t.callExpression(
507
+ t.memberExpression(
508
+ t.memberExpression(
509
+ t.identifier(varName),
510
+ t.identifier("classList"),
511
+ ),
512
+ t.identifier("add"),
513
+ ),
514
+ [t.stringLiteral(hash)],
515
+ ),
516
+ ),
517
+ ]),
518
+ ),
519
+ ]),
520
+ ),
521
+ );
522
+ } else {
523
+ // createEffect(() => varName.realAttr = expr)
524
+ statements.push(
525
+ t.expressionStatement(
526
+ t.callExpression(t.identifier("createEffect"), [
527
+ t.arrowFunctionExpression(
528
+ [],
529
+ t.blockStatement([
530
+ t.expressionStatement(
531
+ t.assignmentExpression(
532
+ "=",
533
+ attrAccess(varName, realAttr),
534
+ expr,
535
+ ),
536
+ ),
537
+ ]),
538
+ ),
539
+ ]),
540
+ ),
541
+ );
542
+ }
543
+ } else {
544
+ // varName.realAttr = expr
545
+ statements.push(
546
+ t.expressionStatement(
547
+ t.assignmentExpression(
548
+ "=",
549
+ attrAccess(varName, realAttr),
550
+ expr,
551
+ ),
552
+ ),
553
+ );
554
+ }
555
+ }
556
+ }
557
+
558
+ // Add scoped CSS hash to classList (if hash is present)
559
+ if (hash) {
560
+ addScopedClass(varName, hash, statements);
561
+ }
562
+
563
+ // children
564
+ for (const child of node.children) {
565
+ if (t.isJSXElement(child)) {
566
+ const childVar = processElement(
567
+ child,
568
+ statements,
569
+ genId,
570
+ signals,
571
+ hash,
572
+ hydrate,
573
+ childNodesVar,
574
+ hydrate ? varName : undefined,
575
+ );
576
+ if (!hydrate) {
577
+ statements.push(
578
+ t.expressionStatement(
579
+ t.callExpression(
580
+ t.memberExpression(t.identifier(varName), t.identifier("append")),
581
+ [t.identifier(childVar)],
582
+ ),
583
+ ),
584
+ );
585
+ }
586
+ } else if (t.isJSXFragment(child)) {
587
+ const childVar = processFragment(
588
+ child,
589
+ statements,
590
+ genId,
591
+ signals,
592
+ hash,
593
+ hydrate,
594
+ childNodesVar,
595
+ hydrate ? varName : undefined,
596
+ );
597
+ if (!hydrate) {
598
+ statements.push(
599
+ t.expressionStatement(
600
+ t.callExpression(
601
+ t.memberExpression(t.identifier(varName), t.identifier("append")),
602
+ [t.identifier(childVar)],
603
+ ),
604
+ ),
605
+ );
606
+ }
607
+ } else if (t.isJSXText(child)) {
608
+ const text = child.value;
609
+ if (!text.trim()) continue;
610
+ if (hydrate) {
611
+ // SSR hydration: text already in DOM from SSR, do nothing.
612
+ // SPA navigation: childNodesVar pool is empty, create text node.
613
+ const poolLen = t.memberExpression(
614
+ t.identifier(childNodesVar!),
615
+ t.identifier("length"),
616
+ );
617
+ statements.push(
618
+ t.ifStatement(
619
+ t.binaryExpression("===", poolLen, t.numericLiteral(0)),
620
+ t.expressionStatement(
621
+ t.callExpression(
622
+ t.memberExpression(
623
+ t.identifier(varName),
624
+ t.identifier("append"),
625
+ ),
626
+ [
627
+ t.callExpression(
628
+ t.memberExpression(
629
+ t.identifier("document"),
630
+ t.identifier("createTextNode"),
631
+ ),
632
+ [t.stringLiteral(text.replace(/\s*\n\s*/g, " "))],
633
+ ),
634
+ ],
635
+ ),
636
+ ),
637
+ ),
638
+ );
639
+ } else {
640
+ statements.push(
641
+ t.expressionStatement(
642
+ t.callExpression(
643
+ t.memberExpression(t.identifier(varName), t.identifier("append")),
644
+ [
645
+ t.callExpression(
646
+ t.memberExpression(
647
+ t.identifier("document"),
648
+ t.identifier("createTextNode"),
649
+ ),
650
+ [t.stringLiteral(text)],
651
+ ),
652
+ ],
653
+ ),
654
+ ),
655
+ );
656
+ }
657
+ } else if (t.isJSXExpressionContainer(child)) {
658
+ if (t.isJSXEmptyExpression(child.expression)) continue;
659
+ const expr = child.expression as t.Expression;
660
+ if (t.isJSXElement(expr)) {
661
+ const childVar = processElement(
662
+ expr,
663
+ statements,
664
+ genId,
665
+ signals,
666
+ hash,
667
+ hydrate,
668
+ childNodesVar,
669
+ hydrate ? varName : undefined,
670
+ );
671
+ if (!hydrate) {
672
+ statements.push(
673
+ t.expressionStatement(
674
+ t.callExpression(
675
+ t.memberExpression(
676
+ t.identifier(varName),
677
+ t.identifier("append"),
678
+ ),
679
+ [t.identifier(childVar)],
680
+ ),
681
+ ),
682
+ );
683
+ }
684
+ } else if (t.isJSXFragment(expr)) {
685
+ const childVar = processFragment(
686
+ expr,
687
+ statements,
688
+ genId,
689
+ signals,
690
+ hash,
691
+ hydrate,
692
+ childNodesVar,
693
+ hydrate ? varName : undefined,
694
+ );
695
+ if (!hydrate) {
696
+ statements.push(
697
+ t.expressionStatement(
698
+ t.callExpression(
699
+ t.memberExpression(
700
+ t.identifier(varName),
701
+ t.identifier("append"),
702
+ ),
703
+ [t.identifier(childVar)],
704
+ ),
705
+ ),
706
+ );
707
+ }
708
+ } else if (containsSignal(expr, signals)) {
709
+ if (isPrimitive(expr)) {
710
+ // fast path: text node + textContent
711
+ buildTextNode(
712
+ varName,
713
+ expr,
714
+ statements,
715
+ genId,
716
+ hydrate,
717
+ childNodesVar,
718
+ hydrate ? varName : undefined,
719
+ );
720
+ } else {
721
+ // Check for keyed list pattern
722
+ const keyed = findKeyedMapExpr(expr, signals);
723
+ if (keyed) {
724
+ buildKeyedList(
725
+ varName,
726
+ keyed,
727
+ statements,
728
+ genId,
729
+ signals,
730
+ processElement,
731
+ hash,
732
+ hydrate,
733
+ childNodesVar,
734
+ hydrate ? varName : undefined,
735
+ );
736
+ } else {
737
+ buildAnchorMount(
738
+ varName,
739
+ expr,
740
+ statements,
741
+ genId,
742
+ hydrate,
743
+ childNodesVar,
744
+ hydrate ? varName : undefined,
745
+ );
746
+ }
747
+ }
748
+ } else {
749
+ // non-reactive: set once
750
+ if (hydrate) {
751
+ const claimCommentArgs: t.Expression[] = [
752
+ t.identifier(childNodesVar!),
753
+ t.stringLiteral("g"),
754
+ ];
755
+ if (varName) claimCommentArgs.push(t.identifier(varName));
756
+ const poolLen = t.memberExpression(
757
+ t.identifier(childNodesVar!),
758
+ t.identifier("length"),
759
+ );
760
+ // Pool has elements (initial load): SSR content already in DOM, just consume delimiters
761
+ const claimG = t.expressionStatement(
762
+ t.callExpression(t.identifier("claimComment"), claimCommentArgs),
763
+ );
764
+ const claimSlashG = (() => {
765
+ const args: t.Expression[] = [
766
+ t.identifier(childNodesVar!),
767
+ t.stringLiteral("/g"),
768
+ ];
769
+ if (varName) args.push(t.identifier(varName));
770
+ return t.expressionStatement(
771
+ t.callExpression(t.identifier("claimComment"), args),
772
+ );
773
+ })();
774
+ // Pool empty (SPA nav): create and insert fresh elements
775
+ const insertCall = t.expressionStatement(
776
+ t.callExpression(t.identifier("insert"), [
777
+ t.identifier(varName),
778
+ expr,
779
+ ]),
780
+ );
781
+ statements.push(
782
+ t.ifStatement(
783
+ t.binaryExpression(">", poolLen, t.numericLiteral(0)),
784
+ t.blockStatement([claimG, claimSlashG]),
785
+ t.blockStatement([insertCall]),
786
+ ),
787
+ );
788
+ } else {
789
+ statements.push(
790
+ t.expressionStatement(
791
+ t.callExpression(t.identifier("insert"), [
792
+ t.identifier(varName),
793
+ expr,
794
+ ]),
795
+ ),
796
+ );
797
+ }
798
+ }
799
+ }
800
+ }
801
+
802
+ return varName;
803
+ }