ripple 0.2.45 → 0.2.47

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 (36) hide show
  1. package/package.json +1 -1
  2. package/src/compiler/phases/1-parse/index.js +8 -1
  3. package/src/compiler/phases/2-analyze/index.js +640 -650
  4. package/src/compiler/phases/3-transform/index.js +1877 -1813
  5. package/src/compiler/phases/3-transform/segments.js +2 -2
  6. package/src/compiler/utils.js +600 -523
  7. package/src/jsx-runtime.js +12 -12
  8. package/src/runtime/array.js +611 -609
  9. package/src/runtime/index.js +29 -17
  10. package/src/runtime/internal/client/array.js +128 -128
  11. package/src/runtime/internal/client/blocks.js +206 -207
  12. package/src/runtime/internal/client/constants.js +2 -2
  13. package/src/runtime/internal/client/context.js +40 -40
  14. package/src/runtime/internal/client/events.js +191 -191
  15. package/src/runtime/internal/client/for.js +355 -355
  16. package/src/runtime/internal/client/if.js +25 -25
  17. package/src/runtime/internal/client/index.js +57 -52
  18. package/src/runtime/internal/client/operations.js +32 -32
  19. package/src/runtime/internal/client/portal.js +20 -20
  20. package/src/runtime/internal/client/render.js +132 -132
  21. package/src/runtime/internal/client/runtime.js +858 -824
  22. package/src/runtime/internal/client/template.js +36 -36
  23. package/src/runtime/internal/client/try.js +113 -113
  24. package/src/runtime/internal/client/types.d.ts +10 -10
  25. package/src/runtime/internal/client/utils.js +5 -5
  26. package/src/runtime/map.js +139 -139
  27. package/src/runtime/set.js +130 -130
  28. package/src/utils/ast.js +189 -189
  29. package/src/utils/builders.js +244 -244
  30. package/src/utils/sanitize_template_string.js +1 -1
  31. package/tests/__snapshots__/composite.test.ripple.snap +1 -1
  32. package/tests/accessors-props.test.ripple +9 -9
  33. package/tests/basic.test.ripple +4 -4
  34. package/tests/boundaries.test.ripple +17 -17
  35. package/tests/composite.test.ripple +43 -72
  36. package/types/index.d.ts +6 -2
@@ -6,1853 +6,1917 @@ import * as b from '../../../utils/builders.js';
6
6
  import { IS_CONTROLLED, TEMPLATE_FRAGMENT } from '../../../constants.js';
7
7
  import { sanitize_template_string } from '../../../utils/sanitize_template_string.js';
8
8
  import {
9
- build_hoisted_params,
10
- is_event_attribute,
11
- is_inside_component,
12
- is_tracked_name,
13
- is_passive_event,
14
- build_assignment,
15
- visit_assignment_expression,
16
- escape_html,
17
- is_boolean_attribute,
18
- is_dom_property,
19
- is_ripple_import,
20
- is_declared_within_component,
21
- is_inside_call_expression,
22
- is_tracked_computed_property,
23
- is_value_static,
24
- is_void_element,
9
+ build_hoisted_params,
10
+ is_event_attribute,
11
+ is_inside_component,
12
+ is_tracked_name,
13
+ is_passive_event,
14
+ build_assignment,
15
+ visit_assignment_expression,
16
+ escape_html,
17
+ is_boolean_attribute,
18
+ is_dom_property,
19
+ is_ripple_import,
20
+ is_declared_function_within_component,
21
+ is_inside_call_expression,
22
+ is_tracked_computed_property,
23
+ is_value_static,
24
+ is_void_element,
25
+ is_component_level_function,
26
+ is_element_dom_element,
25
27
  } from '../../utils.js';
26
28
  import is_reference from 'is-reference';
27
29
  import { extract_paths, object } from '../../../utils/ast.js';
28
30
  import { render_stylesheets } from './stylesheet.js';
29
31
 
32
+ function add_ripple_internal_import(context) {
33
+ if (!context.state.to_ts) {
34
+ if (!context.state.imports.has(`import * as $ from 'ripple/internal/client'`)) {
35
+ context.state.imports.add(`import * as $ from 'ripple/internal/client'`);
36
+ }
37
+ }
38
+ }
39
+
30
40
  function visit_function(node, context) {
31
- if (context.state.to_ts) {
32
- context.next(context.state);
33
- return;
34
- }
35
- const metadata = node.metadata;
36
- const state = context.state;
37
-
38
- delete node.returnType;
39
-
40
- for (const param of node.params) {
41
- delete param.typeAnnotation;
42
- }
43
-
44
- if (metadata?.hoisted === true) {
45
- const params = build_hoisted_params(node, context);
46
-
47
- return /** @type {FunctionExpression} */ ({
48
- ...node,
49
- params,
50
- body: context.visit(node.body, state),
51
- });
52
- }
53
-
54
- let body = context.visit(node.body, state);
55
-
56
- if (metadata?.tracked === true) {
57
- return /** @type {FunctionExpression} */ ({
58
- ...node,
59
- params: node.params.map((param) => context.visit(param, state)),
60
- body:
61
- body.type === 'BlockStatement'
62
- ? { ...body, body: [b.var('__block', b.call('$.scope')), ...body.body] }
63
- : body,
64
- });
65
- }
66
-
67
- context.next(state);
41
+ if (context.state.to_ts) {
42
+ context.next(context.state);
43
+ return;
44
+ }
45
+ const metadata = node.metadata;
46
+ const state = context.state;
47
+
48
+ delete node.returnType;
49
+
50
+ for (const param of node.params) {
51
+ delete param.typeAnnotation;
52
+ }
53
+
54
+ if (metadata?.hoisted === true) {
55
+ const params = build_hoisted_params(node, context);
56
+
57
+ return /** @type {FunctionExpression} */ ({
58
+ ...node,
59
+ params,
60
+ body: context.visit(node.body, state),
61
+ });
62
+ }
63
+
64
+ let body = context.visit(node.body, state);
65
+
66
+ if (metadata?.tracked === true) {
67
+ const new_body = [];
68
+
69
+ if (!is_inside_component(context, true) && is_component_level_function(context)) {
70
+ add_ripple_internal_import(context);
71
+ new_body.push(b.var('__block', b.call('$.scope')));
72
+ }
73
+ if (body.type === 'BlockStatement') {
74
+ new_body.push(...body.body);
75
+ }
76
+
77
+ return /** @type {FunctionExpression} */ ({
78
+ ...node,
79
+ params: node.params.map((param) => context.visit(param, state)),
80
+ body: body.type === 'BlockStatement' ? { ...body, body: new_body } : body,
81
+ });
82
+ }
83
+
84
+ context.next(state);
68
85
  }
69
86
 
70
87
  function build_getter(node, context) {
71
- const state = context.state;
88
+ const state = context.state;
89
+
90
+ for (let i = context.path.length - 1; i >= 0; i -= 1) {
91
+ const binding = state.scope.get(node.name);
92
+ const transform = binding?.transform;
72
93
 
73
- for (let i = context.path.length - 1; i >= 0; i -= 1) {
74
- const binding = state.scope.get(node.name);
75
- const transform = binding?.transform;
94
+ // don't transform the declaration itself
95
+ if (node !== binding?.node) {
96
+ const read_fn = transform?.read;
76
97
 
77
- // don't transform the declaration itself
78
- if (node !== binding?.node) {
79
- const read_fn = transform?.read || (node.tracked && transform?.read_tracked);
98
+ if (read_fn) {
99
+ add_ripple_internal_import(context);
80
100
 
81
- if (read_fn) {
82
- return read_fn(node, context.state?.metadata?.spread, context.visit);
83
- }
84
- }
85
- }
101
+ return read_fn(node, context.state?.metadata?.spread, context.visit);
102
+ }
103
+ }
104
+ }
86
105
 
87
- return node;
106
+ return node;
88
107
  }
89
108
 
90
109
  const visitors = {
91
- _: function set_scope(node, { next, state }) {
92
- const scope = state.scopes.get(node);
93
-
94
- if (scope && scope !== state.scope) {
95
- return next({ ...state, scope });
96
- } else {
97
- return next();
98
- }
99
- },
100
-
101
- Identifier(node, context) {
102
- const parent = /** @type {Node} */ (context.path.at(-1));
103
-
104
- if (is_reference(node, parent) && !context.state.to_ts) {
105
- const binding = context.state.scope.get(node.name);
106
- if (
107
- context.state.metadata?.tracking === false &&
108
- (is_tracked_name(node.name) || node.tracked) &&
109
- binding?.node !== node
110
- ) {
111
- context.state.metadata.tracking = true;
112
- }
113
-
114
- if (node.name === 'structuredClone' && binding === null) {
115
- return b.id('$.structured_clone');
116
- }
117
-
118
- return build_getter(node, context);
119
- }
120
- },
121
-
122
- ImportDeclaration(node, context) {
123
- if (!context.state.to_ts && node.importKind === 'type') {
124
- return b.empty;
125
- }
126
-
127
- return {
128
- ...node,
129
- specifiers: node.specifiers
130
- .filter((spec) => spec.importKind !== 'type')
131
- .map((spec) => context.visit(spec)),
132
- };
133
- },
134
-
135
- CallExpression(node, context) {
136
- const callee = node.callee;
137
- const parent = context.path.at(-1);
138
-
139
- if (context.state.metadata?.tracking === false) {
140
- context.state.metadata.tracking = true;
141
- }
142
-
143
- if (
144
- !context.state.to_ts &&
145
- callee.type === 'Identifier' &&
146
- callee.name === 'tracked' &&
147
- is_ripple_import(callee, context)
148
- ) {
149
- return {
150
- ...node,
151
- arguments: [...node.arguments.map((arg) => context.visit(arg)), b.id('__block')],
152
- };
153
- }
154
-
155
- if (
156
- !is_inside_component(context, true) ||
157
- context.state.to_ts ||
158
- (parent?.type === 'MemberExpression' && parent.property === node) ||
159
- is_inside_call_expression(context) ||
160
- !context.path.some((node) => node.type === 'Component') ||
161
- (is_ripple_import(callee, context) &&
162
- (callee.type !== 'Identifier' ||
163
- (callee.name !== 'array' && callee.name !== 'deferred'))) ||
164
- is_declared_within_component(callee, context)
165
- ) {
166
- return context.next();
167
- }
168
-
169
- // Handle array methods that access the array
170
- if (callee.type === 'MemberExpression') {
171
- const property = callee.property;
172
-
173
- if (property.type === 'Identifier' && !callee.optional) {
174
- const name = property.name;
175
- if (
176
- // TODO support the missing array methods
177
- name === 'reduce' ||
178
- name === 'map' ||
179
- name === 'forEach' ||
180
- name === 'join' ||
181
- name === 'includes' ||
182
- name === 'indexOf' ||
183
- name === 'lastIndexOf' ||
184
- name === 'filter' ||
185
- name === 'every' ||
186
- name === 'some' ||
187
- name === 'toSpliced' ||
188
- name === 'toSorted' ||
189
- name === 'toString' ||
190
- name === 'values' ||
191
- name === 'entries'
192
- ) {
193
- return b.call(
194
- '$.with_scope',
195
- b.id('__block'),
196
- b.thunk(
197
- b.call(
198
- '$.array_' + name,
199
- context.visit(callee.object),
200
- ...node.arguments.map((arg) => context.visit(arg)),
201
- ),
202
- ),
203
- );
204
- }
205
- }
206
-
207
- if (callee.computed) {
208
- return b.call(
209
- '$.with_scope',
210
- b.id('__block'),
211
- b.thunk(
212
- b.call(
213
- '$.call_property',
214
- context.visit(callee.object),
215
- context.visit(property),
216
- callee.optional ? b.true : undefined,
217
- node.optional ? b.true : undefined,
218
- ...node.arguments.map((arg) => context.visit(arg)),
219
- ),
220
- ),
221
- );
222
- }
223
- }
224
-
225
- return b.call(
226
- '$.with_scope',
227
- b.id('__block'),
228
- b.thunk({
229
- ...node,
230
- callee: context.visit(callee),
231
- arguments: node.arguments.map((arg) => context.visit(arg)),
232
- }),
233
- );
234
- },
235
-
236
- TSTypeAliasDeclaration(_, context) {
237
- if (!context.state.to_ts) {
238
- return b.empty;
239
- }
240
- context.next();
241
- },
242
-
243
- TSInterfaceDeclaration(_, context) {
244
- if (!context.state.to_ts) {
245
- return b.empty;
246
- }
247
- context.next();
248
- },
249
-
250
- TSMappedType(_, context) {
251
- if (!context.state.to_ts) {
252
- return b.empty;
253
- }
254
- context.next();
255
- },
256
-
257
- NewExpression(node, context) {
258
- const callee = node.callee;
259
- const parent = context.path.at(-1);
260
-
261
- if (context.state.metadata?.tracking === false) {
262
- context.state.metadata.tracking = true;
263
- }
264
-
265
- if (!is_inside_component(context, true) || is_inside_call_expression(context)) {
266
- return context.next();
267
- }
268
-
269
- if (is_value_static(node)) {
270
- return context.next();
271
- }
272
-
273
- return b.call(
274
- '$.with_scope',
275
- b.id('__block'),
276
- b.thunk({
277
- ...node,
278
- callee: context.visit(callee),
279
- arguments: node.arguments.map((arg) => context.visit(arg)),
280
- }),
281
- );
282
- },
283
-
284
- MemberExpression(node, context) {
285
- const parent = context.path.at(-1);
286
-
287
- if (parent.type !== 'AssignmentExpression') {
288
- const object = node.object;
289
- const property = node.property;
290
- const tracked_name =
291
- property.type === 'Identifier'
292
- ? is_tracked_name(property.name)
293
- : property.type === 'Literal' && is_tracked_name(property.value);
294
-
295
- // TODO should we enforce that the identifier is tracked too?
296
- if (
297
- (node.computed && is_tracked_computed_property(node.object, node.property, context)) ||
298
- tracked_name
299
- ) {
300
- if (context.state.metadata?.tracking === false) {
301
- context.state.metadata.tracking = true;
302
- }
303
-
304
- if (tracked_name) {
305
- return b.call(
306
- '$.get_property',
307
- context.visit(object),
308
- property.type === 'Identifier' ? b.literal(property.name) : property,
309
- node.optional ? b.true : undefined,
310
- );
311
- } else {
312
- return b.call(
313
- '$.get_property',
314
- context.visit(object),
315
- context.visit(property),
316
- node.optional ? b.true : undefined,
317
- );
318
- }
319
- }
320
-
321
- if (object.type === 'Identifier' && object.name === 'Object') {
322
- const binding = context.state.scope.get(object.name);
323
-
324
- if (binding === null) {
325
- if (property.type === 'Identifier' && property.name === 'values') {
326
- return b.id('$.object_values');
327
- } else if (property.type === 'Identifier' && property.name === 'entries') {
328
- return b.id('$.object_entries');
329
- } else if (property.type === 'Identifier' && property.name === 'keys') {
330
- return b.id('$.object_keys');
331
- }
332
- }
333
- }
334
- }
335
-
336
- if (node.object.type === 'MemberExpression' && node.object.optional) {
337
- const metadata = { tracking: false, await: false };
338
-
339
- const object = context.visit(node.object, { ...context.state, metadata });
340
-
341
- if (metadata.tracking) {
342
- if (context.state.metadata?.tracking === false) {
343
- context.state.metadata.tracking = true;
344
- }
345
-
346
- return {
347
- ...node,
348
- optional: true,
349
- object,
350
- property: context.visit(node.property),
351
- };
352
- }
353
- if (metadata.await) {
354
- if (context.state.metadata?.await === false) {
355
- context.state.metadata.await = true;
356
- }
357
- }
358
- } else {
359
- context.next();
360
- }
361
- },
362
-
363
- SpreadElement(node, context) {
364
- const parent = context.path.at(-1);
365
-
366
- if (parent.type === 'ObjectExpression') {
367
- return b.spread(b.call('$.spread_object', context.visit(node.argument)));
368
- }
369
-
370
- context.next();
371
- },
372
-
373
- VariableDeclaration(node, context) {
374
- const declarations = [];
375
-
376
- for (const declarator of node.declarations) {
377
- const metadata = declarator.metadata;
378
-
379
- if (declarator.id.type === 'Identifier') {
380
- const binding = context.state.scope.get(declarator.id.name);
381
-
382
- if (!context.state.to_ts) {
383
- delete declarator.id.typeAnnotation;
384
- }
385
-
386
- if (binding !== null && binding.kind === 'tracked') {
387
- let expression;
388
-
389
- if (context.state.to_ts) {
390
- // TypeScript mode: lighter transformation
391
- if (metadata.tracking && !metadata.await) {
392
- expression = b.call(
393
- '$.computed',
394
- b.thunk(context.visit(declarator.init)),
395
- b.id('__block'),
396
- );
397
- } else {
398
- expression = b.call(
399
- '$.tracked',
400
- declarator.init === null ? undefined : context.visit(declarator.init),
401
- b.id('__block'),
402
- );
403
- }
404
- } else {
405
- // Runtime mode: full transformation
406
- if (metadata.tracking && metadata.await) {
407
- expression = b.call(
408
- b.await(
409
- b.call(
410
- '$.resume_context',
411
- b.call(
412
- '$.async_computed',
413
- b.thunk(context.visit(declarator.init), true),
414
- b.id('__block'),
415
- ),
416
- ),
417
- ),
418
- );
419
- } else if (metadata.tracking && !metadata.await) {
420
- expression = b.call(
421
- '$.computed',
422
- b.thunk(context.visit(declarator.init)),
423
- b.id('__block'),
424
- );
425
- } else {
426
- expression = b.call(
427
- '$.tracked',
428
- declarator.init === null ? undefined : context.visit(declarator.init),
429
- b.id('__block'),
430
- );
431
- }
432
- }
433
-
434
- declarations.push(b.declarator(declarator.id, expression));
435
- } else {
436
- declarations.push(context.visit(declarator));
437
- }
438
- } else {
439
- const paths = extract_paths(declarator.id);
440
- const has_tracked = paths.some(
441
- (path) => path.node.type === 'Identifier' && is_tracked_name(path.node.name),
442
- );
443
-
444
- if (!context.state.to_ts) {
445
- delete declarator.id.typeAnnotation;
446
- }
447
-
448
- if (!has_tracked) {
449
- declarations.push(context.visit(declarator));
450
- continue;
451
- }
452
-
453
- // For TypeScript mode, we still need to transform tracked variables
454
- // but use a lighter approach that maintains type information
455
- if (context.state.to_ts) {
456
- const transformed = declarator.transformed || declarator.id;
457
- let expression;
458
-
459
- if (metadata.tracking && !metadata.await) {
460
- expression = b.call(
461
- '$.computed',
462
- b.thunk(context.visit(declarator.init)),
463
- b.id('__block'),
464
- );
465
- } else {
466
- // Simple tracked variable - always use $.tracked for $ prefixed variables
467
- expression = b.call('$.tracked', context.visit(declarator.init), b.id('__block'));
468
- }
469
-
470
- declarations.push(b.declarator(transformed, expression));
471
- continue;
472
- }
473
-
474
- const transformed = declarator.transformed;
475
- let expression;
476
-
477
- if (metadata.tracking && metadata.await) {
478
- // TODO
479
- debugger;
480
- } else if (metadata.tracking && !metadata.await) {
481
- expression = b.call(
482
- '$.computed',
483
- b.thunk(context.visit(declarator.init)),
484
- b.id('__block'),
485
- );
486
- } else {
487
- expression = context.visit(declarator.init);
488
- }
489
-
490
- declarations.push(b.declarator(transformed, expression));
491
- }
492
- }
493
-
494
- return { ...node, declarations };
495
- },
496
-
497
- FunctionDeclaration(node, context) {
498
- return visit_function(node, context);
499
- },
500
- ArrowFunctionExpression(node, context) {
501
- return visit_function(node, context);
502
- },
503
- FunctionExpression(node, context) {
504
- return visit_function(node, context);
505
- },
506
-
507
- Element(node, context) {
508
- const { state, visit } = context;
509
-
510
- const is_dom_element =
511
- node.id.type === 'Identifier' &&
512
- node.id.name[0].toLowerCase() === node.id.name[0] &&
513
- node.id.name[0] !== '$';
514
- const is_spreading = node.attributes.some((attr) => attr.type === 'SpreadAttribute');
515
- const spread_attributes = is_spreading ? [] : null;
516
-
517
- const handle_static_attr = (name, value) => {
518
- const attr_value = b.literal(
519
- ` ${name}${
520
- is_boolean_attribute(name) && value === true
521
- ? ''
522
- : `="${value === true ? '' : escape_html(value, true)}"`
523
- }`,
524
- );
525
-
526
- if (is_spreading) {
527
- // For spread attributes, store just the actual value, not the full attribute string
528
- const actual_value =
529
- is_boolean_attribute(name) && value === true
530
- ? b.literal(true)
531
- : b.literal(value === true ? '' : value);
532
- spread_attributes.push(b.prop('init', b.literal(name), actual_value));
533
- } else {
534
- state.template.push(attr_value);
535
- }
536
- };
537
-
538
- if (is_dom_element) {
539
- let class_attribute = null;
540
- const local_updates = [];
541
- const is_void = is_void_element(node.id.name);
542
-
543
- state.template.push(`<${node.id.name}`);
544
-
545
- for (const attr of node.attributes) {
546
- if (attr.type === 'Attribute') {
547
- if (attr.name.type === 'Identifier') {
548
- const name = attr.name.name;
549
-
550
- if (attr.value.type === 'Literal' && name !== 'class') {
551
- handle_static_attr(name, attr.value.value);
552
- continue;
553
- }
554
-
555
- if (name === 'class' || name === '$class') {
556
- class_attribute = attr;
557
-
558
- continue;
559
- }
560
-
561
- if (name === 'value' || name === '$value') {
562
- const id = state.flush_node();
563
- const expression = visit(attr.value, state);
564
-
565
- if (name === '$value') {
566
- local_updates.push(b.stmt(b.call('$.set_value', id, expression)));
567
- } else {
568
- state.init.push(b.stmt(b.call('$.set_value', id, expression)));
569
- }
570
-
571
- continue;
572
- }
573
-
574
- if (name === 'checked' || name === '$checked') {
575
- const id = state.flush_node();
576
- const expression = visit(attr.value, state);
577
-
578
- if (name === '$checked') {
579
- local_updates.push(b.stmt(b.call('$.set_checked', id, expression)));
580
- } else {
581
- state.init.push(b.stmt(b.call('$.set_checked', id, expression)));
582
- }
583
- continue;
584
- }
585
-
586
- if (name === 'selected' || name === '$selected') {
587
- const id = state.flush_node();
588
- const expression = visit(attr.value, state);
589
-
590
- if (name === '$selected') {
591
- local_updates.push(b.stmt(b.call('$.set_selected', id, expression)));
592
- } else {
593
- state.init.push(b.stmt(b.call('$.set_selected', id, expression)));
594
- }
595
- continue;
596
- }
597
-
598
- if (is_event_attribute(name)) {
599
- let capture = name.endsWith('Capture');
600
- let event_name = capture
601
- ? name.slice(2, -7).toLowerCase()
602
- : name.slice(2).toLowerCase();
603
- let handler = visit(attr.value, state);
604
-
605
- if (attr.metadata?.delegated) {
606
- let delegated_assignment;
607
-
608
- if (!state.events.has(event_name)) {
609
- state.events.add(event_name);
610
- }
611
-
612
- // Hoist function if we can, otherwise we leave the function as is
613
- if (attr.metadata.delegated.hoisted) {
614
- if (attr.metadata.delegated.function === attr.value) {
615
- const func_name = state.scope.root.unique('on_' + event_name);
616
- state.hoisted.push(b.var(func_name, handler));
617
- handler = func_name;
618
- }
619
-
620
- const hoisted_params = /** @type {Expression[]} */ (
621
- attr.metadata.delegated.function.metadata.hoisted_params
622
- );
623
-
624
- const args = [handler, b.id('__block'), ...hoisted_params];
625
- delegated_assignment = b.array(args);
626
- } else if (
627
- handler.type === 'Identifier' &&
628
- is_declared_within_component(handler, context)
629
- ) {
630
- delegated_assignment = handler;
631
- } else {
632
- delegated_assignment = b.array([handler, b.id('__block')]);
633
- }
634
- const id = state.flush_node();
635
-
636
- state.init.push(
637
- b.stmt(b.assignment('=', b.member(id, '__' + event_name), delegated_assignment)),
638
- );
639
- } else {
640
- const passive = is_passive_event(event_name);
641
- const id = state.flush_node();
642
-
643
- state.init.push(
644
- b.stmt(
645
- b.call(
646
- '$.event',
647
- b.literal(event_name),
648
- id,
649
- handler,
650
- capture && b.true,
651
- passive === undefined ? undefined : b.literal(passive),
652
- ),
653
- ),
654
- );
655
- }
656
-
657
- continue;
658
- }
659
-
660
- // All other attributes
661
- if (is_tracked_name(name)) {
662
- const attribute = name.slice(1);
663
- const id = state.flush_node();
664
- const expression = visit(attr.value, state);
665
-
666
- if (is_dom_property(attribute)) {
667
- local_updates.push(b.stmt(b.assignment('=', b.member(id, attribute), expression)));
668
- } else {
669
- local_updates.push(
670
- b.stmt(b.call('$.set_attribute', id, b.literal(attribute), expression)),
671
- );
672
- }
673
- } else {
674
- const id = state.flush_node();
675
- const expression = visit(attr.value, state);
676
-
677
- if (is_dom_property(name)) {
678
- state.init.push(b.stmt(b.assignment('=', b.member(id, name), expression)));
679
- } else {
680
- state.init.push(b.stmt(b.call('$.set_attribute', id, b.literal(name), expression)));
681
- }
682
- }
683
- }
684
- } else if (attr.type === 'SpreadAttribute') {
685
- spread_attributes.push(b.spread(b.call('$.spread_object', visit(attr.argument, state))));
686
- } else if (attr.type === 'RefAttribute') {
687
- const id = state.flush_node();
688
- state.init.push(b.stmt(b.call('$.ref', id, b.thunk(visit(attr.argument, state)))));
689
- }
690
- }
691
-
692
- if (class_attribute !== null) {
693
- if (class_attribute.value.type === 'Literal') {
694
- let value = class_attribute.value.value;
695
-
696
- if (node.metadata.scoped && state.component.css) {
697
- value = `${state.component.css.hash} ${value}`;
698
- }
699
-
700
- handle_static_attr(class_attribute.name.name, value);
701
- } else {
702
- const id = state.flush_node();
703
- let expression = visit(class_attribute.value, state);
704
-
705
- if (node.metadata.scoped && state.component.css) {
706
- expression = b.binary('+', b.literal(state.component.css.hash + ' '), expression);
707
- }
708
-
709
- if (class_attribute.name.name === '$class') {
710
- local_updates.push(b.stmt(b.call('$.set_class', id, expression)));
711
- } else {
712
- state.init.push(b.stmt(b.call('$.set_class', id, expression)));
713
- }
714
- }
715
- } else if (node.metadata.scoped && state.component.css) {
716
- const value = state.component.css.hash;
717
-
718
- handle_static_attr('class', value);
719
- }
720
-
721
- state.template.push('>');
722
-
723
- if (spread_attributes !== null && spread_attributes.length > 0) {
724
- const id = state.flush_node();
725
- state.init.push(
726
- b.stmt(b.call('$.render_spread', id, b.thunk(b.object(spread_attributes)))),
727
- );
728
- }
729
-
730
- const init = [];
731
- const update = [];
732
-
733
- if (!is_void) {
734
- transform_children(node.children, {
735
- visit,
736
- state: { ...state, init, update },
737
- root: false,
738
- });
739
- state.template.push(`</${node.id.name}>`);
740
- }
741
-
742
- update.push(...local_updates);
743
-
744
- if (init.length > 0) {
745
- state.init.push(b.block(init));
746
- }
747
-
748
- if (update.length > 0) {
749
- state.init.push(b.stmt(b.call('$.render', b.thunk(b.block(update)))));
750
- }
751
- } else {
752
- const id = state.flush_node();
753
-
754
- state.template.push('<!>');
755
-
756
- const is_spreading = node.attributes.some((attr) => attr.type === 'SpreadAttribute');
757
- const tracked = [];
758
- const props = [];
759
- let children_prop = null;
760
-
761
- for (const attr of node.attributes) {
762
- if (attr.type === 'Attribute') {
763
- if (attr.name.type === 'Identifier' && is_tracked_name(attr.name.name)) {
764
- const metadata = { tracking: false, await: false };
765
- let property = visit(attr.value, { ...state, metadata });
766
-
767
- tracked.push(b.literal(attr.name.name));
768
-
769
- if (metadata.tracking) {
770
- const thunk = b.thunk(property);
771
- property = b.call('$.computed_property', thunk, b.id('__block'));
772
-
773
- if (attr.name.name === '$children') {
774
- children_prop = thunk;
775
- }
776
- }
777
-
778
- props.push(b.prop('init', attr.name, property));
779
- } else {
780
- props.push(b.prop('init', attr.name, visit(attr.value, state)));
781
- }
782
- } else if (attr.type === 'SpreadAttribute') {
783
- props.push(
784
- b.spread(
785
- b.call(
786
- '$.spread_object',
787
- visit(attr.argument, { ...state, metadata: { ...state.metadata, spread: true } }),
788
- ),
789
- ),
790
- );
791
- } else if (attr.type === 'RefAttribute') {
792
- props.push(b.prop('init', b.call('$.ref_prop'), visit(attr.argument, state), true));
793
- } else if (attr.type === 'AccessorAttribute') {
794
- // # means it's an accessor to the runtime
795
- tracked.push(b.literal('#' + attr.name.name));
796
- let get_expr;
797
-
798
- if (
799
- attr.get.type === 'FunctionExpression' ||
800
- attr.get.type === 'ArrowFunctionExpression'
801
- ) {
802
- get_expr = context.state.scope.generate(attr.name.name + '_get');
803
-
804
- state.init.push(b.const(get_expr, visit(attr.get, state)));
805
- } else {
806
- get_expr = visit(attr.get, state);
807
- }
808
-
809
- props.push(
810
- b.prop('get', attr.name, b.function(null, [], b.block([b.return(b.call(get_expr))]))),
811
- );
812
-
813
- if (attr.set) {
814
- let set_expr;
815
-
816
- if (
817
- attr.set.type === 'FunctionExpression' ||
818
- attr.set.type === 'ArrowFunctionExpression'
819
- ) {
820
- set_expr = context.state.scope.generate(attr.name.name + '_set');
821
-
822
- state.init.push(b.const(set_expr, visit(attr.set, state)));
823
- } else {
824
- set_expr = visit(attr.set, state);
825
- }
826
-
827
- props.push(
828
- b.prop(
829
- 'set',
830
- attr.name,
831
- b.function(
832
- null,
833
- [b.id('__value')],
834
- b.block([b.return(b.call(set_expr, b.id('__value')))]),
835
- ),
836
- ),
837
- );
838
- }
839
- } else {
840
- throw new Error('TODO');
841
- }
842
- }
843
-
844
- const children_filtered = [];
845
-
846
- for (const child of node.children) {
847
- if (child.type === 'Component') {
848
- const id = child.id;
849
- props.push(b.prop('init', id, visit(child, state)));
850
- } else {
851
- children_filtered.push(child);
852
- }
853
- }
854
-
855
- if (children_filtered.length > 0) {
856
- const component_scope = context.state.scopes.get(node);
857
- const children = visit(b.component(b.id('$children'), [], children_filtered), {
858
- ...context.state,
859
- scope: component_scope,
860
- });
861
-
862
- if (children_prop) {
863
- children_prop.body = b.logical('??', children_prop.body, children);
864
- } else {
865
- props.push(b.prop('init', b.id('$children'), children));
866
- }
867
- }
868
-
869
- if (is_spreading) {
870
- state.init.push(
871
- b.stmt(
872
- b.call(
873
- node.id,
874
- id,
875
- b.call('$.tracked_spread_object', b.thunk(b.object(props))),
876
- b.id('$.active_block'),
877
- ),
878
- ),
879
- );
880
- } else if (tracked.length > 0) {
881
- state.init.push(
882
- b.stmt(
883
- b.call(
884
- node.id,
885
- id,
886
- b.call('$.tracked_object', b.object(props), b.array(tracked), b.id('__block')),
887
- b.id('$.active_block'),
888
- ),
889
- ),
890
- );
891
- } else {
892
- state.init.push(
893
- b.stmt(b.call(visit(node.id, state), id, b.object(props), b.id('$.active_block'))),
894
- );
895
- }
896
- }
897
- },
898
-
899
- Fragment(node, context) {
900
- if (!context.state.to_ts) {
901
- if (!context.state.imports.has(`import * as $ from 'ripple/internal/client'`)) {
902
- context.state.imports.add(`import * as $ from 'ripple/internal/client'`);
903
- }
904
- }
905
-
906
- const metadata = { await: false };
907
-
908
- const body_statements = transform_body(node.body, {
909
- ...context,
910
- state: { ...context.state, component: node, metadata },
911
- });
912
-
913
- return b.function(
914
- node.id,
915
- [b.id('__anchor'), ...node.params.map((param) => context.visit(param, context.state))],
916
- b.block(
917
- metadata.await
918
- ? [b.stmt(b.call('$.async', b.thunk(b.block(body_statements), true)))]
919
- : body_statements,
920
- ),
921
- );
922
- },
923
-
924
- Component(node, context) {
925
- let prop_statements;
926
-
927
- if (!context.state.to_ts) {
928
- if (!context.state.imports.has(`import * as $ from 'ripple/internal/client'`)) {
929
- context.state.imports.add(`import * as $ from 'ripple/internal/client'`);
930
- }
931
- }
932
-
933
- const metadata = { await: false };
934
-
935
- if (context.state.to_ts) {
936
- const body_statements = [
937
- ...transform_body(node.body, {
938
- ...context,
939
- state: { ...context.state, component: node, metadata },
940
- }),
941
- ];
942
-
943
- return b.function(node.id, node.params, b.block(body_statements));
944
- }
945
-
946
- let props = b.id('__props');
947
-
948
- if (node.params.length > 0) {
949
- let props_param = node.params[0];
950
-
951
- if (props_param.type === 'Identifier') {
952
- delete props_param.typeAnnotation;
953
- props = props_param;
954
- } else if (props_param.type === 'ObjectPattern') {
955
- const paths = extract_paths(props_param);
956
-
957
- for (const path of paths) {
958
- const name = path.node.name;
959
- const binding = context.state.scope.get(name);
960
- const key = b.key(name);
961
-
962
- if (binding !== null && !is_tracked_name(name)) {
963
- if (prop_statements === undefined) {
964
- prop_statements = [];
965
- }
966
- if (path.has_default_value) {
967
- const fallback = path.expression(b.id('__props'));
968
-
969
- prop_statements.push(b.var(name, context.visit(fallback)));
970
- } else {
971
- prop_statements.push(b.var(name, b.member(b.id('__props'), key)));
972
- }
973
- } else if (binding !== null && path.has_default_value) {
974
- if (prop_statements === undefined) {
975
- prop_statements = [];
976
- }
977
- const fallback = path.expression(b.id('__props'));
978
-
979
- prop_statements.push(
980
- b.var(name, b.call('$.computed', b.thunk(context.visit(fallback)), b.id('__block'))),
981
- );
982
- }
983
- }
984
- }
985
- }
986
-
987
- const body_statements = [
988
- b.stmt(b.call('$.push_component')),
989
- ...transform_body(node.body, {
990
- ...context,
991
- state: { ...context.state, component: node, metadata },
992
- }),
993
- b.stmt(b.call('$.pop_component')),
994
- ];
995
-
996
- if (node.css !== null && node.css) {
997
- context.state.stylesheets.push(node.css);
998
- }
999
-
1000
- return b.function(
1001
- node.id,
1002
- node.params.length > 0
1003
- ? [b.id('__anchor'), props, b.id('__block')]
1004
- : [b.id('__anchor'), b.id('_'), b.id('__block')],
1005
- b.block([
1006
- ...(prop_statements ?? []),
1007
- ...(metadata.await
1008
- ? [b.stmt(b.call('$.async', b.thunk(b.block(body_statements), true)))]
1009
- : body_statements),
1010
- ]),
1011
- );
1012
- },
1013
-
1014
- AssignmentExpression(node, context) {
1015
- if (context.state.to_ts) {
1016
- return context.next();
1017
- }
1018
-
1019
- const left = node.left;
1020
-
1021
- if (left.type === 'MemberExpression') {
1022
- // need to capture setting length of array to throw a runtime error
1023
- if (
1024
- left.property.type === 'Identifier' &&
1025
- (is_tracked_name(left.property.name) || left.property.name === 'length')
1026
- ) {
1027
- if (left.property.name !== '$length') {
1028
- return b.call(
1029
- '$.set_property',
1030
- context.visit(left.object),
1031
- left.computed ? context.visit(left.property) : b.literal(left.property.name),
1032
- visit_assignment_expression(node, context, build_assignment) ?? context.next(),
1033
- b.id('__block'),
1034
- );
1035
- }
1036
- } else if (!is_tracked_computed_property(left.object, left.property, context)) {
1037
- return context.next();
1038
- }
1039
- }
1040
-
1041
- const visited = visit_assignment_expression(node, context, build_assignment) ?? context.next();
1042
-
1043
- if (
1044
- left.type === 'MemberExpression' &&
1045
- left.property.type === 'Identifier' &&
1046
- left.property.name === '$length' &&
1047
- !left.computed
1048
- ) {
1049
- return b.call('$.with_scope', b.id('__block'), b.thunk(visited));
1050
- }
1051
-
1052
- return visited;
1053
- },
1054
-
1055
- UpdateExpression(node, context) {
1056
- if (context.state.to_ts) {
1057
- context.next();
1058
- return;
1059
- }
1060
- const argument = node.argument;
1061
-
1062
- if (
1063
- argument.type === 'MemberExpression' &&
1064
- ((argument.property.type === 'Identifier' && is_tracked_name(argument.property.name)) ||
1065
- (argument.computed &&
1066
- is_tracked_computed_property(argument.object, argument.property, context)))
1067
- ) {
1068
- return b.call(
1069
- node.prefix ? '$.update_pre_property' : '$.update_property',
1070
- context.visit(argument.object),
1071
- argument.computed ? context.visit(argument.property) : b.literal(argument.property.name),
1072
- b.id('__block'),
1073
- node.operator === '--' ? b.literal(-1) : undefined,
1074
- );
1075
- }
1076
-
1077
- const left = object(argument);
1078
- const binding = context.state.scope.get(left.name);
1079
- const transformers = left && binding?.transform;
1080
-
1081
- if (left === argument) {
1082
- const update_fn = transformers?.update || transformers?.update_tracked;
1083
- if (update_fn) {
1084
- return update_fn(node);
1085
- }
1086
- }
1087
-
1088
- context.next();
1089
- },
1090
-
1091
- ObjectExpression(node, context) {
1092
- const properties = [];
1093
- const tracked = [];
1094
-
1095
- for (const property of node.properties) {
1096
- if (
1097
- property.type === 'Property' &&
1098
- !property.computed &&
1099
- property.key.type === 'Identifier' &&
1100
- property.kind === 'init' &&
1101
- is_tracked_name(property.key.name)
1102
- ) {
1103
- tracked.push(b.literal(property.key.name));
1104
- const metadata = { tracking: false, await: false };
1105
- const tracked_property = context.visit(property, { ...context.state, metadata });
1106
-
1107
- if (metadata.tracking) {
1108
- properties.push({
1109
- ...tracked_property,
1110
- value: b.call('$.computed_property', b.thunk(tracked_property.value), b.id('__block')),
1111
- });
1112
- } else {
1113
- properties.push(tracked_property);
1114
- }
1115
- } else {
1116
- properties.push(context.visit(property));
1117
- }
1118
- }
1119
-
1120
- if (tracked.length > 0) {
1121
- return b.call('$.tracked_object', { ...node, properties }, b.array(tracked), b.id('__block'));
1122
- }
1123
-
1124
- context.next();
1125
- },
1126
-
1127
- ArrayExpression(node, context) {
1128
- // TODO we can bail out of all of this if we know we're inside a computed fn expression
1129
- // as the reactivity will hold from the reference of the $ binding itself
1130
- const elements = [];
1131
- const tracked = [];
1132
- let i = 0;
1133
-
1134
- for (const element of node.elements) {
1135
- if (element === null) {
1136
- elements.push(null);
1137
- } else if (element.type === 'Identifier' && is_tracked_name(element.name)) {
1138
- const metadata = { tracking: false, await: false };
1139
- const tracked_identifier = context.visit(element, { ...context.state, metadata });
1140
-
1141
- if (metadata.tracking) {
1142
- tracked.push(b.literal(i));
1143
- elements.push(
1144
- b.call('$.computed_property', b.thunk(tracked_identifier), b.id('__block')),
1145
- );
1146
- } else {
1147
- elements.push(tracked_identifier);
1148
- }
1149
- } else {
1150
- const metadata = { tracking: false, await: false };
1151
- elements.push(context.visit(element, { ...context.state, metadata }));
1152
- }
1153
- i++;
1154
- }
1155
-
1156
- if (tracked.length > 0) {
1157
- return b.call('$.tracked_object', { ...node, elements }, b.array(tracked), b.id('__block'));
1158
- }
1159
-
1160
- context.next();
1161
- },
1162
-
1163
- ForOfStatement(node, context) {
1164
- if (!is_inside_component(context)) {
1165
- context.next();
1166
- return;
1167
- }
1168
- const is_controlled = node.is_controlled;
1169
-
1170
- // do only if not controller
1171
- if (!is_controlled) {
1172
- context.state.template.push('<!>');
1173
- }
1174
-
1175
- const id = context.state.flush_node(is_controlled);
1176
- const pattern = node.left.declarations[0].id;
1177
- const body_scope = context.state.scopes.get(node.body);
1178
-
1179
- context.state.init.push(
1180
- b.stmt(
1181
- b.call(
1182
- '$.for',
1183
- id,
1184
- b.thunk(context.visit(node.right)),
1185
- b.arrow(
1186
- [b.id('__anchor'), pattern],
1187
- b.block(
1188
- transform_body(node.body.body, {
1189
- ...context,
1190
- state: { ...context.state, scope: body_scope },
1191
- }),
1192
- ),
1193
- ),
1194
- b.literal(is_controlled ? IS_CONTROLLED : 0),
1195
- ),
1196
- ),
1197
- );
1198
- },
1199
-
1200
- IfStatement(node, context) {
1201
- if (!is_inside_component(context)) {
1202
- context.next();
1203
- return;
1204
- }
1205
- context.state.template.push('<!>');
1206
-
1207
- const id = context.state.flush_node();
1208
- const statements = [];
1209
-
1210
- const consequent_scope = context.state.scopes.get(node.consequent);
1211
- const consequent = b.block(
1212
- transform_body(node.consequent.body, {
1213
- ...context,
1214
- state: { ...context.state, scope: consequent_scope },
1215
- }),
1216
- );
1217
- const consequent_id = context.state.scope.generate('consequent');
1218
-
1219
- statements.push(b.var(b.id(consequent_id), b.arrow([b.id('__anchor')], consequent)));
1220
-
1221
- let alternate_id;
1222
-
1223
- if (node.alternate !== null) {
1224
- const alternate_scope = context.state.scopes.get(node.alternate) || context.state.scope;
1225
- let alternate_body = node.alternate.body;
1226
- if (node.alternate.type === 'IfStatement') {
1227
- alternate_body = [node.alternate];
1228
- }
1229
- const alternate = b.block(
1230
- transform_body(alternate_body, {
1231
- ...context,
1232
- state: { ...context.state, scope: alternate_scope },
1233
- }),
1234
- );
1235
- alternate_id = context.state.scope.generate('alternate');
1236
- statements.push(b.var(b.id(alternate_id), b.arrow([b.id('__anchor')], alternate)));
1237
- }
1238
-
1239
- statements.push(
1240
- b.stmt(
1241
- b.call(
1242
- '$.if',
1243
- id,
1244
- b.arrow(
1245
- [b.id('__render')],
1246
- b.block([
1247
- b.if(
1248
- context.visit(node.test),
1249
- b.stmt(b.call(b.id('__render'), b.id(consequent_id))),
1250
- alternate_id
1251
- ? b.stmt(
1252
- b.call(
1253
- b.id('__render'),
1254
- b.id(alternate_id),
1255
- node.alternate ? b.literal(false) : undefined,
1256
- ),
1257
- )
1258
- : undefined,
1259
- ),
1260
- ]),
1261
- ),
1262
- ),
1263
- ),
1264
- );
1265
-
1266
- context.state.init.push(b.block(statements));
1267
- },
1268
-
1269
- TryStatement(node, context) {
1270
- if (!is_inside_component(context)) {
1271
- context.next();
1272
- return;
1273
- }
1274
- context.state.template.push('<!>');
1275
-
1276
- const id = context.state.flush_node();
1277
- const metadata = { await: false };
1278
- let body = transform_body(node.block.body, {
1279
- ...context,
1280
- state: { ...context.state, metadata },
1281
- });
1282
-
1283
- if (metadata.await) {
1284
- body = [b.stmt(b.call('$.async', b.thunk(b.block(body), true)))];
1285
- }
1286
-
1287
- context.state.init.push(
1288
- b.stmt(
1289
- b.call(
1290
- '$.try',
1291
- id,
1292
- b.arrow([b.id('__anchor')], b.block(body)),
1293
- node.handler === null
1294
- ? b.literal(null)
1295
- : b.arrow(
1296
- [b.id('__anchor'), ...(node.handler.param ? [node.handler.param] : [])],
1297
- b.block(transform_body(node.handler.body.body, context)),
1298
- ),
1299
- node.async === null
1300
- ? undefined
1301
- : b.arrow([b.id('__anchor')], b.block(transform_body(node.async.body, context))),
1302
- ),
1303
- ),
1304
- );
1305
- },
1306
-
1307
- AwaitExpression(node, context) {
1308
- if (!is_inside_component(context)) {
1309
- context.next();
1310
- }
1311
-
1312
- if (context.state.metadata?.await === false) {
1313
- context.state.metadata.await = true;
1314
- }
1315
-
1316
- return b.call(b.await(b.call('$.resume_context', context.visit(node.argument))));
1317
- },
1318
-
1319
- BinaryExpression(node, context) {
1320
- return b.binary(node.operator, context.visit(node.left), context.visit(node.right));
1321
- },
1322
-
1323
- TemplateLiteral(node, context) {
1324
- if (node.expressions.length === 0) {
1325
- return b.literal(node.quasis[0].value.cooked);
1326
- }
1327
-
1328
- const expressions = node.expressions.map((expr) => context.visit(expr));
1329
- return b.template(node.quasis, expressions);
1330
- },
1331
-
1332
- RenderFragment(node, context) {
1333
- const identifer = node.expression.callee;
1334
-
1335
- context.state.template.push('<!>');
1336
-
1337
- const id = context.state.flush_node();
1338
-
1339
- context.state.init.push(
1340
- b.stmt(
1341
- b.call(
1342
- context.visit(identifer),
1343
- id,
1344
- ...node.expression.arguments.map((arg) => context.visit(arg, context.state)),
1345
- ),
1346
- ),
1347
- );
1348
- },
1349
-
1350
- BlockStatement(node, context) {
1351
- const statements = [];
1352
-
1353
- for (const statement of node.body) {
1354
- statements.push(context.visit(statement));
1355
- }
1356
-
1357
- return b.block(statements);
1358
- },
1359
-
1360
- Program(node, context) {
1361
- const statements = [];
1362
-
1363
- for (const statement of node.body) {
1364
- statements.push(context.visit(statement));
1365
- }
1366
-
1367
- return { ...node, body: statements };
1368
- },
110
+ _: function set_scope(node, { next, state }) {
111
+ const scope = state.scopes.get(node);
112
+
113
+ if (scope && scope !== state.scope) {
114
+ return next({ ...state, scope });
115
+ } else {
116
+ return next();
117
+ }
118
+ },
119
+
120
+ Identifier(node, context) {
121
+ const parent = /** @type {Node} */ (context.path.at(-1));
122
+
123
+ if (is_reference(node, parent) && !context.state.to_ts) {
124
+ const binding = context.state.scope.get(node.name);
125
+ if (
126
+ (context.state.metadata?.tracking === false ||
127
+ (parent.type !== 'AssignmentExpression' && parent.type !== 'UpdateExpression')) &&
128
+ (is_tracked_name(node.name) ||
129
+ node.tracked ||
130
+ binding?.kind === 'prop' ||
131
+ binding?.kind === 'prop_fallback') &&
132
+ binding?.node !== node
133
+ ) {
134
+ if (context.state.metadata?.tracking === false) {
135
+ context.state.metadata.tracking = true;
136
+ }
137
+ if (node.tracked) {
138
+ return b.call('$.get', build_getter(node, context));
139
+ }
140
+ }
141
+
142
+ if (node.name === 'structuredClone' && binding === null) {
143
+ return b.id('$.structured_clone');
144
+ }
145
+
146
+ return build_getter(node, context);
147
+ }
148
+ },
149
+
150
+ ImportDeclaration(node, context) {
151
+ if (!context.state.to_ts && node.importKind === 'type') {
152
+ return b.empty;
153
+ }
154
+
155
+ return {
156
+ ...node,
157
+ specifiers: node.specifiers
158
+ .filter((spec) => spec.importKind !== 'type')
159
+ .map((spec) => context.visit(spec)),
160
+ };
161
+ },
162
+
163
+ CallExpression(node, context) {
164
+ const callee = node.callee;
165
+ const parent = context.path.at(-1);
166
+
167
+ if (context.state.metadata?.tracking === false) {
168
+ context.state.metadata.tracking = true;
169
+ }
170
+
171
+ if (
172
+ !context.state.to_ts &&
173
+ callee.type === 'Identifier' &&
174
+ callee.name === 'track' &&
175
+ is_ripple_import(callee, context)
176
+ ) {
177
+ return {
178
+ ...node,
179
+ arguments: [...node.arguments.map((arg) => context.visit(arg)), b.id('__block')],
180
+ };
181
+ }
182
+
183
+ if (
184
+ !is_inside_component(context, true) ||
185
+ context.state.to_ts ||
186
+ (parent?.type === 'MemberExpression' && parent.property === node) ||
187
+ is_inside_call_expression(context) ||
188
+ !context.path.some((node) => node.type === 'Component') ||
189
+ (is_ripple_import(callee, context) &&
190
+ (callee.type !== 'Identifier' ||
191
+ (callee.name !== 'array' && callee.name !== 'deferred'))) ||
192
+ is_declared_function_within_component(callee, context)
193
+ ) {
194
+ return context.next();
195
+ }
196
+
197
+ // Handle array methods that access the array
198
+ if (callee.type === 'MemberExpression') {
199
+ const property = callee.property;
200
+
201
+ if (property.type === 'Identifier' && !callee.optional) {
202
+ const name = property.name;
203
+ if (
204
+ // TODO support the missing array methods
205
+ name === 'reduce' ||
206
+ name === 'map' ||
207
+ name === 'forEach' ||
208
+ name === 'join' ||
209
+ name === 'includes' ||
210
+ name === 'indexOf' ||
211
+ name === 'lastIndexOf' ||
212
+ name === 'filter' ||
213
+ name === 'every' ||
214
+ name === 'some' ||
215
+ name === 'toSpliced' ||
216
+ name === 'toSorted' ||
217
+ name === 'toString' ||
218
+ name === 'values' ||
219
+ name === 'entries'
220
+ ) {
221
+ return b.call(
222
+ '$.with_scope',
223
+ b.id('__block'),
224
+ b.thunk(
225
+ b.call(
226
+ '$.array_' + name,
227
+ context.visit(callee.object),
228
+ ...node.arguments.map((arg) => context.visit(arg)),
229
+ ),
230
+ ),
231
+ );
232
+ }
233
+ }
234
+
235
+ if (callee.computed) {
236
+ return b.call(
237
+ '$.with_scope',
238
+ b.id('__block'),
239
+ b.thunk(
240
+ b.call(
241
+ '$.call_property',
242
+ context.visit(callee.object),
243
+ context.visit(property),
244
+ callee.optional ? b.true : undefined,
245
+ node.optional ? b.true : undefined,
246
+ ...node.arguments.map((arg) => context.visit(arg)),
247
+ ),
248
+ ),
249
+ );
250
+ }
251
+ }
252
+
253
+ return b.call(
254
+ '$.with_scope',
255
+ b.id('__block'),
256
+ b.thunk({
257
+ ...node,
258
+ callee: context.visit(callee),
259
+ arguments: node.arguments.map((arg) => context.visit(arg)),
260
+ }),
261
+ );
262
+ },
263
+
264
+ TSTypeAliasDeclaration(_, context) {
265
+ if (!context.state.to_ts) {
266
+ return b.empty;
267
+ }
268
+ context.next();
269
+ },
270
+
271
+ TSInterfaceDeclaration(_, context) {
272
+ if (!context.state.to_ts) {
273
+ return b.empty;
274
+ }
275
+ context.next();
276
+ },
277
+
278
+ TSMappedType(_, context) {
279
+ if (!context.state.to_ts) {
280
+ return b.empty;
281
+ }
282
+ context.next();
283
+ },
284
+
285
+ NewExpression(node, context) {
286
+ const callee = node.callee;
287
+ const parent = context.path.at(-1);
288
+
289
+ if (context.state.metadata?.tracking === false) {
290
+ context.state.metadata.tracking = true;
291
+ }
292
+
293
+ if (!is_inside_component(context, true) || is_inside_call_expression(context)) {
294
+ return context.next();
295
+ }
296
+
297
+ if (is_value_static(node)) {
298
+ return context.next();
299
+ }
300
+
301
+ return b.call(
302
+ '$.with_scope',
303
+ b.id('__block'),
304
+ b.thunk({
305
+ ...node,
306
+ callee: context.visit(callee),
307
+ arguments: node.arguments.map((arg) => context.visit(arg)),
308
+ }),
309
+ );
310
+ },
311
+
312
+ MemberExpression(node, context) {
313
+ const parent = context.path.at(-1);
314
+
315
+ if (node.property.type === 'Identifier' && node.property.tracked) {
316
+ add_ripple_internal_import(context);
317
+
318
+ context.state.metadata.tracking = true;
319
+ return b.call(
320
+ '$.get_property',
321
+ context.visit(node.object),
322
+ node.computed ? context.visit(node.property) : b.literal(node.property.name),
323
+ node.optional ? b.true : undefined,
324
+ );
325
+ }
326
+
327
+ if (parent.type !== 'AssignmentExpression') {
328
+ const object = node.object;
329
+ const property = node.property;
330
+ const tracked_name =
331
+ property.type === 'Identifier'
332
+ ? is_tracked_name(property.name)
333
+ : property.type === 'Literal' && is_tracked_name(property.value);
334
+
335
+ // TODO should we enforce that the identifier is tracked too?
336
+ if (
337
+ (node.computed && is_tracked_computed_property(node.object, node.property, context)) ||
338
+ tracked_name
339
+ ) {
340
+ if (context.state.metadata?.tracking === false) {
341
+ context.state.metadata.tracking = true;
342
+ }
343
+
344
+ if (tracked_name) {
345
+ return b.call(
346
+ '$.old_get_property',
347
+ context.visit(object),
348
+ property.type === 'Identifier' ? b.literal(property.name) : property,
349
+ node.optional ? b.true : undefined,
350
+ );
351
+ } else {
352
+ return b.call(
353
+ '$.old_get_property',
354
+ context.visit(object),
355
+ context.visit(property),
356
+ node.optional ? b.true : undefined,
357
+ );
358
+ }
359
+ }
360
+
361
+ if (object.type === 'Identifier' && object.name === 'Object') {
362
+ const binding = context.state.scope.get(object.name);
363
+
364
+ if (binding === null) {
365
+ if (property.type === 'Identifier' && property.name === 'values') {
366
+ return b.id('$.object_values');
367
+ } else if (property.type === 'Identifier' && property.name === 'entries') {
368
+ return b.id('$.object_entries');
369
+ } else if (property.type === 'Identifier' && property.name === 'keys') {
370
+ return b.id('$.object_keys');
371
+ }
372
+ }
373
+ }
374
+ }
375
+
376
+ if (node.object.type === 'MemberExpression' && node.object.optional) {
377
+ const metadata = { tracking: false, await: false };
378
+
379
+ const object = context.visit(node.object, { ...context.state, metadata });
380
+
381
+ if (metadata.tracking) {
382
+ if (context.state.metadata?.tracking === false) {
383
+ context.state.metadata.tracking = true;
384
+ }
385
+
386
+ return {
387
+ ...node,
388
+ optional: true,
389
+ object,
390
+ property: context.visit(node.property),
391
+ };
392
+ }
393
+ if (metadata.await) {
394
+ if (context.state.metadata?.await === false) {
395
+ context.state.metadata.await = true;
396
+ }
397
+ }
398
+ } else {
399
+ context.next();
400
+ }
401
+ },
402
+
403
+ SpreadElement(node, context) {
404
+ const parent = context.path.at(-1);
405
+
406
+ if (parent.type === 'ObjectExpression') {
407
+ return b.spread(b.call('$.spread_object', context.visit(node.argument)));
408
+ }
409
+
410
+ context.next();
411
+ },
412
+
413
+ VariableDeclaration(node, context) {
414
+ const declarations = [];
415
+
416
+ for (const declarator of node.declarations) {
417
+ const metadata = declarator.metadata;
418
+
419
+ if (declarator.id.type === 'Identifier') {
420
+ const binding = context.state.scope.get(declarator.id.name);
421
+
422
+ if (!context.state.to_ts) {
423
+ delete declarator.id.typeAnnotation;
424
+ }
425
+
426
+ if (binding !== null && binding.kind === 'tracked') {
427
+ let expression;
428
+
429
+ if (context.state.to_ts) {
430
+ // TypeScript mode: lighter transformation
431
+ if (metadata.tracking && !metadata.await) {
432
+ expression = b.call(
433
+ '$.derived',
434
+ b.thunk(context.visit(declarator.init)),
435
+ b.id('__block'),
436
+ );
437
+ } else {
438
+ expression = b.call(
439
+ '$.tracked',
440
+ declarator.init === null ? undefined : context.visit(declarator.init),
441
+ b.id('__block'),
442
+ );
443
+ }
444
+ } else {
445
+ // Runtime mode: full transformation
446
+ if (metadata.tracking && metadata.await) {
447
+ expression = b.call(
448
+ b.await(
449
+ b.call(
450
+ '$.resume_context',
451
+ b.call(
452
+ '$.async_computed',
453
+ b.thunk(context.visit(declarator.init), true),
454
+ b.id('__block'),
455
+ ),
456
+ ),
457
+ ),
458
+ );
459
+ } else if (metadata.tracking && !metadata.await) {
460
+ expression = b.call(
461
+ '$.derived',
462
+ b.thunk(context.visit(declarator.init)),
463
+ b.id('__block'),
464
+ );
465
+ } else {
466
+ expression = b.call(
467
+ '$.tracked',
468
+ declarator.init === null ? undefined : context.visit(declarator.init),
469
+ b.id('__block'),
470
+ );
471
+ }
472
+ }
473
+
474
+ declarations.push(b.declarator(declarator.id, expression));
475
+ } else {
476
+ declarations.push(context.visit(declarator));
477
+ }
478
+ } else {
479
+ const paths = extract_paths(declarator.id);
480
+ const has_tracked = paths.some(
481
+ (path) => path.node.type === 'Identifier' && is_tracked_name(path.node.name),
482
+ );
483
+
484
+ if (!context.state.to_ts) {
485
+ delete declarator.id.typeAnnotation;
486
+ }
487
+
488
+ if (!has_tracked) {
489
+ declarations.push(context.visit(declarator));
490
+ continue;
491
+ }
492
+
493
+ // For TypeScript mode, we still need to transform tracked variables
494
+ // but use a lighter approach that maintains type information
495
+ if (context.state.to_ts) {
496
+ const transformed = declarator.transformed || declarator.id;
497
+ let expression;
498
+
499
+ if (metadata.tracking && !metadata.await) {
500
+ expression = b.call(
501
+ '$.derived',
502
+ b.thunk(context.visit(declarator.init)),
503
+ b.id('__block'),
504
+ );
505
+ } else {
506
+ // Simple tracked variable - always use $.derived for $ prefixed variables
507
+ expression = b.call('$.tracked', context.visit(declarator.init), b.id('__block'));
508
+ }
509
+
510
+ declarations.push(b.declarator(transformed, expression));
511
+ continue;
512
+ }
513
+
514
+ const transformed = declarator.transformed;
515
+ let expression;
516
+
517
+ if (metadata.tracking && metadata.await) {
518
+ // TODO
519
+ debugger;
520
+ } else if (metadata.tracking && !metadata.await) {
521
+ expression = b.call(
522
+ '$.derived',
523
+ b.thunk(context.visit(declarator.init)),
524
+ b.id('__block'),
525
+ );
526
+ } else {
527
+ expression = context.visit(declarator.init);
528
+ }
529
+
530
+ declarations.push(b.declarator(transformed, expression));
531
+ }
532
+ }
533
+
534
+ return { ...node, declarations };
535
+ },
536
+
537
+ FunctionDeclaration(node, context) {
538
+ return visit_function(node, context);
539
+ },
540
+ ArrowFunctionExpression(node, context) {
541
+ return visit_function(node, context);
542
+ },
543
+ FunctionExpression(node, context) {
544
+ return visit_function(node, context);
545
+ },
546
+
547
+ Element(node, context) {
548
+ const { state, visit } = context;
549
+
550
+ const is_dom_element = is_element_dom_element(node, context);
551
+ const is_spreading = node.attributes.some((attr) => attr.type === 'SpreadAttribute');
552
+ const spread_attributes = is_spreading ? [] : null;
553
+
554
+ const handle_static_attr = (name, value) => {
555
+ const attr_value = b.literal(
556
+ ` ${name}${
557
+ is_boolean_attribute(name) && value === true
558
+ ? ''
559
+ : `="${value === true ? '' : escape_html(value, true)}"`
560
+ }`,
561
+ );
562
+
563
+ if (is_spreading) {
564
+ // For spread attributes, store just the actual value, not the full attribute string
565
+ const actual_value =
566
+ is_boolean_attribute(name) && value === true
567
+ ? b.literal(true)
568
+ : b.literal(value === true ? '' : value);
569
+ spread_attributes.push(b.prop('init', b.literal(name), actual_value));
570
+ } else {
571
+ state.template.push(attr_value);
572
+ }
573
+ };
574
+
575
+ if (is_dom_element) {
576
+ let class_attribute = null;
577
+ const local_updates = [];
578
+ const is_void = is_void_element(node.id.name);
579
+
580
+ state.template.push(`<${node.id.name}`);
581
+
582
+ for (const attr of node.attributes) {
583
+ if (attr.type === 'Attribute') {
584
+ if (attr.name.type === 'Identifier') {
585
+ const name = attr.name.name;
586
+
587
+ if (attr.value.type === 'Literal' && name !== 'class') {
588
+ handle_static_attr(name, attr.value.value);
589
+ continue;
590
+ }
591
+
592
+ if (name === 'class' || name === '$class') {
593
+ class_attribute = attr;
594
+
595
+ continue;
596
+ }
597
+
598
+ if (name === 'value' || name === '$value') {
599
+ const id = state.flush_node();
600
+ const metadata = { tracking: false, await: false };
601
+ const expression = visit(attr.value, { ...state, metadata });
602
+
603
+ if (name === '$value' || metadata.tracking) {
604
+ local_updates.push(b.stmt(b.call('$.set_value', id, expression)));
605
+ } else {
606
+ state.init.push(b.stmt(b.call('$.set_value', id, expression)));
607
+ }
608
+
609
+ continue;
610
+ }
611
+
612
+ if (name === 'checked' || name === '$checked') {
613
+ const id = state.flush_node();
614
+ const metadata = { tracking: false, await: false };
615
+ const expression = visit(attr.value, { ...state, metadata });
616
+
617
+ if (name === '$checked' || metadata.tracking) {
618
+ local_updates.push(b.stmt(b.call('$.set_checked', id, expression)));
619
+ } else {
620
+ state.init.push(b.stmt(b.call('$.set_checked', id, expression)));
621
+ }
622
+ continue;
623
+ }
624
+
625
+ if (name === 'selected' || name === '$selected') {
626
+ const id = state.flush_node();
627
+ const metadata = { tracking: false, await: false };
628
+ const expression = visit(attr.value, { ...state, metadata });
629
+
630
+ if (name === '$selected' || metadata.tracking) {
631
+ local_updates.push(b.stmt(b.call('$.set_selected', id, expression)));
632
+ } else {
633
+ state.init.push(b.stmt(b.call('$.set_selected', id, expression)));
634
+ }
635
+ continue;
636
+ }
637
+
638
+ if (is_event_attribute(name)) {
639
+ let capture = name.endsWith('Capture');
640
+ let event_name = capture
641
+ ? name.slice(2, -7).toLowerCase()
642
+ : name.slice(2).toLowerCase();
643
+ let handler = visit(attr.value, state);
644
+
645
+ if (attr.metadata?.delegated) {
646
+ let delegated_assignment;
647
+
648
+ if (!state.events.has(event_name)) {
649
+ state.events.add(event_name);
650
+ }
651
+
652
+ // Hoist function if we can, otherwise we leave the function as is
653
+ if (attr.metadata.delegated.hoisted) {
654
+ if (attr.metadata.delegated.function === attr.value) {
655
+ const func_name = state.scope.root.unique('on_' + event_name);
656
+ state.hoisted.push(b.var(func_name, handler));
657
+ handler = func_name;
658
+ }
659
+
660
+ const hoisted_params = /** @type {Expression[]} */ (
661
+ attr.metadata.delegated.function.metadata.hoisted_params
662
+ );
663
+
664
+ const args = [handler, b.id('__block'), ...hoisted_params];
665
+ delegated_assignment = b.array(args);
666
+ } else if (
667
+ handler.type === 'Identifier' &&
668
+ is_declared_function_within_component(handler, context)
669
+ ) {
670
+ delegated_assignment = handler;
671
+ } else {
672
+ delegated_assignment = b.array([handler, b.id('__block')]);
673
+ }
674
+ const id = state.flush_node();
675
+
676
+ state.init.push(
677
+ b.stmt(b.assignment('=', b.member(id, '__' + event_name), delegated_assignment)),
678
+ );
679
+ } else {
680
+ const passive = is_passive_event(event_name);
681
+ const id = state.flush_node();
682
+
683
+ state.init.push(
684
+ b.stmt(
685
+ b.call(
686
+ '$.event',
687
+ b.literal(event_name),
688
+ id,
689
+ handler,
690
+ capture && b.true,
691
+ passive === undefined ? undefined : b.literal(passive),
692
+ ),
693
+ ),
694
+ );
695
+ }
696
+
697
+ continue;
698
+ }
699
+
700
+ const metadata = { tracking: false, await: false };
701
+ const expression = visit(attr.value, { ...state, metadata });
702
+ // All other attributes
703
+ if (is_tracked_name(name) || metadata.tracking) {
704
+ const attribute = is_tracked_name(name) ? name.slice(1) : name;
705
+ const id = state.flush_node();
706
+
707
+ if (is_dom_property(attribute)) {
708
+ local_updates.push(b.stmt(b.assignment('=', b.member(id, attribute), expression)));
709
+ } else {
710
+ local_updates.push(
711
+ b.stmt(b.call('$.set_attribute', id, b.literal(attribute), expression)),
712
+ );
713
+ }
714
+ } else {
715
+ const id = state.flush_node();
716
+
717
+ if (is_dom_property(name)) {
718
+ state.init.push(b.stmt(b.assignment('=', b.member(id, name), expression)));
719
+ } else {
720
+ state.init.push(b.stmt(b.call('$.set_attribute', id, b.literal(name), expression)));
721
+ }
722
+ }
723
+ }
724
+ } else if (attr.type === 'SpreadAttribute') {
725
+ spread_attributes.push(b.spread(b.call('$.spread_object', visit(attr.argument, state))));
726
+ } else if (attr.type === 'RefAttribute') {
727
+ const id = state.flush_node();
728
+ state.init.push(b.stmt(b.call('$.ref', id, b.thunk(visit(attr.argument, state)))));
729
+ }
730
+ }
731
+
732
+ if (class_attribute !== null) {
733
+ if (class_attribute.value.type === 'Literal') {
734
+ let value = class_attribute.value.value;
735
+
736
+ if (node.metadata.scoped && state.component.css) {
737
+ value = `${state.component.css.hash} ${value}`;
738
+ }
739
+
740
+ handle_static_attr(class_attribute.name.name, value);
741
+ } else {
742
+ const id = state.flush_node();
743
+ const metadata = { tracking: false, await: false };
744
+ let expression = visit(class_attribute.value, { ...state, metadata });
745
+
746
+ if (node.metadata.scoped && state.component.css) {
747
+ expression = b.binary('+', b.literal(state.component.css.hash + ' '), expression);
748
+ }
749
+
750
+ if (class_attribute.name.name === '$class' || metadata.tracking) {
751
+ local_updates.push(b.stmt(b.call('$.set_class', id, expression)));
752
+ } else {
753
+ state.init.push(b.stmt(b.call('$.set_class', id, expression)));
754
+ }
755
+ }
756
+ } else if (node.metadata.scoped && state.component.css) {
757
+ const value = state.component.css.hash;
758
+
759
+ handle_static_attr('class', value);
760
+ }
761
+
762
+ state.template.push('>');
763
+
764
+ if (spread_attributes !== null && spread_attributes.length > 0) {
765
+ const id = state.flush_node();
766
+ state.init.push(
767
+ b.stmt(b.call('$.render_spread', id, b.thunk(b.object(spread_attributes)))),
768
+ );
769
+ }
770
+
771
+ const init = [];
772
+ const update = [];
773
+
774
+ if (!is_void) {
775
+ transform_children(node.children, {
776
+ visit,
777
+ state: { ...state, init, update },
778
+ root: false,
779
+ });
780
+ state.template.push(`</${node.id.name}>`);
781
+ }
782
+
783
+ update.push(...local_updates);
784
+
785
+ if (init.length > 0) {
786
+ state.init.push(b.block(init));
787
+ }
788
+
789
+ if (update.length > 0) {
790
+ state.init.push(b.stmt(b.call('$.render', b.thunk(b.block(update)))));
791
+ }
792
+ } else {
793
+ const id = state.flush_node();
794
+
795
+ state.template.push('<!>');
796
+
797
+ const is_spreading = node.attributes.some((attr) => attr.type === 'SpreadAttribute');
798
+ const props = [];
799
+ let children_prop = null;
800
+
801
+ for (const attr of node.attributes) {
802
+ if (attr.type === 'Attribute') {
803
+ if (attr.name.type === 'Identifier') {
804
+ const metadata = { tracking: false, await: false };
805
+ let property = visit(attr.value, { ...state, metadata });
806
+
807
+ if (metadata.tracking || attr.name.tracked) {
808
+ if (attr.name.name === 'children') {
809
+ children_prop = b.thunk(property);
810
+ continue;
811
+ }
812
+
813
+ props.push(
814
+ b.prop('get', attr.name, b.function(null, [], b.block([b.return(property)]))),
815
+ );
816
+ } else {
817
+ props.push(b.prop('init', attr.name, property));
818
+ }
819
+ } else {
820
+ props.push(b.prop('init', attr.name, visit(attr.value, state)));
821
+ }
822
+ } else if (attr.type === 'SpreadAttribute') {
823
+ props.push(
824
+ b.spread(
825
+ b.call(
826
+ '$.spread_object',
827
+ visit(attr.argument, { ...state, metadata: { ...state.metadata, spread: true } }),
828
+ ),
829
+ ),
830
+ );
831
+ } else if (attr.type === 'RefAttribute') {
832
+ props.push(b.prop('init', b.call('$.ref_prop'), visit(attr.argument, state), true));
833
+ } else if (attr.type === 'AccessorAttribute') {
834
+ let get_expr;
835
+
836
+ if (
837
+ attr.get.type === 'FunctionExpression' ||
838
+ attr.get.type === 'ArrowFunctionExpression'
839
+ ) {
840
+ get_expr = context.state.scope.generate(attr.name.name + '_get');
841
+
842
+ state.init.push(b.const(get_expr, visit(attr.get, state)));
843
+ } else {
844
+ get_expr = visit(attr.get, state);
845
+ }
846
+
847
+ props.push(
848
+ b.prop('get', attr.name, b.function(null, [], b.block([b.return(b.call(get_expr))]))),
849
+ );
850
+
851
+ if (attr.set) {
852
+ let set_expr;
853
+
854
+ if (
855
+ attr.set.type === 'FunctionExpression' ||
856
+ attr.set.type === 'ArrowFunctionExpression'
857
+ ) {
858
+ set_expr = context.state.scope.generate(attr.name.name + '_set');
859
+
860
+ state.init.push(b.const(set_expr, visit(attr.set, state)));
861
+ } else {
862
+ set_expr = visit(attr.set, state);
863
+ }
864
+
865
+ props.push(
866
+ b.prop(
867
+ 'set',
868
+ attr.name,
869
+ b.function(
870
+ null,
871
+ [b.id('__value')],
872
+ b.block([b.return(b.call(set_expr, b.id('__value')))]),
873
+ ),
874
+ ),
875
+ );
876
+ }
877
+ } else {
878
+ throw new Error('TODO');
879
+ }
880
+ }
881
+
882
+ const children_filtered = [];
883
+
884
+ for (const child of node.children) {
885
+ if (child.type === 'Component') {
886
+ const id = child.id;
887
+ props.push(b.prop('init', id, visit(child, state)));
888
+ } else {
889
+ children_filtered.push(child);
890
+ }
891
+ }
892
+
893
+ if (children_filtered.length > 0) {
894
+ const component_scope = context.state.scopes.get(node);
895
+ const children = visit(b.component(b.id('children'), [], children_filtered), {
896
+ ...context.state,
897
+ scope: component_scope,
898
+ });
899
+
900
+ if (children_prop) {
901
+ children_prop.body = b.logical('??', children_prop.body, children);
902
+ } else {
903
+ props.push(b.prop('init', b.id('children'), children));
904
+ }
905
+ }
906
+
907
+ if (is_spreading) {
908
+ state.init.push(
909
+ b.stmt(
910
+ b.call(
911
+ node.id,
912
+ id,
913
+ b.call('$.tracked_spread_object', b.thunk(b.object(props))),
914
+ b.id('$.active_block'),
915
+ ),
916
+ ),
917
+ );
918
+ } else {
919
+ state.init.push(
920
+ b.stmt(b.call(visit(node.id, state), id, b.object(props), b.id('$.active_block'))),
921
+ );
922
+ }
923
+ }
924
+ },
925
+
926
+ Fragment(node, context) {
927
+ if (!context.state.to_ts) {
928
+ add_ripple_internal_import(context);
929
+ }
930
+
931
+ const metadata = { await: false };
932
+
933
+ const body_statements = transform_body(node.body, {
934
+ ...context,
935
+ state: { ...context.state, component: node, metadata },
936
+ });
937
+
938
+ return b.function(
939
+ node.id,
940
+ [b.id('__anchor'), ...node.params.map((param) => context.visit(param, context.state))],
941
+ b.block(
942
+ metadata.await
943
+ ? [b.stmt(b.call('$.async', b.thunk(b.block(body_statements), true)))]
944
+ : body_statements,
945
+ ),
946
+ );
947
+ },
948
+
949
+ Component(node, context) {
950
+ let prop_statements;
951
+
952
+ add_ripple_internal_import(context);
953
+
954
+ const metadata = { await: false };
955
+
956
+ if (context.state.to_ts) {
957
+ const body_statements = [
958
+ ...transform_body(node.body, {
959
+ ...context,
960
+ state: { ...context.state, component: node, metadata },
961
+ }),
962
+ ];
963
+
964
+ return b.function(node.id, node.params, b.block(body_statements));
965
+ }
966
+
967
+ let props = b.id('__props');
968
+
969
+ if (node.params.length > 0) {
970
+ let props_param = node.params[0];
971
+
972
+ if (props_param.type === 'Identifier') {
973
+ delete props_param.typeAnnotation;
974
+ props = props_param;
975
+ } else if (props_param.type === 'ObjectPattern') {
976
+ delete props_param.typeAnnotation;
977
+ }
978
+ }
979
+
980
+ const body_statements = [
981
+ b.stmt(b.call('$.push_component')),
982
+ ...transform_body(node.body, {
983
+ ...context,
984
+ state: { ...context.state, component: node, metadata },
985
+ }),
986
+ b.stmt(b.call('$.pop_component')),
987
+ ];
988
+
989
+ if (node.css !== null && node.css) {
990
+ context.state.stylesheets.push(node.css);
991
+ }
992
+
993
+ return b.function(
994
+ node.id,
995
+ node.params.length > 0
996
+ ? [b.id('__anchor'), props, b.id('__block')]
997
+ : [b.id('__anchor'), b.id('_'), b.id('__block')],
998
+ b.block([
999
+ ...(prop_statements ?? []),
1000
+ ...(metadata.await
1001
+ ? [b.stmt(b.call('$.async', b.thunk(b.block(body_statements), true)))]
1002
+ : body_statements),
1003
+ ]),
1004
+ );
1005
+ },
1006
+
1007
+ AssignmentExpression(node, context) {
1008
+ if (context.state.to_ts) {
1009
+ return context.next();
1010
+ }
1011
+
1012
+ const left = node.left;
1013
+
1014
+ if (
1015
+ left.type === 'MemberExpression' &&
1016
+ left.property.type === 'Identifier' &&
1017
+ left.property.tracked
1018
+ ) {
1019
+ add_ripple_internal_import(context);
1020
+ const operator = node.operator;
1021
+ const right = node.right;
1022
+
1023
+ if (operator !== '=') {
1024
+ context.state.metadata.tracking = true;
1025
+ }
1026
+
1027
+ return b.call(
1028
+ '$.set_property',
1029
+ context.visit(left.object),
1030
+ left.computed ? context.visit(left.property) : b.literal(left.property.name),
1031
+ operator === '='
1032
+ ? context.visit(right)
1033
+ : b.binary(
1034
+ operator === '+=' ? '+' : operator === '-=' ? '-' : operator === '*=' ? '*' : '/',
1035
+ /** @type {Pattern} */ (context.visit(left)),
1036
+ /** @type {Expression} */ (context.visit(right)),
1037
+ ),
1038
+ b.id('__block'),
1039
+ );
1040
+ }
1041
+
1042
+ if (left.type === 'MemberExpression') {
1043
+ // need to capture setting length of array to throw a runtime error
1044
+ if (
1045
+ left.property.type === 'Identifier' &&
1046
+ (is_tracked_name(left.property.name) || left.property.name === 'length')
1047
+ ) {
1048
+ if (left.property.name !== '$length') {
1049
+ return b.call(
1050
+ '$.old_set_property',
1051
+ context.visit(left.object),
1052
+ left.computed ? context.visit(left.property) : b.literal(left.property.name),
1053
+ visit_assignment_expression(node, context, build_assignment) ?? context.next(),
1054
+ b.id('__block'),
1055
+ );
1056
+ }
1057
+ } else if (!is_tracked_computed_property(left.object, left.property, context)) {
1058
+ return context.next();
1059
+ }
1060
+ }
1061
+
1062
+ if (left.type === 'Identifier' && left.tracked) {
1063
+ add_ripple_internal_import(context);
1064
+ const operator = node.operator;
1065
+ const right = node.right;
1066
+
1067
+ return b.call(
1068
+ '$.set',
1069
+ context.visit(left),
1070
+ operator === '='
1071
+ ? context.visit(right)
1072
+ : b.binary(
1073
+ operator === '+=' ? '+' : operator === '-=' ? '-' : operator === '*=' ? '*' : '/',
1074
+ /** @type {Pattern} */ (
1075
+ context.visit(left, { ...context.state, metadata: { tracking: false } })
1076
+ ),
1077
+ /** @type {Expression} */ (context.visit(right)),
1078
+ ),
1079
+ b.id('__block'),
1080
+ );
1081
+ }
1082
+
1083
+ const visited = visit_assignment_expression(node, context, build_assignment) ?? context.next();
1084
+
1085
+ if (
1086
+ left.type === 'MemberExpression' &&
1087
+ left.property.type === 'Identifier' &&
1088
+ left.property.name === '$length' &&
1089
+ !left.computed
1090
+ ) {
1091
+ return b.call('$.with_scope', b.id('__block'), b.thunk(visited));
1092
+ }
1093
+
1094
+ return visited;
1095
+ },
1096
+
1097
+ UpdateExpression(node, context) {
1098
+ if (context.state.to_ts) {
1099
+ context.next();
1100
+ return;
1101
+ }
1102
+ const argument = node.argument;
1103
+
1104
+ if (
1105
+ argument.type === 'MemberExpression' &&
1106
+ argument.property.type === 'Identifier' &&
1107
+ argument.property.tracked
1108
+ ) {
1109
+ add_ripple_internal_import(context);
1110
+ context.state.metadata.tracking = true;
1111
+
1112
+ return b.call(
1113
+ node.prefix ? '$.update_pre_property' : '$.update_property',
1114
+ context.visit(argument.object),
1115
+ argument.computed ? context.visit(argument.property) : b.literal(argument.property.name),
1116
+ b.id('__block'),
1117
+ node.operator === '--' ? b.literal(-1) : undefined,
1118
+ );
1119
+ }
1120
+
1121
+ if (
1122
+ argument.type === 'MemberExpression' &&
1123
+ ((argument.property.type === 'Identifier' && is_tracked_name(argument.property.name)) ||
1124
+ (argument.computed &&
1125
+ is_tracked_computed_property(argument.object, argument.property, context)))
1126
+ ) {
1127
+ return b.call(
1128
+ node.prefix ? '$.old_update_pre_property' : '$.old_update_property',
1129
+ context.visit(argument.object),
1130
+ argument.computed ? context.visit(argument.property) : b.literal(argument.property.name),
1131
+ b.id('__block'),
1132
+ node.operator === '--' ? b.literal(-1) : undefined,
1133
+ );
1134
+ }
1135
+
1136
+ if (argument.type === 'Identifier' && argument.tracked) {
1137
+ return b.call(
1138
+ node.prefix ? '$.update_pre' : '$.update',
1139
+ context.visit(argument),
1140
+ b.id('__block'),
1141
+ node.operator === '--' ? b.literal(-1) : undefined,
1142
+ );
1143
+ }
1144
+
1145
+ const left = object(argument);
1146
+ const binding = context.state.scope.get(left.name);
1147
+ const transformers = left && binding?.transform;
1148
+
1149
+ if (left === argument) {
1150
+ const update_fn = transformers?.update || transformers?.update_tracked;
1151
+ if (update_fn) {
1152
+ return update_fn(node);
1153
+ }
1154
+ }
1155
+
1156
+ context.next();
1157
+ },
1158
+
1159
+ ObjectExpression(node, context) {
1160
+ const properties = [];
1161
+ const tracked = [];
1162
+
1163
+ for (const property of node.properties) {
1164
+ if (
1165
+ property.type === 'Property' &&
1166
+ !property.computed &&
1167
+ property.key.type === 'Identifier' &&
1168
+ property.kind === 'init' &&
1169
+ is_tracked_name(property.key.name)
1170
+ ) {
1171
+ tracked.push(b.literal(property.key.name));
1172
+ const metadata = { tracking: false, await: false };
1173
+ const tracked_property = context.visit(property, { ...context.state, metadata });
1174
+
1175
+ if (metadata.tracking) {
1176
+ properties.push({
1177
+ ...tracked_property,
1178
+ value: b.call('$.computed_property', b.thunk(tracked_property.value), b.id('__block')),
1179
+ });
1180
+ } else {
1181
+ properties.push(tracked_property);
1182
+ }
1183
+ } else {
1184
+ properties.push(context.visit(property));
1185
+ }
1186
+ }
1187
+
1188
+ if (tracked.length > 0) {
1189
+ return b.call('$.tracked_object', { ...node, properties }, b.array(tracked), b.id('__block'));
1190
+ }
1191
+
1192
+ context.next();
1193
+ },
1194
+
1195
+ ArrayExpression(node, context) {
1196
+ // TODO we can bail out of all of this if we know we're inside a computed fn expression
1197
+ // as the reactivity will hold from the reference of the $ binding itself
1198
+ const elements = [];
1199
+ const tracked = [];
1200
+ let i = 0;
1201
+
1202
+ for (const element of node.elements) {
1203
+ if (element === null) {
1204
+ elements.push(null);
1205
+ } else if (element.type === 'Identifier' && is_tracked_name(element.name)) {
1206
+ const metadata = { tracking: false, await: false };
1207
+ const tracked_identifier = context.visit(element, { ...context.state, metadata });
1208
+
1209
+ if (metadata.tracking) {
1210
+ tracked.push(b.literal(i));
1211
+ elements.push(
1212
+ b.call('$.computed_property', b.thunk(tracked_identifier), b.id('__block')),
1213
+ );
1214
+ } else {
1215
+ elements.push(tracked_identifier);
1216
+ }
1217
+ } else {
1218
+ const metadata = { tracking: false, await: false };
1219
+ elements.push(context.visit(element, { ...context.state, metadata }));
1220
+ }
1221
+ i++;
1222
+ }
1223
+
1224
+ if (tracked.length > 0) {
1225
+ return b.call('$.tracked_object', { ...node, elements }, b.array(tracked), b.id('__block'));
1226
+ }
1227
+
1228
+ context.next();
1229
+ },
1230
+
1231
+ ForOfStatement(node, context) {
1232
+ if (!is_inside_component(context)) {
1233
+ context.next();
1234
+ return;
1235
+ }
1236
+ const is_controlled = node.is_controlled;
1237
+
1238
+ // do only if not controller
1239
+ if (!is_controlled) {
1240
+ context.state.template.push('<!>');
1241
+ }
1242
+
1243
+ const id = context.state.flush_node(is_controlled);
1244
+ const pattern = node.left.declarations[0].id;
1245
+ const body_scope = context.state.scopes.get(node.body);
1246
+
1247
+ context.state.init.push(
1248
+ b.stmt(
1249
+ b.call(
1250
+ '$.for',
1251
+ id,
1252
+ b.thunk(context.visit(node.right)),
1253
+ b.arrow(
1254
+ [b.id('__anchor'), pattern],
1255
+ b.block(
1256
+ transform_body(node.body.body, {
1257
+ ...context,
1258
+ state: { ...context.state, scope: body_scope },
1259
+ }),
1260
+ ),
1261
+ ),
1262
+ b.literal(is_controlled ? IS_CONTROLLED : 0),
1263
+ ),
1264
+ ),
1265
+ );
1266
+ },
1267
+
1268
+ IfStatement(node, context) {
1269
+ if (!is_inside_component(context)) {
1270
+ context.next();
1271
+ return;
1272
+ }
1273
+ context.state.template.push('<!>');
1274
+
1275
+ const id = context.state.flush_node();
1276
+ const statements = [];
1277
+
1278
+ const consequent_scope = context.state.scopes.get(node.consequent);
1279
+ const consequent = b.block(
1280
+ transform_body(node.consequent.body, {
1281
+ ...context,
1282
+ state: { ...context.state, scope: consequent_scope },
1283
+ }),
1284
+ );
1285
+ const consequent_id = context.state.scope.generate('consequent');
1286
+
1287
+ statements.push(b.var(b.id(consequent_id), b.arrow([b.id('__anchor')], consequent)));
1288
+
1289
+ let alternate_id;
1290
+
1291
+ if (node.alternate !== null) {
1292
+ const alternate_scope = context.state.scopes.get(node.alternate) || context.state.scope;
1293
+ let alternate_body = node.alternate.body;
1294
+ if (node.alternate.type === 'IfStatement') {
1295
+ alternate_body = [node.alternate];
1296
+ }
1297
+ const alternate = b.block(
1298
+ transform_body(alternate_body, {
1299
+ ...context,
1300
+ state: { ...context.state, scope: alternate_scope },
1301
+ }),
1302
+ );
1303
+ alternate_id = context.state.scope.generate('alternate');
1304
+ statements.push(b.var(b.id(alternate_id), b.arrow([b.id('__anchor')], alternate)));
1305
+ }
1306
+
1307
+ statements.push(
1308
+ b.stmt(
1309
+ b.call(
1310
+ '$.if',
1311
+ id,
1312
+ b.arrow(
1313
+ [b.id('__render')],
1314
+ b.block([
1315
+ b.if(
1316
+ context.visit(node.test),
1317
+ b.stmt(b.call(b.id('__render'), b.id(consequent_id))),
1318
+ alternate_id
1319
+ ? b.stmt(
1320
+ b.call(
1321
+ b.id('__render'),
1322
+ b.id(alternate_id),
1323
+ node.alternate ? b.literal(false) : undefined,
1324
+ ),
1325
+ )
1326
+ : undefined,
1327
+ ),
1328
+ ]),
1329
+ ),
1330
+ ),
1331
+ ),
1332
+ );
1333
+
1334
+ context.state.init.push(b.block(statements));
1335
+ },
1336
+
1337
+ TryStatement(node, context) {
1338
+ if (!is_inside_component(context)) {
1339
+ context.next();
1340
+ return;
1341
+ }
1342
+ context.state.template.push('<!>');
1343
+
1344
+ const id = context.state.flush_node();
1345
+ const metadata = { await: false };
1346
+ let body = transform_body(node.block.body, {
1347
+ ...context,
1348
+ state: { ...context.state, metadata },
1349
+ });
1350
+
1351
+ if (metadata.await) {
1352
+ body = [b.stmt(b.call('$.async', b.thunk(b.block(body), true)))];
1353
+ }
1354
+
1355
+ context.state.init.push(
1356
+ b.stmt(
1357
+ b.call(
1358
+ '$.try',
1359
+ id,
1360
+ b.arrow([b.id('__anchor')], b.block(body)),
1361
+ node.handler === null
1362
+ ? b.literal(null)
1363
+ : b.arrow(
1364
+ [b.id('__anchor'), ...(node.handler.param ? [node.handler.param] : [])],
1365
+ b.block(transform_body(node.handler.body.body, context)),
1366
+ ),
1367
+ node.async === null
1368
+ ? undefined
1369
+ : b.arrow([b.id('__anchor')], b.block(transform_body(node.async.body, context))),
1370
+ ),
1371
+ ),
1372
+ );
1373
+ },
1374
+
1375
+ AwaitExpression(node, context) {
1376
+ if (!is_inside_component(context)) {
1377
+ context.next();
1378
+ }
1379
+
1380
+ if (context.state.metadata?.await === false) {
1381
+ context.state.metadata.await = true;
1382
+ }
1383
+
1384
+ return b.call(b.await(b.call('$.resume_context', context.visit(node.argument))));
1385
+ },
1386
+
1387
+ BinaryExpression(node, context) {
1388
+ return b.binary(node.operator, context.visit(node.left), context.visit(node.right));
1389
+ },
1390
+
1391
+ TemplateLiteral(node, context) {
1392
+ if (node.expressions.length === 0) {
1393
+ return b.literal(node.quasis[0].value.cooked);
1394
+ }
1395
+
1396
+ const expressions = node.expressions.map((expr) => context.visit(expr));
1397
+ return b.template(node.quasis, expressions);
1398
+ },
1399
+
1400
+ RenderFragment(node, context) {
1401
+ const identifer = node.expression.callee;
1402
+
1403
+ context.state.template.push('<!>');
1404
+
1405
+ const id = context.state.flush_node();
1406
+
1407
+ context.state.init.push(
1408
+ b.stmt(
1409
+ b.call(
1410
+ context.visit(identifer),
1411
+ id,
1412
+ ...node.expression.arguments.map((arg) => context.visit(arg, context.state)),
1413
+ ),
1414
+ ),
1415
+ );
1416
+ },
1417
+
1418
+ BlockStatement(node, context) {
1419
+ const statements = [];
1420
+
1421
+ for (const statement of node.body) {
1422
+ statements.push(context.visit(statement));
1423
+ }
1424
+
1425
+ return b.block(statements);
1426
+ },
1427
+
1428
+ Program(node, context) {
1429
+ const statements = [];
1430
+
1431
+ for (const statement of node.body) {
1432
+ statements.push(context.visit(statement));
1433
+ }
1434
+
1435
+ return { ...node, body: statements };
1436
+ },
1369
1437
  };
1370
1438
 
1371
1439
  /**
1372
1440
  * @param {Array<string | Expression>} items
1373
1441
  */
1374
1442
  function join_template(items) {
1375
- let quasi = b.quasi('');
1376
- const template = b.template([quasi], []);
1377
-
1378
- /**
1379
- * @param {Expression} expression
1380
- */
1381
- function push(expression) {
1382
- if (expression.type === 'TemplateLiteral') {
1383
- for (let i = 0; i < expression.expressions.length; i += 1) {
1384
- const q = expression.quasis[i];
1385
- const e = expression.expressions[i];
1386
-
1387
- quasi.value.cooked += /** @type {string} */ (q.value.cooked);
1388
- push(e);
1389
- }
1390
-
1391
- const last = /** @type {TemplateElement} */ (expression.quasis.at(-1));
1392
- quasi.value.cooked += /** @type {string} */ (last.value.cooked);
1393
- } else if (expression.type === 'Literal') {
1394
- /** @type {string} */ (quasi.value.cooked) += expression.value;
1395
- } else {
1396
- template.expressions.push(expression);
1397
- template.quasis.push((quasi = b.quasi('')));
1398
- }
1399
- }
1400
-
1401
- for (const item of items) {
1402
- if (typeof item === 'string') {
1403
- quasi.value.cooked += item;
1404
- } else {
1405
- push(item);
1406
- }
1407
- }
1408
-
1409
- for (const quasi of template.quasis) {
1410
- quasi.value.raw = sanitize_template_string(/** @type {string} */ (quasi.value.cooked));
1411
- }
1412
-
1413
- quasi.tail = true;
1414
-
1415
- return template;
1443
+ let quasi = b.quasi('');
1444
+ const template = b.template([quasi], []);
1445
+
1446
+ /**
1447
+ * @param {Expression} expression
1448
+ */
1449
+ function push(expression) {
1450
+ if (expression.type === 'TemplateLiteral') {
1451
+ for (let i = 0; i < expression.expressions.length; i += 1) {
1452
+ const q = expression.quasis[i];
1453
+ const e = expression.expressions[i];
1454
+
1455
+ quasi.value.cooked += /** @type {string} */ (q.value.cooked);
1456
+ push(e);
1457
+ }
1458
+
1459
+ const last = /** @type {TemplateElement} */ (expression.quasis.at(-1));
1460
+ quasi.value.cooked += /** @type {string} */ (last.value.cooked);
1461
+ } else if (expression.type === 'Literal') {
1462
+ /** @type {string} */ (quasi.value.cooked) += expression.value;
1463
+ } else {
1464
+ template.expressions.push(expression);
1465
+ template.quasis.push((quasi = b.quasi('')));
1466
+ }
1467
+ }
1468
+
1469
+ for (const item of items) {
1470
+ if (typeof item === 'string') {
1471
+ quasi.value.cooked += item;
1472
+ } else {
1473
+ push(item);
1474
+ }
1475
+ }
1476
+
1477
+ for (const quasi of template.quasis) {
1478
+ quasi.value.raw = sanitize_template_string(/** @type {string} */ (quasi.value.cooked));
1479
+ }
1480
+
1481
+ quasi.tail = true;
1482
+
1483
+ return template;
1416
1484
  }
1417
1485
 
1418
1486
  function normalize_child(node, normalized) {
1419
- if (node.type === 'EmptyStatement') {
1420
- return;
1421
- } else if (node.type === 'Element' && node.id.type === 'Identifier' && node.id.name === 'style') {
1422
- return;
1423
- } else {
1424
- normalized.push(node);
1425
- }
1487
+ if (node.type === 'EmptyStatement') {
1488
+ return;
1489
+ } else if (node.type === 'Element' && node.id.type === 'Identifier' && node.id.name === 'style') {
1490
+ return;
1491
+ } else {
1492
+ normalized.push(node);
1493
+ }
1426
1494
  }
1427
1495
 
1428
1496
  function transform_ts_child(node, context) {
1429
- const { state, visit } = context;
1430
-
1431
- if (node.type === 'Text') {
1432
- state.init.push(b.stmt(visit(node.expression, { ...state })));
1433
- } else if (node.type === 'Element') {
1434
- const type = node.id.name;
1435
- const children = [];
1436
- let has_children_props = false;
1437
-
1438
- // Filter out RefAttributes and handle them separately
1439
- const ref_attributes = [];
1440
- const attributes = node.attributes
1441
- .filter((attr) => {
1442
- if (attr.type === 'RefAttribute') {
1443
- ref_attributes.push(attr);
1444
- return false;
1445
- }
1446
- return true;
1447
- })
1448
- .map((attr) => {
1449
- if (attr.type === 'Attribute') {
1450
- const metadata = { await: false };
1451
- const name = visit(attr.name, { ...state, metadata });
1452
- const value = visit(attr.value, { ...state, metadata });
1453
- const jsx_name = b.jsx_id(name.name);
1454
- if (name.name === '$children') {
1455
- has_children_props = true;
1456
- }
1457
- jsx_name.loc = name.loc;
1458
-
1459
- return b.jsx_attribute(jsx_name, b.jsx_expression_container(value));
1460
- } else if (attr.type === 'SpreadAttribute') {
1461
- const metadata = { await: false };
1462
- const argument = visit(attr.argument, { ...state, metadata });
1463
- return b.jsx_spread_attribute(argument);
1464
- }
1465
- });
1466
-
1467
- // Add RefAttribute references separately for sourcemap purposes
1468
- for (const ref_attr of ref_attributes) {
1469
- const metadata = { await: false };
1470
- const argument = visit(ref_attr.argument, { ...state, metadata });
1471
- state.init.push(b.stmt(argument));
1472
- }
1473
-
1474
- if (!node.selfClosing && !has_children_props && node.children.length > 0) {
1475
- const is_dom_element = type[0].toLowerCase() === type[0] && type[0] !== '$';
1476
-
1477
- const component_scope = context.state.scopes.get(node);
1478
- const thunk = b.thunk(
1479
- b.block(
1480
- transform_body(node.children, {
1481
- ...context,
1482
- state: { ...state, scope: component_scope },
1483
- }),
1484
- ),
1485
- );
1486
-
1487
- if (is_dom_element) {
1488
- children.push(b.jsx_expression_container(b.call(thunk)));
1489
- } else {
1490
- const children_name = context.state.scope.generate('component');
1491
- const children_id = b.id(children_name);
1492
- const jsx_id = b.jsx_id('$children');
1493
- jsx_id.loc = node.id.loc;
1494
- state.init.push(b.const(children_id, thunk));
1495
- attributes.push(b.jsx_attribute(jsx_id, b.jsx_expression_container(children_id)));
1496
- }
1497
- }
1498
-
1499
- const opening_type = b.jsx_id(type);
1500
- opening_type.loc = node.id.loc;
1501
-
1502
- let closing_type = undefined;
1503
-
1504
- if (!node.selfClosing) {
1505
- closing_type = b.jsx_id(type);
1506
- closing_type.loc = {
1507
- start: {
1508
- line: node.loc.end.line,
1509
- column: node.loc.end.column - type.length - 1,
1510
- },
1511
- end: {
1512
- line: node.loc.end.line,
1513
- column: node.loc.end.column - 1,
1514
- },
1515
- };
1516
- }
1517
-
1518
- state.init.push(
1519
- b.stmt(b.jsx_element(opening_type, attributes, children, node.selfClosing, closing_type)),
1520
- );
1521
- } else if (node.type === 'IfStatement') {
1522
- const consequent_scope = context.state.scopes.get(node.consequent);
1523
- const consequent = b.block(
1524
- transform_body(node.consequent.body, {
1525
- ...context,
1526
- state: { ...context.state, scope: consequent_scope },
1527
- }),
1528
- );
1529
-
1530
- let alternate;
1531
-
1532
- if (node.alternate !== null) {
1533
- const alternate_scope = context.state.scopes.get(node.alternate) || context.state.scope;
1534
- let alternate_body = node.alternate.body;
1535
- if (node.alternate.type === 'IfStatement') {
1536
- alternate_body = [node.alternate];
1537
- }
1538
- alternate = b.block(
1539
- transform_body(alternate_body, {
1540
- ...context,
1541
- state: { ...context.state, scope: alternate_scope },
1542
- }),
1543
- );
1544
- }
1545
-
1546
- state.init.push(b.if(visit(node.test), consequent, alternate));
1547
- } else if (node.type === 'ForOfStatement') {
1548
- const body_scope = context.state.scopes.get(node.body);
1549
- const body = b.block(
1550
- transform_body(node.body.body, {
1551
- ...context,
1552
- state: { ...context.state, scope: body_scope },
1553
- }),
1554
- );
1555
-
1556
- state.init.push(b.for_of(visit(node.left), visit(node.right), body, node.await));
1557
- } else if (node.type === 'TryStatement') {
1558
- const try_scope = context.state.scopes.get(node.block);
1559
- const try_body = b.block(
1560
- transform_body(node.block.body, {
1561
- ...context,
1562
- state: { ...context.state, scope: try_scope },
1563
- }),
1564
- );
1565
-
1566
- let catch_handler = null;
1567
- if (node.handler) {
1568
- const catch_scope = context.state.scopes.get(node.handler.body);
1569
- const catch_body = b.block(
1570
- transform_body(node.handler.body.body, {
1571
- ...context,
1572
- state: { ...context.state, scope: catch_scope },
1573
- }),
1574
- );
1575
- catch_handler = b.catch_clause(node.handler.param || null, catch_body);
1576
- }
1577
-
1578
- let finally_block = null;
1579
- if (node.finalizer) {
1580
- const finally_scope = context.state.scopes.get(node.finalizer);
1581
- finally_block = b.block(
1582
- transform_body(node.finalizer.body, {
1583
- ...context,
1584
- state: { ...context.state, scope: finally_scope },
1585
- }),
1586
- );
1587
- }
1588
-
1589
- state.init.push(b.try(try_body, catch_handler, finally_block));
1590
- } else if (node.type === 'RenderFragment') {
1591
- const identifer = node.expression.callee;
1592
-
1593
- state.init.push(
1594
- b.stmt(
1595
- b.call(
1596
- context.visit(identifer),
1597
- ...node.expression.arguments.map((arg) => context.visit(arg, context.state)),
1598
- ),
1599
- ),
1600
- );
1601
- } else if (node.type === 'Component') {
1602
- const component = visit(node, context.state);
1603
-
1604
- state.init.push(component);
1605
- } else {
1606
- debugger;
1607
- throw new Error('TODO');
1608
- }
1497
+ const { state, visit } = context;
1498
+
1499
+ if (node.type === 'Text') {
1500
+ state.init.push(b.stmt(visit(node.expression, { ...state })));
1501
+ } else if (node.type === 'Element') {
1502
+ const type = node.id.name;
1503
+ const children = [];
1504
+ let has_children_props = false;
1505
+
1506
+ // Filter out RefAttributes and handle them separately
1507
+ const ref_attributes = [];
1508
+ const attributes = node.attributes
1509
+ .filter((attr) => {
1510
+ if (attr.type === 'RefAttribute') {
1511
+ ref_attributes.push(attr);
1512
+ return false;
1513
+ }
1514
+ return true;
1515
+ })
1516
+ .map((attr) => {
1517
+ if (attr.type === 'Attribute') {
1518
+ const metadata = { await: false };
1519
+ const name = visit(attr.name, { ...state, metadata });
1520
+ const value = visit(attr.value, { ...state, metadata });
1521
+ const jsx_name = b.jsx_id(name.name);
1522
+ if (name.name === 'children') {
1523
+ has_children_props = true;
1524
+ }
1525
+ jsx_name.loc = name.loc;
1526
+
1527
+ return b.jsx_attribute(jsx_name, b.jsx_expression_container(value));
1528
+ } else if (attr.type === 'SpreadAttribute') {
1529
+ const metadata = { await: false };
1530
+ const argument = visit(attr.argument, { ...state, metadata });
1531
+ return b.jsx_spread_attribute(argument);
1532
+ }
1533
+ });
1534
+
1535
+ // Add RefAttribute references separately for sourcemap purposes
1536
+ for (const ref_attr of ref_attributes) {
1537
+ const metadata = { await: false };
1538
+ const argument = visit(ref_attr.argument, { ...state, metadata });
1539
+ state.init.push(b.stmt(argument));
1540
+ }
1541
+
1542
+ if (!node.selfClosing && !has_children_props && node.children.length > 0) {
1543
+ const is_dom_element = is_element_dom_element(node, context);
1544
+
1545
+ const component_scope = context.state.scopes.get(node);
1546
+ const thunk = b.thunk(
1547
+ b.block(
1548
+ transform_body(node.children, {
1549
+ ...context,
1550
+ state: { ...state, scope: component_scope },
1551
+ }),
1552
+ ),
1553
+ );
1554
+
1555
+ if (is_dom_element) {
1556
+ children.push(b.jsx_expression_container(b.call(thunk)));
1557
+ } else {
1558
+ const children_name = context.state.scope.generate('component');
1559
+ const children_id = b.id(children_name);
1560
+ const jsx_id = b.jsx_id('children');
1561
+ jsx_id.loc = node.id.loc;
1562
+ state.init.push(b.const(children_id, thunk));
1563
+ attributes.push(b.jsx_attribute(jsx_id, b.jsx_expression_container(children_id)));
1564
+ }
1565
+ }
1566
+
1567
+ const opening_type = b.jsx_id(type);
1568
+ opening_type.loc = node.id.loc;
1569
+
1570
+ let closing_type = undefined;
1571
+
1572
+ if (!node.selfClosing) {
1573
+ closing_type = b.jsx_id(type);
1574
+ closing_type.loc = {
1575
+ start: {
1576
+ line: node.loc.end.line,
1577
+ column: node.loc.end.column - type.length - 1,
1578
+ },
1579
+ end: {
1580
+ line: node.loc.end.line,
1581
+ column: node.loc.end.column - 1,
1582
+ },
1583
+ };
1584
+ }
1585
+
1586
+ state.init.push(
1587
+ b.stmt(b.jsx_element(opening_type, attributes, children, node.selfClosing, closing_type)),
1588
+ );
1589
+ } else if (node.type === 'IfStatement') {
1590
+ const consequent_scope = context.state.scopes.get(node.consequent);
1591
+ const consequent = b.block(
1592
+ transform_body(node.consequent.body, {
1593
+ ...context,
1594
+ state: { ...context.state, scope: consequent_scope },
1595
+ }),
1596
+ );
1597
+
1598
+ let alternate;
1599
+
1600
+ if (node.alternate !== null) {
1601
+ const alternate_scope = context.state.scopes.get(node.alternate) || context.state.scope;
1602
+ let alternate_body = node.alternate.body;
1603
+ if (node.alternate.type === 'IfStatement') {
1604
+ alternate_body = [node.alternate];
1605
+ }
1606
+ alternate = b.block(
1607
+ transform_body(alternate_body, {
1608
+ ...context,
1609
+ state: { ...context.state, scope: alternate_scope },
1610
+ }),
1611
+ );
1612
+ }
1613
+
1614
+ state.init.push(b.if(visit(node.test), consequent, alternate));
1615
+ } else if (node.type === 'ForOfStatement') {
1616
+ const body_scope = context.state.scopes.get(node.body);
1617
+ const body = b.block(
1618
+ transform_body(node.body.body, {
1619
+ ...context,
1620
+ state: { ...context.state, scope: body_scope },
1621
+ }),
1622
+ );
1623
+
1624
+ state.init.push(b.for_of(visit(node.left), visit(node.right), body, node.await));
1625
+ } else if (node.type === 'TryStatement') {
1626
+ const try_scope = context.state.scopes.get(node.block);
1627
+ const try_body = b.block(
1628
+ transform_body(node.block.body, {
1629
+ ...context,
1630
+ state: { ...context.state, scope: try_scope },
1631
+ }),
1632
+ );
1633
+
1634
+ let catch_handler = null;
1635
+ if (node.handler) {
1636
+ const catch_scope = context.state.scopes.get(node.handler.body);
1637
+ const catch_body = b.block(
1638
+ transform_body(node.handler.body.body, {
1639
+ ...context,
1640
+ state: { ...context.state, scope: catch_scope },
1641
+ }),
1642
+ );
1643
+ catch_handler = b.catch_clause(node.handler.param || null, catch_body);
1644
+ }
1645
+
1646
+ let finally_block = null;
1647
+ if (node.finalizer) {
1648
+ const finally_scope = context.state.scopes.get(node.finalizer);
1649
+ finally_block = b.block(
1650
+ transform_body(node.finalizer.body, {
1651
+ ...context,
1652
+ state: { ...context.state, scope: finally_scope },
1653
+ }),
1654
+ );
1655
+ }
1656
+
1657
+ state.init.push(b.try(try_body, catch_handler, finally_block));
1658
+ } else if (node.type === 'RenderFragment') {
1659
+ const identifer = node.expression.callee;
1660
+
1661
+ state.init.push(
1662
+ b.stmt(
1663
+ b.call(
1664
+ context.visit(identifer),
1665
+ ...node.expression.arguments.map((arg) => context.visit(arg, context.state)),
1666
+ ),
1667
+ ),
1668
+ );
1669
+ } else if (node.type === 'Component') {
1670
+ const component = visit(node, context.state);
1671
+
1672
+ state.init.push(component);
1673
+ } else {
1674
+ debugger;
1675
+ throw new Error('TODO');
1676
+ }
1609
1677
  }
1610
1678
 
1611
- function transform_children(children, { visit, state, root }) {
1612
- const normalized = [];
1613
-
1614
- for (const node of children) {
1615
- normalize_child(node, normalized);
1616
- }
1617
-
1618
- const is_fragment =
1619
- normalized.some(
1620
- (node) =>
1621
- node.type === 'IfStatement' ||
1622
- node.type === 'TryStatement' ||
1623
- node.type === 'ForOfStatement' ||
1624
- node.type === 'RenderFragment' ||
1625
- (node.type === 'Element' &&
1626
- (node.id.type !== 'Identifier' ||
1627
- node.id.name[0].toLowerCase() !== node.id.name[0] ||
1628
- node.id.name[0] === '$')),
1629
- ) ||
1630
- normalized.filter(
1631
- (node) => node.type !== 'VariableDeclaration' && node.type !== 'EmptyStatement',
1632
- ).length > 1;
1633
- let initial = null;
1634
- let prev = null;
1635
- let template_id = null;
1636
-
1637
- const get_id = (node) => {
1638
- return b.id(
1639
- node.type == 'Element' &&
1640
- node.id.type === 'Identifier' &&
1641
- node.id.name[0].toLowerCase() === node.id.name[0] &&
1642
- node.id.name[0] !== '$'
1643
- ? state.scope.generate(node.id.name)
1644
- : node.type == 'Text'
1645
- ? state.scope.generate('text')
1646
- : state.scope.generate('node'),
1647
- );
1648
- };
1649
-
1650
- const create_initial = (node) => {
1651
- const id = is_fragment ? b.id(state.scope.generate('fragment')) : get_id(node);
1652
- initial = id;
1653
- template_id = state.scope.generate('root');
1654
- state.setup.push(b.var(id, b.call(template_id)));
1655
- };
1656
-
1657
- for (let i = normalized.length - 1; i >= 0; i--) {
1658
- const child = normalized[i];
1659
- const prev_child = normalized[i - 1];
1660
-
1661
- if (child.type === 'Text' && prev_child?.type === 'Text') {
1662
- if (child.expression.type === 'Literal' && prev_child.expression.type === 'Literal') {
1663
- prev_child.expression = b.literal(prev_child.expression.value + child.expression.value);
1664
- } else {
1665
- prev_child.expression = b.binary('+', prev_child.expression, child.expression);
1666
- }
1667
- normalized.splice(i, 1);
1668
- }
1669
- }
1670
-
1671
- for (const node of normalized) {
1672
- if (
1673
- node.type === 'VariableDeclaration' ||
1674
- node.type === 'ExpressionStatement' ||
1675
- node.type === 'FunctionDeclaration' ||
1676
- node.type === 'DebuggerStatement' ||
1677
- node.type === 'ClassDeclaration'
1678
- ) {
1679
- const metadata = { await: false };
1680
- state.init.push(visit(node, { ...state, metadata }));
1681
- if (metadata.await) {
1682
- state.init.push(b.if(b.call('$.aborted'), b.return(null)));
1683
- if (state.metadata?.await === false) {
1684
- state.metadata.await = true;
1685
- }
1686
- }
1687
- } else if (state.to_ts) {
1688
- transform_ts_child(node, { visit, state });
1689
- } else {
1690
- if (initial === null && root) {
1691
- create_initial(node);
1692
- }
1693
-
1694
- const current_prev = prev;
1695
- let cached;
1696
- const flush_node = (is_controlled) => {
1697
- if (cached && !is_controlled) {
1698
- return cached;
1699
- } else if (current_prev !== null) {
1700
- const id = get_id(node);
1701
- state.setup.push(b.var(id, b.call('$.sibling', current_prev())));
1702
- cached = id;
1703
- return id;
1704
- } else if (initial !== null) {
1705
- if (is_fragment) {
1706
- const id = get_id(node);
1707
- state.setup.push(b.var(id, b.call('$.child_frag', initial)));
1708
- cached = id;
1709
- return id;
1710
- }
1711
- return initial;
1712
- } else if (state.flush_node !== null) {
1713
- if (is_controlled) {
1714
- return state.flush_node();
1715
- }
1716
-
1717
- const id = get_id(node);
1718
- state.setup.push(b.var(id, b.call('$.child', state.flush_node())));
1719
- cached = id;
1720
- return id;
1721
- } else {
1722
- debugger;
1723
- }
1724
- };
1725
-
1726
- prev = flush_node;
1727
-
1728
- if (node.type === 'Element') {
1729
- visit(node, { ...state, flush_node });
1730
- } else if (node.type === 'Text') {
1731
- const metadata = { tracking: false, await: false };
1732
- const expression = visit(node.expression, { ...state, metadata });
1733
-
1734
- if (metadata.tracking) {
1735
- state.template.push(' ');
1736
- const id = flush_node();
1737
- state.update.push(b.stmt(b.call('$.set_text', id, expression)));
1738
- } else if (normalized.length === 1) {
1739
- if (expression.type === 'Literal') {
1740
- state.template.push(escape_html(expression.value));
1741
- } else {
1742
- const id = state.flush_node();
1743
- state.template.push(' ');
1744
- state.init.push(
1745
- b.stmt(b.assignment('=', b.member(id, b.id('textContent')), expression)),
1746
- );
1747
- }
1748
- } else {
1749
- // Handle Text nodes in fragments
1750
- state.template.push(' ');
1751
- const id = flush_node();
1752
- state.update.push(b.stmt(b.call('$.set_text', id, expression)));
1753
- }
1754
- } else if (node.type === 'ForOfStatement') {
1755
- const is_controlled = normalized.length === 1;
1756
- node.is_controlled = is_controlled;
1757
- visit(node, { ...state, flush_node });
1758
- } else if (node.type === 'IfStatement') {
1759
- const is_controlled = normalized.length === 1;
1760
- node.is_controlled = is_controlled;
1761
- visit(node, { ...state, flush_node });
1762
- } else if (node.type === 'TryStatement') {
1763
- const is_controlled = normalized.length === 1;
1764
- node.is_controlled = is_controlled;
1765
- visit(node, { ...state, flush_node });
1766
- } else if (node.type === 'RenderFragment') {
1767
- const is_controlled = normalized.length === 1;
1768
- node.is_controlled = is_controlled;
1769
- visit(node, { ...state, flush_node });
1770
- } else {
1771
- debugger;
1772
- }
1773
- }
1774
- }
1775
-
1776
- if (root && initial !== null && template_id !== null) {
1777
- const flags = is_fragment ? b.literal(TEMPLATE_FRAGMENT) : b.literal(0);
1778
- state.final.push(b.stmt(b.call('$.append', b.id('__anchor'), initial)));
1779
- state.hoisted.push(
1780
- b.var(template_id, b.call('$.template', join_template(state.template), flags)),
1781
- );
1782
- }
1679
+ function transform_children(children, context) {
1680
+ const { visit, state, root } = context;
1681
+ const normalized = [];
1682
+
1683
+ for (const node of children) {
1684
+ normalize_child(node, normalized);
1685
+ }
1686
+
1687
+ const is_fragment =
1688
+ normalized.some(
1689
+ (node) =>
1690
+ node.type === 'IfStatement' ||
1691
+ node.type === 'TryStatement' ||
1692
+ node.type === 'ForOfStatement' ||
1693
+ node.type === 'RenderFragment' ||
1694
+ (node.type === 'Element' &&
1695
+ (node.id.type !== 'Identifier' || !is_element_dom_element(node, context))),
1696
+ ) ||
1697
+ normalized.filter(
1698
+ (node) => node.type !== 'VariableDeclaration' && node.type !== 'EmptyStatement',
1699
+ ).length > 1;
1700
+ let initial = null;
1701
+ let prev = null;
1702
+ let template_id = null;
1703
+
1704
+ const get_id = (node) => {
1705
+ return b.id(
1706
+ node.type == 'Element' && is_element_dom_element(node, context)
1707
+ ? state.scope.generate(node.id.name)
1708
+ : node.type == 'Text'
1709
+ ? state.scope.generate('text')
1710
+ : state.scope.generate('node'),
1711
+ );
1712
+ };
1713
+
1714
+ const create_initial = (node) => {
1715
+ const id = is_fragment ? b.id(state.scope.generate('fragment')) : get_id(node);
1716
+ initial = id;
1717
+ template_id = state.scope.generate('root');
1718
+ state.setup.push(b.var(id, b.call(template_id)));
1719
+ };
1720
+
1721
+ for (let i = normalized.length - 1; i >= 0; i--) {
1722
+ const child = normalized[i];
1723
+ const prev_child = normalized[i - 1];
1724
+
1725
+ if (child.type === 'Text' && prev_child?.type === 'Text') {
1726
+ if (child.expression.type === 'Literal' && prev_child.expression.type === 'Literal') {
1727
+ prev_child.expression = b.literal(prev_child.expression.value + child.expression.value);
1728
+ } else {
1729
+ prev_child.expression = b.binary('+', prev_child.expression, child.expression);
1730
+ }
1731
+ normalized.splice(i, 1);
1732
+ }
1733
+ }
1734
+
1735
+ for (const node of normalized) {
1736
+ if (
1737
+ node.type === 'VariableDeclaration' ||
1738
+ node.type === 'ExpressionStatement' ||
1739
+ node.type === 'FunctionDeclaration' ||
1740
+ node.type === 'DebuggerStatement' ||
1741
+ node.type === 'ClassDeclaration'
1742
+ ) {
1743
+ const metadata = { await: false };
1744
+ state.init.push(visit(node, { ...state, metadata }));
1745
+ if (metadata.await) {
1746
+ state.init.push(b.if(b.call('$.aborted'), b.return(null)));
1747
+ if (state.metadata?.await === false) {
1748
+ state.metadata.await = true;
1749
+ }
1750
+ }
1751
+ } else if (state.to_ts) {
1752
+ transform_ts_child(node, { visit, state });
1753
+ } else {
1754
+ if (initial === null && root) {
1755
+ create_initial(node);
1756
+ }
1757
+
1758
+ const current_prev = prev;
1759
+ let cached;
1760
+ const flush_node = (is_controlled) => {
1761
+ if (cached && !is_controlled) {
1762
+ return cached;
1763
+ } else if (current_prev !== null) {
1764
+ const id = get_id(node);
1765
+ state.setup.push(b.var(id, b.call('$.sibling', current_prev())));
1766
+ cached = id;
1767
+ return id;
1768
+ } else if (initial !== null) {
1769
+ if (is_fragment) {
1770
+ const id = get_id(node);
1771
+ state.setup.push(b.var(id, b.call('$.child_frag', initial)));
1772
+ cached = id;
1773
+ return id;
1774
+ }
1775
+ return initial;
1776
+ } else if (state.flush_node !== null) {
1777
+ if (is_controlled) {
1778
+ return state.flush_node();
1779
+ }
1780
+
1781
+ const id = get_id(node);
1782
+ state.setup.push(b.var(id, b.call('$.child', state.flush_node())));
1783
+ cached = id;
1784
+ return id;
1785
+ } else {
1786
+ debugger;
1787
+ }
1788
+ };
1789
+
1790
+ prev = flush_node;
1791
+
1792
+ if (node.type === 'Element') {
1793
+ visit(node, { ...state, flush_node });
1794
+ } else if (node.type === 'Text') {
1795
+ const metadata = { tracking: false, await: false };
1796
+ const expression = visit(node.expression, { ...state, metadata });
1797
+
1798
+ if (metadata.tracking) {
1799
+ state.template.push(' ');
1800
+ const id = flush_node();
1801
+ state.update.push(b.stmt(b.call('$.set_text', id, expression)));
1802
+ } else if (normalized.length === 1) {
1803
+ if (expression.type === 'Literal') {
1804
+ state.template.push(escape_html(expression.value));
1805
+ } else {
1806
+ const id = state.flush_node();
1807
+ state.template.push(' ');
1808
+ state.init.push(
1809
+ b.stmt(b.assignment('=', b.member(id, b.id('textContent')), expression)),
1810
+ );
1811
+ }
1812
+ } else {
1813
+ // Handle Text nodes in fragments
1814
+ state.template.push(' ');
1815
+ const id = flush_node();
1816
+ state.update.push(b.stmt(b.call('$.set_text', id, expression)));
1817
+ }
1818
+ } else if (node.type === 'ForOfStatement') {
1819
+ const is_controlled = normalized.length === 1;
1820
+ node.is_controlled = is_controlled;
1821
+ visit(node, { ...state, flush_node });
1822
+ } else if (node.type === 'IfStatement') {
1823
+ const is_controlled = normalized.length === 1;
1824
+ node.is_controlled = is_controlled;
1825
+ visit(node, { ...state, flush_node });
1826
+ } else if (node.type === 'TryStatement') {
1827
+ const is_controlled = normalized.length === 1;
1828
+ node.is_controlled = is_controlled;
1829
+ visit(node, { ...state, flush_node });
1830
+ } else if (node.type === 'RenderFragment') {
1831
+ const is_controlled = normalized.length === 1;
1832
+ node.is_controlled = is_controlled;
1833
+ visit(node, { ...state, flush_node });
1834
+ } else {
1835
+ debugger;
1836
+ }
1837
+ }
1838
+ }
1839
+
1840
+ if (root && initial !== null && template_id !== null) {
1841
+ const flags = is_fragment ? b.literal(TEMPLATE_FRAGMENT) : b.literal(0);
1842
+ state.final.push(b.stmt(b.call('$.append', b.id('__anchor'), initial)));
1843
+ state.hoisted.push(
1844
+ b.var(template_id, b.call('$.template', join_template(state.template), flags)),
1845
+ );
1846
+ }
1783
1847
  }
1784
1848
 
1785
1849
  function transform_body(body, { visit, state }) {
1786
- const body_state = {
1787
- ...state,
1788
- template: [],
1789
- setup: [],
1790
- init: [],
1791
- update: [],
1792
- final: [],
1793
- metadata: state.metadata,
1794
- };
1795
-
1796
- transform_children(body, { visit, state: body_state, root: true });
1797
-
1798
- if (body_state.update.length > 0) {
1799
- body_state.init.push(b.stmt(b.call('$.render', b.thunk(b.block(body_state.update)))));
1800
- }
1801
-
1802
- return [...body_state.setup, ...body_state.init, ...body_state.final];
1850
+ const body_state = {
1851
+ ...state,
1852
+ template: [],
1853
+ setup: [],
1854
+ init: [],
1855
+ update: [],
1856
+ final: [],
1857
+ metadata: state.metadata,
1858
+ };
1859
+
1860
+ transform_children(body, { visit, state: body_state, root: true });
1861
+
1862
+ if (body_state.update.length > 0) {
1863
+ body_state.init.push(b.stmt(b.call('$.render', b.thunk(b.block(body_state.update)))));
1864
+ }
1865
+
1866
+ return [...body_state.setup, ...body_state.init, ...body_state.final];
1803
1867
  }
1804
1868
 
1805
1869
  export function transform(filename, source, analysis, to_ts) {
1806
- const state = {
1807
- imports: new Set(),
1808
- events: new Set(),
1809
- template: null,
1810
- hoisted: [],
1811
- setup: null,
1812
- init: null,
1813
- update: null,
1814
- final: null,
1815
- flush_node: null,
1816
- scope: analysis.scope,
1817
- scopes: analysis.scopes,
1818
- stylesheets: [],
1819
- to_ts,
1820
- };
1821
-
1822
- const program = /** @type {ESTree.Program} */ (walk(analysis.ast, state, visitors));
1823
-
1824
- for (const hoisted of state.hoisted) {
1825
- program.body.unshift(hoisted);
1826
- }
1827
-
1828
- for (const import_node of state.imports) {
1829
- program.body.unshift(b.stmt(b.id(import_node)));
1830
- }
1831
-
1832
- if (state.events.size > 0) {
1833
- program.body.push(
1834
- b.stmt(
1835
- b.call('$.delegate', b.array(Array.from(state.events).map((name) => b.literal(name)))),
1836
- ),
1837
- );
1838
- }
1839
-
1840
- const js = print(
1841
- program,
1842
- tsx({
1843
- comments: analysis.ast.comments || [],
1844
- }),
1845
- {
1846
- sourceMapContent: source,
1847
- sourceMapSource: path.basename(filename),
1848
- },
1849
- );
1850
-
1851
- const css = render_stylesheets(state.stylesheets);
1852
-
1853
- return {
1854
- ast: program,
1855
- js,
1856
- css,
1857
- };
1870
+ const state = {
1871
+ imports: new Set(),
1872
+ events: new Set(),
1873
+ template: null,
1874
+ hoisted: [],
1875
+ setup: null,
1876
+ init: null,
1877
+ update: null,
1878
+ final: null,
1879
+ flush_node: null,
1880
+ scope: analysis.scope,
1881
+ scopes: analysis.scopes,
1882
+ stylesheets: [],
1883
+ to_ts,
1884
+ };
1885
+
1886
+ const program = /** @type {ESTree.Program} */ (walk(analysis.ast, state, visitors));
1887
+
1888
+ for (const hoisted of state.hoisted) {
1889
+ program.body.unshift(hoisted);
1890
+ }
1891
+
1892
+ for (const import_node of state.imports) {
1893
+ program.body.unshift(b.stmt(b.id(import_node)));
1894
+ }
1895
+
1896
+ if (state.events.size > 0) {
1897
+ program.body.push(
1898
+ b.stmt(
1899
+ b.call('$.delegate', b.array(Array.from(state.events).map((name) => b.literal(name)))),
1900
+ ),
1901
+ );
1902
+ }
1903
+
1904
+ const js = print(
1905
+ program,
1906
+ tsx({
1907
+ comments: analysis.ast.comments || [],
1908
+ }),
1909
+ {
1910
+ sourceMapContent: source,
1911
+ sourceMapSource: path.basename(filename),
1912
+ },
1913
+ );
1914
+
1915
+ const css = render_stylesheets(state.stylesheets);
1916
+
1917
+ return {
1918
+ ast: program,
1919
+ js,
1920
+ css,
1921
+ };
1858
1922
  }