forked from decaffeinate/decaffeinate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
conditional_test.js
332 lines (300 loc) · 6.13 KB
/
conditional_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
import check from './support/check.js';
describe('conditionals', () => {
it('surrounds `if` conditions in parentheses and bodies in curly braces', () => {
check(`
if a
b
`, `
if (a) {
b;
}
`);
});
it('surrounds `unless` conditions in parentheses and precedes it with a `!` operator', () => {
check(`
unless a
b
`, `
if (!a) {
b;
}
`);
});
it('surrounds `unless` conditions in additional parentheses if needed for the `!` operator', () => {
check(`
unless a == b
c
`, `
if (a !== b) {
c;
}
`);
});
it('does not add parentheses if the condition is already surrounded by them', () => {
check(`
if (a)
b
`, `
if (a) {
b;
}
`);
check(`
unless (a)
b
`, `
if (!a) {
b;
}
`);
});
it('causes the condition not to add parentheses even if it normally would', () => {
check(`
a = b
if a?
c
`, `
let a = b;
if (a != null) {
c;
}
`);
});
it('correctly inserts parentheses when an `unless` condition requires them when it was surrounded by parentheses', () => {
check(`
unless (a == b)
c
`, `
if (a !== b) {
c;
}
`);
});
it('handles indented `if` statements correctly', () => {
check(`
if a
if b
c
`, `
if (a) {
if (b) {
c;
}
}
`);
});
it('surrounds the `else` clause of an `if` statement in curly braces', () => {
check(`
if a
b
else
c
`, `
if (a) {
b;
} else {
c;
}
`);
});
it('surrounds the `else if` condition in parentheses', () => {
check(`
if a
b
else if c
d
`, `
if (a) {
b;
} else if (c) {
d;
}
`);
});
it('works with several `else if` clauses and an `else`', () => {
check(`
if a
b
else if c
d
else if e
f
else
g
`, `
if (a) {
b;
} else if (c) {
d;
} else if (e) {
f;
} else {
g;
}
`);
});
it('works with `if` with immediate return', () => {
check(`
->
if a
b
c
`, `
(function() {
if (a) {
return b;
}
});
c;
`);
});
it('keeps single-line `if` statements on one line', () => {
check(`if a then b`, `if (a) { b; }`);
});
it('keeps single-line `if` with `else` on one line', () => {
check(`if a then b else c`, `if (a) { b; } else { c; }`);
});
it('handles `if` statements with an implicit call in the condition', () => {
check(`if a b then c`, `if (a(b)) { c; }`);
});
it('turns simple `if` with `else` as an expression into a ternary operator', () => {
check(`a(if b then c else d)`, `a(b ? c : d);`);
});
it('turns simple `if` without `else` as an expression into a ternary operator with undefined', () => {
check(`a(if b then c)`, `a(b ? c : undefined);`);
});
it('adds ternary operator code after any insertions for the consequent', () => {
check(`a(if b then c: d)`, `a(b ? {c: d} : undefined);`);
});
it('turns multi-line `if` into a ternary with sequence expressions', () => {
check(`
->
z(if a
c = a
a + c
else
d = a
a - d)
`, `
(function() {
let c;
let d;
return z(a ?
(c = a,
a + c)
:
(d = a,
a - d));
});
`);
});
it('keeps single-line POST-`if`', () => {
check(`a if b`, `if (b) { a; }`);
check(`
->
return if a is b
null
`, `
(function() {
if (a === b) { return; }
return null;
});
`);
});
it('keeps single-line POST-`unless`', () => {
check(`a unless b`, `if (!b) { a; }`);
});
it('pushes returns into `if` statements', () => {
check(`
->
if a
b
`, `
(function() {
if (a) {
return b;
}
});
`);
});
it('pushes returns into `else if` blocks', () => {
check(`
->
if a
b
else if c
d
`, `
(function() {
if (a) {
return b;
} else if (c) {
return d;
}
});
`);
});
it('works with if then throw inline', () => {
check(`if a then throw err`, `if (a) { throw err; }`);
});
it('works with POST-`if` as the body of a function', () => {
check(`-> a if b`, `(function() { if (b) { return a; } });`);
});
it('works with nested POST-`if`', () => {
check(`
a(b) if c(d) unless e(f)
`, `
if (!e(f)) { if (c(d)) { a(b); } }
`);
});
// TODO: Enable once https://github.com/Rich-Harris/magic-string/pull/89 lands.
it.skip('works with nested POST-`if` with implicit calls', () => {
check(`
a b if c d unless e f
`, `
if (!e(f)) { if (c(d)) { a(b); } }
`);
});
it('surrounds the conditional expression in parens as part of a binary expression', () => {
check(`
a + if b then c else d
`, `
a + (b ? c : d);
`);
});
it('surrounds the conditional expression in parens as part of a unary expression', () => {
check(`
-if b then c else d
`, `
-(b ? c : d);
`);
});
it('does not add extra parens to a conditional expression', () => {
check(`
a + (if b then c else d)
`, `
a + (b ? c : d);
`);
});
it('does not add unnecessary parens to a conditional expression', () => {
check(`
a ** if b then c else d
`, `
Math.pow(a, b ? c : d);
`);
});
it('adds parenthesis if needed', () => {
check(`
alert if (a) and (b)
`, `
if ((a) && (b)) { alert; }
`);
});
it('does not remove parentheses around conditions in conditional expressions', () => {
check(`
r = if (f is 0.5) then a else b
`, `
let r = (f === 0.5) ? a : b;
`);
});
});