forked from decaffeinate/decaffeinate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
for_test.js
562 lines (516 loc) · 11.5 KB
/
for_test.js
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
import check from './support/check.js';
describe('for loops', () => {
it('transforms basic for-of loops into for-in', () => {
check(`
for k of o
k
`, `
for (let k in o) {
k;
}
`);
});
it('transforms for-of loops with both key and value into for-in', () => {
check(`
for k, v of o
k
`, `
for (let k in o) {
let v = o[k];
k;
}
`);
});
it('transforms for-of loops with both key and value plus unsafe-to-repeat target by saving a reference', () => {
check(`
for k, v of object()
k
`, `
let iterable;
for (let k in (iterable = object())) {
let v = iterable[k];
k;
}
`);
});
it('transforms for-of loops with destructured value', () => {
check(`
for k, {x, y} of o
k + x
`, `
for (let k in o) {
let {x, y} = o[k];
k + x;
}
`);
});
it('transforms for-of loops with destructured value plus unsafe-to-repeat target', () => {
check(`
for key, {x, y} of object()
key + x
`, `
let iterable;
for (let key in (iterable = object())) {
let {x, y} = iterable[key];
key + x;
}
`);
});
it('transforms for-in loops to typical `for` loops', () => {
check(`
for a in b
a
`, `
for (let i = 0; i < b.length; i++) {
let a = b[i];
a;
}
`);
});
it('transforms for-in loops with an index to typical `for` loops', () => {
check(`
for a, j in b
a
`, `
for (let j = 0; j < b.length; j++) {
let a = b[j];
a;
}
`);
});
it('transforms for-in loops with destructured value', () => {
check(`
for { a, b } in c
a + b
`, `
for (let i = 0; i < c.length; i++) {
let { a, b } = c[i];
a + b;
}
`);
});
it.skip('transforms `for` loops without an index', () => {
check(`
for [0..1]
2
`, `
for (let i = 0; i <= 1; i++) {
2;
}
`);
});
it.skip('gives `for` loops without an index an index that does not collide with existing bindings', () => {
check(`
for [0..1]
i
`, `
for (let j = 0; j <= 1; j++) {
i;
}
`);
});
it('allows iterating with for-in by a specific step size', () => {
check(`
for a in b by 2
a
`, `
for (let i = 0; i < b.length; i += 2) {
let a = b[i];
a;
}
`);
});
it('allows iterating with for-in in reverse', () => {
check(`
for a in b by -1
a
`, `
for (let i = b.length - 1; i >= 0; i--) {
let a = b[i];
a;
}
`);
});
it('allows iterating with for-in in reverse with a specific step size', () => {
check(`
for a in b by -2
a
`, `
for (let i = b.length - 1; i >= 0; i -= 2) {
let a = b[i];
a;
}
`);
});
it('allows filtering using a `when` clause', () => {
check(`
for a in b when a.c
a
`, `
for (let i = 0; i < b.length; i++) {
let a = b[i];
if (a.c) {
a;
}
}
`);
});
it('allows using both `when` and `by` clauses', () => {
check(`
for a in b by 2 when a.c
a
`, `
for (let i = 0; i < b.length; i += 2) {
let a = b[i];
if (a.c) {
a;
}
}
`);
});
it('extracts unsafe-to-repeat iteration targets before the for-in loop', () => {
check(`
for e in list()
break
`, `
let iterable = list();
for (let i = 0; i < iterable.length; i++) {
let e = iterable[i];
break;
}
`);
});
it.skip('special-cases for-in inclusive range loops to avoid creating arrays', () => {
check(`
for i in [0..10]
i
`, `
for (let i = 0; i <= 10; i++) {
i;
}
`);
});
it.skip('special-cases for-in exclusive range loops to avoid creating arrays', () => {
check(`
for i in [0...10]
i
`, `
for (let i = 0; i < 10; i++) {
i;
}
`);
});
it.skip('special-cases descending for-in inclusive range loops to avoid creating arrays', () => {
check(`
for i in [10..0]
i
`, `
for (let i = 10; i >= 0; i--) {
i;
}
`);
});
it.skip('special-cases descending for-in exclusive range loops to avoid creating arrays', () => {
check(`
for i in [10...0]
i
`, `
for (let i = 10; i > 0; i--) {
i;
}
`);
});
it.skip('special-cases descending for-in range loops with step count to avoid creating arrays', () => {
check(`
for i in [100..0] by -2
i
`, `
for (let i = 100; i >= 0; i -= 2) {
i;
}
`);
});
it('allows using an unsafe-to-repeat step value', () => {
check(`
for a in b by (c d)
a()
`, `
for (let i = 0, step = c(d); i < b.length; i += step) {
let a = b[i];
a();
}
`);
});
it.skip('special-cases variable for-in range loops to avoid creating arrays', () => {
check(`
for i in [a..b]
i
`, `
for (let i = a; a < b ? i <= b : i >= b; a < b ? i++ : i--) {
i;
}
`);
});
it.skip('saves references to unsafe-to-repeat range bounds in variable for-in loops', () => {
check(`
for i in [a()..b()]
i
`, `
let start = a();
let end = b();
for (let i = start; start < end ? i <= end : i >= end; start < end ? i++ : i--) {
i;
}
`);
});
it('moves the body of a one-line for-of loop to the end', () => {
check(`
k for k of o
`, `
for (let k in o) { k; }
`);
});
it('moves the body of a one-line for-in loop to the next line', () => {
check(`
e for e in l
`, `
for (let i = 0; i < l.length; i++) { let e = l[i]; e; }
`);
});
it.skip('saves the list of results when for-of loops are used in an expression context', () => {
check(`
a(k for k of o)
`, `
a((() => {
let result = [];
for (let k in o) {
result.push(k);
}
return result;
})());
`);
});
it.skip('saves the list of results when for-in loops are used in an expression context', () => {
check(`
a(e for e in l)
`, `
a((() => {
let result = [];
for (let i = 0, e; i < l.length; i++) {
e = l[i];
result.push(e);
}
return result;
})());
`);
});
it.skip('saves the list of results when for-in loops are used as an implicit return', () => {
check(`
->
a for a in b
`, `
(function() {
return (() => {
let result = [];
for (let i = 0, a; i < b.length; i++) {
a = b[i];
result.push(a);
}
return result;
})();
});
`);
});
it.skip('closes the call to `result.push()` at the right position', () => {
check(`
->
for a in b
if a
b
# this is here to make the real end of "a" be much later
stuff
`, `
(function() {
return (() => {
let result = [];
for (let i = 0, a; i < b.length; i++) {
a = b[i];
result.push((() => {
if (a) {
return b;
}
})());
}
return result;
})();
});
// this is here to make the real end of "a" be much later
stuff;
`);
});
it('generates counters for nested loops that follow typical convention', () => {
check(`
for a in b
for c in d
a + c
`, `
for (let i = 0; i < b.length; i++) {
let a = b[i];
for (let j = 0; j < d.length; j++) {
let c = d[j];
a + c;
}
}
`);
});
it.skip('handles `for own`', () => {
check(`
for own key of list
console.log key
`, `
for (let key in list) {
if (Object.prototype.hasOwnProperty.call(list, key)) {
console.log(key);
}
}
`);
});
it.skip('handles `for own` with an unsafe-to-repeat iterable', () => {
check(`
for own key of getList()
console.log key
`, `
let iterable;
for (let key in (iterable = getList())) {
if (Object.prototype.hasOwnProperty.call(iterable, key)) {
console.log(key);
}
}
`);
});
it.skip('handles `for own` with both key and value', () => {
check(`
for own key, value of list
console.log key, value
`, `
for (let key in list) {
if (Object.prototype.hasOwnProperty.call(list, key)) {
let value = list[key];
console.log(key, value);
}
}
`);
});
it.skip('handles `for own` with a filter', () => {
check(`
for own key of list when key[0] is '_'
console.log key
`, `
for (let key in list) {
if (Object.prototype.hasOwnProperty.call(list, key)) {
if (key[0] === '_') {
console.log(key);
}
}
}
`);
});
it.skip('handles single-line `for own`', () => {
check(`
for own a of b then a
`, `
for (let a in b) { if (Object.prototype.hasOwnProperty.call(b, a)) { a; } }
`);
});
it('does not consider a `for` loop as an implicit return if it returns itself', () => {
check(`
->
for a in b
return a
`, `
(function() {
for (let i = 0; i < b.length; i++) {
let a = b[i];
return a;
}
});
`);
});
it.skip('considers a `for` loop as an implicit return if it only returns within a function', () => {
check(`
->
for a in b
-> return a
`, `
(function() {
return (() => {
let result = [];
for (let i = 0, a; i < b.length; i++) {
a = b[i];
result.push(function() { return a; });
}
return result;
})();
});
`);
});
it('preserves single-line `for-in` loops', () => {
check(`
for a in b then a()
`, `
for (let i = 0; i < b.length; i++) { let a = b[i]; a(); }
`);
});
it('preserves single-line `for-of` loops', () => {
check(`
for k of o then k
`, `
for (let k in o) { k; }
`);
});
it('handles multi-line body with `then`', () => {
check(`
for a in b then (->
console.log('foo')
)()
`, `
for (let i = 0; i < b.length; i++) { let a = b[i]; (() => console.log('foo'))(); }
`);
});
it('handles destructuring as the first statement in a `for` body', () => {
check(`
for entry in someArray
{x, y} = getPoint(entry)
console.log x + ', ' + y;
`, `
for (let i = 0; i < someArray.length; i++) {
let entry = someArray[i];
let {x, y} = getPoint(entry);
console.log(x + ', ' + y);
}
`);
});
it('handles for...when loops ending in implicit function calls', () =>
check(`
for a in b when c
d e
`, `
for (let i = 0; i < b.length; i++) {
let a = b[i];
if (c) {
d(e);
}
}
`));
it('handles for loops over implicit function calls', () =>
check(`
for a in b c
d()
`, `
let iterable = b(c);
for (let i = 0; i < iterable.length; i++) {
let a = iterable[i];
d();
}
`));
});