Tracker: Software Changes

5 Honor scoping on variable redeclaration warning - ID: 1516656
Last Update: Settings changed ( matthiasmiller )

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.


Cam Woods ( cam_woods ) - 2006-07-03 15:07:46 PDT

5

Closed

None

Nobody/Anonymous

None

None

Public


Comments ( 8 )

Date: 2009-10-22 06:32:08 PDT
Sender: matthiasmillerProject Admin

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


Date: 2007-01-17 12:18:15 PST
Sender: cam_woods


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.


Date: 2007-01-15 10:03:39 PST
Sender: nobody

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.


Date: 2007-01-01 06:21:25 PST
Sender: alfonsoml


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.



Date: 2006-07-15 09:31:36 PDT
Sender: cam_woods

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


Date: 2006-07-15 08:30:20 PDT
Sender: matthiasmillerProject Admin

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".


Date: 2006-07-10 06:05:12 PDT
Sender: matthiasmillerProject Admin

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.


Date: 2006-07-07 08:49:35 PDT
Sender: myreddice

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.


Attached File

No Files Currently Attached

Changes ( 3 )

Field Old Value Date By
status_id Open 2009-10-22 06:32:10 PDT matthiasmiller
allow_comments 1 2009-10-22 06:32:10 PDT matthiasmiller
close_date - 2009-10-22 06:32:10 PDT matthiasmiller