More JSLint, less JSWTF
The JavaScript community has been enjoying WTFJS this week. Seasoned JS programmers know there are a few weird bumps and traps in the language if you’re not watching. You can take some of the burden off yourself, however, by linting your JavaScript.
There are many JavaScript linting programs out there. As a counterpart to his book The Good Parts, Douglas Crockford maintains a tool called JSLint. Matthias Miller was inspired by this, and maintains a similar but less picky tool he confusingly calls JavaScript Lint. If you want to go even further, there is the Google Closure Compiler.
Take this WTFJS caused by implied semicolons:
function laugh()
{
return
{
haha: "ha!"
};
}
laugh();
// returns undefined
The lint programs will warn you (in different ways) that your return gets an implicit semicolon, and the object with “haha” in it never gets executed.
Or this one:
parseInt('06'); // 6
parseInt('08'); // 0
Lint will tell you that you can’t use parseInt safely without specifying that you want base-10, or wrapping it in something that assumes base-10.
They’ll also catch things like this:
{
"ping": "pong",
"foo": "bar",
} // IE says parse error on the last line of your file.
// (It doesn't understand trailing commas, yay!)
and this:
if (foo == bar) {
{
// You doubled a bracket.
// Now need to round-trip to your browser to find out.
}
You can save yourself from these wtfs and parse errors. All you need to do is have your text editor run a lint program instantly when you save your JavaScript. The excellent JavaScript Tools Textmate bundle does this for you using JavaScript Lint1, and is easy to set up. If Textmate isn’t your favourite or you prefer the more aggressive JSLint, you can set up your own save macro on any editor worth its salt.
- Even though JavaScript Tools says it’s using JSLint, it’s actually using the more lax JavaScript Lint. [↩]
February 22, 2010
11:51 pm
Even as a total non-Javascript-programmer I’ve been enjoying it, though the gigantic “I see what you did there” image at the top of every single freakin’ page is starting to get on my nerves a little.
I’m not sure exactly what octal’s deal is, but Javascript is not the only language that has succumbed to its charms. C/C++ do a little better, GCC (at least, probably others, I’m not rebooting into windows to check MSVC) will validate individual digits for you so you can use that EXACT example, but…
printf(“%d\n”, 012); // prints ‘10′
February 23, 2010
8:44 am
JSLint / Javascript Lint have saved my ass a bunch of times. I also use them for syntax checking. IE will give you “expected }” on the last line of the file… JSLint will give you a reference to where the problem actually is. I wrote a how-to on getting them running in Notepad++ here: http://stackoverflow.com/questions/1046810/using-jslint-in-notepad/1046826#1046826 . I hooked those scripts up to “F9″ and run that before I even switch to the browser.
February 23, 2010
10:35 am
Nigel: Indeed. The trailing-zero-means-octal is a lot more dangerous when you’re using strings though, since they can be user input like a credit card expiry year. I guess the confusing part is:
“09″ == 9; // true
parseInt(“09″) == 9; // false
Andy: The nice thing about it running on save is that it can pop up if there’s a problem, and be silent if not. Pretty nice.