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
//! Map-related tests

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

#[test]
fn test_map_creation() {
    assert_eq!(eval("let m = new Map(); m.size"), JsValue::Number(0.0));
}

#[test]
fn test_map_set_get() {
    assert_eq!(
        eval("let m = new Map(); m.set('a', 1); m.get('a')"),
        JsValue::Number(1.0)
    );
}

#[test]
fn test_map_has() {
    assert_eq!(
        eval("let m = new Map(); m.set('a', 1); m.has('a')"),
        JsValue::Boolean(true)
    );
    assert_eq!(
        eval("let m = new Map(); m.has('a')"),
        JsValue::Boolean(false)
    );
}

#[test]
fn test_map_size() {
    assert_eq!(
        eval("let m = new Map(); m.set('a', 1); m.size"),
        JsValue::Number(1.0)
    );
}

#[test]
fn test_map_delete() {
    // Use bracket notation for 'delete' since it's a reserved word
    assert_eq!(
        eval("let m = new Map(); m.set('a', 1); m['delete']('a'); m.has('a')"),
        JsValue::Boolean(false)
    );
}

#[test]
fn test_map_delete_dot_notation() {
    // In JavaScript, reserved words can be used as property names with dot notation
    assert_eq!(
        eval("let m = new Map(); m.set('a', 1); m.delete('a'); m.has('a')"),
        JsValue::Boolean(false)
    );
}

#[test]
fn test_map_clear() {
    assert_eq!(
        eval("let m = new Map(); m.set('a', 1); m.set('b', 2); m.clear(); m.size"),
        JsValue::Number(0.0)
    );
}

#[test]
fn test_map_object_keys() {
    assert_eq!(
        eval("let m = new Map(); let obj = {}; m.set(obj, 'value'); m.get(obj)"),
        JsValue::from("value")
    );
}

#[test]
fn test_map_init_with_array() {
    assert_eq!(
        eval("let m = new Map([['a', 1], ['b', 2]]); m.get('b')"),
        JsValue::Number(2.0)
    );
}

#[test]
fn test_map_foreach() {
    assert_eq!(
        eval(
            "let result = []; let m = new Map([['a', 1], ['b', 2]]); m.forEach((v, k) => result.push(k + ':' + v)); result.join(',')"
        ),
        JsValue::from("a:1,b:2")
    );
}

#[test]
fn test_map_chaining() {
    // Method chaining (set returns Map)
    assert_eq!(
        eval("let m = new Map(); m.set('a', 1).set('b', 2).get('b')"),
        JsValue::Number(2.0)
    );
}

#[test]
fn test_map_keys() {
    assert_eq!(
        eval("let m = new Map([['a', 1], ['b', 2]]); Array.from(m.keys()).join(',')"),
        JsValue::from("a,b")
    );
}

#[test]
fn test_map_values() {
    assert_eq!(
        eval("let m = new Map([['a', 1], ['b', 2]]); Array.from(m.values()).join(',')"),
        JsValue::from("1,2")
    );
}

#[test]
fn test_map_entries() {
    assert_eq!(
        eval(
            "let m = new Map([['a', 1], ['b', 2]]); let result = []; for (let e of m.entries()) { result.push(e[0] + ':' + e[1]); } result.join(',')"
        ),
        JsValue::from("a:1,b:2")
    );
}

#[test]
fn test_map_get_non_null_assertion_method_call() {
    // Non-null assertion followed by method call: m.get(key)!.push(...)
    assert_eq!(
        eval(
            r#"
            let m = new Map();
            m.set('arr', []);
            m.get('arr')!.push(1);
            m.get('arr')!.push(2);
            m.get('arr')!.length
        "#
        ),
        JsValue::Number(2.0)
    );
}

#[test]
fn test_from_as_parameter_name() {
    // 'from' is a contextual keyword (used in imports) but valid as parameter name
    assert_eq!(
        eval(
            r#"
            function addEdge(from, to) {
                return from + "->" + to;
            }
            addEdge("A", "B")
        "#
        ),
        JsValue::from("A->B")
    );
}

#[test]
fn test_union_type_with_generic_and_undefined() {
    // Union type: Set<T> | undefined should parse correctly
    assert_eq!(
        eval(
            r#"
            function test(): Set<string> | undefined {
                return new Set(["a", "b"]);
            }
            test().size
        "#
        ),
        JsValue::Number(2.0)
    );
}

// =============================================================================
// Map.groupBy Tests (ES2024)
// =============================================================================

#[test]
fn test_map_groupby_basic() {
    // Basic grouping by a property - returns a Map
    assert_eq!(
        eval(
            r#"
            const items = [
                { type: 'fruit', name: 'apple' },
                { type: 'vegetable', name: 'carrot' },
                { type: 'fruit', name: 'banana' }
            ];
            const grouped = Map.groupBy(items, (item: any) => item.type);
            grouped.get('fruit').length
        "#
        ),
        JsValue::Number(2.0)
    );
}

#[test]
fn test_map_groupby_empty_array() {
    // Empty array should return empty Map
    assert_eq!(
        eval(
            r#"
            const grouped = Map.groupBy([], (x: any) => x);
            grouped.size
        "#
        ),
        JsValue::Number(0.0)
    );
}

#[test]
fn test_map_groupby_object_keys() {
    // Map.groupBy can use objects as keys (unlike Object.groupBy)
    assert_eq!(
        eval(
            r#"
            const key1 = { id: 1 };
            const key2 = { id: 2 };
            const items = [
                { key: key1, value: 'a' },
                { key: key2, value: 'b' },
                { key: key1, value: 'c' }
            ];
            const grouped = Map.groupBy(items, (item: any) => item.key);
            grouped.get(key1).length
        "#
        ),
        JsValue::Number(2.0)
    );
}

#[test]
fn test_map_groupby_preserves_order() {
    // Items in each group should be in insertion order
    assert_eq!(
        eval(
            r#"
            const nums: number[] = [3, 1, 4, 1, 5, 9, 2, 6];
            const grouped = Map.groupBy(nums, (n: number) => n % 2 === 0 ? "even" : "odd");
            grouped.get('odd').join(',')
        "#
        ),
        JsValue::String("3,1,1,5,9".into())
    );
}

#[test]
fn test_map_groupby_with_index() {
    // Callback receives index as second argument
    assert_eq!(
        eval(
            r#"
            const letters: string[] = ['a', 'b', 'c', 'd'];
            const grouped = Map.groupBy(letters, (_: string, i: number) => i < 2 ? "first" : "second");
            [grouped.get('first').join(''), grouped.get('second').join('')].join('|')
        "#
        ),
        JsValue::String("ab|cd".into())
    );
}

#[test]
fn test_map_groupby_returns_map_instance() {
    // Result should be a Map
    assert_eq!(
        eval(
            r#"
            const grouped = Map.groupBy([1, 2], (x: number) => x);
            grouped instanceof Map
        "#
        ),
        JsValue::Boolean(true)
    );
}

// =============================================================================
// Map Iteration Tests (for...of, spread, Symbol.iterator)
// =============================================================================

#[test]
fn test_map_for_of_iteration() {
    // Map should be iterable with for...of, yielding [key, value] pairs
    assert_eq!(
        eval(
            r#"
            const m = new Map([['a', 1], ['b', 2], ['c', 3]]);
            let result: string[] = [];
            for (const [k, v] of m) {
                result.push(k + ':' + v);
            }
            result.join(',')
        "#
        ),
        JsValue::from("a:1,b:2,c:3")
    );
}

#[test]
fn test_map_for_of_empty() {
    // Empty map iteration should work
    assert_eq!(
        eval(
            r#"
            const m = new Map();
            let count = 0;
            for (const entry of m) {
                count++;
            }
            count
        "#
        ),
        JsValue::Number(0.0)
    );
}

#[test]
fn test_map_spread_operator() {
    // Spread operator should convert Map to array of [key, value] pairs
    assert_eq!(
        eval(
            r#"
            const m = new Map([['x', 10], ['y', 20]]);
            const arr = [...m];
            arr.length
        "#
        ),
        JsValue::Number(2.0)
    );
}

#[test]
fn test_map_spread_operator_contents() {
    // Verify spread contents are [key, value] pairs
    assert_eq!(
        eval(
            r#"
            const m = new Map([['x', 10], ['y', 20]]);
            const arr = [...m];
            arr[0][0] + ':' + arr[0][1] + ',' + arr[1][0] + ':' + arr[1][1]
        "#
        ),
        JsValue::from("x:10,y:20")
    );
}

#[test]
fn test_map_symbol_iterator_exists() {
    // Map should have Symbol.iterator
    assert_eq!(
        eval(
            r#"
            const m = new Map();
            typeof m[Symbol.iterator]
        "#
        ),
        JsValue::from("function")
    );
}

#[test]
fn test_map_array_from() {
    // Array.from should work with Map
    assert_eq!(
        eval(
            r#"
            const m = new Map([['a', 1], ['b', 2]]);
            const arr = Array.from(m);
            arr.length
        "#
        ),
        JsValue::Number(2.0)
    );
}

#[test]
fn test_map_destructuring_in_for_of() {
    // Destructuring should work in for...of
    assert_eq!(
        eval(
            r#"
            const m = new Map([['first', 100], ['second', 200]]);
            let keys: string[] = [];
            let values: number[] = [];
            for (const [key, value] of m) {
                keys.push(key);
                values.push(value);
            }
            keys.join(',') + '|' + values.join(',')
        "#
        ),
        JsValue::from("first,second|100,200")
    );
}

#[test]
fn test_map_iterator_entries_equivalence() {
    // Symbol.iterator should be equivalent to entries()
    assert_eq!(
        eval(
            r#"
            const m = new Map([['a', 1]]);
            const iter1 = m[Symbol.iterator]();
            const iter2 = m.entries();
            const r1 = iter1.next().value;
            const r2 = iter2.next().value;
            // Both should return the same structure
            (r1[0] === r2[0] && r1[1] === r2[1]).toString()
        "#
        ),
        JsValue::from("true")
    );
}