-analyze question

Mar 15, 2011 at 3:50 PM

Howdy,

I'm using AjaxMin (version 4.14) with the -analyze option and I have a question about a coding error that is reported.

What appears to be happening is that AjaxMin will report a 'coding error JS1111' if the loop control variable is defined immediately prior to a for loop.

For example, when I run "AjaxMin.exe - analyze test.js" where test.js is below

function temp()
{
    var i = 0;
    var a = 0;
    for (i = 0; i < 3; i++)
    {
        a = a + i;
    }
}

Minifying file 'test.js'...
test.js(5,10-11): coding error JS1111: 'i' is already defined: i

Global Objects
  temp [global function]

 

Now if I add a statement between the variable declaration and the for, there are no errors defined

function temp()
{
    var i = 0;
    var a = 0;
    a = 5;
    for (i = 0; i < 3; i++)
    {
        a = a + i;
    }
}

Minifying file 'test.js'...
Global Objects
  temp [global function]

 

Thank you for this great tool.

-jimC

Coordinator
Mar 15, 2011 at 3:58 PM

Hmmm… that shouldn’t error like that. Now, if the for-statement had the “var” keyword in its initializer, then yes – it would throw that error. But your snippet doesn’t. Let me dig into this a bit and see what's going on here.

Coordinator
Mar 15, 2011 at 10:40 PM

Yup, found the problem. When moving the var-statement into the initializer of the for-statement, I was creating a new vardecl node from the existing assignment -- but when creating a vardecl, it will throw that warning if the field is already defined. But in this situation we know it already exists because we're moving the var-statement that originally defined it. Added a way to suppress that warning under those circumstances. This fix will be in the next release.

Mar 16, 2011 at 3:26 PM

Thank you for the quick reply.

I'm evaluating incorporating "AjaxMin -analyze" into our js code review process. Your static code analysis provides a more indepth evaluation than does jsLint.

-jimC

Coordinator
Mar 19, 2011 at 4:42 AM

I just released version 4.15, which has this fix in it.