« Another improvement related to 'var'. | Main | Mascara Eclipse Plugin - alpha preview »
Thursday
Nov252010

Mascara 1.7 released

Download here!

I sometimes forget to post on the blog when a new version is released - sorry about that! We are now up to version 1.7. Everyone should upgrade, since each new release contains bug-fixes and improvements. (The Eclipse plug-in is also regularly updated, but should still be considered alpha stage. You will get the updates automatically through Eclipse, if you have it installed.)

Mascara version 1.7 in particular have some improvements which eliminates groups of subtle bugs which traditionally plagues JavaScript development.

The first one I will mention is the handling of the "var" statements.

In JavaScript, the var statement declares a local variable. If you refer to a variable which have not been declared with "var" in the current function, the runtime looks in the outer scopes and the global scope.

Now the subtle issue: A variable is resolved to a local var even if the "var"-statement occurs after the reference to the var.

Say:

function f() {
    location = "somewhere"; // does this assign to a local or global variable?

    {..lots of code...}

    var location; // this statement means that first line refers to a local.
}

The 'var' may even be defined deep inside nested statement block like ifs and loops - it still changes the meaning of the "location" in the first line of the function.

This is obviously bad. It breaks the basic assumption that you shouldn't have to look forward inside a function to determine if a variable is a local or global. Furthermore, no other language works like JavaScript in this case. Most languages will simply not allow you to use a local variable before it is declared. (Python does have semantics closer to JavaScript, but in a subtly different way which avoids the kind of bug JavaScript is prone to.)

The solution for this is simple: Mascara yields a warning if you read or assign to a local variable before it is declared with 'var'.

You may wonder why I made it just a warning which can be ignored, rather than a downright compiler error?

The reason is that some code (legacy code, third party libraries) may use this pattern for whatever reason, but still be thoroughly tested and work correctly. So you might not want to fix it, or you may want to fix it at your own lesiure, while still being able to use the code in the meantime.

Since it is a prime directive for Mascara to work well with libraries written in classic JavaScript, and allow a painless upgrade path from classic JavaScript, this is an important consideration.

Therefore the general principle for warning/error distinction in Mascara is that if something could just theoretically work if the developer knows what they're doing, it is just yields a warning. Errors is reserved for things which could never possibly work, like syntax errors or inconsistent types.

Anyway, hope you enjoy the improvements, and have fun coding.

PrintView Printer Friendly Version

EmailEmail Article to Friend