From df671b0d746ffecf6ad158478230ff3732301871 Mon Sep 17 00:00:00 2001 From: kzc Date: Sun, 10 Apr 2016 15:41:38 -0400 Subject: [PATCH] Fix warnings for referenced non-hoisted functions. Fixes #1034 Also added `expect_warnings` functionality to test framework. --- lib/compress.js | 4 +++- test/compress/issue-1034.js | 36 ++++++++++++++++++++++++++++++++++++ test/run-tests.js | 25 ++++++++++++++++++++++++- 3 files changed, 63 insertions(+), 2 deletions(-) create mode 100644 test/compress/issue-1034.js diff --git a/lib/compress.js b/lib/compress.js index d42f84d1..e47be97b 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -801,7 +801,9 @@ merge(Compressor.prototype, { }; function extract_declarations_from_unreachable_code(compressor, stat, target) { - compressor.warn("Dropping unreachable code [{file}:{line},{col}]", stat.start); + if (!(stat instanceof AST_Defun)) { + compressor.warn("Dropping unreachable code [{file}:{line},{col}]", stat.start); + } stat.walk(new TreeWalker(function(node){ if (node instanceof AST_Definitions) { compressor.warn("Declarations in unreachable code! [{file}:{line},{col}]", node.start); diff --git a/test/compress/issue-1034.js b/test/compress/issue-1034.js new file mode 100644 index 00000000..c3838323 --- /dev/null +++ b/test/compress/issue-1034.js @@ -0,0 +1,36 @@ +non_hoisted_function_def_after_return: { + options = { + hoist_funs: false, dead_code: true, conditionals: true, comparisons: true, + evaluate: true, booleans: true, loops: true, unused: true, keep_fargs: true, + if_return: true, join_vars: true, cascade: true, side_effects: true + } + input: { + function foo(x) { + if (x) { + return bar(); + not_called1(); + } else { + return baz(); + not_called2(); + } + function bar() { return 7; } + return not_reached; + function UnusedFunction() {} + function baz() { return 8; } + } + } + expect: { + function foo(x) { + return x ? bar() : baz(); + function bar() { return 7 } + function baz() { return 8 } + } + } + expect_warnings: [ + "WARN: Dropping unreachable code [test/compress/issue-1034.js:11,16]", + "WARN: Dropping unreachable code [test/compress/issue-1034.js:14,16]", + "WARN: Dropping unreachable code [test/compress/issue-1034.js:17,12]", + "WARN: Dropping unused function UnusedFunction [test/compress/issue-1034.js:18,21]" + ] +} + diff --git a/test/run-tests.js b/test/run-tests.js index fcb1b375..72277b8c 100755 --- a/test/run-tests.js +++ b/test/run-tests.js @@ -88,6 +88,14 @@ function run_compress_tests() { var options = U.defaults(test.options, { warnings: false }); + var warnings_emitted = []; + var original_warn_function = U.AST_Node.warn_function; + if (test.expect_warnings) { + U.AST_Node.warn_function = function(text) { + warnings_emitted.push("WARN: " + text); + }; + options.warnings = true; + } var cmp = new U.Compressor(options, true); var output_options = test.beautify || {}; var expect; @@ -117,6 +125,21 @@ function run_compress_tests() { failures++; failed_files[file] = 1; } + else if (test.expect_warnings) { + U.AST_Node.warn_function = original_warn_function; + var expected_warnings = make_code(test.expect_warnings, { beautify: false }); + var actual_warnings = JSON.stringify(warnings_emitted); + actual_warnings = actual_warnings.split(process.cwd() + "/").join(""); + if (expected_warnings != actual_warnings) { + log("!!! failed\n---INPUT---\n{input}\n---EXPECTED WARNINGS---\n{expected_warnings}\n---ACTUAL WARNINGS---\n{actual_warnings}\n\n", { + input: input_code, + expected_warnings: expected_warnings, + actual_warnings: actual_warnings, + }); + failures++; + failed_files[file] = 1; + } + } } var tests = parse_test(path.resolve(dir, file)); for (var i in tests) if (tests.hasOwnProperty(i)) { @@ -168,7 +191,7 @@ function parse_test(file) { } if (node instanceof U.AST_LabeledStatement) { assert.ok( - node.label.name == "input" || node.label.name == "expect" || node.label.name == "expect_exact", + ["input", "expect", "expect_exact", "expect_warnings"].indexOf(node.label.name) >= 0, tmpl("Unsupported label {name} [{line},{col}]", { name: node.label.name, line: node.label.start.line,