-
-
Notifications
You must be signed in to change notification settings - Fork 185
Expand file tree
/
Copy pathprecedingStatements.spec.ts
More file actions
656 lines (599 loc) · 20.8 KB
/
precedingStatements.spec.ts
File metadata and controls
656 lines (599 loc) · 20.8 KB
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
import * as util from "../util";
const shortCircuitTests: Array<{ operator: string; testValue: unknown }> = [
{ operator: "&&", testValue: true },
{ operator: "&&", testValue: false },
{ operator: "&&", testValue: null },
{ operator: "&&=", testValue: true },
{ operator: "&&=", testValue: false },
{ operator: "&&=", testValue: null },
{ operator: "||", testValue: true },
{ operator: "||", testValue: false },
{ operator: "||", testValue: null },
{ operator: "||=", testValue: true },
{ operator: "||=", testValue: false },
{ operator: "||=", testValue: null },
{ operator: "??", testValue: true },
{ operator: "??", testValue: false },
{ operator: "??", testValue: null },
{ operator: "??=", testValue: true },
{ operator: "??=", testValue: false },
{ operator: "??=", testValue: null },
];
test.each(shortCircuitTests)("short circuit operator (%p)", ({ operator, testValue }) => {
util.testFunction`
let x: unknown = ${testValue};
let y = 1;
const z = x ${operator} y++;
return {x, y, z};
`.expectToMatchJsResult();
});
test.each(shortCircuitTests)("short circuit operator on property (%p)", ({ operator, testValue }) => {
util.testFunction`
let x: { foo: unknown } = { foo: ${testValue} };
let y = 1;
const z = x.foo ${operator} y++;
return {x: x.foo, y, z};
`.expectToMatchJsResult();
});
test.each([true, false])("ternary operator (%p)", condition => {
util.testFunction`
let a = 0, b = 0;
let condition: boolean = ${condition};
const c = condition ? a++ : b++;
return [a, b, c];
`.expectToMatchJsResult();
});
describe("execution order", () => {
const sequenceTests = [
"i++, i",
"i, i++, i, i++",
"...a",
"i, ...a",
"...a, i",
"i, ...a, i++, i, ...a",
"i, ...a, i++, i, ...a, i",
"...[1, i++, 2]",
"...[1, i++, 2], i++",
"i, ...[1, i++, 2]",
"i, ...[1, i++, 2], i",
"i, ...[1, i++, 2], i++",
"i, ...[1, i++, 2], i++, ...[3, i++, 4]",
"i, ...a, i++, ...[1, i++, 2], i, i++, ...a",
"i, inc(), i++",
"i, ...[1, i++, inc(), 2], i++",
"i, ...'foo', i++",
"i, ...([1, i++, 2] as any), i++",
];
test.each(sequenceTests)("array literal ([%p])", sequence => {
util.testFunction`
const a = [7, 8, 9];
let i = 0;
function inc() { ++i; return i; }
return [${sequence}];
`.expectToMatchJsResult();
});
test.each(sequenceTests)("function arguments (foo(%p))", sequence => {
util.testFunction`
const a = [7, 8, 9];
let i = 0;
function inc() { ++i; return i; }
function foo(...args: unknown[]) { return args; }
return foo(${sequence});
`.expectToMatchJsResult();
});
test.each([
"{a: i, b: i++}",
"{a: i, b: i++, c: i}",
"{a: i, ...{b: i++}, c: i}",
"{a: i, ...o, b: i++}",
"{a: i, ...[i], b: i++}",
"{a: i, ...[i++], b: i++}",
"{a: i, ...o, b: i++, ...[i], ...{c: i++}, d: i++}",
])("object literal (%p)", literal => {
util.testFunction`
const o = {a: "A", b: "B", c: "C"};
let i = 0;
const literal = ${literal};
const result: Record<string, unknown> = {};
(Object.keys(result) as Array<number | string>).forEach(
key => { result[key.toString()] = literal[key]; }
);
return result;
`.expectToMatchJsResult();
});
test("object literal with computed property names", () => {
util.testFunction`
let i = "A";
const o = {[i += "B"]: i += "C", [i += "D"]: i += "E"};
return [i, o];
`.expectToMatchJsResult();
});
test("comma operator", () => {
util.testFunction`
let a = 0, b = 0, c = 0;
const d = (a++, b += a, c += b);
return [a, b, c, d];
`.expectToMatchJsResult();
});
test("template expression", () => {
util.testFunction`
let i = 0;
return \`\${i}, \${i++}, \${i}\`;
`.expectToMatchJsResult();
});
test("tagged template literal", () => {
util.testFunction`
let i = 0;
function func(strings: TemplateStringsArray, ...expressions: any[]) {
const x = i > 0 ? "a" : "b";
return { strings: [x, ...strings], raw: strings.raw, expressions };
}
return func\`hello \${i} \${i++}\`;
`.expectToMatchJsResult();
});
test("binary operators", () => {
util.testFunction`
let i = 0;
return i + i++;
`.expectToMatchJsResult();
});
test("index expression", () => {
util.testFunction`
let i = 0;
const a = [["A1", "A2"], ["B1", "B2"]];
const result = a[i][i++];
return [result, i];
`.expectToMatchJsResult();
});
test("void expression", () => {
util.testFunction`
function foo(x: number, y: number, z: number | undefined) {
return z;
}
let i = 0;
const result = foo(i, i++, void(i++));
return {result, i};
`.expectToMatchJsResult();
});
});
describe("assignment execution order", () => {
test("index assignment statement", () => {
util.testFunction`
let i = 0;
const a = [4, 5];
a[i] = i++;
return [a, i];
`.expectToMatchJsResult();
});
test("index assignment expression", () => {
util.testFunction`
let i = 0;
const a = [9, 8];
const result = a[i] = i++;
return [result, a, i];
`.expectToMatchJsResult();
});
test("indirect index assignment statement", () => {
util.testFunction`
let i = 0;
const a = [9, 8];
const b = [7, 6];
function foo(x: number) { return (x > 0) ? b : a; }
foo(i)[i] = i++;
return [a, b, i];
`.expectToMatchJsResult();
});
test("indirect index assignment expression", () => {
util.testFunction`
let i = 0;
const a = [9, 8];
const b = [7, 6];
function foo(x: number) { return (x > 0) ? b : a; }
const result = foo(i)[i] = i++;
return [result, a, b, i];
`.expectToMatchJsResult();
});
test("indirect property assignment statement", () => {
util.testFunction`
const a = {value: 10};
const b = {value: 11};
let i = 0;
function foo(x: number) { return (x > 0) ? b : a; }
foo(i).value = i++;
return [a, b, i];
`.expectToMatchJsResult();
});
test("indirect property assignment expression", () => {
util.testFunction`
const a = {value: 10};
const b = {value: 11};
let i = 0;
function foo(x: number) { return (x > 0) ? b : a; }
const result = foo(i).value = i++;
return [result, a, b, i];
`.expectToMatchJsResult();
});
test("compound index assignment statement", () => {
util.testFunction`
let i = 0;
const a = [9, 8];
a[i] += i++;
return [a, i];
`.expectToMatchJsResult();
});
test("compound index assignment expression", () => {
util.testFunction`
let i = 0;
const a = [9, 8];
const result = a[i] += i++;
return [result, a, i];
`.expectToMatchJsResult();
});
test("compound indirect index assignment statement", () => {
util.testFunction`
let i = 0;
const a = [9, 8];
const b = [7, 6];
function foo(x: number) { return (x > 0) ? b : a; }
foo(i)[i] += i++;
return [a, b, i];
`.expectToMatchJsResult();
});
test("compound indirect index assignment expression", () => {
util.testFunction`
let i = 1;
const a = [9, 8];
const b = [7, 6];
function foo(x: number) { return (x > 0) ? b : a; }
const result = foo(i)[i] += i++;
return [result, a, b, i];
`.expectToMatchJsResult();
});
test("array destructuring assignment statement", () => {
util.testFunction`
const a = [10, 9, 8, 7, 6, 5];
let i = 0;
[a[i], a[i++]] = [i++, i++];
return [a, i];
`.expectToMatchJsResult();
});
test("array destructuring assignment expression", () => {
util.testFunction`
const a = [10, 9, 8, 7, 6, 5];
let i = 0;
const result = [a[i], a[i++]] = [i++, i++];
return [a, i, result];
`.expectToMatchJsResult();
});
test("array destructuring assignment statement with default", () => {
util.testFunction`
const a = [10, 9, 8, 7, 6, 5];
let i = 0;
[a[i] = i++, a[i++]] = [i++, i++];
return [a, i];
`.expectToMatchJsResult();
});
test("array destructuring assignment expression with default", () => {
util.testFunction`
const a = [10, 9, 8, 7, 6, 5];
let i = 0;
const result = [a[i] = i++, a[i++]] = [i++, i++];
return [a, i, result];
`.expectToMatchJsResult();
});
test("array destructuring assignment statement with spread", () => {
util.testFunction`
let i = 0;
let a: number[][] = [[9, 9, 9], [9, 9, 9], [9, 9, 9]];
[a[0][i], ...a[i++]] = [i++, i++];
return [a, i];
`.expectToMatchJsResult();
});
test("array destructuring assignment expression with spread", () => {
util.testFunction`
let i = 0;
let a: number[][] = [[9, 9, 9], [9, 9, 9], [9, 9, 9]];
const result = [a[0][i], ...a[i++]] = [i++, i++];
return [a, i, result];
`.expectToMatchJsResult();
});
test("object destructuring assignment statement", () => {
util.testFunction`
let s = "A";
const o: Record<string, string> = {ABCDEFG: "success", result: ""};
function c(x: string) { s = x + "C"; return o; }
function g(x: string) { s = x + "G"; return o; }
function e(x: string) { s = x + "E"; return s; }
({ [e(s += "D")]: g(s += "F").result } = c(s += "B"));
return [s, o];
`.expectToMatchJsResult();
});
test("object destructuring assignment statement with default", () => {
util.testFunction`
let s = "A";
const o: Record<string, string> = {ABCDEFGHIJ: "success", result: ""};
function c(x: string) { s = x + "C"; return o; }
function g(x: string) { s = x + "G"; return o; }
function i(x: string) { s = x + "I"; return o; }
function e(x: string): any { s = x + "E"; return undefined; }
({ [e(s += "D")]: g(s += "F").result = i(s += "H")[s += "J"] } = c(s += "B"));
return [o, s];
`.expectToMatchJsResult();
});
test("object destructuring assignment expression", () => {
util.testFunction`
let s = "A";
const o: Record<string, string> = {ABCDEFG: "success", result: ""};
function c(x: string) { s = x + "C"; return o; }
function g(x: string) { s = x + "G"; return o; }
function e(x: string) { s = x + "E"; return s; }
const result = ({ [e(s += "D")]: g(s += "F").result } = c(s += "B"));
return [s, o, result];
`.expectToMatchJsResult();
});
test("object destructuring assignment expression with default", () => {
util.testFunction`
let s = "A";
const o: Record<string, string> = {ABCDEFGHIJ: "success", result: ""};
function c(x: string) { s = x + "C"; return o; }
function g(x: string) { s = x + "G"; return o; }
function i(x: string) { s = x + "I"; return o; }
function e(x: string): any { s = x + "E"; return undefined; }
const result = ({ [e(s += "D")]: g(s += "F").result = i(s += "H")[s += "J"] } = c(s += "B"));
return [o, s, result];
`.expectToMatchJsResult();
});
test("object destructuring declaration", () => {
util.testFunction`
let s = "A";
const o: Record<string, string> = {ABCDE: "success"};
function c(x: string) { s = x + "C"; return o; }
function e(x: string) { s = x + "E"; return s; }
const { [e(s += "D")]: result } = c(s += "B");
return [result, s];
`.expectToMatchJsResult();
});
test("object destructuring declaration with default", () => {
util.testFunction`
let s = "A";
const o: Record<string, string> = {ABCDEFGH: "success"};
function c(x: string) { s = x + "C"; return o; }
function g(x: string) { s = x + "G"; return o; }
function e(x: string): any { s = x + "E"; return undefined; }
const { [e(s += "D")]: result = g(s += "F")[s += "H"]} = c(s += "B");
return [result, s];
`.expectToMatchJsResult();
});
test("call expression", () => {
util.testFunction`
let i = 1;
function a(x: number) { return x * 10; }
function b(x: number) { return x * 100; }
function foo(x: number) { return (x > 0) ? b : a; }
const result = foo(i)(i++);
return [result, i];
`.expectToMatchJsResult();
});
test("call expression (function modified)", () => {
util.testFunction`
let i = 1;
let foo = (x: null, y: number) => { return y; };
function changeFoo() {
foo = (x: null, y: number) => { return y * 10; };
return null;
}
const result = foo(changeFoo(), i++);
return [result, i];
`.expectToMatchJsResult();
});
test("method call expression (method modified)", () => {
util.testFunction`
let i = 1;
let o = {
val: 3,
foo(x: null, y: number) { return y + this.val; }
};
function changeFoo(this: void) {
o.foo = function(x: null, y: number) { return (y + this.val) * 10; };
return null;
}
const result = o.foo(changeFoo(), i++);
return [result, i];
`.expectToMatchJsResult();
});
test("method element access call expression (method modified)", () => {
util.testFunction`
let i = 1;
let o = {
val: 3,
foo(x: null, y: number) { return y + this.val; }
};
function changeFoo(this: void) {
o.foo = function(x: null, y: number) { return (y + this.val) * 10; };
return null;
}
function getFoo() { return "foo" as const; }
function getO() { return o; }
const result = getO()[getFoo()](changeFoo(), i++);
return [result, i];
`.expectToMatchJsResult();
});
test("method call expression (object modified)", () => {
util.testFunction`
let i = 1;
let o = {
val: 3,
foo(x: null, y: number) { return y + this.val; }
};
function changeO(this: void) {
o = {
val: 5,
foo: function(x: null, y: number) { return (y + this.val) * 10; }
};
return null;
}
const result = o.foo(changeO(), i++);
return [result, i];
`.expectToMatchJsResult();
});
test("method element access call expression (object modified)", () => {
util.testFunction`
let i = 1;
let o = {
val: 3,
foo(x: null, y: number) { return y + this.val; }
};
function changeO(this: void) {
o = {
val: 5,
foo: function(x: null, y: number) { return (y + this.val) * 10; }
};
return null;
}
function getFoo() { return "foo" as const; }
function getO() { return o; }
const result = getO()[getFoo()](changeO(), i++);
return [result, i];
`.expectToMatchJsResult();
});
test("array method call", () => {
util.testFunction`
let a = [7];
let b = [9];
function foo(x: number) { return (x > 0) ? b : a; }
let i = 0;
foo(i).push(i, i++, i);
return [a, b, i];
`.expectToMatchJsResult();
});
test("function method call", () => {
util.testFunction`
let o = {val: 3};
let a = function(this: typeof o, x: number) { return this.val + x; };
let b = function(this: typeof o, x: number) { return (this.val + x) * 10; };
function foo(x: number) { return (x > 0) ? b : a; }
let i = 0;
const result = foo(i).call(o, i++);
return [result, i];
`.expectToMatchJsResult();
});
test("string method call", () => {
util.testFunction`
function foo(x: number) { return (x > 0) ? "foo" : "bar"; }
let i = 0;
const result = foo(i).substr(++i);
return [result, i];
`.expectToMatchJsResult();
});
test("new call", () => {
util.testFunction`
class A { public val = 3; constructor(x: number) { this.val += x; } };
class B { public val = 5; constructor(x: number) { this.val += (x * 10); } };
function foo(x: number) { return (x > 0) ? B : A; }
let i = 0;
const result = new (foo(i))(i++).val;
return [result, i];
`.expectToMatchJsResult();
});
});
describe("loop expressions", () => {
test("while loop", () => {
util.testFunction`
let i = 0, j = 0;
while (i++ < 5) {
++j;
if (j >= 10) {
break;
}
}
return [i, j];
`.expectToMatchJsResult();
});
test("for loop", () => {
util.testFunction`
let j = 0;
for (let i = 0; i++ < 5;) {
++j;
if (j >= 10) {
break;
}
}
return j;
`.expectToMatchJsResult();
});
test("do while loop", () => {
util.testFunction`
let i = 0, j = 0;
do {
++j;
if (j >= 10) {
break;
}
} while (i++ < 5);
return [i, j];
`.expectToMatchJsResult();
});
test("do while loop scoping", () => {
util.testFunction`
let x = 0;
let result = 0;
do {
let x = -10;
++result;
} while (x++ >= 0 && result < 2);
return result;
`.expectToMatchJsResult();
});
});
test("switch", () => {
util.testFunction`
let i = 0;
let x = 0;
let result = "";
switch (x) {
case i++:
result = "test";
break;
case i++:
}
return [i, result];
`.expectToMatchJsResult();
});
test("else if", () => {
util.testFunction`
let i = 0;
if (i++ === 0) {
} else if (i++ === 1) {
}
return i;
`.expectToMatchJsResult();
});
// https://github.com/TypeScriptToLua/TypeScriptToLua/issues/1208
test("class member initializers", () => {
util.testFunction`
class MyClass {
myField = false ?? true;
constructor(public foo: number = 0 ?? 5) {}
}
const inst = new MyClass();
return [inst.myField, inst.foo];
`
.ignoreDiagnostics([
2869 /* TS2869: Right operand of ?? is unreachable because the left operand is never nullish. */,
])
.expectToMatchJsResult();
});
test("class member initializers in extended class", () => {
util.testFunction`
class A {}
class MyClass extends A {
myField = false ?? true;
}
const inst = new MyClass();
return inst.myField;
`
.ignoreDiagnostics([
2869 /* TS2869: Right operand of ?? is unreachable because the left operand is never nullish. */,
])
.expectToMatchJsResult();
});