Finding those damn trailing commas

Internet Explorer has this wonderful behavior where it completely refuses to load a JavaScript file when it encounters a trailing comma. That is, something that looks like this:

var f = { foo: "bar", baz: { go: "yes" }, };

I got sick of trying to find these beasties and came up with this regex instead:

}[\s\n]*,[\s\n]*[^\[{\w\n\s/\*\"]

Works like a charm in TextMate. Might work in grep too? Anyway, it’s a good way to find your headache quickly. :)

2 thoughts on “Finding those damn trailing commas”

  1. Ben, you need the Javascript bundle for TM. It has a couple of neat lint integrations, one thing which I don’t remember if it’s OTTB or if I tweaked it to work like this, everytime I save a JS file, it runs a sort of quick jslint test and throws the number of errors if any into a tooltip. Just so you can see them.

    Then you can run the big one and lint will tell you all the awful stuff in your code, including the trailing commas. I see it as a auto code testing for JS workflow.

    http://blog.macromates.com/2007/javascript-tools/

  2. Thanks JP! I’ve tried using TextMate but it just doesn’t jive with my emacs-brain. Since the time I put up this post, I’ve found the new js2-mode for emacs from Steve Yegge which does a lot of this stuff on the fly. Works pretty well for me. :)

Comments are closed.