cfjedimaster / brackets-jshint

Adds JSHint support to Brackets
MIT License
130 stars 41 forks source link

wrong '[variable-name] is already defined' warning #33

Closed TitanNano closed 10 years ago

TitanNano commented 10 years ago

Hey,

If you make something like this:

if(a == b)
  var x= 1;
else
  var x= 2;

console.log(x);

he will complain about redefining of x. But I think he should detect that x always only once will be defined.

luizfilipe commented 10 years ago

I think that is dafault behavior of JSHint, this bracket plugin dont change JSHint, only use it.

Javascript has function-level scope, diferent from other C-like languages that are block-level scope like Java and C#

For more information about this you can read this article: http://www.adequatelygood.com/JavaScript-Scoping-and-Hoisting.html

The correct code to by pass this JSHint warnings is

var x;
if (a == b) { 
  x = 1;
} else {
  x = 2;
}

console.log(x);
TitanNano commented 10 years ago

thats all true but the thing is that when "a != b", "var x= 1" never will be executed so "var x=2" will not be a redefinition.

luizfilipe commented 10 years ago

@TitanNano Javascript does not have block-scope level, only have function-scope level Independly wheather first or second statement will be executed, these two statements will share variable declarations.

TitanNano commented 10 years ago

I just entered this in my console:

var test= false;
if(test){
var x= 0; 
}else{ 
console.log(x)
}

"console.log" will print out 'undefined' because x never was defined!

luizfilipe commented 10 years ago

@TitanNano I understand your point of view, but jshint does not an interpreter at all, it's just a javascript analytic tool that dont parse code results, jshint only have the meaning to alert for the bad practices, if you do some code that have two variables declarations with the same name in the same function scope, jshint will warn them to you without take knowledge of the code semantics.

its just to keep maintenability of your code and following one way to code in js and avoid problems with readability

luizfilipe commented 10 years ago

moving to jshint/jshint#1471