tsbyte 0.1.1

Compile TypeScript directly to native JVM bytecode and executable JARs
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
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
use tsbyte::compile_to_bytes;
use ristretto_classfile::{ClassFile, attributes::Attribute};
use std::io::Cursor;

fn parse_class(classes: &[(String, Vec<u8>)]) -> ClassFile {
    let (_, bytes) = classes.iter().find(|(n, _)| !n.contains("Lambda$") && !n.contains("runtime/")).unwrap();
    let mut cursor = Cursor::new(bytes.clone());
    ClassFile::from_bytes(&mut cursor).expect("Failed to parse generated class file")
}

// ── Basic Function Compilation ──────────────────────────────────────

#[test]
fn test_compile_basic_function() {
    let ts = r#"
        function calculate(a: number, b: number): number {
            return a + b;
        }
    "#;

    let bytes = compile_to_bytes(ts, "com.tsbyte.test").unwrap();
    let class = parse_class(&bytes);
    assert_eq!(class.methods.len(), 2, "Expected <init> + calculate");

    let mut found = false;
    for method in &class.methods {
        let name = class.constant_pool.try_get_utf8(method.name_index).unwrap();
        if name == "calculate" {
            found = true;
            let has_code = method.attributes.iter().any(|a| matches!(a, Attribute::Code { .. }));
            assert!(has_code, "calculate missing Code attribute");
        }
    }
    assert!(found, "Could not find 'calculate'");
}

// ── If/Else Control Flow ────────────────────────────────────────────

#[test]
fn test_compile_if_else() {
    let ts = r#"
        function check(x: number): number {
            if (x > 0) {
                return 1;
            } else {
                return 0;
            }
        }
    "#;
    let bytes = compile_to_bytes(ts, "com.tsbyte.test").unwrap();
    let class = parse_class(&bytes);
    assert_eq!(class.methods.len(), 2);
}

#[test]
fn test_compile_nested_if() {
    let ts = r#"
        function classify(x: number): string {
            if (x > 100) {
                return "high";
            } else {
                if (x > 50) {
                    return "medium";
                } else {
                    return "low";
                }
            }
        }
    "#;
    let bytes = compile_to_bytes(ts, "com.tsbyte.test").unwrap();
    assert!(!bytes.is_empty());
}

// ── While Loops ─────────────────────────────────────────────────────

#[test]
fn test_compile_simple_while() {
    let ts = r#"
        function countdown(n: number): number {
            let i = n;
            while (i > 0) {
                i = i - 1;
            }
            return i;
        }
    "#;
    let bytes = compile_to_bytes(ts, "com.tsbyte.test").unwrap();
    assert!(!bytes.is_empty());
}

// ── Ternary Expressions ─────────────────────────────────────────────

#[test]
fn test_compile_ternary_expression() {
    let ts = r#"
        function max(a: number, b: number): number {
            return a > b ? a : b;
        }
    "#;
    let bytes = compile_to_bytes(ts, "com.tsbyte.test").unwrap();
    assert!(!bytes.is_empty());
}

// ── Data Structures ─────────────────────────────────────────────────

#[test]
fn test_compile_arrays_and_objects() {
    let ts = r#"
        function buildData() {
            let arr = [1, 2, 3];
            let obj = { name: "test", val: 5 };
            return arr;
        }
    "#;
    let bytes = compile_to_bytes(ts, "com.tsbyte.test").unwrap();
    assert!(!bytes.is_empty());
}

// ── Class Declarations ──────────────────────────────────────────────

#[test]
fn test_compile_class_declaration() {
    let ts = r#"
        class Animal {
            name: string;
            constructor(name: string) {
                this.name = name;
            }
            speak() {
                return "hello";
            }
        }
    "#;
    let bytes = compile_to_bytes(ts, "com.tsbyte.test").unwrap();
    assert!(!bytes.is_empty());
}

#[test]
fn test_compile_class_with_extends() {
    let ts = r#"
        class Shape {
            area(): number { return 0; }
        }
        class Circle extends Shape {
            radius: number;
            area(): number { return 3.14; }
        }
    "#;
    let bytes = compile_to_bytes(ts, "com.tsbyte.test").unwrap();
    assert!(!bytes.is_empty());
}

// ── String Operations ───────────────────────────────────────────────

#[test]
fn test_compile_string_concat() {
    let ts = r#"
        function greet(name: string): string {
            return "Hello " + name;
        }
    "#;
    let bytes = compile_to_bytes(ts, "com.tsbyte.test").unwrap();
    assert!(!bytes.is_empty());
}

// ── Arrow Functions & Closures ──────────────────────────────────────

#[test]
fn test_compile_arrow_function() {
    let ts = r#"
        function process() {
            let factor = 10;
            let multiply = (x: number) => x * factor;
            return multiply;
        }
    "#;
    let bytes = compile_to_bytes(ts, "com.tsbyte.test").unwrap();
    assert!(!bytes.is_empty(), "Arrow function compilation should produce valid bytecode");
}

// ── Multiple Functions ──────────────────────────────────────────────

#[test]
fn test_compile_multiple_functions() {
    let ts = r#"
        function add(a: number, b: number): number {
            return a + b;
        }
        function subtract(a: number, b: number): number {
            return a - b;
        }
        function multiply(a: number, b: number): number {
            return a * b;
        }
    "#;
    let bytes = compile_to_bytes(ts, "com.tsbyte.test").unwrap();
    let class = parse_class(&bytes);
    // <init> + add + subtract + multiply = 4 methods
    assert_eq!(class.methods.len(), 4);
}

// ── Variable Declarations ───────────────────────────────────────────

#[test]
fn test_compile_typed_variables() {
    let ts = r#"
        function compute() {
            let x: number = 42;
            let name: string = "world";
            let flag: boolean = true;
            return x;
        }
    "#;
    let bytes = compile_to_bytes(ts, "com.tsbyte.test").unwrap();
    assert!(!bytes.is_empty());
}

// ── Complex Enterprise Pattern: Service Layer ───────────────────────

#[test]
fn test_compile_service_class() {
    let ts = r#"
        class UserService {
            baseUrl: string;
            constructor(url: string) {
                this.baseUrl = url;
            }
            getUser(id: number): string {
                return "user";
            }
            createUser(name: string, age: number): boolean {
                return true;
            }
        }
    "#;
    let bytes = compile_to_bytes(ts, "com.tsbyte.test").unwrap();
    assert!(!bytes.is_empty());
}

// ── Module Graph Tests ──────────────────────────────────────────────

#[test]
fn test_module_graph_registration() {
    use tsbyte::codegen::compiler::module_graph::{ModuleGraph, ExportKind};

    let mut graph = ModuleGraph::new("com.tsbyte.app");
    let class = graph.register_module("src/services/math.ts");
    assert_eq!(class, "com/tsbyte/app/services/Math");

    graph.add_export("src/services/math.ts", "add", ExportKind::Function, "(DD)D");

    graph.register_module("src/main.ts");
    let result = graph.resolve_import("src/main.ts", "./services/math", "add");
    assert!(result.is_some());
    let (resolved_class, desc) = result.unwrap();
    assert_eq!(resolved_class, "com/tsbyte/app/services/Math");
    assert_eq!(desc, "(DD)D");
}

// ── JVM Descriptor Generation ───────────────────────────────────────

#[test]
fn test_jvm_descriptors() {
    use tsbyte::codegen::compiler::ir::{Type, build_method_descriptor};

    assert_eq!(Type::Int.to_jvm_descriptor(), "I");
    assert_eq!(Type::Double.to_jvm_descriptor(), "D");
    assert_eq!(Type::Bool.to_jvm_descriptor(), "Z");
    assert_eq!(Type::StringTy.to_jvm_descriptor(), "Ljava/lang/String;");
    assert_eq!(Type::Void.to_jvm_descriptor(), "V");
    assert_eq!(Type::Class("java/util/List".to_string()).to_jvm_descriptor(), "Ljava/util/List;");
    assert_eq!(Type::Array(Box::new(Type::Double)).to_jvm_descriptor(), "[D");
    assert_eq!(Type::Any.to_jvm_descriptor(), "Ljava/lang/Object;");

    // Method descriptor: (double, String) -> boolean
    let desc = build_method_descriptor(
        &[Type::Double, Type::StringTy],
        &Type::Bool,
    );
    assert_eq!(desc, "(DLjava/lang/String;)Z");
}

// ── Newly Added AST Nodes ───────────────────────────────────────────

#[test]
fn test_compile_for_of() {
    let ts = r#"
        function sum(arr: number[]): number {
            let total = 0;
            for (let x of arr) {
                total += x;
            }
            return total;
        }
    "#;
    let bytes = compile_to_bytes(ts, "com.tsbyte.test").unwrap();
    assert!(!bytes.is_empty());
}

#[test]
fn test_compile_do_while() {
    let ts = r#"
        function count(): number {
            let i = 0;
            do {
                i = i + 1;
            } while (i < 5);
            return i;
        }
    "#;
    let bytes = compile_to_bytes(ts, "com.tsbyte.test").unwrap();
    assert!(!bytes.is_empty());
}

#[test]
fn test_compile_update_expr() {
    let ts = r#"
        function inc(x: number): number {
            x++;
            ++x;
            x--;
            --x;
            return x;
        }
    "#;
    let bytes = compile_to_bytes(ts, "com.tsbyte.test").unwrap();
    assert!(!bytes.is_empty());
}

#[test]
fn test_compile_paren_expr() {
    let ts = r#"
        function math(x: number): number {
            return (x + 2) * 3;
        }
    "#;
    let bytes = compile_to_bytes(ts, "com.tsbyte.test").unwrap();
    assert!(!bytes.is_empty());
}

#[test]
fn test_compile_await_expr() {
    let ts = r#"
        async function fetch(url: string): any {
            let result = await doFetch(url);
            return result;
        }
    "#;
    let bytes = compile_to_bytes(ts, "com.tsbyte.test").unwrap();
    assert!(!bytes.is_empty());
}

#[test]
fn test_compile_interface() {
    let ts = r#"
        interface Drawable {
            draw(): void;
            getBounds(x: number, y: number): number[];
        }
        class Circle implements Drawable {
            draw() {}
            getBounds(x: number, y: number): number[] { return []; }
        }
    "#;
    let bytes = compile_to_bytes(ts, "com.tsbyte.test").unwrap();
    assert!(!bytes.is_empty());
}

#[test]
fn test_compile_opt_chain() {
    let ts = r#"
        function getVal(obj: any): any {
            return obj?.prop;
        }
    "#;
    let bytes = compile_to_bytes(ts, "com.tsbyte.test").unwrap();
    assert!(!bytes.is_empty());
}

#[test]
fn test_compile_assign() {
    let ts = r#"
        function setVal(obj: any): void {
            let x = 10;
            x = 20;
            obj.val = 30;
        }
    "#;
    let bytes = compile_to_bytes(ts, "com.tsbyte.test").unwrap();
    assert!(!bytes.is_empty());
}

#[test]
fn test_compile_export() {
    let ts = r#"
        export function myFunc(): void {}
        export default function App(): void {}
    "#;
    let bytes = compile_to_bytes(ts, "com.tsbyte.test").unwrap();
    assert!(!bytes.is_empty());
}

#[test]
fn test_compile_spread_array() {
    let ts = r#"
        function spreadArr(arr: any[]): any[] {
            return [1, ...arr, 2];
        }
    "#;
    let bytes = compile_to_bytes(ts, "com.tsbyte.test").unwrap();
    assert!(!bytes.is_empty());
}

#[test]
fn test_compile_spread_object() {
    let ts = r#"
        function spreadObj(obj: any): any {
            return { a: 1, ...obj, b: 2 };
        }
    "#;
    let bytes = compile_to_bytes(ts, "com.tsbyte.test").unwrap();
    assert!(!bytes.is_empty());
}

#[test]
fn test_compile_rest_parameters() {
    let ts = r#"
        function restParams(a: number, ...args: any[]): void {
            console.log(a, ...args);
        }
    "#;
    let bytes = compile_to_bytes(ts, "com.tsbyte.test").unwrap();
    assert!(!bytes.is_empty());
}

#[test]
fn test_compile_dynamic_any_dispatch() {
    let ts = r#"
        function addAny(a: any, b: any): any {
            return a + b;
        }
    "#;
    let bytes = compile_to_bytes(ts, "com.tsbyte.test").unwrap();
    assert!(!bytes.is_empty());
}

#[test]
fn test_compile_async_function_wrapper() {
    let ts = r#"
        async function fetchUser(): Promise<any> {
            return { name: "Alice" };
        }
    "#;
    let bytes = compile_to_bytes(ts, "com.tsbyte.test").unwrap();
    assert!(!bytes.is_empty());
}

// ── Type Checker Tests ───────────────────────────────────────────────

#[test]
fn test_type_checker_assignment() {
    let ts = r#"
        function testAssign() {
            let x: number = 10;
            x = "hello"; // error
        }
    "#;
    let errors = tsbyte::type_check_to_errors(ts).unwrap();
    assert!(errors.iter().any(|e| e.contains("Cannot assign type 'StringTy' to variable 'x' of type 'Double'")));
}

#[test]
fn test_type_checker_function_call() {
    let ts = r#"
        function greet(name: string): string {
            return "Hello " + name;
        }
        function testCall() {
            greet(123); // error
        }
    "#;
    let errors = tsbyte::type_check_to_errors(ts).unwrap();
    assert!(errors.iter().any(|e| e.contains("Argument 0 to 'greet' is not assignable to 'StringTy', got 'Double'")));
}

#[test]
fn test_type_checker_arithmetic() {
    let ts = r#"
        function testMath() {
            let a = "string" * 5; // error
        }
    "#;
    let errors = tsbyte::type_check_to_errors(ts).unwrap();
    assert!(errors.iter().any(|e| e.contains("Arithmetic operations require numeric types")));
}

#[test]
fn test_type_checker_return() {
    let ts = r#"
        function getNumber(): number {
            return "not a number"; // error
        }
    "#;
    let errors = tsbyte::type_check_to_errors(ts).unwrap();
    assert!(errors.iter().any(|e| e.contains("Cannot return type 'StringTy' from function expecting 'Double'")));
}

#[test]
fn test_type_checker_structural_interface() {
    let ts = r#"
        interface Point {
            x: number;
            y: number;
        }
        function usePoint(p: Point) {
            let n = p.x;
        }
        function testStruct() {
            let p1: Point = { x: 10, y: 20 }; // OK
            let p2: Point = { x: 10 }; // Error: missing y
        }
    "#;
    let errors = tsbyte::type_check_to_errors(ts).unwrap();
    assert!(errors.iter().any(|e| e.contains("Cannot assign type 'Object([(\"x\", Double)])' to variable 'p2' of type 'Object([(\"x\", Double), (\"y\", Double)])'")));
}

#[test]
fn test_type_checker_generic_param() {
    let ts = r#"
        function handlePromise(p: Promise<string>) {}
        function testGen() {
            let bad: Promise<number> = null as any;
            handlePromise(bad); // Error: Promise<number> not assignable to Promise<string>
        }
    "#;
    let _errors = tsbyte::type_check_to_errors(ts).unwrap();
    // In our simplified setup, `null as any` bypasses checks, but the function call arguments will fail
    // wait, we don't have generics correctly mocked in AST for Cast yet, so we will just test assignment
    let ts2 = r#"
        function handlePromise(p: Promise<string>) {}
        function testGen(bad: Promise<number>) {
            handlePromise(bad);
        }
    "#;
    let errors2 = tsbyte::type_check_to_errors(ts2).unwrap();
    assert!(errors2.iter().any(|e| e.contains("Argument 0 to 'handlePromise' is not assignable to 'Generic(\"Promise\", [StringTy])', got 'Generic(\"Promise\", [Double])'")));
}

#[test]
fn test_type_checker_field_access() {
    let ts = r#"
        interface User { name: string; }
        function testField(u: User) {
            let n = u.name; // ok
            let age = u.age; // error
        }
    "#;
    let errors = tsbyte::type_check_to_errors(ts).unwrap();
    assert!(errors.iter().any(|e| e.contains("Property 'age' does not exist on type 'Object([(\"name\", StringTy)])'")));
}

#[test]
fn test_mir_lowering_and_optimization() {
    let ts = r#"
        function testMir() {
            let x = 5 * 10 + 2; // Should fold to 52
            if (x > 50) {
                return 1;
            } else {
                return 0;
            }
            let unreachable = 99; // Should be eliminated by DCE
        }
    "#;
    
    // We mock the pipeline to test MIR specifically
    let mut builder = tsbyte::codegen::compiler::ir_builder::IrBuilder::new();
    let module = tsbyte::swc_frontend::parse_typescript(ts, "test.ts");
    let stmts = builder.build_module(&module);
    
    let mut checker = tsbyte::codegen::compiler::type_checker::TypeChecker::new();
    let _errors = checker.check_program(&stmts);
    
    let lowerer = tsbyte::codegen::compiler::hir_to_mir::HirToMir::new();
    let mir_funcs = lowerer.lower(&stmts);
    
    // Verify it compiled into some MIR blocks
    assert!(!mir_funcs.is_empty());
    
    let optimized = tsbyte::codegen::compiler::mir_opt::MirOptimizer::optimize(mir_funcs);
    
    let test_mir = optimized.iter().find(|f| f.name == "testMir").expect("Expected testMir function");
    
    // We expect several blocks, but the unreachable code should be gone
    assert!(test_mir.blocks.len() > 0);
    
    // Check if Constant Folding worked
    println!("{:#?}", test_mir);
    let mut found_52 = false;
    for block in test_mir.blocks.values() {
        for instr in &block.instrs {
            if let tsbyte::codegen::compiler::mir::MirInstr::Assign(_, tsbyte::codegen::compiler::mir::MirExpr::Operand(tsbyte::codegen::compiler::mir::Operand::Const(tsbyte::codegen::compiler::mir::Constant::Double(v), _))) = instr {
                if *v == 52.0 {
                    found_52 = true;
                }
            }
        }
    }
    assert!(found_52, "Constant folding failed, didn't find 52.0 in optimized MIR");
}

#[test]
fn test_jar_packaging() {
    use std::io::Read;
    use std::path::PathBuf;

    let ts = r#"
        function add(a: number, b: number): number {
            return a + b;
        }
    "#;

    let output_path = PathBuf::from("/tmp/tsbyte_test_output.jar");

    tsbyte::compile_to_jar(ts, "com.example", &output_path)
        .expect("JAR compilation failed");

    assert!(output_path.exists(), "JAR file was not created");

    // Open the JAR and verify its contents
    let file = std::fs::File::open(&output_path).expect("Cannot open JAR");
    let mut zip = zip::ZipArchive::new(file).expect("Not a valid ZIP/JAR archive");

    // Verify MANIFEST.MF exists and contains correct Main-Class (scoped so borrow ends)
    {
        let mut manifest = zip.by_name("META-INF/MANIFEST.MF").expect("MANIFEST.MF missing");
        let mut manifest_content = String::new();
        manifest.read_to_string(&mut manifest_content).unwrap();
        assert!(manifest_content.contains("Manifest-Version: 1.0"), "Manifest-Version missing");
        assert!(manifest_content.contains("Main-Class: com.example.App"), "Main-Class missing");
    }

    // Verify App.class exists
    let class_names: Vec<String> = (0..zip.len())
         .map(|i| zip.by_index(i).unwrap().name().to_string())
         .collect();
    assert!(
        class_names.iter().any(|n| n.ends_with(".class")),
        "No .class files found in JAR: {:?}", class_names
    );

    // Cleanup
    let _ = std::fs::remove_file(output_path);
}

#[test]
fn test_compile_getters_setters() {
    let ts = r#"
        class Temperature {
            _celsius: number = 0;
            get fahrenheit(): number {
                return this._celsius * 1.8 + 32;
            }
            set fahrenheit(value: number) {
                this._celsius = (value - 32) / 1.8;
            }
        }
        function testProp() {
            let t = new Temperature();
            t.fahrenheit = 100;
            return t.fahrenheit;
        }
    "#;
    let bytes = tsbyte::compile_to_bytes(ts, "com.tsbyte.test").unwrap();
    let class = parse_class(&bytes);
    assert!(!class.methods.is_empty(), "Class should compile successfully");
}

#[test]
fn test_compile_define_property() {
    let ts = r#"
        function testDefine() {
            let obj = {};
            Object.defineProperty(obj, "score", {
                value: 100
            });
            Object.defineProperty(obj, "multiplier", {
                get() { return 2; },
                set(v) { }
            });
            return obj.score * obj.multiplier;
        }
    "#;
    let bytes = tsbyte::compile_to_bytes(ts, "com.tsbyte.test").unwrap();
    let class = parse_class(&bytes);
    assert!(!class.methods.is_empty(), "Class should compile successfully");
}

#[test]
fn test_compile_array_length() {
    let ts = r#"
        function testLength() {
            let arr: number[] = [1, 2, 3];
            let len = arr.length;
            return len;
        }
    "#;
    let bytes = tsbyte::compile_to_bytes(ts, "com.tsbyte.test").unwrap();
    let class = parse_class(&bytes);
    assert!(!class.methods.is_empty(), "Class should compile array length");
}

#[test]
fn test_compile_array_push_pop() {
    let ts = r#"
        function testPushPop() {
            let arr: number[] = [1, 2];
            arr.push(3);
            arr.push(4, 5);
            let last = arr.pop();
            let first = arr.shift();
            arr.unshift(0);
            return arr.length;
        }
    "#;
    let bytes = tsbyte::compile_to_bytes(ts, "com.tsbyte.test").unwrap();
    let class = parse_class(&bytes);
    assert!(!class.methods.is_empty(), "Class should compile array push/pop");
}

#[test]
fn test_compile_array_methods() {
    let ts = r#"
        function testArrayMethods() {
            let arr: number[] = [10, 20, 30, 40, 50];
            let idx = arr.indexOf(30);
            let has = arr.includes(20);
            let joined = arr.join("-");
            arr.reverse();
            let sliced = arr.slice(1, 3);
            let combined = arr.concat(sliced);
            return combined.length;
        }
    "#;
    let bytes = tsbyte::compile_to_bytes(ts, "com.tsbyte.test").unwrap();
    let class = parse_class(&bytes);
    assert!(!class.methods.is_empty(), "Class should compile array methods");
}

#[test]
fn test_compile_newly_implemented_features() {
    let ts = r#"
        class Account {
            #balance: bigint;
            #holder: string;

            constructor({ holder, initialBalance }) {
                this.#holder = holder;
                this.#balance = initialBalance;
            }

            #computeBonus(ratio: number) {
                return 100n;
            }

            getDetails() {
                let pattern = /[a-z]+/i;
                let bonus = this.#computeBonus(0.1);
                return this.#holder;
            }
        }
        function testDestructure([first, second]: number[]) {
            return first + second;
        }
    "#;
    let bytes = tsbyte::compile_to_bytes(ts, "com.tsbyte.test").unwrap();
    let class = parse_class(&bytes);
    assert!(!class.methods.is_empty(), "Newly implemented features should compile to JVM bytecode perfectly");
}

#[test]
fn test_compile_further_features() {
    let ts = r#"
        enum Direction {
            Up = 10,
            Down,
            Left,
            Right
        }

        function testFeatures() {
            let x = (1, 2, Direction.Down) satisfies number;
            let y = "constant" as const;
            let z = !true;
            return x;
        }
    "#;
    let bytes = tsbyte::compile_to_bytes(ts, "com.tsbyte.test").unwrap();
    let class = parse_class(&bytes);
    assert!(!class.methods.is_empty(), "Further features (enum, satisfies, sequence, as const) should compile to JVM bytecode perfectly");
}

#[test]
fn test_compile_advanced_roadmapped_features() {
    let ts = r#"
        import type { SomeType } from "./types";
        import { type OtherType, regularImport } from "./types";
        export type { AnotherType };

        const addExpr = function(a: number, b: number) {
            return a + b;
        };

        const AnonymousClass = class {
            greet() { return "hello"; }
        };

        class Config {
            static _debug: boolean = false;
            static get debug(): boolean {
                return this._debug;
            }
            static set debug(value: boolean) {
                this._debug = value;
            }
        }

        function testDestructuring() {
            let [first, ...rest] = [1, 2, 3];
            let { x, ...others } = { x: 10, y: 20, z: 30 };
            return first;
        }
    "#;
    let bytes = tsbyte::compile_to_bytes(ts, "com.tsbyte.test").unwrap();
    let class = parse_class(&bytes);
    assert!(!class.methods.is_empty(), "Advanced roadmapped features should compile to JVM bytecode perfectly");
}

#[test]
fn test_compile_further_advanced_features() {
    let ts = r#"
        // 1. Re-exports
        export { val as aliasVal } from "./some_module";
        export * from "./another_module";
        export * as nsModule from "./ns_module";

        // 2. Abstract fields and Override modifier
        abstract class Animal {
            abstract sound: string;
            makeSound(): string {
                return this.sound;
            }
        }

        class Dog extends Animal {
            override sound: string = "woof";
            override makeSound(): string {
                return "dog says " + this.sound;
            }
        }

        // 3. Nested Destructuring (variables & parameters)
        function testNestedDestructure(obj: any, arr: any) {
            const { a: { b, c: d } } = obj;
            const [first, [second, third]] = arr;
            return b;
        }

        // 4. Meta-Properties
        class TargetDemo {
            target: any;
            constructor() {
                this.target = new.target;
            }
            getTarget() {
                return new.target;
            }
        }

        function getMetaUrl() {
            const meta = import.meta;
            return meta.url;
        }
    "#;
    let bytes = tsbyte::compile_to_bytes(ts, "com.tsbyte.test").unwrap();
    let class = parse_class(&bytes);
    assert!(!class.methods.is_empty(), "Further advanced features should compile to JVM bytecode perfectly");
}

#[test]
fn test_compile_advanced_phase_3() {
    let ts = r#"
        "use strict";
        "use asm";

        // 1. Namespaces
        namespace MathUtils {
            export const PI = 3.14159;
            export function square(x: number): number {
                return x * x;
            }
            const privateHelper = 42;
            function privateSquare(x: number) {
                return x * privateHelper;
            }
        }

        // 2. Ambient Declarations (erased at bytecode level, registered in type checker)
        declare const externalVal: string;
        declare function externalLog(msg: string): void;
        declare class NativeHelper {}

        // 3. Dynamic import() returning a CompletableFuture
        function loadModule() {
            return import("com.tsbyte.runtime.DynamicModule");
        }

        // 4. Advanced TS Types, Predicates, and typeof
        function typeDemonstration() {
            let sym: symbol;
            let status: "success" | "error" = "success";
            
            function isNumber(x: any): x is number {
                return true;
            }
            
            let original = 100;
            let aliasVal: typeof original = 200;
            
            return aliasVal;
        }
    "#;
    let bytes = tsbyte::compile_to_bytes(ts, "com.tsbyte.test").unwrap();
    let class = parse_class(&bytes);
    assert!(!class.methods.is_empty(), "Advanced Phase 3 features should compile to JVM bytecode perfectly");
}

#[test]
fn test_compile_phase_4_features() {
    let ts = r#"
        // 1. Decorators (parsed & cleanly erased)
        @sealed
        class Greeter {
            @format("Hello, %s")
            greeting: string;
            
            constructor(message: string) {
                this.greeting = message;
            }
            
            @log
            greet() {
                return this.greeting;
            }
        }

        // 2. Tagged Template Literals
        function customTag(strings: string[], ...values: any[]) {
            return strings[0] + values[0] + strings[1];
        }
        function testTagged() {
            let name = "World";
            return customTag`Hello ${name}!`;
        }

        // 3. Generators and yield / yield*
        function* numberGenerator() {
            yield 1;
            yield 2;
            return 3;
        }

        function* delegateGenerator() {
            yield* numberGenerator();
            yield 4;
        }

        // 3b. Async Generators and for await...of
        async function* asyncNumGen() {
            yield 10;
            yield 20;
        }
        async function testForAwait(gen: any) {
            let sum = 0;
            for await (const x of gen) {
                sum = sum + x;
            }
            return sum;
        }

        // 4. Advanced TS Type System Features
        type Point = { x: number; y: number };
        type Point3D = Point & { z: number }; // intersection type
        interface StringMap {
            [key: string]: string; // index signature
        }
        function identity<T extends number>(arg: T): T { // generic constraint
            return arg;
        }
        type IsString<T> = T extends string ? true : false; // conditional type
        type ReadonlyPoint = { readonly [P in keyof Point]: Point[P] }; // mapped type, keyof
        type Greeting = `hello ${string}`; // template literal type
        type InferType<T> = T extends (infer U)[] ? U : T; // infer keyword
        type PartialPoint = Partial<Point>; // utility type
    "#;
    let bytes = tsbyte::compile_to_bytes(ts, "com.tsbyte.test").unwrap();
    let class = parse_class(&bytes);
    assert!(!class.methods.is_empty(), "Phase 4 compiler features (generators, yield, yield*, decorators, advanced types) should compile perfectly");
}

#[test]
fn test_compile_destructuring_defaults() {
    let ts = r#"
        function testDestructureDefaults() {
            let [x = 10, y = 20] = [5];
            let { a = 100, b: { c = 200 } = {} } = { a: 50 };
            return x + y + a + c;
        }
    "#;
    let bytes = tsbyte::compile_to_bytes(ts, "com.tsbyte.test").unwrap();
    let class = parse_class(&bytes);
    assert!(!class.methods.is_empty(), "Destructuring with default values should compile to JVM bytecode successfully");
}

#[test]
fn test_compile_advanced_class_blocks_initializers() {
    let ts = r#"
        class ConfigService {
            static #defaultUrl = "http://localhost";
            #timeout = 5000;
            static active: boolean;

            static {
                ConfigService.active = true;
            }

            getSettings() {
                return ConfigService.#defaultUrl + ":" + this.#timeout;
            }
        }
    "#;
    let bytes = tsbyte::compile_to_bytes(ts, "com.tsbyte.test").unwrap();
    let class = parse_class(&bytes);
    assert!(!class.methods.is_empty(), "Advanced class components should compile to JVM bytecode successfully");
}

#[test]
fn test_compile_generic_erasure_lookup() {
    let ts = r#"
        interface Node<T> {
            value: T;
        }
        function processNode(node: Node<number>) {
            return node.value;
        }
    "#;
    let bytes = tsbyte::compile_to_bytes(ts, "com.tsbyte.test").unwrap();
    let class = parse_class(&bytes);
    assert!(!class.methods.is_empty(), "Field access on variables with generic types should compile successfully");
}