diff --git a/packages/babel-plugin-minify-builtins/__tests__/minify-builtins.js b/packages/babel-plugin-minify-builtins/__tests__/minify-builtins.js index c66c892d2..f83413457 100644 --- a/packages/babel-plugin-minify-builtins/__tests__/minify-builtins.js +++ b/packages/babel-plugin-minify-builtins/__tests__/minify-builtins.js @@ -12,46 +12,39 @@ function transform(code) { describe("minify-builtins", () => { it("should minify standard built in methods", () => { - const source = unpad( - ` + const source = unpad(` function c() { let a = 10; const d = Number.isNaN(a); Math.max(a, b) + Math.max(b, a); return d && Number.isFinite(a); } - ` - ); + `); // Jest arranges in alphabetical order, So keeping it as _source expect({ _source: source, expected: transform(source) }).toMatchSnapshot(); }); it("should minify standard built in properties", () => { - const source = unpad( - ` + const source = unpad(` function a () { Number.NAN + Number.NAN; return Math.PI + Math.PI + Number.EPSILON + Number.NAN; } - ` - ); + `); expect({ _source: source, expected: transform(source) }).toMatchSnapshot(); }); it("should take no of occurences in to account", () => { - const source = unpad( - ` + const source = unpad(` function a() { Math.floor(a) + Math.floor(b) + Math.min(a, b); } - ` - ); + `); expect({ _source: source, expected: transform(source) }).toMatchSnapshot(); }); it("should collect and minify no matter any depth", () => { - const source = unpad( - ` + const source = unpad(` function a (){ Math.max(b, a); const b = () => { @@ -62,27 +55,23 @@ describe("minify-builtins", () => { } } } - ` - ); + `); expect({ _source: source, expected: transform(source) }).toMatchSnapshot(); }); it("shouldn't minify builtins in the program scope to avoid leaking", () => { - const source = unpad( - ` + const source = unpad(` Math.max(c, d) function a (){ Math.max(b, a) + Math.max(c, d); } Math.max(e, f) - ` - ); + `); expect({ _source: source, expected: transform(source) }).toMatchSnapshot(); }); it("should minify builtins to method scope for class declarations", () => { - const source = unpad( - ` + const source = unpad(` class Test { foo() { Math.max(c, d) @@ -98,14 +87,12 @@ describe("minify-builtins", () => { Math.min(c, d) } } - ` - ); + `); expect({ _source: source, expected: transform(source) }).toMatchSnapshot(); }); it("should minify builtins to function scopes ", () => { - const source = unpad( - ` + const source = unpad(` var a = () => { Math.floor(b); Math.floor(b); @@ -122,14 +109,12 @@ describe("minify-builtins", () => { Math.floor(d) + Math.floor(d,e); Math.max(e,d); }) - ` - ); + `); expect({ _source: source, expected: transform(source) }).toMatchSnapshot(); }); it("should collect and minify in segments in any depth if there is no LCA", () => { - const source = unpad( - ` + const source = unpad(` function b(){ Math.floor(as, bb); function d(){ @@ -163,49 +148,40 @@ describe("minify-builtins", () => { } }; new A() - ` - ); + `); expect({ _source: source, expected: transform(source) }).toMatchSnapshot(); }); it("should evalaute expressions if applicable and optimize it", () => { - const source = unpad( - ` + const source = unpad(` const a = Math.max(Math.floor(2), 5); let b = 1.8; let x = Math.floor(Math.max(a, b)); foo(x); - ` - ); + `); expect({ _source: source, expected: transform(source) }).toMatchSnapshot(); }); it("should not evaluate if its side effecty", () => { - const source = unpad( - ` + const source = unpad(` Math.max(foo(), 1); Math.random(); - ` - ); + `); expect({ _source: source, expected: transform(source) }).toMatchSnapshot(); }); it("should not minify for computed properties", () => { - const source = unpad( - ` + const source = unpad(` let max = "floor"; Math[max](1.5); - ` - ); + `); expect({ _source: source, expected: transform(source) }).toMatchSnapshot(); }); it("should not minify for arrow fn without block statment", () => { - const source = unpad( - ` + const source = unpad(` const a = () => Math.floor(b) + Math.floor(b); - ` - ); + `); expect({ _source: source, expected: transform(source) }).toMatchSnapshot(); }); }); diff --git a/packages/babel-plugin-minify-constant-folding/__tests__/constant-folding-test.js b/packages/babel-plugin-minify-constant-folding/__tests__/constant-folding-test.js index 7b0eb37ec..f1872e0dd 100644 --- a/packages/babel-plugin-minify-constant-folding/__tests__/constant-folding-test.js +++ b/packages/babel-plugin-minify-constant-folding/__tests__/constant-folding-test.js @@ -11,8 +11,7 @@ function transform(code) { describe("constant-folding-plugin", () => { it("should evaluate some expressions", () => { - const source = unpad( - ` + const source = unpad(` "a" + "b" 2 * 3; 1/3; @@ -21,11 +20,9 @@ describe("constant-folding-plugin", () => { var x = 1; foo(x); "b" + a + "c" + "d" + g + z + "f" + "h" + "z" - ` - ); + `); - const expected = unpad( - ` + const expected = unpad(` "ab"; 6; 1 / 3; @@ -34,99 +31,76 @@ describe("constant-folding-plugin", () => { var x = 1; foo(x); "b" + a + "cd" + g + z + "fhz"; - ` - ); + `); expect(transform(source)).toBe(expected); }); it("should skip -0", () => { - const source = unpad( - ` + const source = unpad(` -0; +-0; +0; - ` - ); + `); - const expected = unpad( - ` + const expected = unpad(` -0; -0; 0; - ` - ); + `); expect(transform(source)).toBe(expected); }); it("should handle runtime errors", () => { - const source = unpad( - ` + const source = unpad(` try { x({ toString: 0 } + ''); } catch (e) {} - ` - ); + `); expect(transform(source)).toBe(source); }); xit("should handle script escape", () => { - const source = unpad( - ` + const source = unpad(` " { - const source = unpad( - ` + const source = unpad(` " { - const source = unpad( - ` + const source = unpad(` " { - const source = unpad( - ` + const source = unpad(` var x = "'cool'" + "test"; - ` - ); - const expected = unpad( - ` + `); + const expected = unpad(` var x = "'cool'test"; - ` - ); + `); expect(transform(source)).toBe(expected); }); }); diff --git a/packages/babel-plugin-minify-dead-code-elimination/__tests__/dead-code-elimination-test.js b/packages/babel-plugin-minify-dead-code-elimination/__tests__/dead-code-elimination-test.js index c57d82740..f1fc9a3fe 100644 --- a/packages/babel-plugin-minify-dead-code-elimination/__tests__/dead-code-elimination-test.js +++ b/packages/babel-plugin-minify-dead-code-elimination/__tests__/dead-code-elimination-test.js @@ -41,19 +41,16 @@ describe("dce-plugin", () => { it("should handle impure right-hands", () => { const source = "function foo() { var x = f(); }"; - const expected = unpad( - ` + const expected = unpad(` function foo() { f(); } - ` - ); + `); expect(transform(source)).toBe(expected); }); it("should remove unused params", () => { - const source = unpad( - ` + const source = unpad(` _(function bar(p) { return 1; }); @@ -73,11 +70,9 @@ describe("dce-plugin", () => { } } new A(); - ` - ); + `); - const expected = unpad( - ` + const expected = unpad(` _(function () { return 1; }); @@ -96,14 +91,12 @@ describe("dce-plugin", () => { foo() {} } new A(); - ` - ); + `); expect(transform(source)).toBe(expected); }); it("should NOT remove params when keepFnArgs is true (preserve fn.length)", () => { - const source = unpad( - ` + const source = unpad(` function foo(p) { return 1; } @@ -121,8 +114,7 @@ describe("dce-plugin", () => { foo(); bar(); new A(); - ` - ); + `); const expected = source; @@ -130,8 +122,7 @@ describe("dce-plugin", () => { }); it("should handle all cases of parameter list - and remove unused ones", () => { - const source = unpad( - ` + const source = unpad(` function a(foo, bar, baz) { return foo; } @@ -150,10 +141,8 @@ describe("dce-plugin", () => { function e({foo}, bar = {}, baz) { return foo; } - ` - ); - const expected = unpad( - ` + `); + const expected = unpad(` function a(foo) { return foo; } @@ -172,55 +161,45 @@ describe("dce-plugin", () => { function e({ foo }, bar = {}) { return foo; } - ` - ); + `); expect(transform(source)).toBe(expected); }); it("should inline binding with one reference", () => { - const source = unpad( - ` + const source = unpad(` function foo() { var x = 1; console.log(x); } - ` - ); - const expected = unpad( - ` + `); + const expected = unpad(` function foo() { console.log(1); } - ` - ); + `); expect(transform(source).trim()).toBe(expected); }); // This isn't considered pure. (it should) it("should inline binding with one reference 2", () => { - const source = unpad( - ` + const source = unpad(` function foo() { var y = 1, x = { y: y }; foo.exports = x; } - ` - ); - const expected = unpad( - ` + `); + const expected = unpad(` function foo() { foo.exports = { y: 1 }; } - ` - ); + `); expect(transform(source).trim()).toBe(expected); }); it("should not inline objects literals in loops", () => { - const source = unpad( - ` + const source = unpad(` function foo() { var x = { y: 1 }; while (true) foo(x); @@ -231,10 +210,8 @@ describe("dce-plugin", () => { var bar = function () {}; while (true) foo(bar); } - ` - ); - const expected = unpad( - ` + `); + const expected = unpad(` function foo() { var x = { y: 1 }; while (true) foo(x); @@ -245,61 +222,51 @@ describe("dce-plugin", () => { var bar = function () {}; while (true) foo(bar); } - ` - ); + `); expect(transform(source).trim()).toBe(expected); }); it("should not inline object literals in exprs in loops", () => { - const source = unpad( - ` + const source = unpad(` function a(p) { var w = p || []; f(function (foo) { return w.concat(foo); }); } - ` - ); + `); - const expected = unpad( - ` + const expected = unpad(` function a(p) { var w = p || []; f(function (foo) { return w.concat(foo); }); } - ` - ); + `); expect(transform(source).trim()).toBe(expected); }); it("should inline objects in if statements", () => { - const source = unpad( - ` + const source = unpad(` function foo() { var x = { y: 1 }, y = ['foo'], z = function () {}; if (wat) foo(x, y, z); } - ` - ); - const expected = unpad( - ` + `); + const expected = unpad(` function foo() { if (wat) foo({ y: 1 }, ['foo'], function () {}); } - ` - ); + `); expect(transform(source).trim()).toBe(expected); }); it("should not inline objects in functions", () => { - const source = unpad( - ` + const source = unpad(` function foo() { var x = { y: 1 }, y = ['foo'], @@ -308,10 +275,8 @@ describe("dce-plugin", () => { foo(x, y , z); }); } - ` - ); - const expected = unpad( - ` + `); + const expected = unpad(` function foo() { var x = { y: 1 }, y = ['foo'], @@ -320,32 +285,26 @@ describe("dce-plugin", () => { foo(x, y, z); }); } - ` - ); + `); expect(transform(source).trim()).toBe(expected); }); it("should remove side effectless statements", () => { - const source = unpad( - ` + const source = unpad(` function foo() { 1; } - ` - ); - const expected = unpad( - ` + `); + const expected = unpad(` function foo() {} - ` - ); + `); expect(transform(source).trim()).toBe(expected); }); it("should work with multiple scopes", () => { - const source = unpad( - ` + const source = unpad(` function x() { var i = 1; function y() { @@ -354,10 +313,8 @@ describe("dce-plugin", () => { y(); y(); } - ` - ); - const expected = unpad( - ` + `); + const expected = unpad(` function x() { function y() { console.log(1); @@ -365,136 +322,114 @@ describe("dce-plugin", () => { y(); y(); } - ` - ); + `); expect(transform(source).trim()).toBe(expected); }); it("should inline function decl", () => { - const source = unpad( - ` + const source = unpad(` function foo() { function x() { return 1; } x(); } - ` - ); - const expected = unpad( - ` + `); + const expected = unpad(` function foo() { (function () { return 1; })(); } - ` - ); + `); expect(transform(source).trim()).toBe(expected); }); it("should inline function expressions", () => { - const source = unpad( - ` + const source = unpad(` function foo() { var x = function() { return 1; }; x(); } - ` - ); - const expected = unpad( - ` + `); + const expected = unpad(` function foo() { (function () { return 1; })(); } - ` - ); + `); expect(transform(source).trim()).toBe(expected); }); it("should not inline in a different scope", () => { - const source = unpad( - ` + const source = unpad(` function foo() { var x = function (a) { return a; }; while (1) x(1); } - ` - ); - const expected = unpad( - ` + `); + const expected = unpad(` function foo() { var x = function (a) { return a; }; while (1) x(1); } - ` - ); + `); expect(transform(source).trim()).toBe(expected); }); it("should handle recursion", () => { - const source = unpad( - ` + const source = unpad(` function baz() { var bar = function foo(config) { return foo; }; exports.foo = bar; } - ` - ); - const expected = unpad( - ` + `); + const expected = unpad(` function baz() { exports.foo = function foo() { return foo; }; } - ` - ); + `); expect(transform(source).trim()).toBe(expected); }); it("should handle recursion 2", () => { - const source = unpad( - ` + const source = unpad(` function baz() { var foo = function foo(config) { return foo; }; exports.foo = foo; } - ` - ); - const expected = unpad( - ` + `); + const expected = unpad(` function baz() { exports.foo = function foo() { return foo; }; } - ` - ); + `); expect(transform(source).trim()).toBe(expected); }); it("should handle mutual recursion", () => { - const source = unpad( - ` + const source = unpad(` function baz() { function foo() { return bar(); @@ -503,10 +438,8 @@ describe("dce-plugin", () => { return foo(); } } - ` - ); - const expected = unpad( - ` + `); + const expected = unpad(` function baz() { function foo() { return bar(); @@ -515,15 +448,13 @@ describe("dce-plugin", () => { return foo(); } } - ` - ); + `); expect(transform(source).trim()).toBe(expected); }); it("should not inline vars with multiple references", () => { - const source = unpad( - ` + const source = unpad(` function foo() { var x = function() { if (!y) { @@ -534,11 +465,9 @@ describe("dce-plugin", () => { x(); var y = null; } - ` - ); + `); - const expected = unpad( - ` + const expected = unpad(` function foo() { var x = function () { if (!y) { @@ -549,59 +478,49 @@ describe("dce-plugin", () => { x(); var y = null; } - ` - ); + `); expect(transform(source).trim()).toBe(expected); }); it("should remove redundant returns", () => { - const source = unpad( - ` + const source = unpad(` function foo() { if (a) { y(); return; } } - ` - ); - const expected = unpad( - ` + `); + const expected = unpad(` function foo() { if (a) { y(); } } - ` - ); + `); expect(transform(source).trim()).toBe(expected); }); it("should remove redundant returns part 2", () => { - const source = unpad( - ` + const source = unpad(` function foo() { y(); return; } - ` - ); - const expected = unpad( - ` + `); + const expected = unpad(` function foo() { y(); } - ` - ); + `); expect(transform(source).trim()).toBe(expected); }); it("should remove redundant returns (complex)", () => { - const source = unpad( - ` + const source = unpad(` function foo() { if (a) { y(); @@ -612,25 +531,21 @@ describe("dce-plugin", () => { } return; } - ` - ); - const expected = unpad( - ` + `); + const expected = unpad(` function foo() { if (a) { y(); if (b) {} } } - ` - ); + `); expect(transform(source).trim()).toBe(expected); }); it("should keep needed returns", () => { - const source = unpad( - ` + const source = unpad(` function foo() { if (a) { y(); @@ -638,10 +553,8 @@ describe("dce-plugin", () => { } x(); } - ` - ); - const expected = unpad( - ` + `); + const expected = unpad(` function foo() { if (a) { y(); @@ -649,36 +562,30 @@ describe("dce-plugin", () => { } x(); } - ` - ); + `); expect(transform(source).trim()).toBe(expected); }); it("should remove code unreachable after return", () => { - const source = unpad( - ` + const source = unpad(` function foo() { z(); return; x(); } - ` - ); - const expected = unpad( - ` + `); + const expected = unpad(` function foo() { z(); } - ` - ); + `); expect(transform(source).trim()).toBe(expected); }); it("should be fine with fun decl after return", () => { - const source = unpad( - ` + const source = unpad(` function foo() { z(); z(); @@ -687,10 +594,8 @@ describe("dce-plugin", () => { wow(); } } - ` - ); - const expected = unpad( - ` + `); + const expected = unpad(` function foo() { z(); z(); @@ -699,57 +604,47 @@ describe("dce-plugin", () => { wow(); } } - ` - ); + `); expect(transform(source).trim()).toBe(expected); }); it("should handle returns that were orphaned", () => { - const source = unpad( - ` + const source = unpad(` var a = true; function foo() { if (a) return; x(); } - ` - ); - const expected = unpad( - ` + `); + const expected = unpad(` var a = true; function foo() {} - ` - ); + `); expect(transform(source).trim()).toBe(expected); }); it("should handle returns that were orphaned 2", () => { - const source = unpad( - ` + const source = unpad(` var a = true; function foo() { if (a) return 1; x(); } - ` - ); - const expected = unpad( - ` + `); + const expected = unpad(` var a = true; function foo() { return 1; } - ` - ); + `); expect(transform(source).trim()).toBe(expected); }); it("should handle orphaned + redundant returns", () => { - const source = unpad( - ` + const source = unpad(` var x = true; function foo() { if (b) { @@ -760,25 +655,21 @@ describe("dce-plugin", () => { y(); } } - ` - ); - const expected = unpad( - ` + `); + const expected = unpad(` var x = true; function foo() { if (b) { z(); } } - ` - ); + `); expect(transform(source).trim()).toBe(expected); }); it("should remove functions only called in themselves", () => { - const source = unpad( - ` + const source = unpad(` function foo() { function baz() { function bar() { @@ -788,16 +679,14 @@ describe("dce-plugin", () => { bar(); } } - ` - ); + `); const expected = "function foo() {}"; expect(transform(source).trim()).toBe(expected); }); it("should remove functions only called in themselves 2", () => { - const source = unpad( - ` + const source = unpad(` function foo() { var baz = function () { function bar() { @@ -807,16 +696,14 @@ describe("dce-plugin", () => { bar(); }; } - ` - ); + `); const expected = "function foo() {}"; expect(transform(source).trim()).toBe(expected); }); it("should remove functions only called in themselves 3", () => { - const source = unpad( - ` + const source = unpad(` function foo() { function boo() {} function baz() { @@ -828,16 +715,14 @@ describe("dce-plugin", () => { boo(); } } - ` - ); + `); const expected = "function foo() {}"; expect(transform(source).trim()).toBe(expected); }); it("should remove functions only called in themselves 3", () => { - const source = unpad( - ` + const source = unpad(` (function () { function foo () { console.log( 'this function was included!' ); @@ -854,25 +739,21 @@ describe("dce-plugin", () => { foo(); })(); - ` - ); - const expected = unpad( - ` + `); + const expected = unpad(` (function () { (function () { console.log('this function was included!'); })(); })(); - ` - ); + `); expect(transform(source).trim()).toBe(expected); }); it("should remove dead if statements", () => { - const source = unpad( - ` + const source = unpad(` if (1) { foo(); } @@ -881,22 +762,18 @@ describe("dce-plugin", () => { } else { bar(); } - ` - ); - const expected = unpad( - ` + `); + const expected = unpad(` foo(); bar(); - ` - ); + `); expect(transform(source).trim()).toBe(expected); }); it("should remove empty if statements block", () => { - const source = unpad( - ` + const source = unpad(` if (a) { } else { foo(); @@ -906,18 +783,15 @@ describe("dce-plugin", () => { } else { } - ` - ); - const expected = unpad( - ` + `); + const expected = unpad(` if (!a) { foo(); } if (a) { foo(); } - ` - ); + `); expect(transform(source).trim()).toBe(expected); }); @@ -958,61 +832,48 @@ describe("dce-plugin", () => { }); it("should not remove needed expressions", () => { - const source = unpad( - ` + const source = unpad(` var n = 1; if (foo) n; console.log(n); - ` - ); - const expected = unpad( - ` + `); + const expected = unpad(` var n = 1; if (foo) ; console.log(n); - ` - ); + `); expect(transform(source).trim()).toBe(expected); }); it("should not remove needed expressions", () => { - const source = unpad( - ` + const source = unpad(` function foo(a) { var a = a ? a : a; } - ` - ); - const expected = unpad( - ` + `); + const expected = unpad(` function foo(a) { var a = a ? a : a; } - ` - ); + `); expect(transform(source).trim()).toBe(expected); }); it("should join the assignment and def", () => { - const source = unpad( - ` + const source = unpad(` var x; x = 1; - ` - ); + `); - const expected = unpad( - ` + const expected = unpad(` var x = 1; - ` - ); + `); expect(transform(source)).toBe(expected); }); it("should not replace the wrong things", () => { - const source = unpad( - ` + const source = unpad(` function foo() { var n = 1; wow(n); @@ -1022,11 +883,9 @@ describe("dce-plugin", () => { } return wat; } - ` - ); + `); - const expected = unpad( - ` + const expected = unpad(` function foo() { wow(1); @@ -1034,14 +893,12 @@ describe("dce-plugin", () => { wow(2); }; } - ` - ); + `); expect(transform(source)).toBe(expected); }); it("should handle case blocks ", () => { - const source = unpad( - ` + const source = unpad(` function a() { switch (foo) { case 6: @@ -1049,11 +906,9 @@ describe("dce-plugin", () => { break; } } - ` - ); + `); - const expected = unpad( - ` + const expected = unpad(` function a() { switch (foo) { case 6: @@ -1061,15 +916,13 @@ describe("dce-plugin", () => { break; } } - ` - ); + `); expect(transform(source)).toBe(expected); }); // TODO: Handle this (blocks that have no semantic meaning). xit("should understand extraneous blocks", () => { - const source = unpad( - ` + const source = unpad(` function a() { var f = 25; function b() { @@ -1090,11 +943,9 @@ describe("dce-plugin", () => { b(); b(); } - ` - ); + `); - const expected = unpad( - ` + const expected = unpad(` function a() { function b() { {} @@ -1112,15 +963,13 @@ describe("dce-plugin", () => { b(); b(); } - ` - ); + `); expect(transform(source)).toBe(expected); }); it("should understand closures", () => { - const source = unpad( - ` + const source = unpad(` function a() { var f = 25; function b() { @@ -1139,11 +988,9 @@ describe("dce-plugin", () => { b(); b(); } - ` - ); + `); - const expected = unpad( - ` + const expected = unpad(` function a() { function b() { function c() { @@ -1160,292 +1007,242 @@ describe("dce-plugin", () => { b(); b(); } - ` - ); + `); expect(transform(source)).toBe(expected); }); it("should handle vars in if statements", () => { - const source = unpad( - ` + const source = unpad(` function a() { if (x()) { var foo = 1; } bar(foo); } - ` - ); + `); - const expected = unpad( - ` + const expected = unpad(` function a() { if (x()) { var foo = 1; } bar(foo); } - ` - ); + `); expect(transform(source)).toBe(expected); }); it("should handle vars in if statements 2", () => { - const source = unpad( - ` + const source = unpad(` function a() { if (x()) var foo = 1; bar(foo); } - ` - ); + `); - const expected = unpad( - ` + const expected = unpad(` function a() { if (x()) var foo = 1; bar(foo); } - ` - ); + `); expect(transform(source)).toBe(expected); }); it("should handle vars in for statements", () => { - const source = unpad( - ` + const source = unpad(` function a() { for (;;) var foo = 1; bar(foo); } - ` - ); + `); - const expected = unpad( - ` + const expected = unpad(` function a() { for (;;) var foo = 1; bar(foo); } - ` - ); + `); expect(transform(source)).toBe(expected); }); it("should handle for statements 2", () => { - const source = unpad( - ` + const source = unpad(` function a() { for (;;) { var foo = 1; bar(foo); } } - ` - ); + `); - const expected = unpad( - ` + const expected = unpad(` function a() { for (;;) { bar(1); } } - ` - ); + `); expect(transform(source)).toBe(expected); }); it("should remove binding and assignment", () => { - const source = unpad( - ` + const source = unpad(` function a() { var a, b, c; a = 1; b = 2; } - ` - ); + `); - const expected = unpad( - ` + const expected = unpad(` function a() {} - ` - ); + `); expect(transform(source)).toBe(expected); }); it("should nore remove binding and assignment if the value is used", () => { - const source = unpad( - ` + const source = unpad(` function a() { var x = 1; while (a) wow = x += 1; } - ` - ); + `); - const expected = unpad( - ` + const expected = unpad(` function a() { var x = 1; while (a) wow = x += 1; } - ` - ); + `); expect(transform(source)).toBe(expected); }); it("should keep side-effectful assignment values", () => { - const source = unpad( - ` + const source = unpad(` function a() { var x; x = wow(); } - ` - ); + `); - const expected = unpad( - ` + const expected = unpad(` function a() { wow(); } - ` - ); + `); expect(transform(source)).toBe(expected); }); it("should not evaluate this binary expression to truthy", () => { - const source = unpad( - ` + const source = unpad(` function boo() { var bar = foo || []; if (!bar || baz.length === 0) { return 'wow'; } } - ` - ); + `); - const expected = unpad( - ` + const expected = unpad(` function boo() { var bar = foo || []; if (!bar || baz.length === 0) { return 'wow'; } } - ` - ); + `); expect(transform(source)).toBe(expected); }); it("eval the following to false", () => { - const source = unpad( - ` + const source = unpad(` function bar () { var x = foo || 'boo'; bar = x === 'wow' ? ' ' + z : ''; } - ` - ); + `); - const expected = unpad( - ` + const expected = unpad(` function bar() { var x = foo || 'boo'; bar = x === 'wow' ? ' ' + z : ''; } - ` - ); + `); expect(transform(source)).toBe(expected); }); it("should get rid of the constant violations", () => { - const source = unpad( - ` + const source = unpad(` function bar () { var x = foo(); x = bar(); } - ` - ); + `); - const expected = unpad( - ` + const expected = unpad(` function bar() { foo(); bar(); } - ` - ); + `); expect(transform(source)).toBe(expected); }); it("should remove names from NFE", () => { - const source = unpad( - ` + const source = unpad(` function bar() { return function wow() { return boo(); }; } - ` - ); + `); - const expected = unpad( - ` + const expected = unpad(` function bar() { return function () { return boo(); }; } - ` - ); + `); expect(transform(source)).toBe(expected); }); it("should not remove names from NFE when referenced", () => { - const source = unpad( - ` + const source = unpad(` function bar() { return function wow() { return wow(); }; } - ` - ); + `); - const expected = unpad( - ` + const expected = unpad(` function bar() { return function wow() { return wow(); }; } - ` - ); + `); expect(transform(source)).toBe(expected); }); it("should remove name from NFE when shadowed", () => { - const source = unpad( - ` + const source = unpad(` function bar() { return function wow() { var wow = foo; @@ -1453,11 +1250,9 @@ describe("dce-plugin", () => { return wow; }; } - ` - ); + `); - const expected = unpad( - ` + const expected = unpad(` function bar() { return function () { var wow = foo; @@ -1465,58 +1260,48 @@ describe("dce-plugin", () => { return wow; }; } - ` - ); + `); expect(transform(source)).toBe(expected); }); // issue#81 it("should NOT remove name from NFE when referenced - issue#81", () => { - const source = unpad( - ` + const source = unpad(` (function (require, module, exports) { var Hub = function Hub(file, options) { (0, _classCallCheck3.default)(this, Hub); }; module.exports = Hub; })(require, module, exports); - ` - ); - const expected = unpad( - ` + `); + const expected = unpad(` (function (require, module) { module.exports = function Hub() { (0, _classCallCheck3.default)(this, Hub); }; })(require, module, exports); - ` - ); + `); expect(transform(source)).toBe(expected); }); it("should remove name from NFE when replaced - issue#81", () => { - const source = unpad( - ` + const source = unpad(` (function () { var x = function foo() {}; module.exports = x; })(); - ` - ); - const expected = unpad( - ` + `); + const expected = unpad(` (function () { module.exports = function () {}; })(); - ` - ); + `); expect(transform(source)).toBe(expected); }); it("should preserve fn names when keepFnName is true", () => { - const source = unpad( - ` + const source = unpad(` (function () { function A() {} exports.A = A; @@ -1524,24 +1309,20 @@ describe("dce-plugin", () => { exports.B = B; onClick(function C() {}); })(); - ` - ); - const expected = unpad( - ` + `); + const expected = unpad(` (function () { exports.A = function A() {}; exports.B = function B() {}; onClick(function C() {}); })(); - ` - ); + `); expect(transform(source, { keepFnName: true })).toBe(expected); }); it("should preserve class names when keepClassName is true", () => { - const source = unpad( - ` + const source = unpad(` (function () { class A {} exports.A = A; @@ -1549,54 +1330,44 @@ describe("dce-plugin", () => { exports.B = B; class AA {} new AA() })(); - ` - ); - const expected = unpad( - ` + `); + const expected = unpad(` (function () { exports.A = class A {}; exports.B = class B {}; new class AA {}(); })(); - ` - ); + `); expect(transform(source, { keepClassName: true })).toBe(expected); }); // NCE = Named Class Expressions it("should remove name from NCE", () => { - const source = unpad( - ` + const source = unpad(` var Foo = class Bar {}; - ` - ); - const expected = unpad( - ` + `); + const expected = unpad(` var Foo = class {}; - ` - ); + `); expect(transform(source)).toBe(expected); }); it("should not remove referenced name from NCE", () => { - const source = unpad( - ` + const source = unpad(` var Foo = class Bar { constructor() { console.log(Bar); } }; - ` - ); + `); const expected = source; expect(transform(source)).toBe(expected); }); // issue#78 it("should not replace NCE with void 0 - issue#78", () => { - const source = unpad( - ` + const source = unpad(` (function() { var B = class A { constructor(x) { @@ -1607,10 +1378,8 @@ describe("dce-plugin", () => { new B(event); }); })(); - ` - ); - const expected = unpad( - ` + `); + const expected = unpad(` (function () { var B = class { constructor(x) { @@ -1621,14 +1390,12 @@ describe("dce-plugin", () => { new B(event); }); })(); - ` - ); + `); expect(transform(source)).toBe(expected); }); it("should handle var decl with same name as class expr name", () => { - const source = unpad( - ` + const source = unpad(` (function () { var A = class A { constructor() { @@ -1639,10 +1406,8 @@ describe("dce-plugin", () => { exports.A = A; exports.B = B; })(); - ` - ); - const expected = unpad( - ` + `); + const expected = unpad(` (function () { exports.A = class A { constructor() { @@ -1651,36 +1416,30 @@ describe("dce-plugin", () => { }; exports.B = class {}; })(); - ` - ); + `); expect(transform(source)).toBe(expected); }); it("should track purity", () => { - const source = unpad( - ` + const source = unpad(` function x(a) { var l = a; var x = l foo(x); } - ` - ); + `); - const expected = unpad( - ` + const expected = unpad(` function x(a) { foo(a); } - ` - ); + `); expect(transform(source)).toBe(expected); }); it("should latch on to exisiting vars", () => { - const source = unpad( - ` + const source = unpad(` function x(a) { if (a) { var x = a.wat; @@ -1689,11 +1448,9 @@ describe("dce-plugin", () => { var z = a.foo, b = b.bar; return z + b; } - ` - ); + `); - const expected = unpad( - ` + const expected = unpad(` function x(a) { if (a) { x = a.wat; @@ -1705,62 +1462,52 @@ describe("dce-plugin", () => { x; return z + b; } - ` - ); + `); expect(transform(source, { optimizeRawSize: true })).toBe(expected); }); it("should put the var in the for in", () => { - const source = unpad( - ` + const source = unpad(` function x(a) { var x; wow(); for (x in a) wow(); } - ` - ); + `); - const expected = unpad( - ` + const expected = unpad(` function x(a) { wow(); for (var x in a) wow(); } - ` - ); + `); expect(transform(source)).toBe(expected); }); it("should put the var in the for in only when the var is alone", () => { - const source = unpad( - ` + const source = unpad(` function x(a) { var x, y; wow(y); for (x in a) wow(y); } - ` - ); + `); - const expected = unpad( - ` + const expected = unpad(` function x(a) { var x, y; wow(y); for (x in a) wow(y); } - ` - ); + `); expect(transform(source)).toBe(expected); }); it("inlining should check name collision", () => { - const source = unpad( - ` + const source = unpad(` function foo() { var a = 1; var b = a; @@ -1771,11 +1518,9 @@ describe("dce-plugin", () => { x(); return a; } - ` - ); + `); - const expected = unpad( - ` + const expected = unpad(` function foo() { var a = 1; var b = a; @@ -1786,15 +1531,13 @@ describe("dce-plugin", () => { x(); return a; } - ` - ); + `); expect(transform(source)).toBe(expected); }); it("inlining should check name collision for expressions", () => { - const source = unpad( - ` + const source = unpad(` function foo() { var a = c + d; function x(c, d) { @@ -1803,11 +1546,9 @@ describe("dce-plugin", () => { x(); x(); } - ` - ); + `); - const expected = unpad( - ` + const expected = unpad(` function foo() { var a = c + d; function x(c, d) { @@ -1816,109 +1557,87 @@ describe("dce-plugin", () => { x(); x(); } - ` - ); + `); expect(transform(source)).toBe(expected); }); it("should replace with empty statement if in body position 1", () => { - const source = unpad( - ` + const source = unpad(` function foo() { var a = 0; while (wat()) a += 1; } - ` - ); + `); - const expected = unpad( - ` + const expected = unpad(` function foo() { while (wat()); } - ` - ); + `); expect(transform(source)).toBe(expected); }); it("should replace with empty statement if in body position 2", () => { - const source = unpad( - ` + const source = unpad(` function foo() { while (wat()) 1; } - ` - ); + `); - const expected = unpad( - ` + const expected = unpad(` function foo() { while (wat()); } - ` - ); + `); expect(transform(source)).toBe(expected); }); it("should replace with empty statement if in body position 3", () => { - const source = unpad( - ` + const source = unpad(` function foo() { while (wat()) var x; } - ` - ); + `); - const expected = unpad( - ` + const expected = unpad(` function foo() { while (wat()); } - ` - ); + `); expect(transform(source)).toBe(expected); }); it("it should update binding path", () => { - const source = unpad( - ` + const source = unpad(` function foo() { var key; for (key in o); for (key in o2); } - ` - ); + `); - const expected = unpad( - ` + const expected = unpad(` function foo() { for (var key in o); for (key in o2); } - ` - ); + `); expect(transform(source)).toBe(expected); }); xit("it should evaluate and remove falsy code", () => { - const source = unpad( - ` + const source = unpad(` foo(0 && bar()); - ` - ); - const expected = unpad( - ` + `); + const expected = unpad(` foo(0); - ` - ); + `); expect(transform(source)).toBe(expected); }); it("should not move functions into other scopes", () => { - const source = unpad( - ` + const source = unpad(` function foo() { var a = 1; var bar = { x: {z: a, v: a} }; @@ -1933,11 +1652,9 @@ describe("dce-plugin", () => { return moo; } - ` - ); + `); - const expected = unpad( - ` + const expected = unpad(` function foo() { var a = 1; var bar = { x: { z: a, v: a } }; @@ -1950,74 +1667,60 @@ describe("dce-plugin", () => { maa(wow, bar, baz, boo, a, a); }; } - ` - ); + `); expect(transform(source)).toBe(expected); }); it("should preserve vars from the removed block", () => { - const source = unpad( - ` + const source = unpad(` if (0) {var a = foo()} if (0) var b = foo(); if (1) { } else { var c = foo() } if (0) var d = bar(); else { } - ` - ); - const expected = unpad( - ` + `); + const expected = unpad(` var a; var b; var c; var d; - ` - ); + `); expect(transform(source)).toBe(expected); }); it("should optimize alternate when empty consequent is replaced with alternate", () => { - const source = unpad( - ` + const source = unpad(` if (baz) { } else { let foo = 'bar'; function foobar() {} console.log('foo' + foo); } - ` - ); - const expected = unpad( - ` + `); + const expected = unpad(` if (!baz) { console.log('foo' + 'bar'); } - ` - ); + `); expect(transform(source)).toBe(expected); }); it("should transform simple switch statement", () => { - const source = unpad( - ` + const source = unpad(` switch (0) { case 0: foo(); break; case 1: bar(); break; } - ` - ); - const expected = unpad( - ` + `); + const expected = unpad(` foo(); - ` - ); + `); expect(transform(source)).toBe(expected); }); it("should NOT optimize when one of the cases is not evaluate-able", () => { - const source = unpad( - ` + const source = unpad(` switch (a) { case 1: break; @@ -2029,36 +1732,30 @@ describe("dce-plugin", () => { foo(); break; } - ` - ); + `); const expected = source; expect(transform(source)).toBe(expected); }); it("should handle cases where there is no break", () => { - const source = unpad( - ` + const source = unpad(` switch (1) { case 1: foo(); case 2: bar(); case 3: baz(); break; case 4: foobarbaz(); } - ` - ); - const expected = unpad( - ` + `); + const expected = unpad(` foo(); bar(); baz(); - ` - ); + `); expect(transform(source)).toBe(expected); }); it("should handle defaults", () => { - const source = unpad( - ` + const source = unpad(` switch (10) { default: foo(); @@ -2079,21 +1776,17 @@ describe("dce-plugin", () => { default: foo(); } - ` - ); - const expected = unpad( - ` + `); + const expected = unpad(` foo(); foo(); - ` - ); + `); expect(transform(source)).toBe(expected); }); it("should predict break statement within blocks", () => { - const source = unpad( - ` + const source = unpad(` switch (1) { case 1: foo(); @@ -2109,11 +1802,9 @@ describe("dce-plugin", () => { case 3: foo(); } - ` - ); + `); - const expected = unpad( - ` + const expected = unpad(` foo(); var i; @@ -2126,15 +1817,13 @@ describe("dce-plugin", () => { } foo(); - ` - ); + `); expect(transform(source)).toBe(expected); }); it("should bail out when break label is above switch's scope", () => { - const source = unpad( - ` + const source = unpad(` x: switch (1) { case 1: break x; @@ -2151,36 +1840,30 @@ describe("dce-plugin", () => { break z; } } - ` - ); + `); const expected = source; expect(transform(source)).toBe(expected); }); it("should NOT bail out when break label is under switch's scope", () => { - const source = unpad( - ` + const source = unpad(` switch (1) { case 1: x: while (true) { break x; } } - ` - ); - const expected = unpad( - ` + `); + const expected = unpad(` x: while (true) { break x; } - ` - ); + `); expect(transform(source)).toBe(expected); }); it("should handle nested switch statements", () => { - const source = unpad( - ` + const source = unpad(` switch (1) { case 1: foo(); @@ -2193,21 +1876,17 @@ describe("dce-plugin", () => { case 2: baz(); } - ` - ); - const expected = unpad( - ` + `); + const expected = unpad(` foo(); bar(); - ` - ); + `); expect(transform(source)).toBe(expected); }); it("should break correctly when there is a break statement after break", () => { - const source = unpad( - ` + const source = unpad(` switch (0) { case 0: foo(); @@ -2215,19 +1894,15 @@ describe("dce-plugin", () => { bar(); break; } - ` - ); - const expected = unpad( - ` + `); + const expected = unpad(` foo(); - ` - ); + `); expect(transform(source)).toBe(expected); }); it("should break correctly for the correct break statement", () => { - const source = unpad( - ` + const source = unpad(` switch (0) { case 0: foo(); @@ -2239,19 +1914,15 @@ describe("dce-plugin", () => { case 1: baz(); } - ` - ); - const expected = unpad( - ` + `); + const expected = unpad(` foo(); - ` - ); + `); expect(transform(source)).toBe(expected); }); it("should bail out for runtime evaluated if(x) break", () => { - const source = unpad( - ` + const source = unpad(` switch (0) { case 0: foo(); @@ -2259,15 +1930,13 @@ describe("dce-plugin", () => { case 1: bar(); } - ` - ); + `); const expected = source; expect(transform(source)).toBe(expected); }); it("should NOT bail out for runtime evaluated if(x) break inside loop", () => { - const source = unpad( - ` + const source = unpad(` switch (0) { case 0: foo(); @@ -2275,24 +1944,20 @@ describe("dce-plugin", () => { case 1: bar(); } - ` - ); - const expected = unpad( - ` + `); + const expected = unpad(` foo(); while (1) { if (x) break; } bar(); - ` - ); + `); expect(transform(source)).toBe(expected); }); it("should optimize While statements", () => { - const source = unpad( - ` + const source = unpad(` while (false) { foo(); } @@ -2302,24 +1967,20 @@ describe("dce-plugin", () => { while (x) { baz(); } - ` - ); - const expected = unpad( - ` + `); + const expected = unpad(` while (true) { bar(); } while (x) { baz(); } - ` - ); + `); expect(transform(source)).toBe(expected); }); it("should optimize For statements", () => { - const source = unpad( - ` + const source = unpad(` for (var i = 0; i < 8; i++) { foo(); } @@ -2329,24 +1990,20 @@ describe("dce-plugin", () => { for (; false;) { baz(); } - ` - ); - const expected = unpad( - ` + `); + const expected = unpad(` for (var i = 0; i < 8; i++) { foo(); } for (;;) { bar(); } - ` - ); + `); expect(transform(source)).toBe(expected); }); it("should optimize dowhile statements", () => { - const source = unpad( - ` + const source = unpad(` do { foo(); } while (1); @@ -2356,10 +2013,8 @@ describe("dce-plugin", () => { do { baz(); } while (a); - ` - ); - const expected = unpad( - ` + `); + const expected = unpad(` do { foo(); } while (1); @@ -2369,32 +2024,26 @@ describe("dce-plugin", () => { do { baz(); } while (a); - ` - ); + `); expect(transform(source)).toBe(expected); }); it("should not evaluate to false and remove conditional", () => { - const source = unpad( - ` + const source = unpad(` function foo(obj) { return obj && typeof obj === 'object' ? x() : obj; } - ` - ); - const expected = unpad( - ` + `); + const expected = unpad(` function foo(obj) { return obj && typeof obj === 'object' ? x() : obj; } - ` - ); + `); expect(transform(source)).toBe(expected); }); it("should extract vars from the removed switch statement", () => { - const source = unpad( - ` + const source = unpad(` switch (0) { case 1: var a = 5; @@ -2405,23 +2054,19 @@ describe("dce-plugin", () => { var a = 1; var b = 2; } - ` - ); - const expected = unpad( - ` + `); + const expected = unpad(` var a, b; var a, b; var a = 1; var b = 2; - ` - ); + `); expect(transform(source)).toBe(expected); }); it("should handle side-effecty things in cases", () => { - const source = unpad( - ` + const source = unpad(` let i = 0; let bar = () => console.log('foo'); switch (1) { @@ -2431,89 +2076,75 @@ describe("dce-plugin", () => { case bar(): baz(); } - ` - ); + `); const expected = source; expect(transform(source)).toBe(expected); }); it("should preserve names in NFEs", () => { - const source = unpad( - ` + const source = unpad(` function method() { var removeListeners = function removeListeners() { log(removeListeners); }; removeListeners(); } - ` - ); + `); - const expected = unpad( - ` + const expected = unpad(` function method() { (function removeListeners() { log(removeListeners); })(); } - ` - ); + `); expect(transform(source)).toBe(expected); }); // https://github.com/babel/babili/issues/130 it("should not convert expression to expression during replace issue#130", () => { - const source = unpad( - ` + const source = unpad(` function outer() { const inner = (d) => d.x; return inner; } - ` - ); - const expected = unpad( - ` + `); + const expected = unpad(` function outer() { return d => d.x; } - ` - ); + `); expect(transform(source)).toBe(expected); }); // https://github.com/babel/babili/issues/151 it("should fix issue#151 - array patterns and object patterns", () => { - const source = unpad( - ` + const source = unpad(` const me = lyfe => { const [swag] = lyfe; return swag; }; - ` - ); + `); const expected = source; expect(transform(source)).toBe(expected); }); // https://github.com/babel/babili/issues/151 it("should fix issue#151 - array patterns and object patterns 2", () => { - const source = unpad( - ` + const source = unpad(` const me = lyfe => { const [swag, yolo] = lyfe; return swag && yolo; }; - ` - ); + `); const expected = source; expect(transform(source)).toBe(expected); }); // https://github.com/babel/babili/issues/232 it("should fix issue#232 - array patterns and object patterns with non constant init", () => { - const source = unpad( - ` + const source = unpad(` const a = { lol: input => { const [hello, world] = input.split('|'); @@ -2523,36 +2154,31 @@ describe("dce-plugin", () => { return 'bottom'; } }; - ` - ); + `); const expected = source; expect(transform(source)).toBe(expected); }); // https://github.com/babel/babili/issues/232 it("should fix issue#232 - array & object patterns with non-constant init", () => { - const source = unpad( - ` + const source = unpad(` function foo() { const { bar1, bar2 } = baz(); return bar1; } - ` - ); + `); const expected = source; expect(transform(source)).toBe(expected); }); it("should preserve variabledeclarations(var) after completion statements", () => { - const source = unpad( - ` + const source = unpad(` function foo() { a = 1; return a; var a; } - ` - ); + `); const expected = source; @@ -2560,51 +2186,43 @@ describe("dce-plugin", () => { }); it("should NOT preserve variabledeclarations(let) after completion statements", () => { - const source = unpad( - ` + const source = unpad(` function foo() { a = 1; b = 2; return a + b; let a, b; } - ` - ); + `); - const expected = unpad( - ` + const expected = unpad(` function foo() { a = 1; b = 2; return a + b; } - ` - ); + `); expect(transform(source)).toBe(expected); }); it("should not remove var from for..in/for..of statements", () => { - const source = unpad( - ` + const source = unpad(` function foo() { for (var i in x) console.log("foo"); for (var j of y) console.log("foo"); } - ` - ); + `); const expected = source; expect(transform(source)).toBe(expected); }); it("should not remove var from for..await statements", () => { - const source = unpad( - ` + const source = unpad(` async function foo() { for await (var x of y) console.log("bar"); } - ` - ); + `); const expected = source; expect( transform( @@ -2619,8 +2237,7 @@ describe("dce-plugin", () => { ).toBe(expected); }); it("should remove empty statements when children of block", () => { - const source = unpad( - ` + const source = unpad(` (function () { function foo() {}; function bar() {}; @@ -2629,19 +2246,15 @@ describe("dce-plugin", () => { function quux() {}; function cake() {}; })(); - ` - ); - const expected = unpad( - ` + `); + const expected = unpad(` (function () {})(); - ` - ); + `); expect(transform(source)).toBe(expected); }); it("should NOT remove fn params for setters", () => { - const source = unpad( - ` + const source = unpad(` function foo() { var x = { set a(b) {} @@ -2653,15 +2266,13 @@ describe("dce-plugin", () => { } return new A(); } - ` - ); + `); expect(transform(source)).toBe(source); }); // https://github.com/babel/babili/issues/265 it("should not remove return void 0; statement if inside a loop", () => { - const source = unpad( - ` + const source = unpad(` function getParentConditionalPath(path) { let parentPath; while (parentPath = path.parentPath) { @@ -2676,16 +2287,14 @@ describe("dce-plugin", () => { } } } - ` - ); + `); expect(transform(source)).toBe(source); }); // https://github.com/babel/babili/issues/265 it("should integrate with simplify plugin changing scopes", () => { - const source = unpad( - ` + const source = unpad(` function getParentConditionalPath(path) { let parentPath; while (parentPath = path.parentPath) { @@ -2700,24 +2309,20 @@ describe("dce-plugin", () => { } } } - ` - ); - const expected = unpad( - ` + `); + const expected = unpad(` function getParentConditionalPath(path) { for (let parentPath; parentPath = path.parentPath;) { if (parentPath.isIfStatement() || parentPath.isConditionalExpression()) return path.key === "test" ? void 0 : parentPath; path = parentPath; } } - ` - ); + `); expect(transformWithSimplify(source)).toBe(expected); }); it("should not remove params from functions containing direct eval", () => { - const source = unpad( - ` + const source = unpad(` function a(b, c, d) { eval(";"); return b; @@ -2726,11 +2331,9 @@ describe("dce-plugin", () => { (1, eval)(";"); return c; } - ` - ); + `); - const expected = unpad( - ` + const expected = unpad(` function a(b, c, d) { eval(";"); return b; @@ -2739,15 +2342,13 @@ describe("dce-plugin", () => { (1, eval)(";"); return c; } - ` - ); + `); expect(transform(source)).toBe(expected); }); it("should not remove params/vars from functions containing direct eval", () => { - const source = unpad( - ` + const source = unpad(` function foo(bar, baz) { function foox(a, b, c) { x.then((data, unused) => { @@ -2767,11 +2368,9 @@ describe("dce-plugin", () => { console.log("fooy"); } } - ` - ); + `); - const expected = unpad( - ` + const expected = unpad(` function foo(bar, baz) { function foox(a, b, c) { x.then((data, unused) => { @@ -2791,15 +2390,13 @@ describe("dce-plugin", () => { console.log("fooy"); } } - ` - ); + `); expect(transform(source)).toBe(expected); }); it("should not optimize/remove vars from functions containing direct eval", () => { - const source = unpad( - ` + const source = unpad(` function foo() { bar(); @@ -2816,11 +2413,9 @@ describe("dce-plugin", () => { return x; } } - ` - ); + `); - const expected = unpad( - ` + const expected = unpad(` function foo() { bar(); @@ -2836,33 +2431,27 @@ describe("dce-plugin", () => { return 10; } } - ` - ); + `); expect(transform(source)).toBe(expected); }); it("should impure expressions in confidently evaluated if statements", () => { - const source = unpad( - ` + const source = unpad(` if (a.b(), true) { foo(); } - ` - ); - const expected = unpad( - ` + `); + const expected = unpad(` a.b(); foo(); - ` - ); + `); expect(transform(source)).toBe(expected); }); it("should extract all necessary things from if statements", () => { - const source = unpad( - ` + const source = unpad(` if (a.b(), false) { var foo = foo1; foo(); @@ -2873,10 +2462,8 @@ describe("dce-plugin", () => { var baz = baz1; baz(); } - ` - ); - const expected = unpad( - ` + `); + const expected = unpad(` a.b(); b.c(); @@ -2884,35 +2471,29 @@ describe("dce-plugin", () => { bar(); var baz; var foo; - ` - ); + `); expect(transform(source)).toBe(expected); }); it("should not remove vars after return statement", () => { - const source = unpad( - ` + const source = unpad(` function f() { return x; var x = 1; } - ` - ); + `); - const expected = unpad( - ` + const expected = unpad(` function f() { return void 0; } - ` - ); + `); expect(transform(source)).toBe(expected); }); it("should not remove vars after return statement #2", () => { - const source = unpad( - ` + const source = unpad(` var x = 0; function f1(){ function f2(){ @@ -2921,11 +2502,9 @@ describe("dce-plugin", () => { return f2(); var x = 1; } - ` - ); + `); - const expected = unpad( - ` + const expected = unpad(` var x = 0; function f1() { return function () { @@ -2933,36 +2512,30 @@ describe("dce-plugin", () => { }(); var x = 1; } - ` - ); + `); expect(transform(source)).toBe(expected); }); it("should not remove vars after return statement #3", () => { - const source = unpad( - ` + const source = unpad(` function foo() { bar = x; var x = 1; } - ` - ); + `); - const expected = unpad( - ` + const expected = unpad(` function foo() { bar = void 0; } - ` - ); + `); expect(transform(source)).toBe(expected); }); it("should deopt for impure tests", () => { - const source = unpad( - ` + const source = unpad(` function foo() { do { bar(); @@ -2974,14 +2547,12 @@ describe("dce-plugin", () => { bar(); } } - ` - ); + `); expect(transform(source)).toBe(source); }); it("should handle confident do..while with break statements", () => { - const source = unpad( - ` + const source = unpad(` function foo() { do { if (x) break; @@ -3013,10 +2584,8 @@ describe("dce-plugin", () => { } } while (false); } - ` - ); - const expected = unpad( - ` + `); + const expected = unpad(` function foo() { do { if (x) break; @@ -3044,14 +2613,12 @@ describe("dce-plugin", () => { } } while (false); } - ` - ); + `); expect(transform(source)).toBe(expected); }); it("should handle confident do..while with continue statements", () => { - const source = unpad( - ` + const source = unpad(` function foo() { do { if (x) continue; @@ -3083,10 +2650,8 @@ describe("dce-plugin", () => { } } while (false); } - ` - ); - const expected = unpad( - ` + `); + const expected = unpad(` function foo() { do { if (x) continue; @@ -3118,8 +2683,7 @@ describe("dce-plugin", () => { } } while (false); } - ` - ); + `); expect(transform(source)).toBe(expected); }); }); diff --git a/packages/babel-plugin-minify-flip-comparisons/__tests__/flip-comparisons-test.js b/packages/babel-plugin-minify-flip-comparisons/__tests__/flip-comparisons-test.js index 8a1d0d82a..771130f42 100644 --- a/packages/babel-plugin-minify-flip-comparisons/__tests__/flip-comparisons-test.js +++ b/packages/babel-plugin-minify-flip-comparisons/__tests__/flip-comparisons-test.js @@ -12,17 +12,13 @@ function transform(code) { describe("flip-comparisons", () => { it("should merge two conditionals if the same consequent", () => { - const source = unpad( - ` + const source = unpad(` x === null ? undefined : x === undefined ? undefined : x ? foo(x) : wat(); - ` - ); + `); - const expected = unpad( - ` + const expected = unpad(` null === x ? undefined : x === undefined ? undefined : x ? foo(x) : wat(); - ` - ); + `); expect(transform(source)).toBe(expected); }); @@ -46,8 +42,7 @@ describe("flip-comparisons", () => { }); it("should put pures first in binary expressions 3", () => { - const source = unpad( - ` + const source = unpad(` function foo() { if (foo !== null) { var bar; @@ -56,10 +51,8 @@ describe("flip-comparisons", () => { x(); return x; } - ` - ); - const expected = unpad( - ` + `); + const expected = unpad(` function foo() { if (null !== foo) { var bar; @@ -68,8 +61,7 @@ describe("flip-comparisons", () => { x(); return x; } - ` - ); + `); expect(transform(source)).toBe(expected); }); @@ -80,27 +72,23 @@ describe("flip-comparisons", () => { }); it("should put constants first", () => { - const source = unpad( - ` + const source = unpad(` x * 100; x + 100; x - 100; x / 100; x > 100; x === void 0; - ` - ); + `); - const expected = unpad( - ` + const expected = unpad(` 100 * x; x + 100; x - 100; x / 100; 100 < x; void 0 === x; - ` - ); + `); expect(transform(source)).toBe(expected); }); diff --git a/packages/babel-plugin-minify-guarded-expressions/__tests__/guarded-expressions-test.js b/packages/babel-plugin-minify-guarded-expressions/__tests__/guarded-expressions-test.js index cd6161fd2..4067e985d 100644 --- a/packages/babel-plugin-minify-guarded-expressions/__tests__/guarded-expressions-test.js +++ b/packages/babel-plugin-minify-guarded-expressions/__tests__/guarded-expressions-test.js @@ -12,170 +12,118 @@ function transform(code) { describe("guarded-expressions-plugin", () => { it("should flip logical expressions", () => { - const source = unpad( - ` + const source = unpad(` !x && foo(); - ` - ); + `); - const expected = unpad( - ` + const expected = unpad(` x || foo(); - ` - ); + `); expect(transform(source).trim()).toBe(expected.trim()); }); it("should simplify falsy logical expressions", function() { - let source = unpad( - ` + let source = unpad(` alert(0 && new Foo()); - ` - ); - let expected = unpad( - ` + `); + let expected = unpad(` alert(0); - ` - ); + `); expect(transform(source)).toBe(expected); - source = unpad( - ` + source = unpad(` if (0 && something()) for(;;); - ` - ); - expected = unpad( - ` + `); + expected = unpad(` if (0) for (;;); - ` - ); + `); expect(transform(source)).toBe(expected); - source = unpad( - ` + source = unpad(` alert(false && new Foo()); - ` - ); - expected = unpad( - ` + `); + expected = unpad(` alert(false); - ` - ); + `); expect(transform(source)).toBe(expected); - source = unpad( - ` + source = unpad(` alert(undefined && new Foo()); - ` - ); - expected = unpad( - ` + `); + expected = unpad(` alert(undefined); - ` - ); + `); expect(transform(source)).toBe(expected); - source = unpad( - ` + source = unpad(` alert(null && new Foo()); - ` - ); - expected = unpad( - ` + `); + expected = unpad(` alert(null); - ` - ); + `); expect(transform(source)).toBe(expected); - source = unpad( - ` + source = unpad(` alert("" && new Foo()); - ` - ); - expected = unpad( - ` + `); + expected = unpad(` alert(""); - ` - ); + `); expect(transform(source)).toBe(expected); - source = unpad( - ` + source = unpad(` alert(new Foo() || false); - ` - ); - expected = unpad( - ` + `); + expected = unpad(` alert(new Foo() || false); - ` - ); + `); expect(transform(source)).toBe(expected); }); it("should simplify truthy expressions", () => { - let source = unpad( - ` + let source = unpad(` alert(1 && new Bar()); - ` - ); - let expected = unpad( - ` + `); + let expected = unpad(` alert(new Bar()); - ` - ); + `); expect(transform(source)).toBe(expected); - source = unpad( - ` + source = unpad(` alert(true && new Bar()); - ` - ); - expected = unpad( - ` + `); + expected = unpad(` alert(new Bar()); - ` - ); + `); expect(transform(source)).toBe(expected); - source = unpad( - ` + source = unpad(` alert("hello" && new Bar()); - ` - ); - expected = unpad( - ` + `); + expected = unpad(` alert(new Bar()); - ` - ); + `); expect(transform(source)).toBe(expected); - source = unpad( - ` + source = unpad(` alert(!false && new Bar()); - ` - ); - expected = unpad( - ` + `); + expected = unpad(` alert(new Bar()); - ` - ); + `); expect(transform(source)).toBe(expected); }); it("should not remove reachable impure statements", () => { - let source = unpad( - ` + let source = unpad(` a && void alert('Side effect'); - ` - ); + `); expect(transform(source)).toBe(source); - source = unpad( - ` + source = unpad(` alert(func() || true); - ` - ); + `); expect(transform(source)).toBe(source); }); }); diff --git a/packages/babel-plugin-minify-infinity/__tests__/infinity-test.js b/packages/babel-plugin-minify-infinity/__tests__/infinity-test.js index a9a96e555..0fb5bd277 100644 --- a/packages/babel-plugin-minify-infinity/__tests__/infinity-test.js +++ b/packages/babel-plugin-minify-infinity/__tests__/infinity-test.js @@ -12,105 +12,81 @@ function transform(code) { describe("boolean-plugin", () => { it("should convert infinity to division over 0", () => { - const source = unpad( - ` + const source = unpad(` Infinity; - ` - ); + `); - const expected = unpad( - ` + const expected = unpad(` 1 / 0; - ` - ); + `); expect(transform(source)).toBe(expected); }); it("should not convert infinity when its a property", () => { - const source = unpad( - ` + const source = unpad(` var x = { Infinity: 0 }; - ` - ); + `); - const expected = unpad( - ` + const expected = unpad(` var x = { Infinity: 0 }; - ` - ); + `); expect(transform(source)).toBe(expected); }); it("should not convert infinity when its a property", () => { - const source = unpad( - ` + const source = unpad(` x.Infinity; - ` - ); + `); - const expected = unpad( - ` + const expected = unpad(` x.Infinity; - ` - ); + `); expect(transform(source)).toBe(expected); }); it("should not convert infinity if its a assignment expression", () => { - const source = unpad( - ` + const source = unpad(` Infinity = 1; - ` - ); + `); - const expected = unpad( - ` + const expected = unpad(` Infinity = 1; - ` - ); + `); expect(transform(source)).toBe(expected); }); it("should not convert infinity when its destructed", () => { - const source = unpad( - ` + const source = unpad(` ({ Infinity } = 1); [Infinity] = foo; [...Infinity] = foo; - ` - ); + `); - const expected = unpad( - ` + const expected = unpad(` ({ Infinity } = 1); [Infinity] = foo; [...Infinity] = foo; - ` - ); + `); expect(transform(source)).toBe(expected); }); it("should not convert infinity when as a function params", () => { - const source = unpad( - ` + const source = unpad(` function a(Infinity) {} function a(...Infinity) {} function a({ Infinity }) {} - ` - ); + `); - const expected = unpad( - ` + const expected = unpad(` function a(Infinity) {} function a(...Infinity) {} function a({ Infinity }) {} - ` - ); + `); expect(transform(source)).toBe(expected); }); diff --git a/packages/babel-plugin-minify-replace/__tests__/replace-test.js b/packages/babel-plugin-minify-replace/__tests__/replace-test.js index b2a88f93c..fbe45152a 100644 --- a/packages/babel-plugin-minify-replace/__tests__/replace-test.js +++ b/packages/babel-plugin-minify-replace/__tests__/replace-test.js @@ -21,27 +21,23 @@ describe("replace-plugin", () => { } ]; - const source = unpad( - ` + const source = unpad(` if (__DEV__) { foo(); } if (!__DEV__) { foo(); } - ` - ); + `); - const expected = unpad( - ` + const expected = unpad(` if (0) { foo(); } if (!0) { foo(); } - ` - ); + `); expect(transform(source, replacements)).toBe(expected); }); @@ -57,27 +53,23 @@ describe("replace-plugin", () => { } ]; - const source = unpad( - ` + const source = unpad(` if (__DEV__) { foo(); } if (a.__DEV__) { foo(); } - ` - ); + `); - const expected = unpad( - ` + const expected = unpad(` if (0) { foo(); } if (a.__DEV__) { foo(); } - ` - ); + `); expect(transform(source, replacements)).toBe(expected); }); @@ -93,21 +85,17 @@ describe("replace-plugin", () => { } ]; - const source = unpad( - ` + const source = unpad(` if (__DEV__) { foo(); } - ` - ); + `); - const expected = unpad( - ` + const expected = unpad(` if (true) { foo(); } - ` - ); + `); expect(transform(source, replacements)).toBe(expected); }); @@ -124,19 +112,15 @@ describe("replace-plugin", () => { } ]; - const source = unpad( - ` + const source = unpad(` console.log('wat'); (console.log)('wat'); - ` - ); + `); - const expected = unpad( - ` + const expected = unpad(` emptyFunction('wat'); emptyFunction('wat'); - ` - ); + `); expect(transform(source, replacements)).toBe(expected); }); @@ -161,21 +145,17 @@ describe("replace-plugin", () => { } ]; - const source = unpad( - ` + const source = unpad(` console.log('wat'); (console.log)('wat'); console.error('wat'); - ` - ); + `); - const expected = unpad( - ` + const expected = unpad(` emptyFunction('wat'); emptyFunction('wat'); emptyFunction('wat'); - ` - ); + `); expect(transform(source, replacements)).toBe(expected); }); diff --git a/packages/babel-plugin-minify-simplify/__tests__/simplify-test.js b/packages/babel-plugin-minify-simplify/__tests__/simplify-test.js index 3c41a1f47..c8826cf7a 100644 --- a/packages/babel-plugin-minify-simplify/__tests__/simplify-test.js +++ b/packages/babel-plugin-minify-simplify/__tests__/simplify-test.js @@ -51,27 +51,23 @@ describe("simplify-plugin", () => { }); it("should turn for loop block to a single statement", () => { - const source = unpad( - ` + const source = unpad(` for (var x = 0; x < 10; x++) { console.log(x); } - ` - ); + `); const expected = "for (var x = 0; x < 10; x++) console.log(x);"; expect(transform(source).trim()).toBe(expected); }); it("should turn block statements to sequence expr", () => { - const source = unpad( - ` + const source = unpad(` for (var x = 0; x < 10; x++) { console.log(x); console.log(x); } - ` - ); + `); const expected = "for (var x = 0; x < 10; x++) console.log(x), console.log(x);"; @@ -79,8 +75,7 @@ describe("simplify-plugin", () => { }); it("should the program into as much sequence exprs", () => { - const source = unpad( - ` + const source = unpad(` x(); y(); for (var x = 0; x < 10; x++) { @@ -90,10 +85,8 @@ describe("simplify-plugin", () => { } a(); b = 1; - ` - ); - const expected = unpad( - ` + `); + const expected = unpad(` x(), y(); for (var x = 0; x < 10; x++) { @@ -101,53 +94,43 @@ describe("simplify-plugin", () => { console.log(z), console.log(z); } a(), b = 1; - ` - ); + `); expect(transform(source).trim()).toBe(expected); }); it("should turn if to gaurded expression", () => { - const source = unpad( - ` + const source = unpad(` function foo() { if (x) a(); } - ` - ); - const expected = unpad( - ` + `); + const expected = unpad(` function foo() { x && a(); } - ` - ); + `); expect(transform(source).trim()).toBe(expected); }); it("should turn if statement to conditional", () => { - const source = unpad( - ` + const source = unpad(` function foo() { if (x) a(); else b(); } - ` - ); - const expected = unpad( - ` + `); + const expected = unpad(` function foo() { x ? a() : b(); } - ` - ); + `); expect(transform(source).trim()).toBe(expected); }); it("should turn this into a conditional", () => { - const source = unpad( - ` + const source = unpad(` function foo(a) { if (a && a.b != null) { if ((a.c--) === 1) { @@ -157,22 +140,18 @@ describe("simplify-plugin", () => { } return bar(a); } - ` - ); - const expected = unpad( - ` + `); + const expected = unpad(` function foo(a) { return a && a.b != null ? (a.c-- === 1 && delete a.c, a.b) : bar(a); } - ` - ); + `); expect(transform(source)).toBe(expected); }); it("should turn this into a conditional", () => { - const source = unpad( - ` + const source = unpad(` function foo(a) { if (a && a.b != null) { if ((a.c--) === 1) { @@ -182,22 +161,18 @@ describe("simplify-plugin", () => { } return bar(a); } - ` - ); - const expected = unpad( - ` + `); + const expected = unpad(` function foo(a) { return a && a.b != null ? (a.c-- === 1 && delete a.c, a.b) : bar(a); } - ` - ); + `); expect(transform(source)).toBe(expected); }); it("should turn this into a conditional", () => { - const source = unpad( - ` + const source = unpad(` function foo(a) { if (a) { return a.b; @@ -205,46 +180,38 @@ describe("simplify-plugin", () => { return bar(a); } } - ` - ); - const expected = unpad( - ` + `); + const expected = unpad(` function foo(a) { return a ? a.b : bar(a); } - ` - ); + `); expect(transform(source)).toBe(expected); }); xit("should turn IIFE to negation", () => { - const source = unpad( - ` + const source = unpad(` (function() { x(); })(); y = function () { x(); }(); - ` - ); - const expected = unpad( - ` + `); + const expected = unpad(` !function () { x(); }(), y = function () { x(); }(); - ` - ); + `); expect(transform(source)).toBe(expected); }); it("should remove the else block if early return", () => { - const source = unpad( - ` + const source = unpad(` function foo() { if (1) { return 2; @@ -253,45 +220,37 @@ describe("simplify-plugin", () => { lol(2); } } - ` - ); + `); - const expected = unpad( - ` + const expected = unpad(` function foo() { return 1 ? 2 : void (lol(1), lol(2)); } - ` - ); + `); expect(transform(source)).toBe(expected); }); it("should merge blocks into a return with sequence expr", () => { - const source = unpad( - ` + const source = unpad(` function foo() { y(); x(); return 1; } - ` - ); + `); - const expected = unpad( - ` + const expected = unpad(` function foo() { return y(), x(), 1; } - ` - ); + `); expect(transform(source)).toBe(expected); }); it("should merge blocks into a return with sequence expr", () => { - const source = unpad( - ` + const source = unpad(` function foo() { try { x(); @@ -302,11 +261,9 @@ describe("simplify-plugin", () => { y(); return 1; } - ` - ); + `); - const expected = unpad( - ` + const expected = unpad(` function foo() { try { x(); @@ -316,102 +273,84 @@ describe("simplify-plugin", () => { return y(), 1; } - ` - ); + `); expect(transform(source)).toBe(expected); }); // https://github.com/babel/babili/issues/208 it("should handle empty blocks when merging to sequences", () => { - const source = unpad( - ` + const source = unpad(` !function () { var x; { } alert(x); }() - ` - ); - const expected = unpad( - ` + `); + const expected = unpad(` !function () { var x; alert(x); }(); - ` - ); + `); expect(transform(source)).toBe(expected); }); it("should merge expressions into the init part of for", () => { - const source = unpad( - ` + const source = unpad(` function foo() { x(); y(); for (; i < 10; i++) z(); } - ` - ); + `); - const expected = unpad( - ` + const expected = unpad(` function foo() { for (x(), y(); i < 10; i++) z(); } - ` - ); + `); expect(transform(source).trim()).toBe(expected); }); it("should merge statements into the init part of for even when there are others", () => { - const source = unpad( - ` + const source = unpad(` function foo() { x(); y(); for (z(); i < 10; i++) z(); } - ` - ); + `); - const expected = unpad( - ` + const expected = unpad(` function foo() { for (x(), y(), z(); i < 10; i++) z(); } - ` - ); + `); expect(transform(source).trim()).toBe(expected); }); it("should remove empty returns", () => { - const source = unpad( - ` + const source = unpad(` function foo() { lol(); return; } - ` - ); + `); - const expected = unpad( - ` + const expected = unpad(` function foo() { lol(); } - ` - ); + `); expect(transform(source)).toBe(expected); }); it("should merge if statements with following expressions using void", () => { - const source = unpad( - ` + const source = unpad(` function foo() { if(window.self != window.top) { if(__DEV__) { @@ -421,23 +360,19 @@ describe("simplify-plugin", () => { } lol(); } - ` - ); + `); - const expected = unpad( - ` + const expected = unpad(` function foo() { return window.self == window.top ? void lol() : void (__DEV__ && console.log('lol', name)); } - ` - ); + `); expect(transform(source)).toBe(expected); }); it("should not try to merge `if` when there are multiple statements to follow", () => { - const source = unpad( - ` + const source = unpad(` function foo() { if(window.self != window.top) { if(__DEV__) { @@ -448,11 +383,9 @@ describe("simplify-plugin", () => { lol(); try { lol() } catch (e) {} } - ` - ); + `); - const expected = unpad( - ` + const expected = unpad(` function foo() { if (window.self != window.top) return void (__DEV__ && console.log('lol', name)); lol(); @@ -461,15 +394,13 @@ describe("simplify-plugin", () => { lol(); } catch (e) {} } - ` - ); + `); expect(transform(source)).toBe(expected); }); it("should handle missing return arg when merging if statements", () => { - const source = unpad( - ` + const source = unpad(` function foo() { if (a) { return; @@ -477,23 +408,19 @@ describe("simplify-plugin", () => { return wow; } - ` - ); + `); - const expected = unpad( - ` + const expected = unpad(` function foo() { return a ? void 0 : wow; } - ` - ); + `); expect(transform(source)).toBe(expected); }); it("should handle returns with no args", () => { - const source = unpad( - ` + const source = unpad(` function foo(a) { if (a && a.b != null) { if ((a.c--) === 1) { @@ -503,66 +430,54 @@ describe("simplify-plugin", () => { } return bar(a); } - ` - ); - const expected = unpad( - ` + `); + const expected = unpad(` function foo(a) { return a && a.b != null ? a.c-- === 1 ? void 0 : a.b : bar(a); } - ` - ); + `); expect(transform(source)).toBe(expected); }); it("should convert whiles to fors", () => { - const source = unpad( - ` + const source = unpad(` function foo(a) { while(true) { bar(); } } - ` - ); - const expected = unpad( - ` + `); + const expected = unpad(` function foo(a) { for (; true;) bar(); } - ` - ); + `); expect(transform(source)).toBe(expected); }); it("should convert whiles to fors and merge vars", () => { - const source = unpad( - ` + const source = unpad(` function foo() { let bar = baz; while(true) { bar(); } } - ` - ); - const expected = unpad( - ` + `); + const expected = unpad(` function foo() { for (let bar = baz; true;) bar(); } - ` - ); + `); expect(transform(source)).toBe(expected); }); // https://github.com/babel/babili/issues/198 it("should convert while->for and NOT merge let/const if any is refereced outside the loop", () => { - const source = unpad( - ` + const source = unpad(` function foo() { let a, { b } = x; @@ -571,10 +486,8 @@ describe("simplify-plugin", () => { } return [a, b]; } - ` - ); - const expected = unpad( - ` + `); + const expected = unpad(` function foo() { let a, { b } = x; @@ -583,14 +496,12 @@ describe("simplify-plugin", () => { return [a, b]; } - ` - ); + `); expect(transform(source)).toBe(expected); }); it("should convert while->for and merge var even if any is refereced outside the loop", () => { - const source = unpad( - ` + const source = unpad(` function foo() { var a = 1; while (true) { @@ -598,23 +509,19 @@ describe("simplify-plugin", () => { } return a; } - ` - ); - const expected = unpad( - ` + `); + const expected = unpad(` function foo() { for (var a = 1; true;) bar(a); return a; } - ` - ); + `); expect(transform(source)).toBe(expected); }); it("should combine returns", () => { - const source = unpad( - ` + const source = unpad(` function foo() { if (a) { if (a.b) { @@ -627,23 +534,19 @@ describe("simplify-plugin", () => { } for (; true;) wat(); } - ` - ); + `); - const expected = unpad( - ` + const expected = unpad(` function foo() { if (!(a && a.b && a.b.c && a.b.c())) for (; true;) wat(); } - ` - ); + `); expect(transform(source)).toBe(expected); }); it("should convert early returns to negated if blocks", () => { - const source = unpad( - ` + const source = unpad(` function foo(a) { if (lol) return; doThings(); @@ -663,10 +566,8 @@ describe("simplify-plugin", () => { while (wow) if (lol) return; boo(); } - ` - ); - const expected = unpad( - ` + `); + const expected = unpad(` function foo(a) { lol || (doThings(), doOtherThings()); } @@ -683,51 +584,41 @@ describe("simplify-plugin", () => { boo(); } - ` - ); + `); expect(transform(source)).toBe(expected); }); it("should remove early return when no other statements", () => { - const source = unpad( - ` + const source = unpad(` function foo() { wow(); if (x) { return; } } - ` - ); + `); - const expected = unpad( - ` + const expected = unpad(` function foo() { wow(); x; } - ` - ); + `); expect(transform(source)).toBe(expected); }); it("earlyReturnTransform: it shouldn't error on shorthand arrow functions", () => { - const source = unpad( - ` + const source = unpad(` const f = () => a; - ` - ); - const expected = unpad( - ` + `); + const expected = unpad(` const f = () => a; - ` - ); + `); expect(transform(source)).toBe(expected); }); it("should merge function blocks into sequence expressions", () => { - const source = unpad( - ` + const source = unpad(` function foo() { a(); var x = bar(); @@ -746,12 +637,10 @@ describe("simplify-plugin", () => { c(); z(); } - ` - ); + `); // TODO merge var z into the init part of for. - const expected = unpad( - ` + const expected = unpad(` function foo() { a(); @@ -770,106 +659,88 @@ describe("simplify-plugin", () => { c(), z(); } - ` - ); + `); expect(transform(source)).toBe(expected); }); it("should merge function blocks into sequence expressions (part 2)", () => { - const source = unpad( - ` + const source = unpad(` function bar() { var z; c(); for (z in { a: 1}) x(z); z(); } - ` - ); + `); - const expected = unpad( - ` + const expected = unpad(` function bar() { var z; for (z in c(), { a: 1 }) x(z); z(); } - ` - ); + `); expect(transform(source)).toBe(expected); }); it("should merge if statements when there is no alternate", () => { - const source = unpad( - ` + const source = unpad(` if (a) { if (b) { throw 'wow'; } } - ` - ); + `); - const expected = unpad( - ` + const expected = unpad(` if (a && b) throw 'wow'; - ` - ); + `); expect(transform(source)).toBe(expected); }); it("should not merge if statements if changes semantics", () => { - const source = unpad( - ` + const source = unpad(` function foo() { if (a) { if (b()) return false; } else if (c()) return true; } - ` - ); + `); - const expected = unpad( - ` + const expected = unpad(` function foo() { if (a) { if (b()) return false; } else if (c()) return true; } - ` - ); + `); expect(transform(source)).toBe(expected); }); it("should merge if/return statements", () => { - const source = unpad( - ` + const source = unpad(` function foo() { if (a) return b; if (c) return d; return e; } - ` - ); + `); - const expected = unpad( - ` + const expected = unpad(` function foo() { return a ? b : c ? d : e; } - ` - ); + `); expect(transform(source)).toBe(expected); }); it("should merge if/return statements 2", () => { - const source = unpad( - ` + const source = unpad(` function foo() { if (bar) return; if (far) return; @@ -877,68 +748,56 @@ describe("simplify-plugin", () => { return e; } - ` - ); + `); - const expected = unpad( - ` + const expected = unpad(` function foo() { return bar || far || faz ? void 0 : e; } - ` - ); + `); expect(transform(source)).toBe(expected); }); it("should merge return statements with expression in between", () => { - const source = unpad( - ` + const source = unpad(` function foo() { if (a) return b; c = d; return z; } - ` - ); + `); - const expected = unpad( - ` + const expected = unpad(` function foo() { return a ? b : (c = d, z); } - ` - ); + `); expect(transform(source)).toBe(expected); }); it("should hoist functions", () => { - const source = unpad( - ` + const source = unpad(` function foo() { a(); function bar() {} b(); } - ` - ); + `); - const expected = unpad( - ` + const expected = unpad(` function foo() { function bar() {} a(), b(); } - ` - ); + `); expect(transform(source)).toBe(expected); }); it("should not return inside a loop", () => { - const source = unpad( - ` + const source = unpad(` function foo() { while(1) { if (a === null) { @@ -949,26 +808,22 @@ describe("simplify-plugin", () => { b(); } } - ` - ); + `); - const expected = unpad( - ` + const expected = unpad(` function foo() { for (; 1;) { if (a === null) return void b(); a(), b(); } } - ` - ); + `); expect(transform(source)).toBe(expected); }); it("should make if with one return into a conditional", () => { - const source = unpad( - ` + const source = unpad(` function foo() { if (b) { return foo; @@ -977,23 +832,19 @@ describe("simplify-plugin", () => { b(); } } - ` - ); + `); - const expected = unpad( - ` + const expected = unpad(` function foo() { return b ? foo : void (a(), b()); } - ` - ); + `); expect(transform(source)).toBe(expected); }); it("should make if with one return into a conditional 2", () => { - const source = unpad( - ` + const source = unpad(` function foo() { if (b) { foo(); @@ -1001,23 +852,19 @@ describe("simplify-plugin", () => { return bar; } } - ` - ); + `); - const expected = unpad( - ` + const expected = unpad(` function foo() { return b ? void foo() : bar; } - ` - ); + `); expect(transform(source)).toBe(expected); }); it("should make if with one return into a conditional 3", () => { - const source = unpad( - ` + const source = unpad(` function foo() { if (b) { foo(); @@ -1025,23 +872,19 @@ describe("simplify-plugin", () => { return; } } - ` - ); + `); - const expected = unpad( - ` + const expected = unpad(` function foo() { b && foo(); } - ` - ); + `); expect(transform(source)).toBe(expected); }); it("should make if with one return into a conditional 4", () => { - const source = unpad( - ` + const source = unpad(` function foo() { if (b) { return; @@ -1049,23 +892,19 @@ describe("simplify-plugin", () => { foo(); } } - ` - ); + `); - const expected = unpad( - ` + const expected = unpad(` function foo() { b || foo(); } - ` - ); + `); expect(transform(source)).toBe(expected); }); it("should not merge if", () => { - const source = unpad( - ` + const source = unpad(` if (x) { try { foo(); @@ -1079,16 +918,13 @@ describe("simplify-plugin", () => { for (;;) 1; } } - ` - ); + `); - const expected = unpad( - ` + const expected = unpad(` if (x) try { foo(); } catch (e) {} else if (y) if (a) bar();else if (b) baz();else for (;;) 1; - ` - ); + `); expect(transform(source)).toBe(expected); }); @@ -1096,25 +932,20 @@ describe("simplify-plugin", () => { it("should understand continue statements"); it("should handle do while statements", () => { - const source = unpad( - ` + const source = unpad(` do { foo(); } while (1); - ` - ); + `); - const expected = unpad( - ` + const expected = unpad(` do foo(); while (1); - ` - ); + `); expect(transform(source)).toBe(expected); }); it("should handle multiple interplays of if statements and returns", () => { - const source = unpad( - ` + const source = unpad(` function lawl() { var a = 1; if (b) { @@ -1137,23 +968,19 @@ describe("simplify-plugin", () => { haha(); return butts; } - ` - ); + `); - const expected = unpad( - ` + const expected = unpad(` function lawl() { var a = 1; return b ? c : a ? void bar() : d ? g ? (this['s'] = morebutts, wat) : boo : (haha(), butts); } - ` - ); + `); expect(transform(source)).toBe(expected); }); it("should handle empty blocks in if statements", () => { - const source = unpad( - ` + const source = unpad(` if (a) { } else { foo(); @@ -1167,21 +994,17 @@ describe("simplify-plugin", () => { if (a) { } else { } - ` - ); + `); - const expected = unpad( - ` + const expected = unpad(` a || foo(), a ? foo() : b, a; - ` - ); + `); expect(transform(source)).toBe(expected); }); it("re-arrange conditionals for assignment", () => { - const source = unpad( - ` + const source = unpad(` var x; if (a) { x = foo; @@ -1190,22 +1013,18 @@ describe("simplify-plugin", () => { } else { x = baz; } - ` - ); + `); - const expected = unpad( - ` + const expected = unpad(` var x; x = a ? foo : b ? bar : baz; - ` - ); + `); expect(transform(source)).toBe(expected); }); it("should bail on re-arranging conditionals for assignment", () => { - const source = unpad( - ` + const source = unpad(` var x; if (a) { x = foo; @@ -1214,22 +1033,18 @@ describe("simplify-plugin", () => { } else { y = baz; } - ` - ); + `); - const expected = unpad( - ` + const expected = unpad(` var x; a ? x = foo : b ? x = bar : y = baz; - ` - ); + `); expect(transform(source)).toBe(expected); }); it("should bail on re-arranging conditionals for assignment", () => { - const source = unpad( - ` + const source = unpad(` var x; if (a) { x = foo; @@ -1238,22 +1053,18 @@ describe("simplify-plugin", () => { } else { baz(); } - ` - ); + `); - const expected = unpad( - ` + const expected = unpad(` var x; a ? x = foo : b ? x = bar : baz(); - ` - ); + `); expect(transform(source)).toBe(expected); }); it("re-arranging conditionals for assignment member exprs", () => { - const source = unpad( - ` + const source = unpad(` if (a) { x.b = foo; } else if (b) { @@ -1261,21 +1072,17 @@ describe("simplify-plugin", () => { } else { x.b = baz; } - ` - ); + `); - const expected = unpad( - ` + const expected = unpad(` x.b = a ? foo : b ? bar : baz; - ` - ); + `); expect(transform(source)).toBe(expected); }); it("re-arranging conditionals for assignment with operators", () => { - const source = unpad( - ` + const source = unpad(` if (a) { x.b += foo; } else if (b) { @@ -1283,21 +1090,17 @@ describe("simplify-plugin", () => { } else { x.b += baz; } - ` - ); + `); - const expected = unpad( - ` + const expected = unpad(` x.b += a ? foo : b ? bar : baz; - ` - ); + `); expect(transform(source)).toBe(expected); }); it("should bail on different operators", () => { - const source = unpad( - ` + const source = unpad(` if (a) { x.b += foo; } else if (b) { @@ -1305,93 +1108,73 @@ describe("simplify-plugin", () => { } else { x.b += baz; } - ` - ); + `); - const expected = unpad( - ` + const expected = unpad(` a ? x.b += foo : b ? x.b -= bar : x.b += baz; - ` - ); + `); expect(transform(source)).toBe(expected); }); it("should bail on different member exprs", () => { - const source = unpad( - ` + const source = unpad(` if (a) { this.a = 1; } else { this.b = 2; } - ` - ); + `); - const expected = unpad( - ` + const expected = unpad(` a ? this.a = 1 : this.b = 2; - ` - ); + `); expect(transform(source)).toBe(expected); }); it("should turn continue into negated if", () => { - const source = unpad( - ` + const source = unpad(` for (var p in foo) { if (p) continue; bar(); } - ` - ); + `); - const expected = unpad( - ` + const expected = unpad(` for (var p in foo) p || bar(); - ` - ); + `); expect(transform(source)).toBe(expected); }); it("should flip logical expressions", () => { - const source = unpad( - ` + const source = unpad(` !x && foo(); if (!(null == r)) for (;;); - ` - ); + `); - const expected = unpad( - ` + const expected = unpad(` if (!x && foo(), null != r) for (;;); - ` - ); + `); expect(transform(source).trim()).toBe(expected.trim()); }); it("should flip logical expressions 2", () => { - const source = unpad( - ` + const source = unpad(` if (!(1 !== foo || !bar)) for (;;); - ` - ); + `); - const expected = unpad( - ` + const expected = unpad(` if (1 === foo && bar) for (;;); - ` - ); + `); expect(transform(source)).toBe(expected); }); it("should combine to a single return statement", () => { - const source = unpad( - ` + const source = unpad(` function foo() { if (foo) { bar(foo); @@ -1404,60 +1187,48 @@ describe("simplify-plugin", () => { return wat; } } - ` - ); + `); - const expected = unpad( - ` + const expected = unpad(` function foo() { return foo ? (bar(foo), foo) : baz ? (bar(baz), baz) : wat ? (bar(wat), wat) : void 0; } - ` - ); + `); expect(transform(source)).toBe(expected); }); it("should inline break condition in for test", () => { - const source = unpad( - ` + const source = unpad(` for (i = 1; i <= j; i++) { if (bar) break; } - ` - ); + `); - const expected = unpad( - ` + const expected = unpad(` for (i = 1; i <= j && !bar; i++); - ` - ); + `); expect(transform(source)).toBe(expected); }); it("should inline break condition in for test 2", () => { - const source = unpad( - ` + const source = unpad(` for (i = 1; i <= j; i++) { foo(); if (bar) break; } - ` - ); + `); - const expected = unpad( - ` + const expected = unpad(` for (i = 1; i <= j && (foo(), !bar); i++); - ` - ); + `); expect(transform(source)).toBe(expected); }); it("should inline break condition in for test 3", () => { - const source = unpad( - ` + const source = unpad(` for (i = 1; i <= j; i++) { if (bar) { break; @@ -1466,21 +1237,17 @@ describe("simplify-plugin", () => { if (x) throw 1 } } - ` - ); + `); - const expected = unpad( - ` + const expected = unpad(` for (i = 1; i <= j && !bar; i++) if (wat(), x) throw 1; - ` - ); + `); expect(transform(source)).toBe(expected); }); it("should inline break condition in for test 4", () => { - const source = unpad( - ` + const source = unpad(` for (i = 1; i <= j; i++) { if (bar) { wat(); @@ -1489,22 +1256,18 @@ describe("simplify-plugin", () => { break; } } - ` - ); + `); // TODO: see `!!` below. - const expected = unpad( - ` + const expected = unpad(` for (i = 1; i <= j && !!bar; i++) if (wat(), x) throw 1; - ` - ); + `); expect(transform(source)).toBe(expected); }); it("should inline break condition in for test 5", () => { - const source = unpad( - ` + const source = unpad(` for (i = 1; i <= j; i++) { foo(); if (bar) { @@ -1515,25 +1278,21 @@ describe("simplify-plugin", () => { } hi(); } - ` - ); + `); // TODO: only apply ! unary to last in seq expr - const expected = unpad( - ` + const expected = unpad(` for (i = 1; i <= j && (foo(), !bar); i++) { if (wat(), x) throw 1; hi(); } - ` - ); + `); expect(transform(source)).toBe(expected); }); it("should merge conditional returns into test", () => { - const source = unpad( - ` + const source = unpad(` function foo() { if (x) { delete x.x; @@ -1546,23 +1305,19 @@ describe("simplify-plugin", () => { y(); } } - ` - ); + `); - const expected = unpad( - ` + const expected = unpad(` function foo() { x && (delete x.x, bar()) || (bar ? x() : y()); } - ` - ); + `); expect(transform(source)).toBe(expected); }); it("should bail on mrege conditional return into test", () => { - const source = unpad( - ` + const source = unpad(` function foo() { if (x) { delete x.x; @@ -1575,69 +1330,57 @@ describe("simplify-plugin", () => { y(); } } - ` - ); + `); - const expected = unpad( - ` + const expected = unpad(` function foo() { return x && (delete x.x, bar()) ? 2 : void (bar ? x() : y()); } - ` - ); + `); expect(transform(source)).toBe(expected); }); it("should merge conditional return into test 2", () => { - const source = unpad( - ` + const source = unpad(` function foo() { if (x) { delete x.x; if (bar()) return; } } - ` - ); + `); - const expected = unpad( - ` + const expected = unpad(` function foo() { x && (delete x.x, bar()); } - ` - ); + `); expect(transform(source)).toBe(expected); }); it("should handle return argument", () => { - const source = unpad( - ` + const source = unpad(` function foo() { if (x) { delete x.x; if (bar()) return x; } } - ` - ); + `); - const expected = unpad( - ` + const expected = unpad(` function foo() { if (x && (delete x.x, bar())) return x; } - ` - ); + `); expect(transform(source)).toBe(expected); }); it("should bail on conditional return into test", () => { - const source = unpad( - ` + const source = unpad(` function foo() { if (x) { var f = wow; @@ -1645,11 +1388,9 @@ describe("simplify-plugin", () => { if (bar()) return; } } - ` - ); + `); - const expected = unpad( - ` + const expected = unpad(` function foo() { if (x) { var f = wow; @@ -1657,35 +1398,29 @@ describe("simplify-plugin", () => { if (delete x.x, bar()) return; } } - ` - ); + `); expect(transform(source)).toBe(expected); }); it("should recombine after continue merging", () => { - const source = unpad( - ` + const source = unpad(` for (;;) { a = b; if (!foo) continue; bar = foo; } - ` - ); + `); - const expected = unpad( - ` + const expected = unpad(` for (;;) a = b, foo && (bar = foo); - ` - ); + `); expect(transform(source)).toBe(expected); }); it("should not assume undefined", () => { - const source = unpad( - ` + const source = unpad(` function foo() { if (foo) { if (bar) { @@ -1697,11 +1432,9 @@ describe("simplify-plugin", () => { } return true; } - ` - ); + `); - const expected = unpad( - ` + const expected = unpad(` function foo() { if (foo) { if (bar) return false; @@ -1709,38 +1442,32 @@ describe("simplify-plugin", () => { } return true; } - ` - ); + `); expect(transform(source)).toBe(expected); }); it("should keep directives", () => { - const source = unpad( - ` + const source = unpad(` function a() { 'use strict'; foo(); } - ` - ); + `); - const expected = unpad( - ` + const expected = unpad(` function a() { 'use strict'; foo(); } - ` - ); + `); expect(transform(source)).toBe(expected); }); it("should handle continue in nested if", () => { - const source = unpad( - ` + const source = unpad(` function wow() { for(;;) { if (foo) { @@ -1751,95 +1478,75 @@ describe("simplify-plugin", () => { wat(); } } - ` - ); + `); - const expected = unpad( - ` + const expected = unpad(` function wow() { for (;;) foo && bar || wat(); } - ` - ); + `); expect(transform(source)).toBe(expected); }); it("should convert gaurded nots to ors", () => { - const source = unpad( - ` + const source = unpad(` x(); if (!foo.bar) foo.bar = wat; - ` - ); + `); - const expected = unpad( - ` + const expected = unpad(` x(), !foo.bar && (foo.bar = wat); - ` - ); + `); expect(transform(source)).toBe(expected); }); it("should convert gaurded nots to ors", () => { - const source = unpad( - ` + const source = unpad(` if (!foo && foo !== bar) { wow(); such(); } - ` - ); + `); - const expected = unpad( - ` + const expected = unpad(` !foo && foo !== bar && (wow(), such()); - ` - ); + `); expect(transform(source)).toBe(expected); }); it("should put the empty vars first", () => { - const source = unpad( - ` + const source = unpad(` var x = 1, y, z = 2, zx, a; - ` - ); + `); - const expected = unpad( - ` + const expected = unpad(` var y, zx, a, x = 1, z = 2; - ` - ); + `); expect(transform(source)).toBe(expected); }); it("function expression in sequennce doesnt need parens", () => { - const source = unpad( - ` + const source = unpad(` x, (function() {})(); - ` - ); + `); - const expected = unpad( - ` + const expected = unpad(` x, function () {}(); - ` - ); + `); expect(transform(source)).toBe(expected); }); it("should turn early return else block into statement", () => { - const source = unpad( - ` + const source = unpad(` function x() { for (;;) { x(); @@ -1847,26 +1554,22 @@ describe("simplify-plugin", () => { else y(); } } - ` - ); + `); - const expected = unpad( - ` + const expected = unpad(` function x() { for (;;) { if (x(), foo) return 1; y(); } } - ` - ); + `); expect(transform(source)).toBe(expected); }); it("should remove block", () => { - const source = unpad( - ` + const source = unpad(` function x() { if (a) { if (b) { @@ -1878,45 +1581,37 @@ describe("simplify-plugin", () => { wat(); } } - ` - ); + `); - const expected = unpad( - ` + const expected = unpad(` function x() { if (!a) wat();else if (b) for (;;) a && b(); } - ` - ); + `); expect(transform(source)).toBe(expected); }); // TODO it("should merge things into throw statement seq expr", () => { - const source = unpad( - ` + const source = unpad(` function x() { z(); throw y; } - ` - ); + `); - const expected = unpad( - ` + const expected = unpad(` function x() { throw z(), y; } - ` - ); + `); expect(transform(source)).toBe(expected); }); it("should negate early return if", () => { - const source = unpad( - ` + const source = unpad(` function x() { if (!bar) return; var x = foo; @@ -1924,28 +1619,24 @@ describe("simplify-plugin", () => { if (y) throw y; } - ` - ); + `); // TODO: // Fix indenting - const expected = unpad( - ` + const expected = unpad(` function x() { if (bar) { var x = foo; if (foo && y) throw y; } } - ` - ); + `); expect(transform(source)).toBe(expected); }); it("should not negate early return if", () => { - const source = unpad( - ` + const source = unpad(` function x() { var x = foo; if (hi) { @@ -1955,11 +1646,9 @@ describe("simplify-plugin", () => { } x(); } - ` - ); + `); - const expected = unpad( - ` + const expected = unpad(` function x() { var x = foo; if (hi) { @@ -1969,37 +1658,31 @@ describe("simplify-plugin", () => { } x(); } - ` - ); + `); expect(transform(source)).toBe(expected); }); it("switch if to avoid blocking", () => { - const source = unpad( - ` + const source = unpad(` function x() { if (a) { if (b) for (;;) wow(); } else c(); } - ` - ); + `); - const expected = unpad( - ` + const expected = unpad(` function x() { if (!a) c();else if (b) for (;;) wow(); } - ` - ); + `); expect(transform(source)).toBe(expected); }); it("should remove last break statement in switch", () => { - const source = unpad( - ` + const source = unpad(` switch (foo) { case 'foo': throw bar(); @@ -2008,11 +1691,9 @@ describe("simplify-plugin", () => { wow(); break; } - ` - ); + `); - const expected = unpad( - ` + const expected = unpad(` switch (foo) { case 'foo': throw bar(); @@ -2021,15 +1702,13 @@ describe("simplify-plugin", () => { wow(); } - ` - ); + `); expect(transform(source)).toBe(expected); }); it("should not remove last break statement if it contains a label", () => { - const source = unpad( - ` + const source = unpad(` loop: while (foo) { switch (bar) { case 47: @@ -2040,10 +1719,8 @@ describe("simplify-plugin", () => { break loop; } } - ` - ); - const expected = unpad( - ` + `); + const expected = unpad(` loop: for (; foo;) { switch (bar) { case 47: @@ -2053,14 +1730,12 @@ describe("simplify-plugin", () => { break loop; } } - ` - ); + `); expect(transform(source)).toBe(expected); }); it("should convert consequents in switch into sequence expressions", () => { - const source = unpad( - ` + const source = unpad(` function bar() { switch (foo) { case 'foo': @@ -2073,11 +1748,9 @@ describe("simplify-plugin", () => { break; } } - ` - ); + `); - const expected = unpad( - ` + const expected = unpad(` function bar() { switch (foo) { case 'foo': @@ -2089,15 +1762,13 @@ describe("simplify-plugin", () => { } } - ` - ); + `); expect(transform(source)).toBe(expected); }); it("should convert switch statements to returns", () => { - const source = unpad( - ` + const source = unpad(` function bar() { switch (foo) { case 'foo': @@ -2111,23 +1782,19 @@ describe("simplify-plugin", () => { return 0; } } - ` - ); + `); - const expected = unpad( - ` + const expected = unpad(` function bar() { return foo === 'foo' ? 1 : foo === foo.bar ? 2 : foo === wow ? (wow(), 3) : 0; } - ` - ); + `); expect(transform(source)).toBe(expected); }); it("should convert switch statements with next return as default to returns", () => { - const source = unpad( - ` + const source = unpad(` function bar() { switch (foo) { case 'foo': @@ -2140,23 +1807,19 @@ describe("simplify-plugin", () => { } return 0; } - ` - ); + `); - const expected = unpad( - ` + const expected = unpad(` function bar() { return foo === 'foo' ? 1 : foo === foo.bar ? 2 : foo === wow ? (wow(), 3) : 0; } - ` - ); + `); expect(transform(source)).toBe(expected); }); it("if last statement in function should consider the default return a void", () => { - const source = unpad( - ` + const source = unpad(` function bar() { switch (foo) { case 'foo': @@ -2168,23 +1831,19 @@ describe("simplify-plugin", () => { return 3; } } - ` - ); + `); - const expected = unpad( - ` + const expected = unpad(` function bar() { return foo === 'foo' ? 1 : foo === foo.bar ? 2 : foo === wow ? (wow(), 3) : void 0; } - ` - ); + `); expect(transform(source)).toBe(expected); }); it("should convert switch statements w/ fallthrough to return", () => { - const source = unpad( - ` + const source = unpad(` function bar() { switch (foo) { case 'foo': @@ -2202,24 +1861,20 @@ describe("simplify-plugin", () => { return 0; } } - ` - ); + `); /*eslint max-len: 0 */ - const expected = unpad( - ` + const expected = unpad(` function bar() { return foo === 'foo' ? 1 : foo === foo.bar || foo === wow ? (wow(), 3) : foo === boo ? 4 : foo === baz || foo === wat ? 5 : 0; } - ` - ); + `); expect(transform(source)).toBe(expected); }); it("should convert non-return switch to conditionals", () => { - const source = unpad( - ` + const source = unpad(` function bar() { switch (foo) { case 'foo': @@ -2237,23 +1892,19 @@ describe("simplify-plugin", () => { meh(); } } - ` - ); + `); - const expected = unpad( - ` + const expected = unpad(` function bar() { foo === 'foo' ? foo() : foo === foo.bar ? (wow(), wat()) : foo === shh || foo === wow ? baa() : meh(); } - ` - ); + `); expect(transform(source)).toBe(expected); }); it("should put sequence in switch test", () => { - const source = unpad( - ` + const source = unpad(` function bar() { wow(); switch (foo) { @@ -2262,11 +1913,9 @@ describe("simplify-plugin", () => { break; } } - ` - ); + `); - const expected = unpad( - ` + const expected = unpad(` function bar() { switch (wow(), foo) { case 'foo': @@ -2274,38 +1923,32 @@ describe("simplify-plugin", () => { } } - ` - ); + `); expect(transform(source)).toBe(expected); }); it("should put sequence in if test", () => { - const source = unpad( - ` + const source = unpad(` function bar() { wow(); if (foo) { throw x(); } } - ` - ); + `); - const expected = unpad( - ` + const expected = unpad(` function bar() { if (wow(), foo) throw x(); } - ` - ); + `); expect(transform(source)).toBe(expected); }); it("should convert non-return switch to conditionals", () => { - const source = unpad( - ` + const source = unpad(` function bar() { switch (foo) { case 'foo': @@ -2323,139 +1966,109 @@ describe("simplify-plugin", () => { meh(); } } - ` - ); + `); - const expected = unpad( - ` + const expected = unpad(` function bar() { foo === 'foo' ? foo() : foo === foo.bar ? (wow(), wat()) : foo === shh || foo === wow ? baa() : meh(); } - ` - ); + `); expect(transform(source)).toBe(expected); }); it("should not change type", () => { - const source = unpad( - ` + const source = unpad(` function x(a) { return !!a; } - ` - ); + `); - const expected = unpad( - ` + const expected = unpad(` function x(a) { return !!a; } - ` - ); + `); expect(transform(source)).toBe(expected); }); it("should not change type", () => { - const source = unpad( - ` + const source = unpad(` function x(a, b) { a = a || b; return b === a || !a; } - ` - ); + `); - const expected = unpad( - ` + const expected = unpad(` function x(a, b) { return a = a || b, b === a || !a; } - ` - ); + `); expect(transform(source)).toBe(expected); }); it("should apply unary to only the last element of a sequence expr", () => { - const source = unpad( - ` + const source = unpad(` !(a, b, c); - ` - ); + `); - const expected = unpad( - ` + const expected = unpad(` a, b, !c; - ` - ); + `); expect(transform(source)).toBe(expected); }); it("should apply unary to both sides of the conditional", () => { - const source = unpad( - ` + const source = unpad(` !(a ? b : c); - ` - ); + `); - const expected = unpad( - ` + const expected = unpad(` a ? !b : !c; - ` - ); + `); expect(transform(source)).toBe(expected); }); it("should flip alt and cons if condition is unary", () => { - const source = unpad( - ` + const source = unpad(` !(!a && b) ? b : c - ` - ); + `); - const expected = unpad( - ` + const expected = unpad(` !a && b ? c : b; - ` - ); + `); expect(transform(source)).toBe(expected); }); it("should merge previous expressions in the for loop right", () => { - const source = unpad( - ` + const source = unpad(` function foo() { x = 1; a(); for (var a in b) wow(); } - ` - ); + `); - const expected = unpad( - ` + const expected = unpad(` function foo() { for (var a in x = 1, a(), b) wow(); } - ` - ); + `); expect(transform(source)).toBe(expected); }); it("should convert empty blocks to empty statements", () => { - const source = unpad( - ` + const source = unpad(` function foo() { for (i in p) {} for (; ;) {} switch(1) {} try { a } catch(e) {} } - ` - ); + `); - const expected = unpad( - ` + const expected = unpad(` function foo() { for (i in p); for (;;); @@ -2464,30 +2077,24 @@ describe("simplify-plugin", () => { a; } catch (e) {} } - ` - ); + `); expect(transform(source)).toBe(expected); }); it("should flip binary expressions", () => { - const source = unpad( - ` + const source = unpad(` if (!(!a && b == a && !b && b < a)) for(;;) a(); - ` - ); + `); - const expected = unpad( - ` + const expected = unpad(` if (a || b != a || b || !(b < a)) for (;;) a(); - ` - ); + `); expect(transform(source)).toBe(expected); }); it("should simplify common conditional expression patterns", () => { // From UglifyJS - const source = unpad( - ` + const source = unpad(` function f1() { return a == b ? true : x; } function f2() { return a == b ? false : x; } function f3() { return a < b ? !0 : x; } @@ -2504,10 +2111,8 @@ describe("simplify-plugin", () => { function g6() { return c ? x : !1; } function g7() { return !c ? x : !0; } function g8() { return !c ? x : false; } - ` - ); - const expected = unpad( - ` + `); + const expected = unpad(` function f1() { return !(a != b) || x; } @@ -2556,8 +2161,7 @@ describe("simplify-plugin", () => { function g8() { return !c && x; } - ` - ); + `); expect(transform(source)).toBe(expected); }); @@ -2744,27 +2348,22 @@ describe("simplify-plugin", () => { // https://github.com/babel/babili/issues/115 it("should transform impure conditional statements correctly - issue#115", () => { - const source = unpad( - ` + const source = unpad(` (function () { a = x ? true : false; c = 1 ? (this.get(x), a = b, true) : (foo.bar, false); })(); - ` - ); - const expected = unpad( - ` + `); + const expected = unpad(` (function () { a = !!x, c = 1 ? (this.get(x), a = b, true) : (foo.bar, false); })(); - ` - ); + `); expect(transform(source)).toBe(expected); }); it("should require block for single block scoped declaration in if/else", () => { - const source = unpad( - ` + const source = unpad(` if (false) { let { a } = foo(); } else if (true) { @@ -2772,15 +2371,13 @@ describe("simplify-plugin", () => { } else { function baz() {} } - ` - ); + `); const expected = source; expect(transform(source)).toBe(expected); }); it("should simplify assignments", () => { - const source = unpad( - ` + const source = unpad(` x = x + 1, x = x - 1, x = x * 1, @@ -2793,8 +2390,7 @@ describe("simplify-plugin", () => { x = x ^ 1, x = x / 1, x = x ** 1; - ` - ); + `); const expected = unpad( ` ++x, @@ -2816,8 +2412,7 @@ describe("simplify-plugin", () => { }); it("should not simplify assignments when it is not an equal operator", () => { - const source = unpad( - ` + const source = unpad(` x += x + 1, x -= x - 1, x *= x * 1, @@ -2830,8 +2425,7 @@ describe("simplify-plugin", () => { x ^= x ^ 1, x /= x / 1, x **= x ** 1; - ` - ); + `); const expected = unpad( ` x += x + 1, @@ -2853,8 +2447,7 @@ describe("simplify-plugin", () => { }); it("should not simplify assignments further when it is not an equal operator", () => { - const source = unpad( - ` + const source = unpad(` x = x + (x >> 1), x = x - (x >> 1), x = x * (x >> 1), @@ -2867,8 +2460,7 @@ describe("simplify-plugin", () => { x = x ^ (x >> 1), x = x / (x >> 1), x = x ** (x >> 1); - ` - ); + `); const expected = unpad( ` x += x >> 1, @@ -2890,16 +2482,14 @@ describe("simplify-plugin", () => { }); it("should simplify assignments 2", () => { - const source = unpad( - ` + const source = unpad(` foo = foo + bar, foo = foo * function(){}, foo += 123, foo = 1 + foo, x = x = x + 1, foo = foo + bar + baz - ` - ); + `); const expected = unpad( ` foo += bar, @@ -2915,8 +2505,7 @@ describe("simplify-plugin", () => { }); it("should simplify assignments w. member expressions", () => { - const source = unpad( - ` + const source = unpad(` foo.bar = foo.bar + 1, foo.bar = foo.bar + 2, foo["x"] = foo[x] + 2, @@ -2936,8 +2525,7 @@ describe("simplify-plugin", () => { foo[null] = foo[null] + 1, foo[undefined] = foo[undefined] + 1, foo.bar = foo.bar || {}; - ` - ); + `); // TODO: foo[void 0] = foo[void 0] + 1; const expected = unpad( ` @@ -2967,24 +2555,20 @@ describe("simplify-plugin", () => { }); it("should simplify assignments w. super", () => { - const source = unpad( - ` + const source = unpad(` class Foo { foo() { super.foo = super.foo + 1; } }; - ` - ); - const expected = unpad( - ` + `); + const expected = unpad(` class Foo { foo() { ++super.foo; } }; - ` - ); + `); expect(transform(source)).toBe(expected); }); @@ -2996,8 +2580,7 @@ describe("simplify-plugin", () => { }); it("should consider hoisted definitions in if_return", () => { - const source = unpad( - ` + const source = unpad(` function foo() { bar(); if(x) return; @@ -3007,12 +2590,10 @@ describe("simplify-plugin", () => { bar(); } } - ` - ); + `); // TODO: // Fix indenting - const expected = unpad( - ` + const expected = unpad(` function foo() { function bar() { baz(), bar(); @@ -3022,14 +2603,12 @@ describe("simplify-plugin", () => { const { a } = b; } } - ` - ); + `); expect(transform(source)).toBe(expected); }); it("should optimize if..else..returns", () => { - const source = unpad( - ` + const source = unpad(` function foo() { if (a) { if (x) return; @@ -3038,60 +2617,49 @@ describe("simplify-plugin", () => { const b = 1; return "doesn't matter if this is reached or not"; } - ` - ); - const expected = unpad( - ` + `); + const expected = unpad(` function foo() { if (a) return x ? void 0 : x; const b = 1; return "doesn't matter if this is reached or not"; } - ` - ); + `); expect(transform(source)).toBe(expected); }); it("should fix issue#281 with if..return", () => { - const source = unpad( - ` + const source = unpad(` function foo() { if (x) return; function bar() {} bar(a); } - ` - ); - const expected = unpad( - ` + `); + const expected = unpad(` function foo() { function bar() {} x || bar(a); } - ` - ); + `); expect(transform(source)).toBe(expected); }); it("should fix issue#323 with != and !==", () => { - const source = unpad( - ` + const source = unpad(` function foo() { var x, y; y = o[x]; foo(y !== undefined); } - ` - ); - const expected = unpad( - ` + `); + const expected = unpad(` function foo() { var x, y; y = o[x], foo(y !== undefined); } - ` - ); + `); function transform(code) { return babel.transform(code, { plugins: [plugin, comparisonPlugin] @@ -3101,8 +2669,7 @@ describe("simplify-plugin", () => { }); it("should fix issue#423 with fallthrough in default case", () => { - const source = unpad( - ` + const source = unpad(` function foo(bar) { switch (bar) { case 'a': @@ -3114,21 +2681,17 @@ describe("simplify-plugin", () => { return 3; } } - ` - ); - const expected = unpad( - ` + `); + const expected = unpad(` function foo(bar) { return bar === 'a' ? 1 : bar === 'c' ? 3 : 4; } - ` - ); + `); expect(transform(source)).toBe(expected); }); it("should convert multiple fallthrough in switch to conditional expression", () => { - const source = unpad( - ` + const source = unpad(` function foo(bar) { switch (bar) { case 'a': @@ -3141,21 +2704,17 @@ describe("simplify-plugin", () => { return 3; } } - ` - ); - const expected = unpad( - ` + `); + const expected = unpad(` function foo(bar) { return bar === 'a' || bar === 'b' ? 1 : bar === 'c' ? 3 : 4; } - ` - ); + `); expect(transform(source)).toBe(expected); }); it("should fix#455 and deopt when scope tree is not updated", () => { - const source = unpad( - ` + const source = unpad(` function foo(param) { if (param === null) return; let thingA = param.a; @@ -3163,10 +2722,8 @@ describe("simplify-plugin", () => { if (!thingA && !thingB) return; let thingC = param.c; } - ` - ); - const expected = unpad( - ` + `); + const expected = unpad(` function foo(param) { if (param !== null) { let thingA = param.a; @@ -3176,8 +2733,7 @@ describe("simplify-plugin", () => { } } } - ` - ); + `); expect(transform(source)).toBe(expected); }); }); diff --git a/packages/babel-plugin-minify-type-constructors/__tests__/type-constructors-test.js b/packages/babel-plugin-minify-type-constructors/__tests__/type-constructors-test.js index 4f54ed1b4..5466198fc 100644 --- a/packages/babel-plugin-minify-type-constructors/__tests__/type-constructors-test.js +++ b/packages/babel-plugin-minify-type-constructors/__tests__/type-constructors-test.js @@ -42,8 +42,7 @@ describe("type-constructors-plugin", () => { }); it("should turn Array(nonNumericValue) to [nonNumericValue]", () => { - const source = unpad( - ` + const source = unpad(` Array("Rome"); Array(false); Array(null); @@ -53,10 +52,8 @@ describe("type-constructors-plugin", () => { Array(t); new Array(a.b); new Array((() => 5)()); - ` - ); - const expected = unpad( - ` + `); + const expected = unpad(` ["Rome"]; [false]; [null]; @@ -66,62 +63,49 @@ describe("type-constructors-plugin", () => { Array(t); Array(a.b); Array((() => 5)()); - ` - ); + `); expect(transform(source)).toBe(expected); }); it("should turn Array(number) to [,] only if number is <=6", () => { - const source = unpad( - ` + const source = unpad(` Array(0); Array(1); Array(2 + 4); Array(7); - ` - ); - const expected = unpad( - ` + `); + const expected = unpad(` []; [,]; [,,,,,,]; Array(7); - ` - ); + `); expect(transform(source)).toBe(expected); }); it("should turn new Array(number) to Array(number) if number is >6", () => { - const source = unpad( - ` + const source = unpad(` new Array(6); new Array(7); - ` - ); - const expected = unpad( - ` + `); + const expected = unpad(` [,,,,,,]; Array(7); - ` - ); + `); expect(transform(source)).toBe(expected); }); it("should turn Array(value, value) to [value, value]", () => { - const source = unpad( - ` + const source = unpad(` Array("a", "b"); new Array("0", "1", {}); Array(10, Symbol(), foo()); - ` - ); - const expected = unpad( - ` + `); + const expected = unpad(` ["a", "b"]; ["0", "1", {}]; [10, Symbol(), foo()]; - ` - ); + `); expect(transform(source)).toBe(expected); }); @@ -138,71 +122,60 @@ describe("type-constructors-plugin", () => { }); it("should change Object(null|undefined) to {}", () => { - const source = unpad( - ` + const source = unpad(` [ Object(null), Object(undefined), new Object(void 0) ] - ` - ); + `); const expected = "[{}, {}, {}];"; expect(transform(source)).toBe(expected); }); it("should change Object({a:b}) to {a:b}", () => { - const source = unpad( - ` + const source = unpad(` [ Object({}), Object({a:b}), Object({a:b, c:d}), ] - ` - ); // todo: add Object(Array()) + `); // todo: add Object(Array()) const expected = "[{}, { a: b }, { a: b, c: d }];"; expect(transform(source)).toBe(expected); }); it("should change Object([]) to []", () => { - const source = unpad( - ` + const source = unpad(` [ Object([]), Object([1]), Object([1,2]), new Object([null]) ] - ` - ); // todo: add Object(Array()) + `); // todo: add Object(Array()) const expected = "[[], [1], [1, 2], [null]];"; expect(transform(source)).toBe(expected); }); it("should change Object(localFn) to localFn", () => { - const source = unpad( - ` + const source = unpad(` function a() {}; [ Object(function () {}), new Object(a), Object(Array) ] - ` - ); - const expected = unpad( - ` + `); + const expected = unpad(` function a() {}; [function () {}, a, Object(Array)]; - ` - ); + `); expect(transform(source)).toBe(expected); }); it("shouldn't change Object(value) for unrecognized values", () => { - const source = unpad( - ` + const source = unpad(` [ Object("undefined"), Object(nulled), @@ -210,16 +183,14 @@ describe("type-constructors-plugin", () => { Object(false), Object(stuff()) ] - ` - ); + `); const expected = '[Object("undefined"), Object(nulled), Object(0), Object(false), Object(stuff())];'; expect(transform(source)).toBe(expected); }); it("should change new Object(value) to Object(value) for unrecognized values", () => { - const source = unpad( - ` + const source = unpad(` [ new Object("function"), new Object(Symbol), @@ -227,46 +198,37 @@ describe("type-constructors-plugin", () => { new Object(1), new Object(call({ me: true })) ] - ` - ); + `); const expected = '[Object("function"), Object(Symbol), Object(true), Object(1), Object(call({ me: true }))];'; expect(transform(source)).toBe(expected); }); it("should change Object() to ({}) in ambiguous contexts", () => { - const source = unpad( - ` + const source = unpad(` new Object(); var foo = () => Object(); var bar = () => Object({ baz: 3 }); - ` - ); - const expected = unpad( - ` + `); + const expected = unpad(` ({}); var foo = () => ({}); var bar = () => ({ baz: 3 }); - ` - ); + `); expect(transform(source)).toBe(expected); }); it("shouldn't change referenced identifiers", () => { - const source = unpad( - ` + const source = unpad(` (function (Boolean, String, Number, Array, Object) { return Boolean(a), String(b), Number(c), Array(d), Object(d); })(MyBoolean, MyString, MyNumber, MyArray, MyObject); - ` - ); - const expected = unpad( - ` + `); + const expected = unpad(` (function (Boolean, String, Number, Array, Object) { return Boolean(a), String(b), Number(c), Array(d), Object(d); })(MyBoolean, MyString, MyNumber, MyArray, MyObject); - ` - ); + `); expect(transform(source)).toBe(expected); }); @@ -297,18 +259,14 @@ describe("type-constructors-plugin", () => { // https://github.com/babel/babili/issues/206 it("should handle floating point numbers in Array()", () => { - const source = unpad( - ` + const source = unpad(` new Array(-0.01); new Array(-1); - ` - ); - const expected = unpad( - ` + `); + const expected = unpad(` Array(-0.01); Array(-1); - ` - ); + `); expect(transform(source)).toBe(expected); }); }); diff --git a/packages/babel-plugin-transform-inline-consecutive-adds/__tests__/transform-inline-consecutive-adds-test.js b/packages/babel-plugin-transform-inline-consecutive-adds/__tests__/transform-inline-consecutive-adds-test.js index 6ce2ce2b5..6e9d31fa0 100644 --- a/packages/babel-plugin-transform-inline-consecutive-adds/__tests__/transform-inline-consecutive-adds-test.js +++ b/packages/babel-plugin-transform-inline-consecutive-adds/__tests__/transform-inline-consecutive-adds-test.js @@ -12,8 +12,7 @@ function transform(code) { describe("transform-inline-consecutive-adds-plugin", () => { it("should collapse simple consecutive", () => { - const source = unpad( - ` + const source = unpad(` const foo = { z: 3.0 }; @@ -21,10 +20,8 @@ describe("transform-inline-consecutive-adds-plugin", () => { foo.b = ["hi"]; foo.c = bar(); foo.d = "str"; - ` - ); - const expected = unpad( - ` + `); + const expected = unpad(` const foo = { z: 3.0, a: 42, @@ -32,63 +29,51 @@ describe("transform-inline-consecutive-adds-plugin", () => { c: bar(), d: "str" }; - ` - ); + `); expect(transform(source)).toBe(expected); }); it("should collapse only up to last ExpressionStatement", () => { - const source = unpad( - ` + const source = unpad(` const foo = {}; foo.a = 42; console.log(foo); - ` - ); - const expected = unpad( - ` + `); + const expected = unpad(` const foo = { a: 42 }; console.log(foo); - ` - ); + `); expect(transform(source)).toBe(expected); }); it("should not collapse if lval is nested MemberExpression", () => { - const source = unpad( - ` + const source = unpad(` const foo = {}; foo.bar.a = 42; - ` - ); + `); expect(transform(source)).toBe(source); }); it("should not collapse if lval has wrong name", () => { - const source = unpad( - ` + const source = unpad(` const foo = {}; bar.a = 42; - ` - ); + `); expect(transform(source)).toBe(source); }); it("should not collapse if has direct dependency issues", () => { - const source = unpad( - ` + const source = unpad(` const foo = {}; foo.a = function () { console.log(3); }; foo.b = foo.a(); - ` - ); - const expected = unpad( - ` + `); + const expected = unpad(` const foo = { a: function () { console.log(3); @@ -96,14 +81,12 @@ describe("transform-inline-consecutive-adds-plugin", () => { }; foo.b = foo.a(); - ` - ); + `); expect(transform(source)).toBe(expected); }); it("should not collapse if has indirect dependency issues", () => { - const source = unpad( - ` + const source = unpad(` var foo = {}; foo.a = 4; foo.b = cat(); @@ -114,10 +97,8 @@ describe("transform-inline-consecutive-adds-plugin", () => { console.log(foo); return 0; } - ` - ); - const expected = unpad( - ` + `); + const expected = unpad(` var foo = { a: 4 }; @@ -130,14 +111,12 @@ describe("transform-inline-consecutive-adds-plugin", () => { console.log(foo); return 0; } - ` - ); + `); expect(transform(source)).toBe(expected); }); it("should not collapse if has indirect, nested dependency issues", () => { - const source = unpad( - ` + const source = unpad(` var foo = {}; foo.a = 4; foo.b = cat(); @@ -148,10 +127,8 @@ describe("transform-inline-consecutive-adds-plugin", () => { return 0; } } - ` - ); - const expected = unpad( - ` + `); + const expected = unpad(` var foo = { a: 4 }; @@ -164,243 +141,192 @@ describe("transform-inline-consecutive-adds-plugin", () => { return 0; } } - ` - ); + `); expect(transform(source)).toBe(expected); }); it("should collapse computed properties if they are literals", () => { - const source = unpad( - ` + const source = unpad(` var foo = {}; foo["a"] = 0; foo[4] = 1; - ` - ); - const expected = unpad( - ` + `); + const expected = unpad(` var foo = { "a": 0, 4: 1 }; - ` - ); + `); expect(transform(source)).toBe(expected); }); it("should not collapse computed properties otherwise", () => { - const source = unpad( - ` + const source = unpad(` var foo = {}; foo[global] = 0; - ` - ); + `); expect(transform(source)).toBe(source); }); it("should not collapse computed properties with dependency issues", () => { - const source = unpad( - ` + const source = unpad(` var foo = {}; foo[bar()] = 0; function bar() { console.log(foo); return 0; } - ` - ); + `); expect(transform(source)).toBe(source); }); it("should not collapse computed properties with circular reference", () => { - const source = unpad( - ` + const source = unpad(` var foo = {}; foo.bar = foo; - ` - ); + `); expect(transform(source)).toBe(source); }); it("should collapse statements with multiple assignments", () => { - const source = unpad( - ` + const source = unpad(` var foo = {}; foo.a = 0, foo.b = 2; - ` - ); - const expected = unpad( - ` + `); + const expected = unpad(` var foo = { a: 0, b: 2 }; - ` - ); + `); expect(transform(source)).toBe(expected); }); it("should not collapse statements with multiple assignments and dependency issues", () => { - const source = unpad( - ` + const source = unpad(` var foo = {}; foo.a = 0, foo.b = bar(); function bar() { console.log(foo); return 0; } - ` - ); + `); expect(transform(source)).toBe(source); }); it("should collapse statements for arrays", () => { - const source = unpad( - ` + const source = unpad(` var foo = [1, 2]; foo.push(3, 4), foo.push(5); foo.push(6); - ` - ); - const expected = unpad( - ` + `); + const expected = unpad(` var foo = [1, 2, 3, 4, 5, 6]; - ` - ); + `); expect(transform(source)).toBe(expected); }); it("should collapse statements for sets", () => { - const source = unpad( - ` + const source = unpad(` var foo = new Set(); foo.add(1), foo.add(2); foo.add(3); - ` - ); - const expected = unpad( - ` + `); + const expected = unpad(` var foo = new Set([1, 2, 3]); - ` - ); + `); expect(transform(source)).toBe(expected); }); it("should collapse statements for array-initialized sets", () => { - const source = unpad( - ` + const source = unpad(` var foo = new Set([1, 2]); foo.add(3); - ` - ); - const expected = unpad( - ` + `); + const expected = unpad(` var foo = new Set([1, 2, 3]); - ` - ); + `); expect(transform(source)).toBe(expected); }); it("should not collapse statements for array-initialized sets with circular reference", () => { - const source = unpad( - ` + const source = unpad(` var foo = new Set([1, 2]); foo.add(foo); - ` - ); + `); expect(transform(source)).toBe(source); }); it("should collapse array property assignments", () => { - const source = unpad( - ` + const source = unpad(` var foo = []; foo[5] = 'blah'; foo[3] = 'blah'; foo[7] = 'blah'; - ` - ); - const expected = unpad( - ` + `); + const expected = unpad(` var foo = [,,, 'blah',, 'blah',, 'blah']; - ` - ); + `); expect(transform(source)).toBe(expected); }); it("should not collapse array property assignments if long", () => { - const source = unpad( - ` + const source = unpad(` var foo = []; foo[10] = 'blah'; - ` - ); + `); expect(transform(source)).toBe(source); }); it("should not collapse array property assignments if override initial", () => { - const source = unpad( - ` + const source = unpad(` var foo = [1, 2, 3]; foo[2] = 'blah'; - ` - ); + `); expect(transform(source)).toBe(source); }); it("should not collapse array property assignments if override dynamic", () => { - const source = unpad( - ` + const source = unpad(` var foo = [1, 2]; foo[2] = 'blah'; foo[2] = 'ok'; - ` - ); + `); expect(transform(source)).toBe(source); }); it("should not collapse array property assignments if index is float", () => { - const source = unpad( - ` + const source = unpad(` var foo = []; foo[2.1] = 'blah'; - ` - ); + `); expect(transform(source)).toBe(source); }); it("should not collapse array property assignments if index is non-int as string", () => { - const source = unpad( - ` + const source = unpad(` var foo = []; foo['2.1'] = 'blah'; - ` - ); + `); expect(transform(source)).toBe(source); }); it("should collapse array property assignments if index is int as string", () => { - const source = unpad( - ` + const source = unpad(` var foo = []; foo['2'] = 'blah'; - ` - ); - const expected = unpad( - ` + `); + const expected = unpad(` var foo = [,, 'blah']; - ` - ); + `); expect(transform(source)).toBe(expected); }); it("should not collapse array property assignments if it is circular reference", () => { - const source = unpad( - ` + const source = unpad(` var foo = []; foo[2] = foo; - ` - ); + `); expect(transform(source)).toBe(source); }); }); diff --git a/packages/babel-plugin-transform-member-expression-literals/__tests__/transform-member-expression-literals-test.js b/packages/babel-plugin-transform-member-expression-literals/__tests__/transform-member-expression-literals-test.js index 80b26eb65..2b878b4aa 100644 --- a/packages/babel-plugin-transform-member-expression-literals/__tests__/transform-member-expression-literals-test.js +++ b/packages/babel-plugin-transform-member-expression-literals/__tests__/transform-member-expression-literals-test.js @@ -29,12 +29,10 @@ describe("transform-member-expressions-literals-plugin", () => { }); it("should not transform invalid identifiers", () => { - const source = unpad( - ` + const source = unpad(` foo["default"]; foo["import"]; - ` - ); + `); expect(transform(source)).toBe(source); }); diff --git a/packages/babel-plugin-transform-merge-sibling-variables/__tests__/transform-merge-sibling-variables-test.js b/packages/babel-plugin-transform-merge-sibling-variables/__tests__/transform-merge-sibling-variables-test.js index ca35a3a84..adb8af4bc 100644 --- a/packages/babel-plugin-transform-merge-sibling-variables/__tests__/transform-merge-sibling-variables-test.js +++ b/packages/babel-plugin-transform-merge-sibling-variables/__tests__/transform-merge-sibling-variables-test.js @@ -12,32 +12,26 @@ function transform(code) { describe("transform-merge-sibling-variables-plugin", () => { it("concat vars", () => { - const source = unpad( - ` + const source = unpad(` var i = 0; var x = 0; var y = 0; - ` - ); - const expected = unpad( - ` + `); + const expected = unpad(` var i = 0, x = 0, y = 0; - ` - ); + `); expect(transform(source)).toBe(expected); }); it("concat vars in for loops", () => { - const source = unpad( - ` + const source = unpad(` var i = 0; var j = 0; for (var x = 0; x < 10; x++) console.log(i + x); - ` - ); + `); const expected = "for (var i = 0, j = 0, x = 0; x < 10; x++) console.log(i + x);"; @@ -45,154 +39,126 @@ describe("transform-merge-sibling-variables-plugin", () => { }); it("don't concat block-scoped variables in for loops", () => { - const source = unpad( - ` + const source = unpad(` let i = 0; for (let x = 0; x < 10; x++) console.log(i + x); - ` - ); + `); expect(transform(source)).toBe(source); }); it("don't concat constants in for loops", () => { - const source = unpad( - ` + const source = unpad(` const j = 0; for (const x = 0;;) console.log(j + x); - ` - ); + `); expect(transform(source)).toBe(source); }); it("concat block-scoped vars next to, but not into for loops", () => { - const source = unpad( - ` + const source = unpad(` let i = 0; let y = 0; for (let x = 0; x < 10; x++) console.log(i + x); - ` - ); - const expected = unpad( - ` + `); + const expected = unpad(` let i = 0, y = 0; for (let x = 0; x < 10; x++) console.log(i + x); - ` - ); + `); expect(transform(source)).toBe(expected); }); it("lift var declarations to loop intializer", () => { - const source = unpad( - ` + const source = unpad(` for (var i = 0; i < 0; i++) { var j = jj(); } for (var i=0;;) var j = 0; - ` - ); - const expected = unpad( - ` + `); + const expected = unpad(` for (var i = 0, j; i < 0; i++) { j = jj(); } for (var i = 0, j;;) j = 0; - ` - ); + `); expect(transform(source)).toBe(expected); }); it("lift let declarations to loop intializer", () => { - const source = unpad( - ` + const source = unpad(` for (let i = 0; i < 0; i++) { let j = jj(); } - ` - ); - const expected = unpad( - ` + `); + const expected = unpad(` for (let i = 0, j; i < 0; i++) { j = jj(); } - ` - ); + `); expect(transform(source)).toBe(expected); }); it("dont lift declarations on object/array pattern", () => { - const source = unpad( - ` + const source = unpad(` for (var i = 0; i < 0; i++) { var [j] = jj(); } for (var i = 0; i < 0; i++) { var { j } = jj(); } - ` - ); + `); expect(transform(source)).toBe(source); }); it("dont lift declarations when no body is present", () => { - const source = unpad( - ` + const source = unpad(` for (;;) {} for (;;) var i = 0; - ` - ); + `); expect(transform(source)).toBe(source); }); it("dont lift when the declarations are of different kind", () => { - const source = unpad( - ` + const source = unpad(` for (let i = 0; i < 0; i++) { var i = 0; } - ` - ); + `); expect(transform(source)).toBe(source); }); it("dont lift when the declarations are not initialized", () => { - const source = unpad( - ` + const source = unpad(` for (var i = 0;;) { var i; } - ` - ); + `); expect(transform(source)).toBe(source); }); it("dont lift when there are multiple declarations", () => { - const source = unpad( - ` + const source = unpad(` for (var i = 0; i < 0; i++) { var i = 0, k = 0; } - ` - ); + `); - const expected = unpad( - ` + const expected = unpad(` for (var i = 0; i < 0; i++) { var i = 0, k = 0; } - ` - ); + `); expect(transform(source)).toBe(expected); }); diff --git a/packages/babel-plugin-transform-property-literals/__tests__/transform-property-literals-test.js b/packages/babel-plugin-transform-property-literals/__tests__/transform-property-literals-test.js index 8126b87b7..7dc2ab0fa 100644 --- a/packages/babel-plugin-transform-property-literals/__tests__/transform-property-literals-test.js +++ b/packages/babel-plugin-transform-property-literals/__tests__/transform-property-literals-test.js @@ -24,48 +24,40 @@ describe("transform-property-literals-plugin", () => { }); it("should not strip necessaary quotes for numeric like things", () => { - const source = unpad( - ` + const source = unpad(` var data = { "00": 1, "01": 2 }; - ` - ); + `); expect(transform(source)).toBe(source); }); it("should not transform invalid identifiers", () => { - const source = unpad( - ` + const source = unpad(` ({ "default": null, "import": null }); - ` - ); + `); expect(transform(source)).toBe(source); }); it("should not transform non-string properties", () => { - const source = unpad( - ` + const source = unpad(` ({ foo: null }); - ` - ); + `); expect(transform(source)).toBe(source); }); it("should not transform propety keys that are computed", () => { - const source = unpad( - ` + const source = unpad(` ({ [a]: null }); - ` - ); + `); expect(transform(source)).toBe(source); }); }); diff --git a/packages/babel-plugin-transform-remove-console/__tests__/remove-console-test.js b/packages/babel-plugin-transform-remove-console/__tests__/remove-console-test.js index 014bc19dc..a61a6787c 100644 --- a/packages/babel-plugin-transform-remove-console/__tests__/remove-console-test.js +++ b/packages/babel-plugin-transform-remove-console/__tests__/remove-console-test.js @@ -12,107 +12,86 @@ function transform(code) { describe("remove-console-plugin", () => { it("statement-nested", () => { - const source = unpad( - ` + const source = unpad(` function foo() { console.log("foo"); blah(); } - ` - ); + `); - const expected = unpad( - ` + const expected = unpad(` function foo() { blah(); } - ` - ); + `); expect(transform(source)).toBe(expected); }); it("expression-nested", () => { - const source = unpad( - ` + const source = unpad(` function foo() { true && console.log("foo"); blah(); } - ` - ); + `); - const expected = unpad( - ` + const expected = unpad(` function foo() { true && void 0; blah(); } - ` - ); + `); expect(transform(source)).toBe(expected); }); it("expression-top-level", () => { - const source = unpad( - ` + const source = unpad(` true && console.log("foo"); blah(); - ` - ); + `); - const expected = unpad( - ` + const expected = unpad(` true && void 0; blah(); - ` - ); + `); expect(transform(source)).toBe(expected); }); it("statement-top-level", () => { - const source = unpad( - ` + const source = unpad(` console.log("foo"); blah(); - ` - ); + `); - const expected = unpad( - ` + const expected = unpad(` blah(); - ` - ); + `); expect(transform(source).trim()).toBe(expected); }); it("statement no block", () => { - const source = unpad( - ` + const source = unpad(` if (blah) console.log(blah); for (;;) console.log(blah); for (var blah in []) console.log(blah); for (var blah of []) console.log(blah); while (blah) console.log(blah); do console.log(blah); while (blah); - ` - ); + `); - const expected = unpad( - ` + const expected = unpad(` if (blah) {} for (;;) {} for (var blah in []) {} for (var blah of []) {} while (blah) {} do {} while (blah); - ` - ); + `); expect(transform(source).trim()).toBe(expected); }); it("should remove console.* assignments to other variables", () => { - const source = unpad( - ` + const source = unpad(` const a = console.log; a(); const b = console.log.bind(console); @@ -125,10 +104,8 @@ describe("remove-console-plugin", () => { } console.log.call(console, "foo"); console.log.apply(null, {}); - ` - ); - const expected = unpad( - ` + `); + const expected = unpad(` const a = function () {}; a(); const b = function () {}; @@ -137,14 +114,12 @@ describe("remove-console-plugin", () => { function foo() { if (function () {}) {} } - ` - ); + `); expect(transform(source)).toBe(expected); }); it("should NOT remove local bindings of name console", () => { - const source = unpad( - ` + const source = unpad(` function foo(console) { console.foo("hi"); const bar = console.foo.bind(console); @@ -156,14 +131,12 @@ describe("remove-console-plugin", () => { return; } } - ` - ); + `); expect(transform(source)).toBe(source); }); it("should convert assigments to no-op", () => { - const source = unpad( - ` + const source = unpad(` function foo() { console.foo = function foo() { console.log("foo"); @@ -172,16 +145,13 @@ describe("remove-console-plugin", () => { console.foo(); console.error("asdf"); } - ` - ); - const expected = unpad( - ` + `); + const expected = unpad(` function foo() { console.foo = function () {}; console.error = function () {}; } - ` - ); + `); expect(transform(source)).toBe(expected); }); }); diff --git a/packages/babel-plugin-transform-remove-debugger/__tests__/remove-debugger-test.js b/packages/babel-plugin-transform-remove-debugger/__tests__/remove-debugger-test.js index d8b15bc90..e40bf8d1f 100644 --- a/packages/babel-plugin-transform-remove-debugger/__tests__/remove-debugger-test.js +++ b/packages/babel-plugin-transform-remove-debugger/__tests__/remove-debugger-test.js @@ -24,27 +24,23 @@ describe("remove-debugger-plugin", () => { }); it("statement no block", () => { - const source = unpad( - ` + const source = unpad(` if (blah) debugger; for (;;) debugger; for (var blah in []) debugger; for (var blah of []) debugger; while (blah) debugger; do debugger; while (blah); - ` - ); + `); - const expected = unpad( - ` + const expected = unpad(` if (blah) {} for (;;) {} for (var blah in []) {} for (var blah of []) {} while (blah) {} do {} while (blah); - ` - ); + `); expect(transform(source).trim()).toBe(expected); }); }); diff --git a/packages/babel-plugin-transform-remove-undefined/__tests__/babel-plugin-transform-remove-undefined-test.js b/packages/babel-plugin-transform-remove-undefined/__tests__/babel-plugin-transform-remove-undefined-test.js index 5f13c9f4d..74cb3abf4 100644 --- a/packages/babel-plugin-transform-remove-undefined/__tests__/babel-plugin-transform-remove-undefined-test.js +++ b/packages/babel-plugin-transform-remove-undefined/__tests__/babel-plugin-transform-remove-undefined-test.js @@ -35,108 +35,85 @@ describe("transform-remove-undefined-plugin", () => { }); it("should remove undefined return value", () => { - const source = unpad( - ` + const source = unpad(` function foo() { return undefined; - }` - ); - const expected = unpad( - ` + }`); + const expected = unpad(` function foo() { return; - }` - ); + }`); expect(transform(source)).toBe(expected); }); it("should remove var declarations in functions", () => { - const source = unpad( - ` + const source = unpad(` function foo() { var a = undefined; - }` - ); - const expected = unpad( - ` + }`); + const expected = unpad(` function foo() { var a; - }` - ); + }`); expect(transform(source)).toBe(expected); }); it("should remove let-assignments in inner blocks", () => { - const source = unpad( - ` + const source = unpad(` let a = 1; { let a = undefined; - }` - ); - const expected = unpad( - ` + }`); + const expected = unpad(` let a = 1; { let a; - }` - ); + }`); expect(transform(source)).toBe(expected); }); it("should remove var-assignments in loops if no modify", () => { - const source = unpad( - ` + const source = unpad(` for (var a = undefined;;) { var b = undefined; - }` - ); - const expected = unpad( - ` + }`); + const expected = unpad(` for (var a;;) { var b; - }` - ); + }`); expect(transform(source)).toBe(expected); }); it("should not remove var-assignments in loops if modify", () => { - const source = unpad( - ` + const source = unpad(` for (var a;;) { var b = undefined; console.log(b); b = 3; - }` - ); + }`); expect(transform(source)).toBe(source); }); it("should not remove var-assignments if referenced before", () => { - const source = unpad( - ` + const source = unpad(` function foo() { a = 3; var a = undefined; - }` - ); + }`); expect(transform(source)).toBe(source); }); it("should not remove nested var-assignments if referenced before", () => { - const source = unpad( - ` + const source = unpad(` function foo() { aa = 3; var { a: aa, b: bb } = undefined; - }` - ); + }`); expect(transform(source)).toBe(source); }); it("should not remove if lval is reference before via a function", () => { - const source = unpad( - ` + const source = unpad(` function foo() { bar(); var x = undefined; @@ -144,14 +121,12 @@ describe("transform-remove-undefined-plugin", () => { function bar() { x = 3; } - }` - ); + }`); expect(transform(source)).toBe(source); }); it("should remove if not referenced in any way before", () => { - const source = unpad( - ` + const source = unpad(` function foo() { var x = undefined; bar(); @@ -159,10 +134,8 @@ describe("transform-remove-undefined-plugin", () => { function bar() { x = 3; } - }` - ); - const expected = unpad( - ` + }`); + const expected = unpad(` function foo() { var x; bar(); @@ -170,14 +143,12 @@ describe("transform-remove-undefined-plugin", () => { function bar() { x = 3; } - }` - ); + }`); expect(transform(source)).toBe(expected); }); it("should not remove on mutually recursive function", () => { - const source = unpad( - ` + const source = unpad(` function foo() { a(); var c = undefined; @@ -188,72 +159,57 @@ describe("transform-remove-undefined-plugin", () => { a(); c = 3; } - }` - ); + }`); expect(transform(source)).toBe(source); }); it("should not remove if rval has side effects", () => { - const source = unpad( - ` + const source = unpad(` function foo() { var a = void b(); return void bar(); - }` - ); + }`); expect(transform(source)).toBe(source); }); it("should remove from sequence expressions", () => { - const source = unpad( - ` + const source = unpad(` a = b, void 0, b = c, d.e.f(), void 0, hello.world(); - ` - ); - const expected = unpad( - ` + `); + const expected = unpad(` a = b, b = c, d.e.f(), hello.world(); - ` - ); + `); expect(transform(source)).toBe(expected); }); it("should NOT remove last undefined from sequence expressions", () => { - const source = unpad( - ` + const source = unpad(` if (foo.bar(), void 0) { foo.baz(); } function bar() { return a.b(), void 0; } - ` - ); + `); expect(transform(source)).toBe(source); }); it("should remove last undefined from sequence expressions if safe", () => { - const source = unpad( - ` + const source = unpad(` a = b, void 0, b = c, d.e.f(), void 0, hello.world(), void 0; - ` - ); - const expected = unpad( - ` + `); + const expected = unpad(` a = b, b = c, d.e.f(), hello.world(); - ` - ); + `); expect(transform(source)).toBe(expected); }); it("should remove not remove local undefined", () => { - const source = unpad( - ` + const source = unpad(` function foo(undefined) { a = b, undefined; return undefined; - }` - ); + }`); expect(transform(source)).toBe(source); }); }); diff --git a/packages/babel-plugin-transform-simplify-comparison-operators/__tests__/strict-equals-test.js b/packages/babel-plugin-transform-simplify-comparison-operators/__tests__/strict-equals-test.js index e75dd7306..9467c9138 100644 --- a/packages/babel-plugin-transform-simplify-comparison-operators/__tests__/strict-equals-test.js +++ b/packages/babel-plugin-transform-simplify-comparison-operators/__tests__/strict-equals-test.js @@ -24,54 +24,42 @@ describe("simplify-comparison-operators-plugin", () => { }); it("should comparison operations 2", () => { - const source = unpad( - ` + const source = unpad(` var x = null; x === null; - ` - ); - const expected = unpad( - ` + `); + const expected = unpad(` var x = null; x == null; - ` - ); + `); expect(transform(source)).toBe(expected); }); it("should not simplify comparison", () => { - const source = unpad( - ` + const source = unpad(` var x; x === null; - ` - ); - const expected = unpad( - ` + `); + const expected = unpad(` var x; x === null; - ` - ); + `); expect(transform(source)).toBe(expected); }); it("should not simplify comparison 2", () => { - const source = unpad( - ` + const source = unpad(` var x; if (wow) x = foo(); x === null; - ` - ); - const expected = unpad( - ` + `); + const expected = unpad(` var x; if (wow) x = foo(); x === null; - ` - ); + `); expect(transform(source)).toBe(expected); }); diff --git a/packages/babel-preset-babili/__tests__/preset-tests.js b/packages/babel-preset-babili/__tests__/preset-tests.js index 9694a4951..6e6fb4324 100644 --- a/packages/babel-preset-babili/__tests__/preset-tests.js +++ b/packages/babel-preset-babili/__tests__/preset-tests.js @@ -14,8 +14,7 @@ function transform(code, options = {}, sourceType = "script") { describe("preset", () => { // https://github.com/babel/babili/issues/122 it("should fix issue#122", () => { - const source = unpad( - ` + const source = unpad(` function foo() { var a, b, c; if (a) { @@ -29,70 +28,54 @@ describe("preset", () => { } } } - ` - ); - const expected = unpad( - ` + `); + const expected = unpad(` function foo() { var d, a, b; d ? a && b : a || b; } - ` - ); + `); expect(transform(source)).toBe(expected); }); it("should fix remove comments", () => { - const source = unpad( - ` + const source = unpad(` var asdf = 1; // test - ` - ); - const expected = unpad( - ` + `); + const expected = unpad(` var asdf = 1; - ` - ); + `); expect(transform(source)).toBe(expected); }); it("should keep license/preserve annotated comments", () => { - const source = unpad( - ` + const source = unpad(` /* @license */ var asdf = 1; - ` - ); - const expected = unpad( - ` + `); + const expected = unpad(` /* @license */ var asdf = 1; - ` - ); + `); expect(transform(source)).toBe(expected); }); it("should fix issue#385 - impure if statements with Sequence and DCE", () => { - const source = unpad( - ` + const source = unpad(` a = b; c = d; if (false) { const x = y } - ` - ); - const expected = unpad( - ` + `); + const expected = unpad(` a = b, c = d; - ` - ); + `); expect(transform(source)).toBe(expected); }); it("should fix issue#402 - lifting var decl & DCE", () => { - const source = unpad( - ` + const source = unpad(` function a() { if (0) { for (var i;;) { @@ -101,20 +84,16 @@ describe("preset", () => { } } a(); - ` - ); - const expected = unpad( - ` + `); + const expected = unpad(` function a() {} a(); - ` - ); + `); expect(transform(source)).toBe(expected); }); it("should fix issue#425 - mangles the alaises from builtins transform", () => { - const source = unpad( - ` + const source = unpad(` function a (){ const d = Math.max(foo, bar); function b() { @@ -124,17 +103,14 @@ describe("preset", () => { Math.max(foo, bar) * Math.floor(baz); } } - ` - ); - const expected = unpad( - ` + `); + const expected = unpad(` function a() { var a = Math.floor, b = Math.max; b(foo, bar); } - ` - ); + `); expect(transform(source)).toBe(expected); }); diff --git a/packages/gulp-babili/__tests__/gulp-babili-test.js b/packages/gulp-babili/__tests__/gulp-babili-test.js index 1fc54a44f..88a6d36ef 100644 --- a/packages/gulp-babili/__tests__/gulp-babili-test.js +++ b/packages/gulp-babili/__tests__/gulp-babili-test.js @@ -12,14 +12,12 @@ describe("gulp-babili", () => { return new Promise((resolve, reject) => { const stream = gulpBabili(); - const source = unpad( - ` + const source = unpad(` function foo() { const a = 10; return a; } - ` - ); + `); const expected = "function foo(){return 10}"; stream.on("data", function(file) { @@ -47,13 +45,11 @@ describe("gulp-babili", () => { } }); - const source = unpad( - ` + const source = unpad(` function foo(bar, baz) { return bar + baz; } - ` - ); + `); const expected = "function foo(bar,a){return bar+a}"; stream.on("data", function(file) { @@ -197,8 +193,7 @@ describe("gulp-babili", () => { } ); - const source = unpad( - ` + const source = unpad(` /** * @license * throw away @@ -219,8 +214,7 @@ describe("gulp-babili", () => { } test(); } - ` - ); + `); stream.on("data", function(file) { expect(file.contents.toString()).toMatchSnapshot();