fix block-scoped function for ES6

fixes #1903
This commit is contained in:
alexlamsl
2017-05-27 19:13:09 +08:00
parent aa835eb0f6
commit 94d2aeee89
4 changed files with 23 additions and 17 deletions

View File

@@ -1249,7 +1249,14 @@ merge(Compressor.prototype, {
return true; return true;
} }
if (node instanceof AST_Defun && (node === stat || !compressor.has_directive("use strict"))) { 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; return true;
} }
if (node instanceof AST_Scope) { if (node instanceof AST_Scope) {
@@ -2388,7 +2395,7 @@ merge(Compressor.prototype, {
} }
if (hoist_funs && node instanceof AST_Defun if (hoist_funs && node instanceof AST_Defun
&& !(tt.parent() instanceof AST_Export) && !(tt.parent() instanceof AST_Export)
&& (tt.parent() === self || !compressor.has_directive("use strict"))) { && tt.parent() === self) {
hoisted.push(node); hoisted.push(node);
return make_node(AST_EmptyStatement, node); return make_node(AST_EmptyStatement, node);
} }

View File

@@ -54,12 +54,12 @@ dead_code_2_should_warn: {
x = 10; x = 10;
throw new Error("foo"); throw new Error("foo");
var x; var x;
function g(){}; var g;
} }
f(); f();
} }
expect_stdout: true expect_stdout: true
node_version: "<=4" node_version: ">=6"
} }
dead_code_2_should_warn_strict: { dead_code_2_should_warn_strict: {
@@ -99,7 +99,7 @@ dead_code_2_should_warn_strict: {
f(); f();
} }
expect_stdout: true expect_stdout: true
node_version: "=4" node_version: ">=4"
} }
dead_code_constant_boolean_should_warn_more: { dead_code_constant_boolean_should_warn_more: {
@@ -126,7 +126,7 @@ dead_code_constant_boolean_should_warn_more: {
} }
expect: { expect: {
var foo; var foo;
function bar() {} var bar;
// nothing for the while // nothing for the while
// as for the for, it should keep: // as for the for, it should keep:
var x = 10, y; var x = 10, y;
@@ -134,7 +134,7 @@ dead_code_constant_boolean_should_warn_more: {
bar(); bar();
} }
expect_stdout: true expect_stdout: true
node_version: "<=4" node_version: ">=6"
} }
dead_code_constant_boolean_should_warn_more_strict: { dead_code_constant_boolean_should_warn_more_strict: {
@@ -217,7 +217,7 @@ dead_code_const_declaration: {
var unused; var unused;
const CONST_FOO = !1; const CONST_FOO = !1;
var moo; var moo;
function bar() {} var bar;
} }
expect_stdout: true expect_stdout: true
} }
@@ -245,7 +245,7 @@ dead_code_const_annotation: {
var unused; var unused;
var CONST_FOO_ANN = !1; var CONST_FOO_ANN = !1;
var moo; var moo;
function bar() {} var bar;
} }
expect_stdout: true expect_stdout: true
} }
@@ -312,7 +312,7 @@ dead_code_const_annotation_complex_scope: {
var CONST_FOO_ANN = !1; var CONST_FOO_ANN = !1;
var unused_var_2; var unused_var_2;
var moo; var moo;
function bar() {} var bar;
var beef = 'good'; var beef = 'good';
var meat = 'beef'; var meat = 'beef';
var pork = 'bad'; var pork = 'bad';

View File

@@ -185,25 +185,25 @@ hoist_funs: {
console.log(6, typeof f, typeof g); console.log(6, typeof f, typeof g);
} }
expect: { expect: {
function f() {}
function g() {} function g() {}
console.log(1, typeof f, typeof g); console.log(1, typeof f, typeof g);
if (console.log(2, typeof f, typeof g)) if (console.log(2, typeof f, typeof g))
console.log(3, typeof f, typeof g); console.log(3, typeof f, typeof g);
else { else {
console.log(4, typeof f, typeof g); console.log(4, typeof f, typeof g);
function f() {}
console.log(5, typeof f, typeof g); console.log(5, typeof f, typeof g);
} }
console.log(6, typeof f, typeof g); console.log(6, typeof f, typeof g);
} }
expect_stdout: [ expect_stdout: [
"1 'function' 'function'", "1 'undefined' 'function'",
"2 'function' 'function'", "2 'undefined' 'function'",
"4 'function' 'function'", "4 'function' 'function'",
"5 'function' 'function'", "5 'function' 'function'",
"6 'function' 'function'", "6 'function' 'function'",
] ]
node_version: "<=4" node_version: ">=6"
} }
hoist_funs_strict: { hoist_funs_strict: {
@@ -243,5 +243,5 @@ hoist_funs_strict: {
"5 'function' 'function'", "5 'function' 'function'",
"6 'undefined' 'function'", "6 'undefined' 'function'",
] ]
node_version: "=4" node_version: ">=4"
} }

View File

@@ -134,9 +134,8 @@ defun_hoist_funs: {
expect: { expect: {
function e() { function e() {
function f() {} function f() {}
function g() {}
function h() {} function h() {}
!window; if (window) function g() {}
} }
} }
} }