ripple 0.2.75 → 0.2.77

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