Skip to content

Commit

Permalink
fix hoist_funs on block-scoped function under "use strict" (#2013)
Browse files Browse the repository at this point in the history
Technically not part of ES5, but commonly used code exists in the wild.
  • Loading branch information
alexlamsl authored May 27, 2017
1 parent 95094b9 commit 7b13159
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 2 deletions.
5 changes: 3 additions & 2 deletions lib/compress.js
Original file line number Diff line number Diff line change
Expand Up @@ -2251,11 +2251,12 @@ merge(Compressor.prototype, {
dirs.push(node);
return make_node(AST_EmptyStatement, node);
}
if (node instanceof AST_Defun && hoist_funs) {
if (hoist_funs && node instanceof AST_Defun
&& (tt.parent() === self || !compressor.has_directive("use strict"))) {
hoisted.push(node);
return make_node(AST_EmptyStatement, node);
}
if (node instanceof AST_Var && hoist_vars) {
if (hoist_vars && node instanceof AST_Var) {
node.definitions.forEach(function(def){
vars.set(def.name.name, def);
++vars_found;
Expand Down
78 changes: 78 additions & 0 deletions test/compress/functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,3 +167,81 @@ function_returning_constant_literal: {
}
expect_stdout: "Hello there"
}

hoist_funs: {
options = {
hoist_funs: true,
}
input: {
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);
}
function g() {}
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);
console.log(5, typeof f, typeof g);
}
console.log(6, typeof f, typeof g);
}
expect_stdout: [
"1 'function' 'function'",
"2 'function' 'function'",
"4 'function' 'function'",
"5 'function' 'function'",
"6 'function' 'function'",
]
node_version: "<=4"
}

hoist_funs_strict: {
options = {
hoist_funs: true,
}
input: {
"use strict";
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);
}
function g() {}
console.log(6, typeof f, typeof g);
}
expect: {
"use strict";
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 'undefined' 'function'",
"2 'undefined' 'function'",
"4 'function' 'function'",
"5 'function' 'function'",
"6 'undefined' 'function'",
]
node_version: "=4"
}

0 comments on commit 7b13159

Please sign in to comment.