macroforge_ts 0.1.80

TypeScript macro expansion engine - write compile-time macros in Rust
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
//! IR Variant Coverage Tests - Expressions
//!
//! Tests for: ClassExpr, FnExpr, UnaryExpr, UpdateExpr, AwaitExpr, YieldExpr,
//! ParenExpr, SeqExpr, OptChainExpr, TsAsExpr, TsNonNullExpr, TsSatisfiesExpr,
//! TsInstantiation, SuperExpr, TaggedTpl, PrivateName, BigIntLit

use macroforge_ts::macros::ts_template;

// =============================================================================
// Class Expressions
// =============================================================================

#[test]
fn test_class_expr_anonymous() {
    let stream = ts_template! {
        const MyClass = class {
            value: number;
            constructor() {
                this.value = 0;
            }
        };
    };
    let source = stream.source();
    assert!(
        source.contains("= class") || source.contains("=class"),
        "Expected '= class'. Got:\n{}",
        source
    );
}

#[test]
fn test_class_expr_named() {
    let stream = ts_template! {
        const Factory = class FactoryImpl {
            create() {
                return new FactoryImpl();
            }
        };
    };
    let source = stream.source();
    assert!(
        source.contains("class FactoryImpl") || source.contains("class  FactoryImpl"),
        "Expected 'class FactoryImpl'. Got:\n{}",
        source
    );
}

#[test]
fn test_class_expr_with_extends() {
    let stream = ts_template! {
        const Child = class extends Parent {
            constructor() {
                super();
            }
        };
    };
    let source = stream.source();
    assert!(
        source.contains("extends Parent") || source.contains("extends  Parent"),
        "Expected 'extends Parent'. Got:\n{}",
        source
    );
}

// =============================================================================
// Function Expressions
// =============================================================================

#[test]
fn test_fn_expr_anonymous() {
    let stream = ts_template! {
        const handler = function(event: Event): void {
            console.log(event);
        };
    };
    let source = stream.source();
    assert!(
        source.contains("function(") || source.contains("function ("),
        "Expected 'function('. Got:\n{}",
        source
    );
}

#[test]
fn test_fn_expr_named() {
    let stream = ts_template! {
        const factorial = function fact(n: number): number {
            return n <= 1 ? 1 : n * fact(n - 1);
        };
    };
    let source = stream.source();
    assert!(
        source.contains("function fact") || source.contains("function  fact"),
        "Expected 'function fact'. Got:\n{}",
        source
    );
}

#[test]
fn test_fn_expr_async() {
    let stream = ts_template! {
        const fetchData = async function() {
            return await fetch(url);
        };
    };
    let source = stream.source();
    assert!(
        source.contains("async") && source.contains("function"),
        "Expected 'async function'. Got:\n{}",
        source
    );
}

#[test]
fn test_fn_expr_generator() {
    let stream = ts_template! {
        const gen = function*() {
            yield 1;
            yield 2;
        };
    };
    let source = stream.source();
    assert!(
        source.contains("function*") || source.contains("function *"),
        "Expected 'function*'. Got:\n{}",
        source
    );
}

// =============================================================================
// Unary Expressions
// =============================================================================

#[test]
fn test_unary_expr_minus() {
    let stream = ts_template! {
        const negative = -value;
    };
    let source = stream.source();
    assert!(
        source.contains("-value") || source.contains("- value"),
        "Expected '-value'. Got:\n{}",
        source
    );
}

#[test]
fn test_unary_expr_plus() {
    let stream = ts_template! {
        const num = +stringValue;
    };
    let source = stream.source();
    assert!(
        source.contains("+stringValue") || source.contains("+ stringValue"),
        "Expected '+stringValue'. Got:\n{}",
        source
    );
}

#[test]
fn test_unary_expr_not() {
    let stream = ts_template! {
        const inverted = !condition;
    };
    let source = stream.source();
    assert!(
        source.contains("!condition") || source.contains("! condition"),
        "Expected '!condition'. Got:\n{}",
        source
    );
}

#[test]
fn test_unary_expr_bitwise_not() {
    let stream = ts_template! {
        const flipped = ~bits;
    };
    let source = stream.source();
    assert!(
        source.contains("~bits") || source.contains("~ bits"),
        "Expected '~bits'. Got:\n{}",
        source
    );
}

#[test]
fn test_unary_expr_typeof() {
    let stream = ts_template! {
        const typeStr = typeof value;
    };
    let source = stream.source();
    assert!(
        source.contains("typeof value") || source.contains("typeof  value"),
        "Expected 'typeof value'. Got:\n{}",
        source
    );
}

#[test]
fn test_unary_expr_void() {
    let stream = ts_template! {
        void runSideEffect();
    };
    let source = stream.source();
    assert!(
        source.contains("void runSideEffect") || source.contains("void  runSideEffect"),
        "Expected 'void runSideEffect'. Got:\n{}",
        source
    );
}

#[test]
fn test_unary_expr_delete() {
    let stream = ts_template! {
        delete obj.property;
    };
    let source = stream.source();
    assert!(
        source.contains("delete obj") || source.contains("delete  obj"),
        "Expected 'delete obj'. Got:\n{}",
        source
    );
}

// =============================================================================
// Update Expressions
// =============================================================================

#[test]
fn test_update_expr_postfix_increment() {
    let stream = ts_template! {
        counter++;
    };
    let source = stream.source();
    assert!(
        source.contains("counter++") || source.contains("counter ++"),
        "Expected 'counter++'. Got:\n{}",
        source
    );
}

#[test]
fn test_update_expr_prefix_increment() {
    let stream = ts_template! {
        const next = ++counter;
    };
    let source = stream.source();
    assert!(
        source.contains("++counter") || source.contains("++ counter"),
        "Expected '++counter'. Got:\n{}",
        source
    );
}

#[test]
fn test_update_expr_postfix_decrement() {
    let stream = ts_template! {
        remaining--;
    };
    let source = stream.source();
    assert!(
        source.contains("remaining--") || source.contains("remaining --"),
        "Expected 'remaining--'. Got:\n{}",
        source
    );
}

#[test]
fn test_update_expr_prefix_decrement() {
    let stream = ts_template! {
        const prev = --counter;
    };
    let source = stream.source();
    assert!(
        source.contains("--counter") || source.contains("-- counter"),
        "Expected '--counter'. Got:\n{}",
        source
    );
}

// =============================================================================
// Await Expressions
// =============================================================================

#[test]
fn test_await_expr_simple() {
    let stream = ts_template! {
        async function load() {
            const data = await fetch(url);
            return data;
        }
    };
    let source = stream.source();
    assert!(
        source.contains("await fetch") || source.contains("await  fetch"),
        "Expected 'await fetch'. Got:\n{}",
        source
    );
}

#[test]
fn test_await_expr_chained() {
    let stream = ts_template! {
        async function getJson() {
            const response = await fetch(url);
            const json = await response.json();
            return json;
        }
    };
    let source = stream.source();
    assert!(
        source.contains("await") && source.contains("json"),
        "Expected await and json. Got:\n{}",
        source
    );
}

// =============================================================================
// Yield Expressions
// =============================================================================

#[test]
fn test_yield_expr_simple() {
    let stream = ts_template! {
        function* generator() {
            yield 1;
            yield 2;
            yield 3;
        }
    };
    let source = stream.source();
    assert!(
        source.contains("yield"),
        "Expected 'yield'. Got:\n{}",
        source
    );
}

#[test]
fn test_yield_expr_with_value() {
    let stream = ts_template! {
        function* range(start: number, end: number) {
            for (let i = start; i < end; i++) {
                yield i;
            }
        }
    };
    let source = stream.source();
    assert!(
        source.contains("yield i") || source.contains("yield  i"),
        "Expected 'yield i'. Got:\n{}",
        source
    );
}

#[test]
fn test_yield_expr_delegate() {
    let stream = ts_template! {
        function* composed() {
            yield* [1, 2, 3];
        }
    };
    let source = stream.source();
    assert!(
        source.contains("yield*") || source.contains("yield *"),
        "Expected 'yield*'. Got:\n{}",
        source
    );
}

// =============================================================================
// Parenthesized Expressions
// =============================================================================

#[test]
fn test_paren_expr_arithmetic() {
    let stream = ts_template! {
        const result = (a + b) * c;
    };
    let source = stream.source();
    // Parentheses should be present for correct precedence
    assert!(
        source.contains("(") && source.contains(")"),
        "Expected parentheses. Got:\n{}",
        source
    );
    assert!(
        source.contains("a") && source.contains("b") && source.contains("c"),
        "Expected a, b, c. Got:\n{}",
        source
    );
}

#[test]
fn test_paren_expr_for_precedence() {
    let stream = ts_template! {
        const value = (condition ? a : b).property;
    };
    let source = stream.source();
    assert!(
        source.contains("condition"),
        "Expected 'condition'. Got:\n{}",
        source
    );
    assert!(
        source.contains("property"),
        "Expected 'property'. Got:\n{}",
        source
    );
}

// =============================================================================
// Sequence Expressions
// =============================================================================

#[test]
fn test_seq_expr_simple() {
    let stream = ts_template! {
        const last = (a = 1, b = 2, c = 3);
    };
    let source = stream.source();
    assert!(
        source.contains("a") && source.contains("b") && source.contains("c"),
        "Expected a, b, c. Got:\n{}",
        source
    );
}

#[test]
fn test_seq_expr_in_for() {
    let stream = ts_template! {
        for (let i = 0, j = 10; i < j; i++, j--) {
            console.log(i, j);
        }
    };
    let source = stream.source();
    assert!(
        source.contains("i") && source.contains("j"),
        "Expected 'i' and 'j'. Got:\n{}",
        source
    );
}

// =============================================================================
// Optional Chaining
// =============================================================================

#[test]
fn test_opt_chain_property() {
    let stream = ts_template! {
        const name = user?.profile?.name;
    };
    let source = stream.source();
    assert!(source.contains("?."), "Expected '?.'. Got:\n{}", source);
}

#[test]
fn test_opt_chain_element() {
    let stream = ts_template! {
        const first = array?.[0];
    };
    let source = stream.source();
    assert!(source.contains("?.["), "Expected '?.['. Got:\n{}", source);
}

#[test]
fn test_opt_chain_call() {
    let stream = ts_template! {
        const result = callback?.();
    };
    let source = stream.source();
    assert!(source.contains("?.("), "Expected '?.('. Got:\n{}", source);
}

// =============================================================================
// TypeScript Type Assertions
// =============================================================================

#[test]
fn test_ts_as_expr() {
    let stream = ts_template! {
        const str = value as string;
    };
    let source = stream.source();
    assert!(
        source.contains("as string") || source.contains("as  string"),
        "Expected 'as string'. Got:\n{}",
        source
    );
}

#[test]
fn test_ts_as_const() {
    let stream = ts_template! {
        const tuple = [1, 2, 3] as const;
    };
    let source = stream.source();
    assert!(
        source.contains("as const") || source.contains("as  const"),
        "Expected 'as const'. Got:\n{}",
        source
    );
}

#[test]
fn test_ts_as_any() {
    let stream = ts_template! {
        const unsafe = unknownValue as any;
    };
    let source = stream.source();
    assert!(
        source.contains("as any") || source.contains("as  any"),
        "Expected 'as any'. Got:\n{}",
        source
    );
}

// =============================================================================
// Non-Null Assertions
// =============================================================================

#[test]
fn test_ts_non_null_expr() {
    let stream = ts_template! {
        const definite = maybeNull!;
    };
    let source = stream.source();
    assert!(
        source.contains("maybeNull!") || source.contains("maybeNull !"),
        "Expected non-null assertion. Got:\n{}",
        source
    );
}

#[test]
fn test_ts_non_null_chained() {
    let stream = ts_template! {
        const value = obj!.property!.nested;
    };
    let source = stream.source();
    // Should have multiple non-null assertions
    let count = source.matches("!").count();
    assert!(
        count >= 2,
        "Expected at least 2 '!' assertions. Got {} in:\n{}",
        count,
        source
    );
}

// =============================================================================
// Satisfies Expressions
// =============================================================================

#[test]
fn test_ts_satisfies_expr() {
    let stream = ts_template! {
        const config = { name: "test" } satisfies Config;
    };
    let source = stream.source();
    assert!(
        source.contains("satisfies Config") || source.contains("satisfies  Config"),
        "Expected 'satisfies Config'. Got:\n{}",
        source
    );
}

#[test]
fn test_ts_satisfies_complex() {
    let stream = ts_template! {
        const palette = {
            red: [255, 0, 0],
            green: "#00ff00"
        } satisfies Record<string, string | number[]>;
    };
    let source = stream.source();
    assert!(
        source.contains("satisfies"),
        "Expected 'satisfies'. Got:\n{}",
        source
    );
}

// =============================================================================
// Type Instantiation
// =============================================================================

#[test]
fn test_ts_instantiation() {
    let stream = ts_template! {
        const specificFn = genericFn<string>;
    };
    let source = stream.source();
    assert!(
        source.contains("genericFn<string>")
            || source.contains("genericFn< string >")
            || source.contains("genericFn <string>"),
        "Expected type instantiation. Got:\n{}",
        source
    );
}

#[test]
fn test_ts_instantiation_multiple() {
    let stream = ts_template! {
        const mapFn = transform<Input, Output>;
    };
    let source = stream.source();
    assert!(
        source.contains("Input") && source.contains("Output"),
        "Expected Input, Output. Got:\n{}",
        source
    );
}

// =============================================================================
// Super Expressions
// =============================================================================

#[test]
fn test_super_call() {
    let stream = ts_template! {
        class Child extends Parent {
            constructor() {
                super();
            }
        }
    };
    let source = stream.source();
    assert!(
        source.contains("super()") || source.contains("super ()"),
        "Expected 'super()'. Got:\n{}",
        source
    );
}

#[test]
fn test_super_call_with_args() {
    let stream = ts_template! {
        class Child extends Parent {
            constructor(name: string) {
                super(name);
            }
        }
    };
    let source = stream.source();
    assert!(
        source.contains("super(name)") || source.contains("super( name )"),
        "Expected 'super(name)'. Got:\n{}",
        source
    );
}

#[test]
fn test_super_property() {
    let stream = ts_template! {
        class Child extends Parent {
            method() {
                return super.method();
            }
        }
    };
    let source = stream.source();
    assert!(
        source.contains("super.method"),
        "Expected 'super.method'. Got:\n{}",
        source
    );
}

// =============================================================================
// Tagged Template Literals
// =============================================================================
// Note: Backticks (`) are not valid Rust tokens, so tagged template literals
// cannot be tested directly in ts_template! macros. These would need to be
// tested via string-based template compilation instead.

// =============================================================================
// Private Names
// =============================================================================

#[test]
fn test_private_name_field() {
    let stream = ts_template! {
        class Counter {
            #count: number = 0;

            increment() {
                this.#count++;
            }

            get value() {
                return this.#count;
            }
        }
    };
    let source = stream.source();
    assert!(
        source.contains("#count"),
        "Expected '#count'. Got:\n{}",
        source
    );
}

#[test]
fn test_private_name_method() {
    let stream = ts_template! {
        class Service {
            #validate(input: string): boolean {
                return input.length > 0;
            }

            process(input: string) {
                if (this.#validate(input)) {
                    return input;
                }
                return null;
            }
        }
    };
    let source = stream.source();
    assert!(
        source.contains("#validate"),
        "Expected '#validate'. Got:\n{}",
        source
    );
}

#[test]
fn test_private_name_static() {
    let stream = ts_template! {
        class Singleton {
            static #instance: Singleton;

            static getInstance() {
                if (!Singleton.#instance) {
                    Singleton.#instance = new Singleton();
                }
                return Singleton.#instance;
            }
        }
    };
    let source = stream.source();
    assert!(
        source.contains("#instance"),
        "Expected '#instance'. Got:\n{}",
        source
    );
}

// =============================================================================
// BigInt Literals
// =============================================================================

#[test]
fn test_bigint_lit_simple() {
    let stream = ts_template! {
        const big = 9007199254740991n;
    };
    let source = stream.source();
    assert!(
        source.contains("9007199254740991n"),
        "Expected BigInt literal. Got:\n{}",
        source
    );
}

#[test]
fn test_bigint_lit_zero() {
    let stream = ts_template! {
        const zero = 0n;
    };
    let source = stream.source();
    assert!(source.contains("0n"), "Expected '0n'. Got:\n{}", source);
}

#[test]
fn test_bigint_lit_operations() {
    let stream = ts_template! {
        const result = 100n + 200n;
    };
    let source = stream.source();
    assert!(
        source.contains("100n") && source.contains("200n"),
        "Expected BigInt operations. Got:\n{}",
        source
    );
}

#[test]
fn test_bigint_lit_negative() {
    let stream = ts_template! {
        const negative = -42n;
    };
    let source = stream.source();
    assert!(source.contains("42n"), "Expected '42n'. Got:\n{}", source);
}