I was chatting with some others on the CB yesterday about ways to detect the following in a JS file:
var myObject = { foo: 'foo', bar: 'bar', //baz: 'baz' };
While Firefox blissfully ignores that extra comma, IE and Chrome treat it as if there's a null element after it which can cause problems that are difficult to debug. After many iterations, I came up with the following substitution:
s#^((?:[^/\n]|/(?!/))*),((?:\s*|//.*$|/\*(?:[^*]|\*(?!/))*\*/)*\s*[}\] +])#$1$2#mg
To decode: It detects any comma that does not follow a // on the same line, and searches for a closing ] or } with any amount of whitespace or comments inbetween. It's not ridiculously fast but it gets the job done. This operates on a string that contains the whole file, line breaks and all. It's not perfect... it sometimes detects false positives (for instance, the character class \w,) and it may also behave strangely on strings containing anything comment-like. That's why I wrote a detection script that can be run before the one that fixes it. I've uploaded my code to a SourceForge project: https://sourceforge.net/projects/jscommas/

Replies are listed 'Best First'.
Re: Detecting Dangling Commas in JavaScript
by MidLifeXis (Monsignor) on Oct 27, 2010 at 21:43 UTC
      Lint is great but it only detects problems, it doesn't fix them to my knowledge. When I was faced with correcting 300 of these problems, I didn't want to do it manually. For me, it was also about discovering a way to effectively use lookarounds to detect whether something was inside a C-style comment or not.
Re: Detecting Dangling Commas in JavaScript
by Anonymous Monk on Oct 27, 2010 at 19:12 UTC

    The backreferences \1 and \2 should only be used inside a regular expression, outside a regular expression you should use $1 and $2 instead so that should be:

    s#^((?:[^/\n]|/(?!/))*),((?:\s*|//.*$|/\*(?:[^*]|\*(?!/))*\*/)*\s*[}\] +])#$1$2#mg
      Duly noted, thanks, I will change it. This is only the 2nd Perl script I've written in the last year... I'm a bit rusty. ;)
Re: Detecting Dangling Commas in JavaScript
by LanX (Saint) on Oct 28, 2010 at 19:14 UTC
    Maybe of interest:

    There is a Perl port of Crockford's JSLint on CPAN: JavaScript::JSLint

    Cheers Rolf

Re: Detecting Dangling Commas in JavaScript
by choroba (Cardinal) on Oct 28, 2010 at 13:25 UTC
    Just being curious: isn't a curly bracket missing somewhere in your JS example?
      Er, I suppose. I typed it up in a hurry, and made the elements object-style while calling it an array. Fixed, not that it matter. ;)
Re: Detecting Dangling Commas in JavaScript
by bellaire (Hermit) on Jan 20, 2011 at 18:26 UTC
    Nice! Thus, to map this replacement to a vim command (using a vim with perl support):
    :command FixCommas perl $s=join(qq{\n},$curbuf->Get(0 .. $curbuf->Coun +t()));$s =~ s#^((?:[^/\n]|/(?!/))*),((?:\s*|//.*$|/\*(?:[^*]|\*(?!/)) +*\*/)*\s*[}\]])#$1$2#mg; $curbuf->Set(0,0,split(qq{\n},$s));
    (Tried it as a simple :%!perl -pi -e at first, but vim hated the # characters.)