Safari and the Stray Comma

Posted 2006-07-10…

Here’s a little tidbit. Safari doesn’t like stray commas in Javascript (Firefox just ignores them, and IE is too busy choking on everything else to notice). Here’s an example of an offense:

new Effect.Highlight('foo', {duration:0.5,startcolor:'#ff99ff',});

See that extra trailing comma? Safari completely hates it. No neat highlight for you— sloppy coders are punished severely in Safari-land.

A few months back, I added a test to catch these. It goes something like this (it’s weak, I know, but it works):

  def test_no_stray_commas_for_safari
    Dir[File::join(RAILS_ROOT,'public/javascripts','*.js')].each do |filename|
      found = File::read(filename) =~ /([^\r\n]*),\s*[\]\})]/sm
      assert_nil( found, "Found possible stray comma in #{File::basename filename} at char index #{found} near:\n--START JAVASCRIPT--\n#{$1}\n--END JAVASCRIPT--" )  
    end
  end

… and, what do you know, today it saves the day.