diff --git a/lib/compress.js b/lib/compress.js index 8fb39992..9779d656 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -1249,7 +1249,14 @@ merge(Compressor.prototype, { return true; } if (node instanceof AST_Defun && (node === stat || !compressor.has_directive("use strict"))) { - target.push(node); + target.push(node === stat ? node : make_node(AST_Var, node, { + definitions: [ + make_node(AST_VarDef, node, { + name: make_node(AST_SymbolVar, node.name, node.name), + value: null + }) + ] + })); return true; } if (node instanceof AST_Scope) { @@ -2388,7 +2395,7 @@ merge(Compressor.prototype, { } if (hoist_funs && node instanceof AST_Defun && !(tt.parent() instanceof AST_Export) - && (tt.parent() === self || !compressor.has_directive("use strict"))) { + && tt.parent() === self) { hoisted.push(node); return make_node(AST_EmptyStatement, node); } diff --git a/test/compress/dead-code.js b/test/compress/dead-code.js index d04ebb3d..e1536652 100644 --- a/test/compress/dead-code.js +++ b/test/compress/dead-code.js @@ -54,12 +54,12 @@ dead_code_2_should_warn: { x = 10; throw new Error("foo"); var x; - function g(){}; + var g; } f(); } expect_stdout: true - node_version: "<=4" + node_version: ">=6" } dead_code_2_should_warn_strict: { @@ -99,7 +99,7 @@ dead_code_2_should_warn_strict: { f(); } expect_stdout: true - node_version: "=4" + node_version: ">=4" } dead_code_constant_boolean_should_warn_more: { @@ -126,7 +126,7 @@ dead_code_constant_boolean_should_warn_more: { } expect: { var foo; - function bar() {} + var bar; // nothing for the while // as for the for, it should keep: var x = 10, y; @@ -134,7 +134,7 @@ dead_code_constant_boolean_should_warn_more: { bar(); } expect_stdout: true - node_version: "<=4" + node_version: ">=6" } dead_code_constant_boolean_should_warn_more_strict: { @@ -217,7 +217,7 @@ dead_code_const_declaration: { var unused; const CONST_FOO = !1; var moo; - function bar() {} + var bar; } expect_stdout: true } @@ -245,7 +245,7 @@ dead_code_const_annotation: { var unused; var CONST_FOO_ANN = !1; var moo; - function bar() {} + var bar; } expect_stdout: true } @@ -312,7 +312,7 @@ dead_code_const_annotation_complex_scope: { var CONST_FOO_ANN = !1; var unused_var_2; var moo; - function bar() {} + var bar; var beef = 'good'; var meat = 'beef'; var pork = 'bad'; diff --git a/test/compress/functions.js b/test/compress/functions.js index ce8bab80..68830364 100644 --- a/test/compress/functions.js +++ b/test/compress/functions.js @@ -185,25 +185,25 @@ hoist_funs: { console.log(6, typeof f, typeof g); } expect: { - function f() {} function g() {} console.log(1, typeof f, typeof g); if (console.log(2, typeof f, typeof g)) console.log(3, typeof f, typeof g); else { console.log(4, typeof f, typeof g); + function f() {} console.log(5, typeof f, typeof g); } console.log(6, typeof f, typeof g); } expect_stdout: [ - "1 'function' 'function'", - "2 'function' 'function'", + "1 'undefined' 'function'", + "2 'undefined' 'function'", "4 'function' 'function'", "5 'function' 'function'", "6 'function' 'function'", ] - node_version: "<=4" + node_version: ">=6" } hoist_funs_strict: { @@ -243,5 +243,5 @@ hoist_funs_strict: { "5 'function' 'function'", "6 'undefined' 'function'", ] - node_version: "=4" + node_version: ">=4" } diff --git a/test/compress/issue-1052.js b/test/compress/issue-1052.js index e3dc7322..3eb00309 100644 --- a/test/compress/issue-1052.js +++ b/test/compress/issue-1052.js @@ -134,9 +134,8 @@ defun_hoist_funs: { expect: { function e() { function f() {} - function g() {} function h() {} - !window; + if (window) function g() {} } } }