tsrun 0.1.23

A TypeScript interpreter designed for embedding in applications
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
//! Tests exploring enum behavior and comparing with TypeScript semantics

use super::eval;
use tsrun::JsValue;

// =============================================================================
// BASIC ENUM FUNCTIONALITY
// =============================================================================

#[test]
fn test_enum_numeric_basic() {
    // Basic numeric enum - auto-incrementing from 0
    assert_eq!(
        eval(
            r#"
            enum Direction {
                Up,
                Down,
                Left,
                Right
            }
            Direction.Up
        "#
        ),
        JsValue::Number(0.0)
    );
    assert_eq!(
        eval(
            r#"
            enum Direction { Up, Down, Left, Right }
            Direction.Right
        "#
        ),
        JsValue::Number(3.0)
    );
}

#[test]
fn test_enum_numeric_explicit_values() {
    // Numeric enum with explicit values
    assert_eq!(
        eval(
            r#"
            enum Status {
                Pending = 10,
                Active = 20,
                Closed = 30
            }
            Status.Active
        "#
        ),
        JsValue::Number(20.0)
    );
}

#[test]
fn test_enum_string_values() {
    // String enum
    assert_eq!(
        eval(
            r#"
            enum Color {
                Red = "red",
                Green = "green",
                Blue = "blue"
            }
            Color.Green
        "#
        ),
        JsValue::from("green")
    );
}

#[test]
fn test_enum_mixed_values() {
    // Mixed enum (numeric + string) - TypeScript allows this
    assert_eq!(
        eval(
            r#"
            enum Mixed {
                A = 0,
                B = "hello",
                C = 1
            }
            Mixed.B
        "#
        ),
        JsValue::from("hello")
    );
}

// =============================================================================
// REVERSE MAPPING (Numeric enums only in TypeScript)
// =============================================================================

#[test]
fn test_enum_reverse_mapping_numeric() {
    // TypeScript generates reverse mappings for numeric enums
    // Direction[0] === "Up"
    assert_eq!(
        eval(
            r#"
            enum Direction {
                Up,
                Down,
                Left,
                Right
            }
            Direction[0]
        "#
        ),
        JsValue::from("Up")
    );
}

#[test]
fn test_enum_reverse_mapping_all_numeric() {
    // All reverse mappings should work
    assert_eq!(
        eval(
            r#"
            enum Direction { Up, Down, Left, Right }
            Direction[0] + "," + Direction[1] + "," + Direction[2] + "," + Direction[3]
        "#
        ),
        JsValue::from("Up,Down,Left,Right")
    );
}

#[test]
fn test_enum_no_reverse_mapping_for_strings() {
    // TypeScript does NOT generate reverse mappings for string enums
    // Color["red"] should be undefined
    assert_eq!(
        eval(
            r#"
            enum Color {
                Red = "red",
                Green = "green"
            }
            Color["red"]
        "#
        ),
        JsValue::Undefined
    );
}

// =============================================================================
// Object.keys / Object.values BEHAVIOR
// =============================================================================

#[test]
fn test_enum_object_keys_numeric() {
    // For numeric enums, Object.keys includes both names AND reverse mappings
    // TypeScript compiled output: { "0": "Up", "1": "Down", "Up": 0, "Down": 1 }
    // So Object.keys returns ["0", "1", "Up", "Down"]
    assert_eq!(
        eval(
            r#"
        enum Direction { Up, Down }
        Object.keys(Direction).sort().join(",")
    "#
        ),
        JsValue::from("0,1,Down,Up")
    );
}

#[test]
fn test_enum_object_keys_string() {
    // For string enums, Object.keys should only include the member names
    // No reverse mappings
    assert_eq!(
        eval(
            r#"
        enum Color {
            Red = "red",
            Green = "green"
        }
        Object.keys(Color).sort().join(",")
    "#
        ),
        JsValue::from("Green,Red")
    );
}

#[test]
fn test_enum_object_values_numeric() {
    // Object.values on numeric enum includes both numbers and strings (reverse mappings)
    // Note: sorting mixed types may vary, so we'll just check length
    assert_eq!(
        eval(
            r#"
        enum Direction { Up, Down }
        Object.values(Direction).length
    "#
        ),
        JsValue::Number(4.0) // 0, 1, "Up", "Down"
    );
}

#[test]
fn test_enum_object_values_string() {
    // Object.values on string enum - just the string values
    assert_eq!(
        eval(
            r#"
        enum Color {
            Red = "red",
            Green = "green"
        }
        Object.values(Color).sort().join(",")
    "#
        ),
        JsValue::from("green,red")
    );
}

// =============================================================================
// ENUM MUTABILITY
// =============================================================================

#[test]
fn test_enum_is_mutable_like_object() {
    // In TypeScript compiled JS, enums are plain objects and ARE mutable
    // (though TypeScript types prevent this)
    assert_eq!(
        eval(
            r#"
            enum Direction { Up, Down }
            Direction.Up = 999;
            Direction.Up
        "#
        ),
        JsValue::Number(999.0)
    );
}

#[test]
fn test_enum_can_add_properties() {
    // Can add new properties to enum objects
    assert_eq!(
        eval(
            r#"
            enum Direction { Up, Down }
            (Direction as any).NewMember = 100;
            (Direction as any).NewMember
        "#
        ),
        JsValue::Number(100.0)
    );
}

// =============================================================================
// FOR...IN ITERATION
// =============================================================================

#[test]
fn test_enum_for_in_numeric() {
    // for...in on numeric enum iterates over all keys (including reverse mappings)
    assert_eq!(
        eval(
            r#"
        enum Direction { Up, Down }
        let keys: string[] = [];
        for (let k in Direction) {
            keys.push(k);
        }
        keys.sort().join(",")
    "#
        ),
        JsValue::from("0,1,Down,Up")
    );
}

#[test]
fn test_enum_for_in_string() {
    // for...in on string enum - only member names
    assert_eq!(
        eval(
            r#"
        enum Color {
            Red = "red",
            Green = "green"
        }
        let keys: string[] = [];
        for (let k in Color) {
            keys.push(k);
        }
        keys.sort().join(",")
    "#
        ),
        JsValue::from("Green,Red")
    );
}

// =============================================================================
// TYPEOF AND INSTANCEOF
// =============================================================================

#[test]
fn test_enum_typeof() {
    // typeof enum is "object"
    assert_eq!(
        eval(
            r#"
            enum Direction { Up, Down }
            typeof Direction
        "#
        ),
        JsValue::from("object")
    );
}

#[test]
fn test_enum_member_typeof() {
    // typeof enum member depends on value type
    assert_eq!(
        eval(
            r#"
            enum Direction { Up, Down }
            typeof Direction.Up
        "#
        ),
        JsValue::from("number")
    );
    assert_eq!(
        eval(
            r#"
            enum Color { Red = "red" }
            typeof Color.Red
        "#
        ),
        JsValue::from("string")
    );
}

// =============================================================================
// CONST ENUMS (TypeScript-specific - should be inlined at compile time)
// =============================================================================

#[test]
fn test_const_enum_basic() {
    // const enums in TypeScript are typically inlined and don't exist at runtime
    // Our interpreter may or may not support this - let's test
    // Note: Most interpreters treat const enum same as regular enum
    assert_eq!(
        eval(
            r#"
            const enum Direction { Up, Down }
            Direction.Up
        "#
        ),
        JsValue::Number(0.0)
    );
}

// =============================================================================
// ENUM IN EXPRESSIONS
// =============================================================================

#[test]
fn test_enum_in_arithmetic() {
    // Numeric enum values can be used in arithmetic
    assert_eq!(
        eval(
            r#"
            enum Bits {
                Read = 1,
                Write = 2,
                Execute = 4
            }
            Bits.Read | Bits.Write | Bits.Execute
        "#
        ),
        JsValue::Number(7.0)
    );
}

#[test]
fn test_enum_in_comparison() {
    // Enum values can be compared
    assert_eq!(
        eval(
            r#"
            enum Status { A = 1, B = 2 }
            Status.A < Status.B
        "#
        ),
        JsValue::Boolean(true)
    );
    assert_eq!(
        eval(
            r#"
            enum Status { A = 1, B = 2 }
            Status.A === 1
        "#
        ),
        JsValue::Boolean(true)
    );
}

// =============================================================================
// ENUM COMPUTED MEMBERS
// =============================================================================

#[test]
fn test_enum_computed_from_prior_members() {
    // Enum members can reference prior members
    assert_eq!(
        eval(
            r#"
            enum FileAccess {
                None = 0,
                Read = 1,
                Write = 2,
                ReadWrite = Read | Write
            }
            FileAccess.ReadWrite
        "#
        ),
        JsValue::Number(3.0)
    );
}

// =============================================================================
// JSON.stringify BEHAVIOR
// =============================================================================

#[test]
fn test_enum_json_stringify() {
    // JSON.stringify on enum - should stringify as object
    let result = eval(
        r#"
        enum Direction { Up, Down }
        let json = JSON.stringify(Direction);
        json.includes("Up") && json.includes("Down")
    "#,
    );
    assert_eq!(result, JsValue::Boolean(true));
}

// =============================================================================
// PROTOTYPE CHAIN
// =============================================================================

#[test]
fn test_enum_has_object_prototype() {
    // Enum objects should have Object.prototype methods
    assert_eq!(
        eval(
            r#"
            enum Direction { Up, Down }
            Direction.hasOwnProperty("Up")
        "#
        ),
        JsValue::Boolean(true)
    );
}

#[test]
fn test_enum_tostring() {
    // toString on enum object
    assert_eq!(
        eval(
            r#"
            enum Direction { Up, Down }
            Direction.toString()
        "#
        ),
        JsValue::from("[object Object]")
    );
}

// =============================================================================
// NEGATIVE/FLOAT NUMERIC VALUES
// =============================================================================

#[test]
fn test_enum_negative_values() {
    // Enums can have negative values
    assert_eq!(
        eval(
            r#"
            enum Temperature {
                Cold = -10,
                Warm = 20,
                Hot = 40
            }
            Temperature.Cold
        "#
        ),
        JsValue::Number(-10.0)
    );
}

#[test]
fn test_enum_negative_reverse_mapping() {
    // Do negative values get reverse mappings?
    // In TypeScript, they do but with string keys like "-10"
    assert_eq!(
        eval(
            r#"
            enum Temperature {
                Cold = -10,
                Warm = 20
            }
            Temperature[-10]
        "#
        ),
        JsValue::from("Cold")
    );
}

#[test]
fn test_enum_float_values() {
    // Float values in enums
    assert_eq!(
        eval(
            r#"
            enum Ratio {
                Half = 0.5,
                Quarter = 0.25
            }
            Ratio.Half
        "#
        ),
        JsValue::Number(0.5)
    );
}

#[test]
fn test_enum_float_reverse_mapping() {
    // TypeScript generates reverse mappings with string keys like "0.5"
    // Accessing Ratio[0.5] converts 0.5 to "0.5" string key
    assert_eq!(
        eval(
            r#"
            enum Ratio {
                Half = 0.5,
                Quarter = 0.25
            }
            Ratio[0.5]
        "#
        ),
        JsValue::from("Half")
    );
}

// =============================================================================
// AUTO-INCREMENT AFTER EXPLICIT VALUE
// =============================================================================

#[test]
fn test_enum_auto_increment_after_explicit() {
    // After an explicit numeric value, auto-increment continues from there
    assert_eq!(
        eval(
            r#"
            enum Mixed {
                A,        // 0
                B = 10,   // 10
                C,        // 11
                D         // 12
            }
            Mixed.A.toString() + "," + Mixed.B + "," + Mixed.C + "," + Mixed.D
        "#
        ),
        JsValue::from("0,10,11,12")
    );
}

// =============================================================================
// ENUM DECLARATION IN DIFFERENT SCOPES
// =============================================================================

#[test]
fn test_enum_in_function_scope() {
    // Enums declared inside functions
    assert_eq!(
        eval(
            r#"
            function test(): number {
                enum Local { A = 5 }
                return Local.A;
            }
            test()
        "#
        ),
        JsValue::Number(5.0)
    );
}

#[test]
fn test_enum_shadowing() {
    // Enum name shadowing
    assert_eq!(
        eval(
            r#"
            enum X { A = 1 }
            function test(): number {
                enum X { A = 99 }
                return X.A;
            }
            test() + X.A
        "#
        ),
        JsValue::Number(100.0) // 99 + 1
    );
}

// =============================================================================
// ENUM AS OBJECT OPERATIONS
// =============================================================================

#[test]
fn test_enum_object_freeze() {
    // Can we freeze an enum?
    assert_eq!(
        eval(
            r#"
            enum Direction { Up, Down }
            Object.freeze(Direction);
            Object.isFrozen(Direction)
        "#
        ),
        JsValue::Boolean(true)
    );
}

#[test]
fn test_enum_object_keys_after_freeze() {
    // Object.keys still works after freeze
    assert_eq!(
        eval(
            r#"
            enum Direction { Up, Down }
            Object.freeze(Direction);
            Object.keys(Direction).length
        "#
        ),
        JsValue::Number(4.0) // 0, 1, Up, Down
    );
}

// =============================================================================
// EDGE CASES
// =============================================================================

#[test]
fn test_enum_empty() {
    // Empty enum
    assert_eq!(
        eval(
            r#"
            enum Empty {}
            Object.keys(Empty).length
        "#
        ),
        JsValue::Number(0.0)
    );
}

#[test]
fn test_enum_single_member() {
    // Single member enum
    assert_eq!(
        eval(
            r#"
            enum Single { Only }
            Single.Only
        "#
        ),
        JsValue::Number(0.0)
    );
}

#[test]
fn test_enum_member_named_constructor() {
    // Member named "constructor" - potential issue
    assert_eq!(
        eval(
            r#"
            enum Special { constructor = 1 }
            Special.constructor
        "#
        ),
        JsValue::Number(1.0)
    );
}

#[test]
fn test_enum_member_named_prototype() {
    // Member named "prototype" - potential issue
    assert_eq!(
        eval(
            r#"
            enum Special { prototype = 1 }
            Special.prototype
        "#
        ),
        JsValue::Number(1.0)
    );
}