#1 Honor scoping on variable redeclaration warning

closed
nobody
None
5
2009-10-22
2006-07-03
Cam Woods
No

I would like Lint to NOT report declaration of two
variables, with the same name and within two
different scopes of the same method, as a variable
redeclaration warning. If however, one of the scopes
were nested within the other, Lint SHOULD warn.

Discussion

  • Martin Harper

    Martin Harper - 2006-07-07

    Logged In: YES
    user_id=861973

    I believe that this is by design. Javascript is unlike C++,
    as it does not support scoping within methods. For example:

    function x() {
    if( true ) {
    var j = 'Am I in scope?';
    }
    alert( j ); // var j is still in scope here
    }

    will alert "Am I in scope?".

    The variable redeclaration warning is accurate, as a
    variable is indeed being redeclared.

     
  • Matthias Miller

    Matthias Miller - 2006-07-10

    Logged In: YES
    user_id=1357599

    The solution is to add a separate warning when one variable
    hides another variable in a parent scope. That would allow
    Cam Woods to enable this warning but to disable the
    same-scope warning.

     
  • Matthias Miller

    Matthias Miller - 2006-07-15

    Logged In: YES
    user_id=1357599

    The implementation is a bit interesting. For example:

    function foo() {
    function bar() {
    /* redeclared */
    var x;
    }

    var x;
    bar();
    }

    One solution would be to add two warnings: "variable x hides
    variable in parent scope" and "variable x is hidden in child
    scope".

     
  • Cam Woods

    Cam Woods - 2006-07-15

    Logged In: YES
    user_id=1549553

    Here is the error the MS DevStudio C# compiler generates
    when redeclaring lst2 where lst2 is also declared in a
    parent scope:

    "A local variable named 'lst2' cannot be declared in this
    scope because it would give a different meaning to 'lst2',
    which is already used in a 'parent or current' scope to
    denote something else"

    This seems a little wordy to me, but the right idea.
    Maybe something like:

    "Variable lst2 redeclares variable in parent scope. This
    may cause unexpected results in parent scope."

    Cam

     
  • Alfonso Martinez

    Logged In: YES
    user_id=1356422
    Originator: NO

    I think that although for Javascript different scopes doesn't exist in the same method it would be very useful to be sure that everything is fine if we could tell JSLint to behave like a C++ compiler with regards to scopes.

    now this function gives a warning because it says that i has been redeclared, but it we apply different scopes then everything is fine:
    function foo(a, b)
    {
    for(var i=0; i<a.length; i++)
    {
    //do something...
    /*jsl:pass*/
    }

    for(var i=0; i<b.length; i++)
    {
    //do other thing...
    /*jsl:pass*/
    }

    }

    On the other side this is OK now, but it doesn't look fine
    function foo(a, b)
    {
    if ( a==3)
    {
    var x = 8;
    }
    alert(x) ;
    }
    the variable x is created in the if block only in one case, but it's used always out of that block and that might be an unexpected situation.

    having some kind of +simulate_C_scope switch could help in finding some code that hasn't been written properly and also it could help with the task of updating existing code so it doesn't give any warning with code that will behave as expected in production.

     
  • Nobody/Anonymous

    Logged In: NO

    You do realize that Javascript does not create a new scope when you put code within { } braces, right? There are only two scopes: global and function. The only way to create a new scope within a function is to create a new [inner] function.

     
  • Cam Woods

    Cam Woods - 2007-01-17

    Logged In: YES
    user_id=1549553
    Originator: YES

    I do realize that now, but did not at the time I submitted the request. Even so, a warning like this (disableable) would be helpful because such a redeclaration would likely indicate a bug.

     
  • Matthias Miller

    Matthias Miller - 2009-10-22

    This has been implemented in the Python version (see trunk).

     
  • Matthias Miller

    Matthias Miller - 2009-10-22
    • status: open --> closed
     

Log in to post a comment.

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:

JavaScript is required for this form.





No, thanks