Tuesday, January 24, 2012

The Trailing Comma Bomb

Well, they got me again.  Those pesky trailing commas in JSON objects. If you don't know what I'm talking about, then you probably haven't been doing JavaScript in IE7, because Firefox, Chrome, and IE8 and up all ignore them. But IE7 blows up. Here's what it looks like:
var meal = {
  mainCourse: "roast beef",
  sideDish: "mashed potatoes",
  dessert: "ice cream", // trailing comma
 };
 document.write(meal.mainCourse);
SCRIPT1028: Expected identifier, string or number
Now when I say "blows up", I mean it just gives up doing anything else in that script block.  All because of one extra seemingly benign character.   (Well, that and the fact that people just don't get around to upgrading their five year old browsers.  But hey, customers who use IE7 are still customers, right?) Thus began my rather brief quest to find a way to catch this problem as soon as possible -- in the IDE.

JSLint is arguably the most popular static code analysis tool for JavaScript.  But, at least in my Aptana plug-in in Eclipse, it wasn't catching those trailing commas.  On the other hand, it found literally everything else you could possibly imagine.

So instead, I found JSHint, which was branched off of JSLint. It's not so noisy, and not quite as abrasive.  As with JSLint, you can open the online version and plop your code in there, but if I've got something wrong, I wanna know about it pronto. And thanks to some fine work by Ralf Sternberg, there's now a plugin for Eclispe that does just that.

You can install it using the Eclipse updater.  Once it's installed, you'll need to manually activate it for the project (right click the project in the navigator >> properties >> jshint).  You can also selectively disable certain files or folders by pulling up their properties (right click the file or folder >> properties >> jshint).  Then every time you save, you'll get updated warnings in your line number gutter.  Sweet!

And finally, when you're telling your friends about it, please, please, please pronounce it "J-S-Hint" and not "J-Shint". Because, odds are, you'll accidentally say it wrong in front of your momma and end up with a bar of soap in your mouth...

No comments:

Post a Comment

Profile Picture
Dave Leeds
My Hobbies:
  • Programming
  • Cartooning
  • Music Writing
Full Profile