From 919b2733ab52d3072bb4276210fe442bc0ade5fd Mon Sep 17 00:00:00 2001 From: Mihai Bazon Date: Fri, 7 Sep 2012 15:18:32 +0300 Subject: always keep declarations found in unreachable code a few more tests and some cleanups. --- test/compress/conditionals.js | 74 +++++++++++++++++++++++++++++++++++++++++++ test/compress/dead-code.js | 30 ++++++++++++++++++ test/run-tests.js | 6 ++-- 3 files changed, 108 insertions(+), 2 deletions(-) create mode 100644 test/compress/conditionals.js (limited to 'test') diff --git a/test/compress/conditionals.js b/test/compress/conditionals.js new file mode 100644 index 00000000..ba5e674d --- /dev/null +++ b/test/compress/conditionals.js @@ -0,0 +1,74 @@ +ifs_1: { + options = { + conditionals: true + }; + input: { + if (foo) bar(); + if (!foo); else bar(); + if (foo); else bar(); + if (foo); else; + } + expect: { + foo&&bar(); + foo&&bar(); + foo||bar(); + foo; + } +} + +ifs_2: { + options = { + conditionals: true + }; + input: { + if (foo) { + x(); + } else if (bar) { + y(); + } else if (baz) { + z(); + } + + if (foo) { + x(); + } else if (bar) { + y(); + } else if (baz) { + z(); + } else { + t(); + } + } + expect: { + foo ? x() : bar ? y() : baz && z(); + foo ? x() : bar ? y() : baz ? z() : t(); + } +} + +ifs_3_should_warn: { + options = { + conditionals : true, + dead_code : true, + evaluate : true, + booleans : true + }; + input: { + if (x && !(x + "1") && y) { // 1 + var qq; + foo(); + } else { + bar(); + } + + if (x || !!(x + "1") || y) { // 2 + foo(); + } else { + var jj; + bar(); + } + } + expect: { + var qq; bar(); // 1 + var jj; foo(); // 2 + } +} diff --git a/test/compress/dead-code.js b/test/compress/dead-code.js index e84b4011..0fd066eb 100644 --- a/test/compress/dead-code.js +++ b/test/compress/dead-code.js @@ -57,3 +57,33 @@ dead_code_2_should_warn: { } } } + +dead_code_constant_boolean_should_warn_more: { + options = { + dead_code : true, + loops : true, + booleans : true, + conditionals : true, + evaluate : true + }; + input: { + while (!((foo && bar) || (x + "0"))) { + console.log("unreachable"); + var foo; + function bar() {} + } + for (var x = 10; x && (y || x) && (!typeof x); ++x) { + asdf(); + foo(); + var moo; + } + } + expect: { + var foo; + function bar() {} + // nothing for the while + // as for the for, it should keep: + var x = 10; + var moo; + } +} diff --git a/test/run-tests.js b/test/run-tests.js index b70e2291..158c9d35 100755 --- a/test/run-tests.js +++ b/test/run-tests.js @@ -119,8 +119,10 @@ function parse_test(file) { }) ); var stat = node.body; - if (stat instanceof U.AST_BlockStatement && stat.body.length == 1) - stat = stat.body[0]; + if (stat instanceof U.AST_BlockStatement) { + if (stat.body.length == 1) stat = stat.body[0]; + else if (stat.body.length == 0) stat = new U.AST_EmptyStatement(); + } test[node.label.name] = stat; return true; } -- cgit v1.2.3