@katabatic/compiler 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (78) hide show
  1. package/README.md +3 -0
  2. package/package.json +32 -0
  3. package/src/analyse/context.js +31 -0
  4. package/src/analyse/index.js +47 -0
  5. package/src/analyse/visitors/AssignmentExpression.js +14 -0
  6. package/src/analyse/visitors/Attribute.js +11 -0
  7. package/src/analyse/visitors/CallExpression.js +36 -0
  8. package/src/analyse/visitors/ClassBody.js +20 -0
  9. package/src/analyse/visitors/ClassDeclaration.js +5 -0
  10. package/src/analyse/visitors/CustomElement.js +10 -0
  11. package/src/analyse/visitors/EachBlock.js +10 -0
  12. package/src/analyse/visitors/Element.js +16 -0
  13. package/src/analyse/visitors/ExpressionTag.js +9 -0
  14. package/src/analyse/visitors/IfBlock.js +16 -0
  15. package/src/analyse/visitors/ImportDeclaration.js +35 -0
  16. package/src/analyse/visitors/MethodDefinition.js +17 -0
  17. package/src/analyse/visitors/Program.js +24 -0
  18. package/src/analyse/visitors/PropertyDefinition.js +12 -0
  19. package/src/analyse/visitors/Selector.js +23 -0
  20. package/src/analyse/visitors/Template.js +14 -0
  21. package/src/builders.js +1663 -0
  22. package/src/checkers.js +120 -0
  23. package/src/css-matcher.js +100 -0
  24. package/src/css-transform.js +21 -0
  25. package/src/css.js +65 -0
  26. package/src/exp-matcher.js +65 -0
  27. package/src/id-matcher.js +17 -0
  28. package/src/index.js +19 -0
  29. package/src/module-matcher.js +17 -0
  30. package/src/parser/attributes.js +66 -0
  31. package/src/parser/each-block.js +73 -0
  32. package/src/parser/element.js +115 -0
  33. package/src/parser/expression.js +44 -0
  34. package/src/parser/if-block.js +71 -0
  35. package/src/parser/index.js +10 -0
  36. package/src/parser/parser.js +259 -0
  37. package/src/parser/root.js +33 -0
  38. package/src/parser/script.js +59 -0
  39. package/src/parser/style.js +57 -0
  40. package/src/parser/template.js +30 -0
  41. package/src/parser/tokentype.js +75 -0
  42. package/src/router/html.js +18 -0
  43. package/src/router/index.js +130 -0
  44. package/src/transform/context.js +74 -0
  45. package/src/transform/index.js +52 -0
  46. package/src/transform/static/index.js +40 -0
  47. package/src/transform/static/visitors/Attribute.js +27 -0
  48. package/src/transform/static/visitors/EachBlock.js +23 -0
  49. package/src/transform/static/visitors/Element.js +17 -0
  50. package/src/transform/static/visitors/ExpressionTag.js +6 -0
  51. package/src/transform/static/visitors/IfBlock.js +28 -0
  52. package/src/transform/static/visitors/Program.js +10 -0
  53. package/src/transform/static/visitors/Script.js +9 -0
  54. package/src/transform/static/visitors/SlotElement.js +8 -0
  55. package/src/transform/static/visitors/Style.js +9 -0
  56. package/src/transform/static/visitors/Template.js +12 -0
  57. package/src/transform/static/visitors/Text.js +5 -0
  58. package/src/transform/visitors/AssignmentExpression.js +7 -0
  59. package/src/transform/visitors/Attribute.js +79 -0
  60. package/src/transform/visitors/CallExpression.js +17 -0
  61. package/src/transform/visitors/ClassBody.js +36 -0
  62. package/src/transform/visitors/CustomElement.js +43 -0
  63. package/src/transform/visitors/EachBlock.js +39 -0
  64. package/src/transform/visitors/Element.js +49 -0
  65. package/src/transform/visitors/ExpressionTag.js +6 -0
  66. package/src/transform/visitors/Fragment.js +42 -0
  67. package/src/transform/visitors/Identifier.js +10 -0
  68. package/src/transform/visitors/IfBlock.js +55 -0
  69. package/src/transform/visitors/ImportDeclaration.js +21 -0
  70. package/src/transform/visitors/MethodDefinition.js +115 -0
  71. package/src/transform/visitors/Program.js +65 -0
  72. package/src/transform/visitors/PropertyDefinition.js +7 -0
  73. package/src/transform/visitors/Selector.js +41 -0
  74. package/src/transform/visitors/Style.js +11 -0
  75. package/src/transform/visitors/Template.js +44 -0
  76. package/src/transform/visitors/Text.js +5 -0
  77. package/src/utils/misc.js +9 -0
  78. package/src/utils/template.js +15 -0
@@ -0,0 +1,1663 @@
1
+ export function declaration(id, init, kind = 'const') {
2
+ if (typeof id === 'string') {
3
+ id = { type: 'Identifier', name: id }
4
+ }
5
+
6
+ return {
7
+ type: 'VariableDeclaration',
8
+ declarations: [
9
+ {
10
+ type: 'VariableDeclarator',
11
+ id,
12
+ init
13
+ }
14
+ ],
15
+ kind
16
+ }
17
+ }
18
+
19
+ export function literal(value) {
20
+ return { type: 'Literal', value }
21
+ }
22
+
23
+ export function binary(operator, left, right) {
24
+ return {
25
+ type: 'BinaryExpression',
26
+ operator,
27
+ left,
28
+ right
29
+ }
30
+ }
31
+
32
+ export function template({ text, expressions }) {
33
+ if (text.length === expressions.length) {
34
+ // template literals must start and end with a text
35
+ text.push('')
36
+ }
37
+
38
+ const quasis = text.map((raw) => ({
39
+ type: 'TemplateElement',
40
+ value: { raw }
41
+ }))
42
+
43
+ return {
44
+ type: 'TemplateLiteral',
45
+ quasis,
46
+ expressions
47
+ }
48
+ }
49
+
50
+ export function assignment(left, right, operator = '=') {
51
+ return {
52
+ type: 'AssignmentExpression',
53
+ operator,
54
+ left,
55
+ right
56
+ }
57
+ }
58
+
59
+ export function member(object, property) {
60
+ if (typeof object === 'string') {
61
+ object = { type: 'Identifier', name: object }
62
+ }
63
+
64
+ if (typeof property === 'string') {
65
+ property = { type: 'Identifier', name: property }
66
+ }
67
+
68
+ return {
69
+ type: 'MemberExpression',
70
+ object,
71
+ property,
72
+ computed: false,
73
+ optional: false
74
+ }
75
+ }
76
+
77
+ export function thisMember(property) {
78
+ if (typeof property === 'string') {
79
+ property = { type: 'Identifier', name: property }
80
+ }
81
+
82
+ return {
83
+ type: 'MemberExpression',
84
+ object: { type: 'ThisExpression' },
85
+ property,
86
+ computed: false,
87
+ optional: false
88
+ }
89
+ }
90
+
91
+ export function eventListener(body) {
92
+ return {
93
+ type: 'ArrowFunctionExpression',
94
+ id: null,
95
+ expression: false,
96
+ generator: false,
97
+ async: false,
98
+ params: [{ type: 'Identifier', name: 'event' }],
99
+ body: {
100
+ type: 'BlockStatement',
101
+ body: [body]
102
+ }
103
+ }
104
+ }
105
+
106
+ export function importNamespace(local, source) {
107
+ return {
108
+ type: 'ImportDeclaration',
109
+ specifiers: [
110
+ {
111
+ type: 'ImportNamespaceSpecifier',
112
+ local
113
+ }
114
+ ],
115
+ source: {
116
+ type: 'Literal',
117
+ value: source
118
+ },
119
+ attributes: []
120
+ }
121
+ }
122
+
123
+ export function importSpecifier(name, source) {
124
+ return {
125
+ type: 'ImportDeclaration',
126
+ specifiers: [
127
+ {
128
+ type: 'ImportSpecifier',
129
+ imported: {
130
+ type: 'Identifier',
131
+ name
132
+ },
133
+ local: {
134
+ type: 'Identifier',
135
+ name
136
+ }
137
+ }
138
+ ],
139
+ source: {
140
+ type: 'Literal',
141
+ value: source
142
+ },
143
+ attributes: []
144
+ }
145
+ }
146
+
147
+ export function importNamespaceSpecifier(local) {
148
+ if (typeof local === 'string') {
149
+ local = { type: 'Identifier', name: local }
150
+ }
151
+
152
+ return { type: 'ImportNamespaceSpecifier', local }
153
+ }
154
+
155
+ export function property(key, value) {
156
+ if (typeof key === 'string') {
157
+ key = { type: 'Identifier', name: key }
158
+ }
159
+
160
+ return { type: 'Property', key, value }
161
+ }
162
+
163
+ export function object() {
164
+ return { type: 'ObjectExpression', properties: [] }
165
+ }
166
+
167
+ export function id(name, isPrivate = false) {
168
+ const type = isPrivate ? 'PrivateIdentifier' : 'Identifier'
169
+
170
+ if (typeof name === 'string') return { type, name }
171
+ return { ...name, type }
172
+ }
173
+
174
+ export function undefined() {
175
+ return { type: 'Identifier', name: 'undefined' }
176
+ }
177
+
178
+ export function thisExp() {
179
+ return { type: 'ThisExpression' }
180
+ }
181
+
182
+ export function exportDec(declaration) {
183
+ return { type: 'ExportNamedDeclaration', declaration }
184
+ }
185
+
186
+ export function returnStmt(argument) {
187
+ return { type: 'ReturnStatement', argument }
188
+ }
189
+
190
+ export function program(body = []) {
191
+ return { type: 'Program', body }
192
+ }
193
+
194
+ export function func(id, body = []) {
195
+ return {
196
+ type: 'FunctionDeclaration',
197
+ id,
198
+ expression: false,
199
+ generator: false,
200
+ async: false,
201
+ params: [],
202
+ body: {
203
+ type: 'BlockStatement',
204
+ body
205
+ }
206
+ }
207
+ }
208
+
209
+ export function array(elements) {
210
+ elements = elements.map((value) => ({ type: 'Literal', value }))
211
+
212
+ return { type: 'ArrayExpression', elements }
213
+ }
214
+
215
+ export function call(callee) {
216
+ return { type: 'CallExpression', callee, arguments: [], optional: false }
217
+ }
218
+
219
+ export function ifStmt(test, consequent, alternate) {
220
+ return {
221
+ type: 'IfStatement',
222
+ test,
223
+ consequent: {
224
+ type: 'BlockStatement',
225
+ body: consequent
226
+ },
227
+ alternate: alternate && {
228
+ type: 'BlockStatement',
229
+ body: alternate
230
+ }
231
+ }
232
+ }
233
+
234
+ export function forStmt(id, right, body) {
235
+ return {
236
+ type: 'ForOfStatement',
237
+ await: false,
238
+ left: {
239
+ type: 'VariableDeclaration',
240
+ declarations: [
241
+ {
242
+ type: 'VariableDeclarator',
243
+ id,
244
+ init: null
245
+ }
246
+ ],
247
+ kind: 'const'
248
+ },
249
+ right,
250
+ body: {
251
+ type: 'BlockStatement',
252
+ body
253
+ }
254
+ }
255
+ }
256
+
257
+ export function unary(operator, argument) {
258
+ return {
259
+ type: 'UnaryExpression',
260
+ operator,
261
+ prefix: true,
262
+ argument
263
+ }
264
+ }
265
+
266
+ export function logical(operator, left, right) {
267
+ return {
268
+ type: 'LogicalExpression',
269
+ operator,
270
+ left,
271
+ right
272
+ }
273
+ }
274
+
275
+ //
276
+ // advanced builders
277
+ //
278
+
279
+ export function textContent(object) {
280
+ if (typeof object === 'string') {
281
+ object = { type: 'Identifier', name: object }
282
+ }
283
+
284
+ return {
285
+ type: 'MemberExpression',
286
+ object,
287
+ property: {
288
+ type: 'Identifier',
289
+ name: 'textContent'
290
+ },
291
+ computed: false,
292
+ optional: false
293
+ }
294
+ }
295
+
296
+ export function innerHTML(object) {
297
+ if (typeof object === 'string') {
298
+ object = { type: 'Identifier', name: object }
299
+ }
300
+
301
+ return {
302
+ type: 'MemberExpression',
303
+ object,
304
+ property: {
305
+ type: 'Identifier',
306
+ name: 'innerHTML'
307
+ },
308
+ computed: false,
309
+ optional: false
310
+ }
311
+ }
312
+
313
+ export function child(object) {
314
+ if (typeof object === 'string') {
315
+ object = { type: 'Identifier', name: object }
316
+ }
317
+
318
+ return {
319
+ type: 'MemberExpression',
320
+ object,
321
+ property: {
322
+ type: 'Identifier',
323
+ name: 'firstChild'
324
+ },
325
+ computed: false,
326
+ optional: false
327
+ }
328
+ }
329
+
330
+ export function sibling(object, count) {
331
+ let stmt = object
332
+ while (count-- > 0) {
333
+ stmt = {
334
+ type: 'MemberExpression',
335
+ object: stmt,
336
+ property: {
337
+ type: 'Identifier',
338
+ name: 'nextSibling'
339
+ },
340
+ computed: false,
341
+ optional: false
342
+ }
343
+ }
344
+ return stmt
345
+ }
346
+
347
+ export function setAttribute(object, attribute, value) {
348
+ if (typeof object === 'string') {
349
+ object = { type: 'Identifier', name: object }
350
+ }
351
+
352
+ if (typeof attribute === 'string') {
353
+ attribute = { type: 'Identifier', name: attribute }
354
+ }
355
+
356
+ if (typeof value === 'string') {
357
+ value = { type: 'Identifier', name: value }
358
+ }
359
+
360
+ return {
361
+ type: 'CallExpression',
362
+ callee: {
363
+ type: 'MemberExpression',
364
+ object,
365
+ property: {
366
+ type: 'Identifier',
367
+ name: 'setAttribute'
368
+ },
369
+ computed: false,
370
+ optional: false
371
+ },
372
+ arguments: [attribute, value],
373
+ optional: false
374
+ }
375
+ }
376
+
377
+ export function setProperty(object, property, right) {
378
+ if (typeof object === 'string') {
379
+ object = { type: 'Identifier', name: object }
380
+ }
381
+
382
+ if (typeof property === 'string') {
383
+ property = { type: 'Identifier', name: property }
384
+ }
385
+
386
+ if (typeof right === 'string') {
387
+ right = { type: 'Identifier', name: right }
388
+ }
389
+
390
+ return {
391
+ type: 'AssignmentExpression',
392
+ operator: '=',
393
+ left: {
394
+ type: 'MemberExpression',
395
+ object,
396
+ property,
397
+ computed: true,
398
+ optional: false
399
+ },
400
+ right
401
+ }
402
+ }
403
+
404
+ export function setSignalProperty(object, property, right) {
405
+ if (typeof object === 'string') {
406
+ object = { type: 'Identifier', name: object }
407
+ }
408
+
409
+ if (typeof property === 'string') {
410
+ property = { type: 'Identifier', name: property }
411
+ }
412
+
413
+ if (typeof right === 'string') {
414
+ right = { type: 'Identifier', name: right }
415
+ }
416
+
417
+ return {
418
+ type: 'AssignmentExpression',
419
+ operator: '=',
420
+ left: {
421
+ type: 'MemberExpression',
422
+ object: {
423
+ type: 'MemberExpression',
424
+ object,
425
+ property,
426
+ computed: true,
427
+ optional: false
428
+ },
429
+ property: { type: 'Identifier', name: 'value' },
430
+ computed: false,
431
+ optional: false
432
+ },
433
+ right
434
+ }
435
+ }
436
+
437
+ export function includes(object, value) {
438
+ if (typeof value === 'string') {
439
+ value = { type: 'Identifier', name: value }
440
+ }
441
+
442
+ return {
443
+ type: 'CallExpression',
444
+ callee: {
445
+ type: 'MemberExpression',
446
+ object,
447
+ property: {
448
+ type: 'Identifier',
449
+ name: 'includes'
450
+ },
451
+ computed: false,
452
+ optional: false
453
+ },
454
+ arguments: [value],
455
+ optional: false
456
+ }
457
+ }
458
+
459
+ export function defineCustomElement(elementName, className) {
460
+ return {
461
+ type: 'ExpressionStatement',
462
+ expression: {
463
+ type: 'CallExpression',
464
+ callee: {
465
+ type: 'MemberExpression',
466
+ object: {
467
+ type: 'Identifier',
468
+ name: 'customElements'
469
+ },
470
+ property: {
471
+ type: 'Identifier',
472
+ name: 'define'
473
+ },
474
+ computed: false,
475
+ optional: false
476
+ },
477
+ arguments: [
478
+ {
479
+ type: 'Literal',
480
+ value: elementName
481
+ },
482
+ {
483
+ type: 'Identifier',
484
+ name: className
485
+ }
486
+ ],
487
+ optional: false
488
+ }
489
+ }
490
+ }
491
+
492
+ export function getCustomElement(value) {
493
+ return {
494
+ type: 'CallExpression',
495
+ callee: {
496
+ type: 'MemberExpression',
497
+ object: {
498
+ type: 'Identifier',
499
+ name: 'customElements'
500
+ },
501
+ property: {
502
+ type: 'Identifier',
503
+ name: 'get'
504
+ },
505
+ computed: false,
506
+ optional: false
507
+ },
508
+ arguments: [
509
+ {
510
+ type: 'Literal',
511
+ value
512
+ }
513
+ ],
514
+ optional: false
515
+ }
516
+ }
517
+
518
+ export function $name(object) {
519
+ if (typeof object === 'string') {
520
+ object = { type: 'Identifier', name: object }
521
+ }
522
+
523
+ return {
524
+ type: 'MemberExpression',
525
+ object,
526
+ property: {
527
+ type: 'Identifier',
528
+ name: '$name'
529
+ },
530
+ computed: false,
531
+ optional: false
532
+ }
533
+ }
534
+
535
+ export function $nameDecl(value) {
536
+ return {
537
+ type: 'ExportNamedDeclaration',
538
+ declaration: {
539
+ type: 'VariableDeclaration',
540
+ declarations: [
541
+ {
542
+ type: 'VariableDeclarator',
543
+ id: {
544
+ type: 'Identifier',
545
+ name: '$name'
546
+ },
547
+ init: { type: 'Literal', value }
548
+ }
549
+ ],
550
+ kind: 'const'
551
+ },
552
+ specifiers: [],
553
+ source: null
554
+ }
555
+ }
556
+
557
+ export function $set(object, nodeId, attribute, value) {
558
+ return {
559
+ type: 'CallExpression',
560
+ callee: {
561
+ type: 'MemberExpression',
562
+ object,
563
+ property: {
564
+ type: 'Identifier',
565
+ name: '$set'
566
+ },
567
+ computed: false,
568
+ optional: false
569
+ },
570
+ arguments: [
571
+ nodeId,
572
+ {
573
+ type: 'Literal',
574
+ value: attribute
575
+ },
576
+ value
577
+ ],
578
+ optional: false
579
+ }
580
+ }
581
+
582
+ export function $setDecl(body) {
583
+ return {
584
+ type: 'ExportNamedDeclaration',
585
+ declaration: {
586
+ type: 'FunctionDeclaration',
587
+ id: {
588
+ type: 'Identifier',
589
+ name: '$set'
590
+ },
591
+ expression: false,
592
+ generator: false,
593
+ async: false,
594
+ params: [
595
+ { type: 'Identifier', name: 'node' },
596
+ { type: 'Identifier', name: 'attribute' },
597
+ { type: 'Identifier', name: 'value' }
598
+ ],
599
+ body: {
600
+ type: 'BlockStatement',
601
+ body
602
+ }
603
+ },
604
+ specifiers: [],
605
+ source: null
606
+ }
607
+ }
608
+
609
+ export function $$init(property, value) {
610
+ const argumentsStmts = [
611
+ { type: 'ThisExpression' },
612
+ {
613
+ type: 'Literal',
614
+ value: property
615
+ }
616
+ ]
617
+
618
+ if (value) {
619
+ argumentsStmts.push(value)
620
+ }
621
+
622
+ return {
623
+ type: 'CallExpression',
624
+ callee: {
625
+ type: 'MemberExpression',
626
+ object: {
627
+ type: 'Identifier',
628
+ name: '$$'
629
+ },
630
+ property: {
631
+ type: 'Identifier',
632
+ name: 'init'
633
+ },
634
+ computed: false,
635
+ optional: false
636
+ },
637
+ arguments: argumentsStmts,
638
+ optional: false
639
+ }
640
+ }
641
+
642
+ export function $instrument(value) {
643
+ return {
644
+ type: 'CallExpression',
645
+ callee: {
646
+ type: 'MemberExpression',
647
+ object: { type: 'ThisExpression' },
648
+ property: {
649
+ type: 'MemberExpression',
650
+ object: {
651
+ type: 'Identifier',
652
+ name: '$'
653
+ },
654
+ property: {
655
+ type: 'Identifier',
656
+ name: 'instrument'
657
+ },
658
+ computed: false,
659
+ optional: false
660
+ },
661
+ computed: false,
662
+ optional: false
663
+ },
664
+ arguments: [{ type: 'Literal', value }],
665
+ optional: false
666
+ }
667
+ }
668
+
669
+ export function $getBindingProp(object, value, body) {
670
+ return {
671
+ type: 'CallExpression',
672
+ callee: {
673
+ type: 'MemberExpression',
674
+ object: { type: 'ThisExpression' },
675
+ property: {
676
+ type: 'MemberExpression',
677
+ object: {
678
+ type: 'Identifier',
679
+ name: '$'
680
+ },
681
+ property: {
682
+ type: 'Identifier',
683
+ name: 'getBindingProp'
684
+ },
685
+ computed: false,
686
+ optional: false
687
+ },
688
+ computed: false,
689
+ optional: false
690
+ },
691
+ arguments: [
692
+ object,
693
+ { type: 'Literal', value },
694
+ {
695
+ type: 'ArrowFunctionExpression',
696
+ id: null,
697
+ expression: false,
698
+ generator: false,
699
+ async: false,
700
+ params: [{ type: 'Identifier', name: 'v' }],
701
+ body
702
+ }
703
+ ],
704
+ optional: false
705
+ }
706
+ }
707
+
708
+ export function $setBindingProp(object, value, arg) {
709
+ return {
710
+ type: 'CallExpression',
711
+ callee: {
712
+ type: 'MemberExpression',
713
+ object: { type: 'ThisExpression' },
714
+ property: {
715
+ type: 'MemberExpression',
716
+ object: {
717
+ type: 'Identifier',
718
+ name: '$'
719
+ },
720
+ property: {
721
+ type: 'Identifier',
722
+ name: 'setBindingProp'
723
+ },
724
+ computed: false,
725
+ optional: false
726
+ },
727
+ computed: false,
728
+ optional: false
729
+ },
730
+ arguments: [object, { type: 'Literal', value }, arg],
731
+ optional: false
732
+ }
733
+ }
734
+
735
+ export function createElement(value) {
736
+ return {
737
+ type: 'CallExpression',
738
+ callee: {
739
+ type: 'MemberExpression',
740
+ object: { type: 'Identifier', name: 'document' },
741
+ property: { type: 'Identifier', name: 'createElement' },
742
+ computed: false,
743
+ optional: false
744
+ },
745
+ arguments: [{ type: 'Literal', value }],
746
+ optional: false
747
+ }
748
+ }
749
+
750
+ export function $() {
751
+ return {
752
+ type: 'MemberExpression',
753
+ object: {
754
+ type: 'ThisExpression'
755
+ },
756
+ property: {
757
+ type: 'Identifier',
758
+ name: '$'
759
+ },
760
+ computed: false,
761
+ optional: false
762
+ }
763
+ }
764
+
765
+ export function $$() {
766
+ return {
767
+ type: 'CallExpression',
768
+ callee: {
769
+ type: 'Identifier',
770
+ name: '$$'
771
+ },
772
+ arguments: [{ type: 'ThisExpression' }],
773
+ optional: false
774
+ }
775
+ }
776
+
777
+ export function shadow() {
778
+ return {
779
+ type: 'MemberExpression',
780
+ object: {
781
+ type: 'ThisExpression'
782
+ },
783
+ property: {
784
+ type: 'Identifier',
785
+ name: 'shadow'
786
+ },
787
+ computed: false,
788
+ optional: false
789
+ }
790
+ }
791
+
792
+ export function attachShadow(value = 'open') {
793
+ return {
794
+ type: 'CallExpression',
795
+ callee: {
796
+ type: 'MemberExpression',
797
+ object: { type: 'ThisExpression' },
798
+ property: {
799
+ type: 'Identifier',
800
+ name: 'attachShadow'
801
+ },
802
+ computed: false,
803
+ optional: false
804
+ },
805
+ arguments: [
806
+ {
807
+ type: 'ObjectExpression',
808
+ properties: [
809
+ {
810
+ type: 'Property',
811
+ method: false,
812
+ shorthand: false,
813
+ computed: false,
814
+ key: {
815
+ type: 'Identifier',
816
+ name: 'mode'
817
+ },
818
+ value: {
819
+ type: 'Literal',
820
+ value
821
+ },
822
+ kind: 'init'
823
+ }
824
+ ]
825
+ }
826
+ ],
827
+ optional: false
828
+ }
829
+ }
830
+
831
+ export function replaceChildren(object, node) {
832
+ return {
833
+ type: 'CallExpression',
834
+ callee: {
835
+ type: 'MemberExpression',
836
+ object,
837
+ property: {
838
+ type: 'Identifier',
839
+ name: 'replaceChildren'
840
+ },
841
+ computed: false,
842
+ optional: false
843
+ },
844
+ arguments: [node],
845
+ optional: false
846
+ }
847
+ }
848
+
849
+ export function insertAdjacentHTML(object, value) {
850
+ if (typeof object === 'string') {
851
+ object = { type: 'Identifier', name: object }
852
+ }
853
+
854
+ return {
855
+ type: 'CallExpression',
856
+ callee: {
857
+ type: 'MemberExpression',
858
+ object,
859
+ property: {
860
+ type: 'Identifier',
861
+ name: 'insertAdjacentHTML'
862
+ },
863
+ computed: false,
864
+ optional: false
865
+ },
866
+ arguments: [
867
+ {
868
+ type: 'Literal',
869
+ value: 'beforeend'
870
+ },
871
+ value
872
+ ],
873
+ optional: false
874
+ }
875
+ }
876
+
877
+ export function insertBefore(node, insertNode) {
878
+ if (typeof node === 'string') {
879
+ node = { type: 'Identifier', name: node }
880
+ }
881
+
882
+ return {
883
+ type: 'ExpressionStatement',
884
+ expression: {
885
+ type: 'CallExpression',
886
+ callee: {
887
+ type: 'MemberExpression',
888
+ object: {
889
+ type: 'MemberExpression',
890
+ object: node,
891
+ property: {
892
+ type: 'Identifier',
893
+ name: 'parentNode'
894
+ },
895
+ computed: false,
896
+ optional: false
897
+ },
898
+ property: {
899
+ type: 'Identifier',
900
+ name: 'insertBefore'
901
+ },
902
+ computed: false,
903
+ optional: false
904
+ },
905
+ arguments: [insertNode, node],
906
+ optional: false
907
+ }
908
+ }
909
+ }
910
+
911
+ export function appendChild(object, node) {
912
+ if (typeof node === 'string') {
913
+ node = { type: 'Identifier', name: node }
914
+ }
915
+
916
+ return {
917
+ type: 'ExpressionStatement',
918
+ expression: {
919
+ type: 'CallExpression',
920
+ callee: {
921
+ type: 'MemberExpression',
922
+ object,
923
+ property: {
924
+ type: 'Identifier',
925
+ name: 'appendChild'
926
+ },
927
+ computed: false,
928
+ optional: false
929
+ },
930
+ arguments: [node],
931
+ optional: false
932
+ }
933
+ }
934
+ }
935
+
936
+ export function queueMicrotask(body) {
937
+ return {
938
+ type: 'ExpressionStatement',
939
+ expression: {
940
+ type: 'CallExpression',
941
+ callee: { type: 'Identifier', name: 'queueMicrotask' },
942
+ arguments: [
943
+ {
944
+ type: 'ArrowFunctionExpression',
945
+ id: null,
946
+ expression: false,
947
+ generator: false,
948
+ async: false,
949
+ params: [],
950
+ body: {
951
+ type: 'BlockStatement',
952
+ body
953
+ }
954
+ }
955
+ ],
956
+ optional: false
957
+ }
958
+ }
959
+ }
960
+
961
+ export function connectedMoveCallback() {
962
+ return {
963
+ type: 'ExpressionStatement',
964
+ expression: {
965
+ type: 'CallExpression',
966
+ callee: {
967
+ type: 'MemberExpression',
968
+ object: {
969
+ type: 'ThisExpression'
970
+ },
971
+ property: {
972
+ type: 'Identifier',
973
+ name: 'connectedMoveCallback'
974
+ },
975
+ computed: false,
976
+ optional: false
977
+ },
978
+ arguments: [],
979
+ optional: true
980
+ }
981
+ }
982
+ }
983
+
984
+ export function $dispose() {
985
+ return {
986
+ type: 'CallExpression',
987
+ callee: {
988
+ type: 'MemberExpression',
989
+ object: {
990
+ type: 'MemberExpression',
991
+ object: {
992
+ type: 'ThisExpression'
993
+ },
994
+ property: {
995
+ type: 'Identifier',
996
+ name: '$'
997
+ },
998
+ computed: false,
999
+ optional: false
1000
+ },
1001
+ property: {
1002
+ type: 'Identifier',
1003
+ name: 'dispose'
1004
+ },
1005
+ computed: false,
1006
+ optional: false
1007
+ },
1008
+ arguments: [],
1009
+ optional: false
1010
+ }
1011
+ }
1012
+
1013
+ export function $lifecycle(value) {
1014
+ return {
1015
+ type: 'CallExpression',
1016
+ callee: {
1017
+ type: 'MemberExpression',
1018
+ object: {
1019
+ type: 'MemberExpression',
1020
+ object: {
1021
+ type: 'ThisExpression'
1022
+ },
1023
+ property: {
1024
+ type: 'Identifier',
1025
+ name: '$'
1026
+ },
1027
+ computed: false,
1028
+ optional: false
1029
+ },
1030
+ property: {
1031
+ type: 'Identifier',
1032
+ name: 'lifecycle'
1033
+ },
1034
+ computed: false,
1035
+ optional: false
1036
+ },
1037
+ arguments: [{ type: 'Literal', value }],
1038
+ optional: false
1039
+ }
1040
+ }
1041
+
1042
+ export function $trackAttribute(argumentsStmt) {
1043
+ argumentsStmt = [...argumentsStmt]
1044
+ argumentsStmt[0] ??= { type: 'Identifier', name: 'name' }
1045
+
1046
+ return {
1047
+ type: 'CallExpression',
1048
+ callee: {
1049
+ type: 'MemberExpression',
1050
+ object: {
1051
+ type: 'MemberExpression',
1052
+ object: {
1053
+ type: 'ThisExpression'
1054
+ },
1055
+ property: {
1056
+ type: 'Identifier',
1057
+ name: '$'
1058
+ },
1059
+ computed: false,
1060
+ optional: false
1061
+ },
1062
+ property: {
1063
+ type: 'Identifier',
1064
+ name: 'trackAttribute'
1065
+ },
1066
+ computed: false,
1067
+ optional: false
1068
+ },
1069
+ arguments: argumentsStmt,
1070
+ optional: false
1071
+ }
1072
+ }
1073
+
1074
+ export function $attributeChanged(argumentsStmt) {
1075
+ argumentsStmt = [...argumentsStmt]
1076
+ argumentsStmt[0] ??= { type: 'Identifier', name: 'name' }
1077
+ argumentsStmt[1] ??= { type: 'Identifier', name: 'value' }
1078
+ argumentsStmt[2] ??= { type: 'Identifier', name: 'nextValue' }
1079
+
1080
+ return {
1081
+ type: 'CallExpression',
1082
+ callee: {
1083
+ type: 'MemberExpression',
1084
+ object: {
1085
+ type: 'MemberExpression',
1086
+ object: {
1087
+ type: 'ThisExpression'
1088
+ },
1089
+ property: {
1090
+ type: 'Identifier',
1091
+ name: '$'
1092
+ },
1093
+ computed: false,
1094
+ optional: false
1095
+ },
1096
+ property: {
1097
+ type: 'Identifier',
1098
+ name: 'attributeChanged'
1099
+ },
1100
+ computed: false,
1101
+ optional: false
1102
+ },
1103
+ arguments: argumentsStmt,
1104
+ optional: false
1105
+ }
1106
+ }
1107
+
1108
+ export function $effect(body) {
1109
+ return {
1110
+ type: 'ExpressionStatement',
1111
+ expression: {
1112
+ type: 'CallExpression',
1113
+ callee: {
1114
+ type: 'MemberExpression',
1115
+ object: {
1116
+ type: 'Identifier',
1117
+ name: '$'
1118
+ },
1119
+ property: {
1120
+ type: 'Identifier',
1121
+ name: 'effect'
1122
+ },
1123
+ computed: false,
1124
+ optional: false
1125
+ },
1126
+ arguments: [
1127
+ {
1128
+ type: 'ArrowFunctionExpression',
1129
+ id: null,
1130
+ expression: false,
1131
+ generator: false,
1132
+ async: false,
1133
+ params: [],
1134
+ body: {
1135
+ type: 'BlockStatement',
1136
+ body
1137
+ }
1138
+ }
1139
+ ],
1140
+ optional: false
1141
+ }
1142
+ }
1143
+ }
1144
+
1145
+ export function $animate(value, body) {
1146
+ return {
1147
+ type: 'ExpressionStatement',
1148
+ expression: {
1149
+ type: 'CallExpression',
1150
+ callee: {
1151
+ type: 'MemberExpression',
1152
+ object: {
1153
+ type: 'Identifier',
1154
+ name: '$'
1155
+ },
1156
+ property: {
1157
+ type: 'Identifier',
1158
+ name: 'animate'
1159
+ },
1160
+ computed: false,
1161
+ optional: false
1162
+ },
1163
+ arguments: [
1164
+ { type: 'Literal', value },
1165
+ {
1166
+ type: 'ArrowFunctionExpression',
1167
+ id: null,
1168
+ expression: false,
1169
+ generator: false,
1170
+ async: false,
1171
+ params: [{ type: 'Identifier', name: 'o' }],
1172
+ body
1173
+ }
1174
+ ],
1175
+ optional: false
1176
+ }
1177
+ }
1178
+ }
1179
+
1180
+ export function addEventListener(object, value, body) {
1181
+ return {
1182
+ type: 'ExpressionStatement',
1183
+ expression: {
1184
+ type: 'CallExpression',
1185
+ callee: {
1186
+ type: 'MemberExpression',
1187
+ object,
1188
+ property: {
1189
+ type: 'Identifier',
1190
+ name: 'addEventListener'
1191
+ },
1192
+ computed: false,
1193
+ optional: false
1194
+ },
1195
+ arguments: [
1196
+ { type: 'Literal', value },
1197
+ {
1198
+ type: 'ArrowFunctionExpression',
1199
+ id: null,
1200
+ expression: false,
1201
+ generator: false,
1202
+ async: false,
1203
+ params: [
1204
+ {
1205
+ type: 'Identifier',
1206
+ name: 'event'
1207
+ }
1208
+ ],
1209
+ body: {
1210
+ type: 'BlockStatement',
1211
+ body
1212
+ }
1213
+ }
1214
+ ],
1215
+ optional: false
1216
+ }
1217
+ }
1218
+ }
1219
+
1220
+ export function constructor(body = []) {
1221
+ return {
1222
+ type: 'MethodDefinition',
1223
+ static: false,
1224
+ computed: false,
1225
+ key: {
1226
+ type: 'Identifier',
1227
+ name: 'constructor'
1228
+ },
1229
+ kind: 'constructor',
1230
+ value: {
1231
+ type: 'FunctionExpression',
1232
+ id: null,
1233
+ expression: false,
1234
+ generator: false,
1235
+ async: false,
1236
+ params: [],
1237
+ body: {
1238
+ type: 'BlockStatement',
1239
+ body: [
1240
+ {
1241
+ type: 'CallExpression',
1242
+ callee: { type: 'Super' },
1243
+ arguments: [],
1244
+ optional: false
1245
+ }
1246
+ ]
1247
+ }
1248
+ }
1249
+ }
1250
+ }
1251
+
1252
+ export function connectedCallback(body = []) {
1253
+ return {
1254
+ type: 'MethodDefinition',
1255
+ static: false,
1256
+ computed: false,
1257
+ key: {
1258
+ type: 'Identifier',
1259
+ name: 'connectedCallback'
1260
+ },
1261
+ kind: 'method',
1262
+ value: {
1263
+ type: 'FunctionExpression',
1264
+ id: null,
1265
+ expression: false,
1266
+ generator: false,
1267
+ async: false,
1268
+ params: [],
1269
+ body: {
1270
+ type: 'BlockStatement',
1271
+ body
1272
+ }
1273
+ }
1274
+ }
1275
+ }
1276
+
1277
+ export function disconnectedCallback(body = []) {
1278
+ return {
1279
+ type: 'MethodDefinition',
1280
+ static: false,
1281
+ computed: false,
1282
+ key: {
1283
+ type: 'Identifier',
1284
+ name: 'disconnectedCallback'
1285
+ },
1286
+ kind: 'method',
1287
+ value: {
1288
+ type: 'FunctionExpression',
1289
+ id: null,
1290
+ expression: false,
1291
+ generator: false,
1292
+ async: false,
1293
+ params: [],
1294
+ body: {
1295
+ type: 'BlockStatement',
1296
+ body
1297
+ }
1298
+ }
1299
+ }
1300
+ }
1301
+
1302
+ export function attributeChangedCallback(body = []) {
1303
+ return {
1304
+ type: 'MethodDefinition',
1305
+ static: false,
1306
+ computed: false,
1307
+ key: {
1308
+ type: 'Identifier',
1309
+ name: 'attributeChangedCallback'
1310
+ },
1311
+ kind: 'method',
1312
+ value: {
1313
+ type: 'FunctionExpression',
1314
+ id: null,
1315
+ expression: false,
1316
+ generator: false,
1317
+ async: false,
1318
+ params: [{ type: 'Identifier', name: 'name' }],
1319
+ body: {
1320
+ type: 'BlockStatement',
1321
+ body
1322
+ }
1323
+ }
1324
+ }
1325
+ }
1326
+
1327
+ export function getAttribute() {
1328
+ return {
1329
+ type: 'MethodDefinition',
1330
+ static: false,
1331
+ computed: false,
1332
+ key: {
1333
+ type: 'Identifier',
1334
+ name: 'getAttribute'
1335
+ },
1336
+ kind: 'method',
1337
+ value: {
1338
+ type: 'FunctionExpression',
1339
+ id: null,
1340
+ expression: false,
1341
+ generator: false,
1342
+ async: false,
1343
+ params: [{ type: 'Identifier', name: 'name' }],
1344
+ body: {
1345
+ type: 'BlockStatement',
1346
+ body: [
1347
+ {
1348
+ type: 'ReturnStatement',
1349
+ argument: {
1350
+ type: 'CallExpression',
1351
+ callee: {
1352
+ type: 'MemberExpression',
1353
+ object: {
1354
+ type: 'Super'
1355
+ },
1356
+ property: {
1357
+ type: 'Identifier',
1358
+ name: 'getAttribute'
1359
+ },
1360
+ computed: false,
1361
+ optional: false
1362
+ },
1363
+ arguments: [{ type: 'Identifier', name: 'name' }],
1364
+ optional: false
1365
+ }
1366
+ }
1367
+ ]
1368
+ }
1369
+ }
1370
+ }
1371
+ }
1372
+
1373
+ export function render(body = []) {
1374
+ return {
1375
+ type: 'FunctionDeclaration',
1376
+ id: {
1377
+ type: 'Identifier',
1378
+ name: 'render'
1379
+ },
1380
+ expression: false,
1381
+ generator: false,
1382
+ async: false,
1383
+ params: [
1384
+ {
1385
+ type: 'Identifier',
1386
+ name: 'data'
1387
+ },
1388
+ {
1389
+ type: 'Identifier',
1390
+ name: 'slot'
1391
+ }
1392
+ ],
1393
+ body: {
1394
+ type: 'BlockStatement',
1395
+ body
1396
+ }
1397
+ }
1398
+ }
1399
+
1400
+ export function signal(id) {
1401
+ return {
1402
+ type: 'MemberExpression',
1403
+ object: {
1404
+ type: 'MemberExpression',
1405
+ object: {
1406
+ type: 'ThisExpression'
1407
+ },
1408
+ property: id,
1409
+ computed: false,
1410
+ optional: false
1411
+ },
1412
+ property: {
1413
+ type: 'Identifier',
1414
+ name: 'value'
1415
+ },
1416
+ computed: false,
1417
+ optional: false
1418
+ }
1419
+ }
1420
+
1421
+ export function ifBlock(anchor, test, consequent, alternate) {
1422
+ const argumentsStmts = [
1423
+ anchor,
1424
+ {
1425
+ type: 'ArrowFunctionExpression',
1426
+ id: null,
1427
+ expression: true,
1428
+ generator: false,
1429
+ async: false,
1430
+ params: [],
1431
+ body: test
1432
+ },
1433
+ {
1434
+ type: 'ArrowFunctionExpression',
1435
+ id: null,
1436
+ expression: true,
1437
+ generator: false,
1438
+ async: false,
1439
+ params: [
1440
+ { type: 'Identifier', name: '$' },
1441
+ { type: 'Identifier', name: 'anchor' }
1442
+ ],
1443
+ body: { type: 'BlockStatement', body: consequent }
1444
+ }
1445
+ ]
1446
+
1447
+ if (alternate) {
1448
+ argumentsStmts.push({
1449
+ type: 'ArrowFunctionExpression',
1450
+ id: null,
1451
+ expression: true,
1452
+ generator: false,
1453
+ async: false,
1454
+ params: [
1455
+ { type: 'Identifier', name: '$' },
1456
+ { type: 'Identifier', name: 'anchor' }
1457
+ ],
1458
+ body: { type: 'BlockStatement', body: alternate }
1459
+ })
1460
+ }
1461
+
1462
+ return {
1463
+ type: 'ExpressionStatement',
1464
+ expression: {
1465
+ type: 'CallExpression',
1466
+ callee: {
1467
+ type: 'MemberExpression',
1468
+ object: {
1469
+ type: 'Identifier',
1470
+ name: '$'
1471
+ },
1472
+ property: {
1473
+ type: 'Identifier',
1474
+ name: 'ifBlock'
1475
+ },
1476
+ computed: false,
1477
+ optional: false
1478
+ },
1479
+ arguments: argumentsStmts,
1480
+ optional: false
1481
+ }
1482
+ }
1483
+ }
1484
+
1485
+ export function eachBlock(anchor, expression, context, key, body) {
1486
+ let keyStmt
1487
+
1488
+ if (key) {
1489
+ keyStmt = {
1490
+ type: 'ArrowFunctionExpression',
1491
+ id: null,
1492
+ expression: true,
1493
+ generator: false,
1494
+ async: false,
1495
+ params: [context],
1496
+ body: key
1497
+ }
1498
+ } else {
1499
+ keyStmt = {
1500
+ type: 'ArrowFunctionExpression',
1501
+ id: null,
1502
+ expression: true,
1503
+ generator: false,
1504
+ async: false,
1505
+ params: [
1506
+ { type: 'Identifier', name: 'v' },
1507
+ { type: 'Identifier', name: 'i' }
1508
+ ],
1509
+ body: { type: 'Identifier', name: 'i' }
1510
+ }
1511
+ }
1512
+
1513
+ return {
1514
+ type: 'ExpressionStatement',
1515
+ expression: {
1516
+ type: 'CallExpression',
1517
+ callee: {
1518
+ type: 'MemberExpression',
1519
+ object: {
1520
+ type: 'Identifier',
1521
+ name: '$'
1522
+ },
1523
+ property: {
1524
+ type: 'Identifier',
1525
+ name: 'eachBlock'
1526
+ },
1527
+ computed: false,
1528
+ optional: false
1529
+ },
1530
+ arguments: [
1531
+ anchor,
1532
+ {
1533
+ type: 'ArrowFunctionExpression',
1534
+ id: null,
1535
+ expression: true,
1536
+ generator: false,
1537
+ async: false,
1538
+ params: [],
1539
+ body: expression
1540
+ },
1541
+ keyStmt,
1542
+ {
1543
+ type: 'ArrowFunctionExpression',
1544
+ id: null,
1545
+ expression: true,
1546
+ generator: false,
1547
+ async: false,
1548
+ params: [
1549
+ { type: 'Identifier', name: '$' },
1550
+ { type: 'Identifier', name: 'anchor' },
1551
+ context
1552
+ ],
1553
+ body: { type: 'BlockStatement', body }
1554
+ }
1555
+ ],
1556
+ optional: false
1557
+ }
1558
+ }
1559
+ }
1560
+
1561
+ export function $block(body) {
1562
+ return {
1563
+ type: 'ExpressionStatement',
1564
+ expression: {
1565
+ type: 'CallExpression',
1566
+ callee: {
1567
+ type: 'MemberExpression',
1568
+ object: {
1569
+ type: 'MemberExpression',
1570
+ object: {
1571
+ type: 'ThisExpression'
1572
+ },
1573
+ property: {
1574
+ type: 'Identifier',
1575
+ name: '$'
1576
+ },
1577
+ computed: false,
1578
+ optional: false
1579
+ },
1580
+ property: {
1581
+ type: 'Identifier',
1582
+ name: 'block'
1583
+ },
1584
+ computed: false,
1585
+ optional: false
1586
+ },
1587
+ arguments: [
1588
+ {
1589
+ type: 'ArrowFunctionExpression',
1590
+ id: null,
1591
+ expression: true,
1592
+ generator: false,
1593
+ async: false,
1594
+ params: [{ type: 'Identifier', name: '$' }],
1595
+ body: { type: 'BlockStatement', body }
1596
+ }
1597
+ ],
1598
+ optional: false
1599
+ }
1600
+ }
1601
+ }
1602
+
1603
+ export function $boundary(body) {
1604
+ return {
1605
+ type: 'ExpressionStatement',
1606
+ expression: {
1607
+ type: 'CallExpression',
1608
+ callee: {
1609
+ type: 'MemberExpression',
1610
+ object: {
1611
+ type: 'MemberExpression',
1612
+ object: {
1613
+ type: 'ThisExpression'
1614
+ },
1615
+ property: {
1616
+ type: 'Identifier',
1617
+ name: '$'
1618
+ },
1619
+ computed: false,
1620
+ optional: false
1621
+ },
1622
+ property: {
1623
+ type: 'Identifier',
1624
+ name: 'boundary'
1625
+ },
1626
+ computed: false,
1627
+ optional: false
1628
+ },
1629
+ arguments: [
1630
+ {
1631
+ type: 'ArrowFunctionExpression',
1632
+ id: null,
1633
+ expression: true,
1634
+ generator: false,
1635
+ async: false,
1636
+ params: [],
1637
+ body: { type: 'BlockStatement', body }
1638
+ }
1639
+ ],
1640
+ optional: false
1641
+ }
1642
+ }
1643
+ }
1644
+
1645
+ // html
1646
+
1647
+ export function attribute(name, value, metadata) {
1648
+ if (typeof value === 'string') {
1649
+ value = [{ type: 'Text', data: value }]
1650
+ }
1651
+
1652
+ return { type: 'Attribute', name, value, metadata }
1653
+ }
1654
+
1655
+ export function text(data) {
1656
+ return { type: 'Text', data }
1657
+ }
1658
+
1659
+ // css
1660
+
1661
+ export function classSelector(name) {
1662
+ return { type: 'ClassSelector', name }
1663
+ }