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