swc_ecma_transforms 0.16.1

rust port of babel and closure compiler.
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
//! Copied from PeepholeIntegrationTest from the google closure compiler.

#![feature(test)]
use swc_common::chain;
use swc_ecma_transforms::{optimization::simplifier, resolver};

#[macro_use]
mod common;

fn test(src: &str, expected: &str) {
    test_transform!(
        ::swc_ecma_parser::Syntax::default(),
        |_| chain!(resolver(), simplifier(Default::default())),
        src,
        expected,
        true
    )
}

fn test_same(src: &str) {
    test(src, src)
}

macro_rules! to {
    ($name:ident, $src:expr, $expected:expr) => {
        test!(
            Default::default(),
            |_| chain!(resolver(), simplifier(Default::default())),
            $name,
            $src,
            $expected
        );
    };
}

macro_rules! optimized_out {
    ($name:ident, $src:expr) => {
        to!($name, $src, "");
    };
}

optimized_out!(
    single_pass,
    "
const a = 1;

if (a) {
    const b = 2;
}
"
);

optimized_out!(issue_607, "let a");

to!(
    multi_run,
    "
let b = 2;

let a = 1;
if (b) { // Removed by first run of remove_dead_branch
    a = 2; // It becomes `flat assignment` to a on second run of inlining
}

let c;
if (a) { // Removed by second run of remove_dead_branch
    c = 3; // It becomes `flat assignment` to c on third run of inlining.
}
console.log(c); // Prevent optimizing out.
",
    "console.log(3)"
);

#[test]
#[ignore] // TODO
fn test_fold_one_child_blocks_integration() {
    test(
        "function f(){switch(foo()){default:{break}}}",
        "function f(){foo()}",
    );

    test(
        "function f(){switch(x){default:{break}}} use(f);",
        "function f(){} use(f);",
    );

    test(
        "function f(){switch(x){default:x;case 1:return 2}} use(f);",
        "function f(){switch(x){default:case 1:return 2}} use(f);",
    );

    // ensure that block folding does not break hook ifs
    test(
        "if(x){if(true){foo();foo()}else{bar();bar()}}",
        "if(x){foo();foo()}",
    );

    test(
        "if(x){if(false){foo();foo()}else{bar();bar()}}",
        "if(x){bar();bar()}",
    );

    // Cases where the then clause has no side effects.
    test("if(x()){}", "x()");

    test("if(x()){} else {x()}", "x()||x()");
    test("if(x){}", ""); // Even the condition has no side effect.
    test(
        "if(a()){A()} else if (b()) {} else {C()}",
        "a()?A():b()||C()",
    );

    test(
        "if(a()){} else if (b()) {} else {C()}",
        "a() || (b() || C())",
    );
    test(
        "if(a()){A()} else if (b()) {} else if (c()) {} else{D()}",
        "a() ? A() : b() || (c() || D())",
    );
    test(
        "if(a()){} else if (b()) {} else if (c()) {} else{D()}",
        "a() || (b() || (c() || D()))",
    );
    test(
        "if(a()){A()} else if (b()) {} else if (c()) {} else{}",
        "a()?A():b()||c()",
    );

    // Verify that non-global scope works.
    test("function foo(){if(x()){}}", "function foo(){x()}");
}

#[test]
#[ignore] // swc optimizes out everything
fn test_fold_one_child_blocks_string_compare() {
    test(
        "if (x) {if (y) { var x; } } else{ var z; }",
        "if (x) { if (y) var x } else var z",
    );
}

#[test]
fn test_necessary_dangling_else() {
    test(
        "if (x) if (y){ y(); z() } else; else x()",
        "if (x) { if(y) { y(); z() } } else x()",
    );
}

#[test]
#[ignore] // TODO
fn test_fold_returns_integration() {
    // if-then-else duplicate statement removal handles this case:
    test("function f(){if(x)return;else return}", "function f(){}");
}

#[test]
fn test_bug1059649() {
    // ensure that folding blocks with a single var node doesn't explode
    test(
        "if(x){var y=3;}var z=5; use(y, z)",
        "if(x)var y=3; use(y, 5)",
    );

    test(
        "for(var i=0;i<10;i++){var y=3;}var z=5; use(y, z)",
        "for(var i=0;i<10;i++)var y=3; use(y, 5)",
    );
    test(
        "for(var i in x){var y=3;}var z=5; use(y, z)",
        "for(var i in x)var y=3; use(y, 5)",
    );
    test(
        "do{var y=3;}while(x);var z=5; use(y, z)",
        "do var y=3;while(x); use(y, 5)",
    );
}

#[test]
#[ignore] // TODO
fn test_hook_if_integration() {
    test(
        "if (false){ x = 1; } else if (cond) { x = 2; } else { x = 3; }",
        "x=cond?2:3",
    );

    test("x?void 0:y()", "x||y()");
    test("!x?void 0:y()", "x&&y()");
    test("x?y():void 0", "x&&y()");
}

#[test]
#[ignore] // normalize
fn test_remove_duplicate_statements_integration() {
    test(
        concat!(
            "function z() {if (a) { return true }",
            "else if (b) { return true }",
            "else { return true }}",
        ),
        "function z() {return true;}",
    );

    test(
        concat!(
            "function z() {if (a()) { return true }",
            "else if (b()) { return true }",
            "else { return true }}",
        ),
        "function z() {a()||b();return true;}",
    );
}

#[test]
#[ignore] // TODO
fn test_fold_logical_op_integration() {
    test("if(x && true) z()", "x&&z()");
    test("if(x && false) z()", "");
    test("if(x || 3) z()", "z()");
    test("if(x || false) z()", "x&&z()");
    test("if(x==y && false) z()", "");
    test("if(y() || x || 3) z()", "y();z()");
}

#[test]
#[ignore] // TODO: This is a bug, but does anyone write code like this?
fn test_fold_bitwise_op_string_compare_integration() {
    test("for (;-1 | 0;) {}", "for (;;);");
}

#[test]
fn test_var_lifting_integration() {
    test("if(true);else var a;", "");
    test("if(false) foo();else var a;", "");
    test("if(true)var a;else;", "");
    test("if(false)var a;else;", "");
    test("if(false)var a,b;", "");
    test("if(false){var a;var a;}", "");
    test("if(false)var a=function(){var b};", "");
    test("if(a)if(false)var a;else var b;", "");
}

#[test]
fn test_bug1438784() {
    test_same("for(var i=0;i<10;i++)if(x)x.y;");
}

#[test]
fn test_fold_useless_for_integration() {
    test("for(;!true;) { foo() }", "");
    test("for(;void 0;) { foo() }", "");
    test("for(;undefined;) { foo() }", "");
    test("for(;1;) foo()", "for(;;) foo()");
    test("for(;!void 0;) foo()", "for(;;) foo()");

    // Make sure proper empty nodes are inserted.
    //    test("if(foo())for(;false;){foo()}else bar()", "foo()||bar()");
    test(
        "if(foo())for(;false;){foo()}else bar()",
        "if (foo()); else bar();",
    );
}

#[test]
fn test_fold_useless_do_integration() {
    test("do { foo() } while(!true);", "foo()");
    test("do { foo() } while(void 0);", "foo()");
    test("do { foo() } while(undefined);", "foo()");
    test("do { foo() } while(!void 0);", "for(;;)foo();");

    // Make sure proper empty nodes are inserted.
    //    test(
    //        "if(foo())do {foo()} while(false) else bar()",
    //        "foo()?foo():bar()",
    //    );

    test(
        "if(foo())do {foo()} while(false) else bar()",
        "if (foo()) foo(); else bar();",
    );
}

#[test]
#[ignore] // TODO
fn test_minimize_expr() {
    test("!!true", "");

    test("!!x()", "x()");
    test("!(!x()&&!y())", "x()||y()");
    test("x()||!!y()", "x()||y()");

    /* This is similar to the !!true case */
    test("!!x()&&y()", "x()&&y()");
}

#[test]
fn test_bug_issue3() {
    test_same(concat!(
        "function foo() {",
        "  if(sections.length != 1) children[i] = 0;",
        "  else var selectedid = children[i]",
        "}",
        "foo()"
    ));
}

#[test]
fn test_bug_issue43() {
    test_same("function foo() {\n  if (a) var b = bar(); else a.b = 1; \n} use(foo);");
}

#[test]
fn test_fold_negative_bug() {
    test("for (;-3;){};", "for (;;);");
}

#[test]
#[ignore] // normalize
fn test_no_normalize_labeled_expr() {
    test_same("var x; foo:{x = 3;}");
    test_same("var x; foo:x = 3;");
}

#[test]
fn test_short_circuit1() {
    test("1 && a()", "a()");
}

#[test]
fn test_short_circuit2() {
    test("1 && a() && 2", "a()");
}

#[test]
fn test_short_circuit3() {
    test("a() && 1 && 2", "a()");
}

#[test]
#[ignore]
fn test_short_circuit4() {
    test("a() && (1 && b())", "a() && b()");
    test("a() && 1 && b()", "a() && b()");
    test("(a() && 1) && b()", "a() && b()");
}

#[test]
#[ignore] // TODO
fn test_minimize_expr_condition() {
    test("(x || true) && y()", "y()");
    test("(x || false) && y()", "x&&y()");
    test("(x && true) && y()", "x && y()");
    test("(x && false) && y()", "");
    test("a = x || false ? b : c", "a=x?b:c");
    test("do {x()} while((x && false) && y())", "x()");
}

// A few miscellaneous cases where one of the peephole passes increases the
// size, but it does it in such a way that a later pass can decrease it.
// Test to make sure the overall change is a decrease, not an increase.
#[test]
#[ignore] // TODO
fn test_misc() {
    test("use(x = [foo()] && x)", "use(x = (foo(),x))");
    test("x = foo() && false || bar()", "x = (foo(), bar())");
    test("if(foo() && false) z()", "foo()");
}

#[test]
#[ignore] // swc strips out whole of this
fn test_comma_spliting_constant_condition() {
    test("(b=0,b=1);if(b)x=b;", "b=0;b=1;x=b;");
    test("(b=0,b=1);if(b)x=b;", "b=0;b=1;x=b;");
}

#[test]
#[ignore]
fn test_avoid_comma_splitting() {
    test("x(),y(),z()", "x();y();z()");
}

#[test]
fn test_object_literal() {
    test("({})", "");
    test("({a:1})", "");
    test("({a:foo()})", "foo()");
    test("({'a':foo()})", "foo()");
}

#[test]
fn test_array_literal() {
    test("([])", "");
    test("([1])", "");
    test("([a])", "");
    test("([foo()])", "foo()");
}

#[test]
#[ignore] // TODO
fn test_fold_ifs1() {
    test(
        "function f() {if (x) return 1; else if (y) return 1;}",
        "function f() {if (x||y) return 1;}",
    );
    test(
        "function f() {if (x) return 1; else {if (y) return 1; else foo();}}",
        "function f() {if (x||y) return 1; foo();}",
    );
}

#[test]
#[ignore] // TODO
fn test_fold_ifs2() {
    test(
        "function f() {if (x) { a(); } else if (y) { a() }}",
        "function f() {x?a():y&&a();}",
    );
}

#[test]
#[ignore] // TODO
fn test_fold_hook2() {
    test(
        "function f(a) {if (!a) return a; else return a;}",
        "function f(a) {return a}",
    );
}

#[test]
#[ignore] // TODO
fn test_template_strings_known_methods() {
    test("x = `abcdef`.indexOf('b')", "x = 1");
    test("x = [`a`, `b`, `c`].join(``)", "x='abc'");
    test("x = `abcdef`.substr(0,2)", "x = 'ab'");
    test("x = `abcdef`.substring(0,2)", "x = 'ab'");
    test("x = `abcdef`.slice(0,2)", "x = 'ab'");
    test("x = `abcdef`.charAt(0)", "x = 'a'");
    test("x = `abcdef`.charCodeAt(0)", "x = 97");
    test("x = `abc`.toUpperCase()", "x = 'ABC'");
    test("x = `ABC`.toLowerCase()", "x = 'abc'");
    //test("x = `\t\n\uFEFF\t asd foo bar \r\n`.trim()", "x = 'asd foo bar'");
    test("x = parseInt(`123`)", "x = 123");
    test("x = parseFloat(`1.23`)", "x = 1.23");
}