@sigil-dev/compiler 0.6.11 → 0.7.0

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,520 +1,582 @@
1
1
  import { types as t } from "@babel/core";
2
- import { buildBind } from "../util/bind";
2
+ import {generate} from "@babel/generator"
3
+ import { buildBind, buildProxyBind } from "../util/bind";
3
4
  import { buildAnchorMount } from "./anchor-mount";
4
5
  import { collectChildren } from "./children";
5
6
  import { processFragment } from "./fragment";
6
7
  import { buildKeyedList, findKeyedMapExpr } from "./keyed-list";
7
8
  import { buildTextNode } from "./text-node";
8
9
  import {
9
- ATTR_MAP,
10
- buildHydrationScope,
11
- containsSignal,
12
- getCreateElement,
13
- isPrimitive,
10
+ ATTR_MAP,
11
+ buildHydrationScope,
12
+ containsSignal,
13
+ getCreateElement,
14
+ isPrimitive,
14
15
  } from "./utils";
15
16
 
16
17
  /**
17
18
  * Add classList.add(hash) statement for scoped CSS.
18
19
  */
19
20
  function addScopedClass(
20
- varName: string,
21
- hash: string,
22
- statements: t.Statement[],
21
+ varName: string,
22
+ hash: string,
23
+ statements: t.Statement[],
23
24
  ): void {
24
- statements.push(
25
- t.expressionStatement(
26
- t.callExpression(
27
- t.memberExpression(
28
- t.memberExpression(t.identifier(varName), t.identifier("classList")),
29
- t.identifier("add"),
30
- ),
31
- [t.stringLiteral(hash)],
32
- ),
33
- ),
34
- );
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
+ );
35
36
  }
36
37
 
37
38
  export function processElement(
38
- node: t.JSXElement,
39
- statements: t.Statement[],
40
- genId: () => string,
41
- signals: Set<string>,
42
- hash?: string,
43
- hydrate?: boolean,
44
- nodesVar?: string,
45
- parentVar?: string,
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,
46
47
  ): string {
47
- const varName = genId();
48
- const tag = (node.openingElement.name as t.JSXIdentifier).name;
49
- const isComponent = /^[A-Z]/.test(tag);
48
+ const varName = genId();
49
+ const tag = (node.openingElement.name as t.JSXIdentifier).name;
50
+ const isComponent = /^[A-Z]/.test(tag);
50
51
 
51
- if (isComponent) {
52
- // Component: call the function with a props object
53
- const propsObj = t.objectExpression([]);
52
+ if (isComponent) {
53
+ // Component: call the function with a props object
54
+ const propsObj = t.objectExpression([]);
54
55
 
55
- // Add attributes to props (including spread)
56
- for (const attr of node.openingElement.attributes) {
57
- if (t.isJSXSpreadAttribute(attr)) {
58
- // {…obj} — spread into props
59
- propsObj.properties.push(t.spreadElement(attr.argument));
60
- continue;
61
- }
62
- if (!t.isJSXAttribute(attr)) continue;
63
- const attrName = (attr.name as t.JSXIdentifier).name;
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;
64
65
 
65
- if (t.isStringLiteral(attr.value)) {
66
- propsObj.properties.push(
67
- t.objectProperty(t.identifier(attrName), attr.value),
68
- );
69
- } else if (t.isJSXExpressionContainer(attr.value)) {
70
- const expr = attr.value.expression as t.Expression;
71
- propsObj.properties.push(
72
- t.objectProperty(t.identifier(attrName), expr),
73
- );
74
- }
75
- }
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
+ }
76
77
 
77
- // Add children to props
78
- const childrenExpr = collectChildren(
79
- node,
80
- statements,
81
- genId,
82
- signals,
83
- hash,
84
- hydrate,
85
- nodesVar,
86
- );
87
- if (childrenExpr) {
88
- propsObj.properties.push(
89
- t.objectProperty(t.identifier("children"), childrenExpr),
90
- );
91
- }
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
+ }
92
93
 
93
- // const _el0 = TagName({ ...props })
94
- statements.push(
95
- t.variableDeclaration("const", [
96
- t.variableDeclarator(
97
- t.identifier(varName),
98
- t.callExpression(t.identifier(tag), [propsObj]),
99
- ),
100
- ]),
101
- );
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
+ );
102
103
 
103
- return varName;
104
- }
104
+ return varName;
105
+ }
105
106
 
106
- // Native element: claim (hydrate) or create (dom)
107
- const currentNodesVar = nodesVar ?? "__nodes";
108
- statements.push(
109
- t.variableDeclaration("const", [
110
- t.variableDeclarator(
111
- t.identifier(varName),
112
- getCreateElement(
113
- tag,
114
- !!hydrate,
115
- currentNodesVar,
116
- hydrate ? parentVar : undefined,
117
- ),
118
- ),
119
- ]),
120
- );
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
+ );
121
122
 
122
- // In hydrate mode, scope to this element's children for descendants
123
- let childNodesVar: string | undefined;
124
- if (hydrate) {
125
- childNodesVar = genId();
126
- buildHydrationScope(varName, statements, childNodesVar);
127
- }
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
+ }
128
129
 
129
- // attributes
130
- const seenAttrs = new Set<string>();
131
- for (const attr of node.openingElement.attributes) {
132
- if (t.isJSXSpreadAttribute(attr)) {
133
- // {…obj} — spread onto native element
134
- statements.push(
135
- t.expressionStatement(
136
- t.callExpression(t.identifier("Object.assign"), [
137
- t.identifier(varName),
138
- attr.argument as t.Expression,
139
- ]),
140
- ),
141
- );
142
- continue;
143
- }
144
- if (!t.isJSXAttribute(attr)) continue;
145
- const attrName = (attr.name as t.JSXIdentifier).name;
146
- const realAttr = ATTR_MAP[attrName] ?? attrName;
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;
147
148
 
148
- if (seenAttrs.has(realAttr)) {
149
- throw new TypeError(
150
- `Duplicate attribute "${realAttr}" (via "${attrName}"). Use class or className, not both.`,
151
- );
152
- }
153
- seenAttrs.add(realAttr);
154
- if (attrName === "use") {
155
- // use={directive} or use={[directive, params]}
156
- const expr = (attr.value as t.JSXExpressionContainer)
157
- .expression as t.Expression;
158
- statements.push(
159
- t.expressionStatement(
160
- t.callExpression(t.identifier("applyDirective"), [
161
- t.identifier(varName),
162
- expr,
163
- ]),
164
- ),
165
- );
166
- } else if (/^on[A-Z]/.test(attrName)) {
167
- // Standard JSX: onClick, onSubmit, onMouseenter, etc.
168
- // Convert camelCase to lowercase: onClick -> click, onMouseenter -> mouseenter
169
- const event = attrName.slice(2).toLowerCase();
170
- const handler = (attr.value as t.JSXExpressionContainer)
171
- .expression as t.Expression;
172
- statements.push(
173
- t.expressionStatement(
174
- t.callExpression(
175
- t.memberExpression(
176
- t.identifier(varName),
177
- t.identifier("addEventListener"),
178
- ),
179
- [t.stringLiteral(event), handler],
180
- ),
181
- ),
182
- );
183
- } else if (attrName.startsWith("bind")) {
184
- buildBind(varName, attrName, attr, statements);
185
- } else if (t.isStringLiteral(attr.value)) {
186
- statements.push(
187
- t.expressionStatement(
188
- t.assignmentExpression(
189
- "=",
190
- t.memberExpression(t.identifier(varName), t.identifier(realAttr)),
191
- attr.value,
192
- ),
193
- ),
194
- );
195
- } else if (t.isJSXExpressionContainer(attr.value)) {
196
- const expr = attr.value.expression as t.Expression;
197
- if (containsSignal(expr, signals)) {
198
- // Dynamic class with scoped CSS: use classList.value to preserve hash
199
- if (attrName === "class" && hash) {
200
- statements.push(
201
- t.expressionStatement(
202
- t.callExpression(t.identifier("createEffect"), [
203
- t.arrowFunctionExpression(
204
- [],
205
- t.blockStatement([
206
- t.expressionStatement(
207
- t.assignmentExpression(
208
- "=",
209
- t.memberExpression(
210
- t.memberExpression(
211
- t.identifier(varName),
212
- t.identifier("classList"),
213
- ),
214
- t.identifier("value"),
215
- ),
216
- expr,
217
- ),
218
- ),
219
- t.expressionStatement(
220
- t.callExpression(
221
- t.memberExpression(
222
- t.memberExpression(
223
- t.identifier(varName),
224
- t.identifier("classList"),
225
- ),
226
- t.identifier("add"),
227
- ),
228
- [t.stringLiteral(hash)],
229
- ),
230
- ),
231
- ]),
232
- ),
233
- ]),
234
- ),
235
- );
236
- } else {
237
- // createEffect(() => varName.realAttr = expr)
238
- statements.push(
239
- t.expressionStatement(
240
- t.callExpression(t.identifier("createEffect"), [
241
- t.arrowFunctionExpression(
242
- [],
243
- t.blockStatement([
244
- t.expressionStatement(
245
- t.assignmentExpression(
246
- "=",
247
- t.memberExpression(
248
- t.identifier(varName),
249
- t.identifier(realAttr),
250
- ),
251
- expr,
252
- ),
253
- ),
254
- ]),
255
- ),
256
- ]),
257
- ),
258
- );
259
- }
260
- } else {
261
- // varName.realAttr = expr
262
- statements.push(
263
- t.expressionStatement(
264
- t.assignmentExpression(
265
- "=",
266
- t.memberExpression(t.identifier(varName), t.identifier(realAttr)),
267
- expr,
268
- ),
269
- ),
270
- );
271
- }
272
- }
273
- }
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;
274
187
 
275
- // Add scoped CSS hash to classList (if hash is present)
276
- if (hash) {
277
- addScopedClass(varName, hash, statements);
278
- }
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);
279
191
 
280
- // children
281
- for (const child of node.children) {
282
- if (t.isJSXElement(child)) {
283
- const childVar = processElement(
284
- child,
285
- statements,
286
- genId,
287
- signals,
288
- hash,
289
- hydrate,
290
- childNodesVar,
291
- hydrate ? varName : undefined,
292
- );
293
- if (!hydrate) {
294
- statements.push(
295
- t.expressionStatement(
296
- t.callExpression(
297
- t.memberExpression(t.identifier(varName), t.identifier("append")),
298
- [t.identifier(childVar)],
299
- ),
300
- ),
301
- );
302
- }
303
- } else if (t.isJSXFragment(child)) {
304
- const childVar = processFragment(
305
- child,
306
- statements,
307
- genId,
308
- signals,
309
- hash,
310
- hydrate,
311
- childNodesVar,
312
- hydrate ? varName : undefined,
313
- );
314
- if (!hydrate) {
315
- statements.push(
316
- t.expressionStatement(
317
- t.callExpression(
318
- t.memberExpression(t.identifier(varName), t.identifier("append")),
319
- [t.identifier(childVar)],
320
- ),
321
- ),
322
- );
323
- }
324
- } else if (t.isJSXText(child)) {
325
- const text = child.value;
326
- if (!text.trim()) continue;
327
- if (hydrate) {
328
- // SSR hydration: text already in DOM from SSR, do nothing.
329
- // SPA navigation: childNodesVar pool is empty, create text node.
330
- const poolLen = t.memberExpression(
331
- t.identifier(childNodesVar!),
332
- t.identifier("length"),
333
- );
334
- statements.push(
335
- t.ifStatement(
336
- t.binaryExpression("===", poolLen, t.numericLiteral(0)),
337
- t.expressionStatement(
338
- t.callExpression(
339
- t.memberExpression(
340
- t.identifier(varName),
341
- t.identifier("append"),
342
- ),
343
- [
344
- t.callExpression(
345
- t.memberExpression(
346
- t.identifier("document"),
347
- t.identifier("createTextNode"),
348
- ),
349
- [t.stringLiteral(text.replace(/\s*\n\s*/g, " "))],
350
- ),
351
- ],
352
- ),
353
- ),
354
- ),
355
- );
356
- } else {
357
- statements.push(
358
- t.expressionStatement(
359
- t.callExpression(
360
- t.memberExpression(t.identifier(varName), t.identifier("append")),
361
- [
362
- t.callExpression(
363
- t.memberExpression(
364
- t.identifier("document"),
365
- t.identifier("createTextNode"),
366
- ),
367
- [t.stringLiteral(text)],
368
- ),
369
- ],
370
- ),
371
- ),
372
- );
373
- }
374
- } else if (t.isJSXExpressionContainer(child)) {
375
- if (t.isJSXEmptyExpression(child.expression)) continue;
376
- const expr = child.expression as t.Expression;
377
- if (t.isJSXElement(expr)) {
378
- const childVar = processElement(
379
- expr,
380
- statements,
381
- genId,
382
- signals,
383
- hash,
384
- hydrate,
385
- childNodesVar,
386
- hydrate ? varName : undefined,
387
- );
388
- if (!hydrate) {
389
- statements.push(
390
- t.expressionStatement(
391
- t.callExpression(
392
- t.memberExpression(
393
- t.identifier(varName),
394
- t.identifier("append"),
395
- ),
396
- [t.identifier(childVar)],
397
- ),
398
- ),
399
- );
400
- }
401
- } else if (t.isJSXFragment(expr)) {
402
- const childVar = processFragment(
403
- expr,
404
- statements,
405
- genId,
406
- signals,
407
- hash,
408
- hydrate,
409
- childNodesVar,
410
- hydrate ? varName : undefined,
411
- );
412
- if (!hydrate) {
413
- statements.push(
414
- t.expressionStatement(
415
- t.callExpression(
416
- t.memberExpression(
417
- t.identifier(varName),
418
- t.identifier("append"),
419
- ),
420
- [t.identifier(childVar)],
421
- ),
422
- ),
423
- );
424
- }
425
- } else if (containsSignal(expr, signals)) {
426
- if (isPrimitive(expr)) {
427
- // fast path: text node + textContent
428
- buildTextNode(
429
- varName,
430
- expr,
431
- statements,
432
- genId,
433
- hydrate,
434
- childNodesVar,
435
- hydrate ? varName : undefined,
436
- );
437
- } else {
438
- // Check for keyed list pattern
439
- const keyed = findKeyedMapExpr(expr, signals);
440
- if (keyed) {
441
- buildKeyedList(
442
- varName,
443
- keyed,
444
- statements,
445
- genId,
446
- signals,
447
- processElement,
448
- hash,
449
- hydrate,
450
- childNodesVar,
451
- hydrate ? varName : undefined,
452
- );
453
- } else {
454
- buildAnchorMount(
455
- varName,
456
- expr,
457
- statements,
458
- genId,
459
- hydrate,
460
- childNodesVar,
461
- hydrate ? varName : undefined,
462
- );
463
- }
464
- }
465
- } else {
466
- // non-reactive: set once
467
- if (hydrate) {
468
- const claimCommentArgs: t.Expression[] = [
469
- t.identifier(childNodesVar!),
470
- t.stringLiteral("g"),
471
- ];
472
- if (varName) claimCommentArgs.push(t.identifier(varName));
473
- const poolLen = t.memberExpression(
474
- t.identifier(childNodesVar!),
475
- t.identifier("length"),
476
- );
477
- // Pool has elements (initial load): SSR content already in DOM, just consume delimiters
478
- const claimG = t.expressionStatement(
479
- t.callExpression(t.identifier("claimComment"), claimCommentArgs),
480
- );
481
- const claimSlashG = (() => {
482
- const args: t.Expression[] = [
483
- t.identifier(childNodesVar!),
484
- t.stringLiteral("/g"),
485
- ];
486
- if (varName) args.push(t.identifier(varName));
487
- return t.expressionStatement(
488
- t.callExpression(t.identifier("claimComment"), args),
489
- );
490
- })();
491
- // Pool empty (SPA nav): create and insert fresh elements
492
- const insertCall = t.expressionStatement(
493
- t.callExpression(t.identifier("insert"), [
494
- t.identifier(varName),
495
- expr,
496
- ]),
497
- );
498
- statements.push(
499
- t.ifStatement(
500
- t.binaryExpression(">", poolLen, t.numericLiteral(0)),
501
- t.blockStatement([claimG, claimSlashG]),
502
- t.blockStatement([insertCall]),
503
- ),
504
- );
505
- } else {
506
- statements.push(
507
- t.expressionStatement(
508
- t.callExpression(t.identifier("insert"), [
509
- t.identifier(varName),
510
- expr,
511
- ]),
512
- ),
513
- );
514
- }
515
- }
516
- }
517
- }
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);
518
196
 
519
- return varName;
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;
520
582
  }