Parsing Requires Two Steps #29

GarrettS opened this Issue Feb 19, 2011 · 6 comments


None yet
4 participants

The following input is valid and should not result in error. Yet JSHint calls it a fail.

function a(){}

if(!b) {
var b = 1;

Result: failed.
'a' is not defined.

Even when unchecked "Require variables to be declared before usage", the result is still an error.

The first is not an error; a is defined and is available to be called. The second is not an error because during lexical mode if b is not a global property, then it is created and given value undefined. Then, when Statements are evaluated, the if(!b) is evalueated as false and b is given value 1.

I think JSHint should not

Agreed, accidentally posted a duplicate, think we can get a better title?


valueof commented Mar 20, 2011

Fixed in master. By default it won't complain but you can roll back to the old behavior by using the new latedef option.

valueof closed this Mar 20, 2011

baryluk commented Dec 28, 2011

Can you change default on web page? It have option to enable/disable, 'When variable is undefined', but I think having functions declared later is not considered very bad practice, and sometimes unavoidable. I cannot uncheck this box however, because I would like still to check for things declared using var x;, and used before their definition:

function a() {
var b = 5;

This is actually subcase of original bug report.

Now unchecking, 'When variable is undefined' option, I do not have false positive "'a' is not defined", but also I do not have true positive "'b' is not defined'.

It makes jshint almost useless (for example for me), if you have lots of forward references in dense code (because filtering out manually functions names from report will take time).

Here is my saved report:
In fact in my case it reports this false "'f' is not defined", despite disabling option about undefined variables.

So my proposition is to split "latedef" option into two separate ones. latedef functions and latedef non-functions.

baryluk commented Dec 28, 2011

As of importance of this issue, let me remember you, that almost similar bug is 3 or 4 times duplicated in issues list! And I still do not consider it fixed.


valueof commented Dec 28, 2011

For simplicity, we don't provide UI for all options on the website, you can always use /*jshint latedef:true */ there though.

baryluk commented Dec 28, 2011

I think there is important distinction beetween, later definitions of variables, and later definitions of functions! They are even working in different ways in language (functions are parsed and added to scope before anyexecution takes place, and can be called, but variables declared with "var" will also be available before declaration place, but will be useless, because will contain "undefined").

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment