HoriSun / closure-compiler

Automatically exported from code.google.com/p/closure-compiler
0 stars 0 forks source link

function definition in if block is incorrectly minified (function somename(){} --> var somename = function(){} ) #1307

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. minify and run this code:
function hello(name) {
  alert('Hello, ' + name);
  if(1==1){
    setTimeout(function(){hi()},50);
    if(2==2){hi();}
    function hi(){
       alert("hi");
       someFunc("something");
    }
  }
}

function someFunc(abc){
    console.log(abc)
}
hello("name");

2. This is the minified code I get:
function hello(a){alert("Hello, "+a);setTimeout(function(){b()},50);b();var 
b=function(){alert("hi");someFunc("something")}}function 
someFunc(a){console.log(a)};

What is the expected output? What do you see instead?

Expected: several alert windows, no javascript error

Reality: Type Error caused by calling b() in time when var b=function().. is 
not yet initialized

What version of the product are you using? On what operating system?
I verified the bug on your online demo: http://closure-compiler.appspot.com/home

Please provide any additional information below.

function someName(){} is minified as var b= function(){} and that initializes 
it in runtime. So if you call the function in block before, it's not yet 
defined. See sample and minified code

Original issue reported on code.google.com by roman.ma...@gmail.com on 16 Apr 2014 at 4:02

GoogleCodeExporter commented 9 years ago
when reading after myself it might not be obvious so the culprit is:

function hi(){
       alert("hi");
       someFunc("something");
    }

which becomes

var b=function(){alert("hi");someFunc("something")}

Original comment by roman.ma...@gmail.com on 16 Apr 2014 at 4:06

GoogleCodeExporter commented 9 years ago
I think this is why we have the JSC_BAD_FUNCTION_DECLARATION warning. I believe 
if you rewrite your code such that you don't see that warning, then it should 
solve the problem.

Original comment by tbreisac...@google.com on 17 Apr 2014 at 7:01

GoogleCodeExporter commented 9 years ago
I believe this is correct behavior. Notice that the following code throws an 
exception on firefox:

if (true) { f(1);  function f() {} }

because, according to the spec, it's equivalent to:

if (true) { f(1);  var f = function f() {} }

Original comment by Nicholas.J.Santos on 18 Apr 2014 at 2:06

GoogleCodeExporter commented 9 years ago
we should make sure that the BAD_FUNCTION_DECLARATION warning shows up in the 
default mode. i think it's only VERBOSE mode currently.

Original comment by Nicholas.J.Santos on 18 Apr 2014 at 2:08

GoogleCodeExporter commented 9 years ago
Issue tracking has been migrated to github. Please make any further comments on 
this issue through https://github.com/google/closure-compiler/issues

Original comment by blic...@google.com on 1 May 2014 at 6:31

GoogleCodeExporter commented 9 years ago

Original comment by blic...@google.com on 1 May 2014 at 6:34