Wednesday, October 1st, 2008
Finding those trailing commas
<p>Dan Fabulich got bitten by the "trailing comma" issue one too many times, so he created a script to solve his problem:“How many thousands of developer hours have been lost to random IE bugs like this?” I asked myself. I decided that there had to be a good way to detect this problem in an automated way, without firing up a copy of IE and running a full test suite.
It turns out that these and other syntax errors can be detected automatically from the Windows command line, using the Windows Scripting Host (WSH). On Windows XP and higher, the command-line tool “cscript.exe” can be used to run JavaScript (ahem, JScript) headlessly (outside of any browser).
Just create a file called “wsh-parser.js” like this:
JAVASCRIPT:
var fso = new ActiveXObject( "Scripting.FileSystemObject" ); function parse(fname) { var file = fso.OpenTextFile( fname, 1 ); ret = file.ReadAll(); file.Close(); try { eval("(function(){n"+ret+"n});"); } catch (e) { WScript.Echo("Syntax error parsing " + fname); WScript.Echo(" " + e.message); } } function findJavaScript(folder) { for (var fc = new Enumerator(folder.files); !fc.atEnd(); fc.moveNext()) { var file=fc.item(); if (/.js$/.test(file.Name)) { parse(file); } } for (var fc = new Enumerator(folder.subfolders); !fc.atEnd(); fc.moveNext()) { var subfolder = fc.item(); if (subfolder.Name == ".svn") continue; // ignore .svn folders findJavaScript(subfolder); } } var rootPath = "src/main/javascript"; var rootFolder = fso.GetFolder(rootPath); findJavaScript(rootFolder);
Other tools will help you hunt this down too, and I am sure some of you have Perl/Ruby/Python one liners to do the same ;)
Related Content:











Leave a comment