1

Trailing commas in JavaScript object literals are not accepted as a valid JavaScript syntax by IE7 :

var a = {
  foo: 12,
  bar: 13,//this is ok in all browsers except ie7
};

For the moment the way I deal with this issue is to open my website using IE7 and use the console to find the invalid js files.

Do you know how to locate (or even better remove) trailing commas in a javascript file using UNIX command line ?

I tried to grep these commas but a multiline regex is needed. I also googled the subject and found nothing useful.

olivieradam666
  • 4,522
  • 2
  • 20
  • 25

3 Answers3

4

JSHint can find those for you. You can download it and run it from the command line. It also has lots of useful additional things it can check for you (and options to turn the ones off that you don't want).

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
2

You can write a script that will do this for you:

$ cat 1.pl 
local $/;

my $data = <>;
$data =~ s@,(\s*)((?://.*?\n)?)(\s*)}@ $1$2$3}@msg;
print $data;

$ cat 1.txt 
var a = {
  foo: 12,
  bar: 13, //this is ok in all browsers except ie7
};

var b = {
  foo: 14,
  bar: 15,
};

$ perl -i 1.pl 1.txt

$ cat 1.txt 
var a = {
  foo: 12,
  bar: 13  //this is ok in all browsers except ie7
};

var b = {
  foo: 14,
  bar: 15 
};
Igor Chubin
  • 61,765
  • 13
  • 122
  • 144
2

It's not perfect but it locates files (it's not printing lines tho):

find -name '*.js' -exec grep -Pzl ",\s*\n+(\s*\/\/.*\n)*\s*[\}\)\]]" {} \;

Answer is partially taken from this question:

Regex (grep) for multi-line search needed

PS

For sure there is one flaw like that:

// whatever is done next,
}

It will be reported as trailing coma, while it's not.

Community
  • 1
  • 1
Drachenfels
  • 3,037
  • 2
  • 32
  • 47