10 July 2007

As much as I like some of the newer dojo-friendly Eclipse-based JavaScript+HTML+CSS IDEs like Aptana and Eclipse ATF, they all seem to barf on my JavaScript+JSP+JSTL+Portlet taglib source. Using the web-based JSLint JavaScript Verifier directly definitely works better, but there's only so many times I'm willing to copy and paste code into a browser when hunting down problems. There are some folks that have talked about validating within Ant builds but my development team is on a pre-1.6 copy of Ant which makes putting the pieces together a bit troublesome. It's pretty straightforward to run JSLint from the command line with Rhino but it's not well suited to running against a directory of files at once.

After some messing around with Windows XP batch files I got a useful little utility working. Once you download Rhino into a RHINO_HOME directory along with a copy of jslint.js, you can use this little batch file to run JSLint against many files at the same time:

@echo offrem Windows batch file to run jslint on a collection of JavaScript filesrem see http://agentzlerich.blogspot.com/2007/07/running-jslint-against-many-files-in.htmlrem for original postrem To use, download Rhino from http://www.mozilla.org/rhino/rem and uncompress it in a folder. Set RHINO_HOME environmentrem variable to point to the folder. Download JavaScript Lint (jslint)rem from http://www.jslint.com/rhino/index.html and place it intorem RHINO_HOME. Ensure JAVA_HOME points to your JREif"%RHINO_HOME%"==""goto rhinoHomeNotSetif"%JAVA_HOME%"==""goto javaHomeNotSetif"%1"==""goto noJavaScriptFilesSpecifiedrem N.B. next line is wrapped!for%%f in (%*) do echo. & echo Processing %%f &amp; "%JAVA_HOME%\bin\java" -classpath "%RHINO_HOME%\js.jar" org.mozilla.javascript.tools.shell.Main "%RHINO_HOME%\jslint.js" %%fexit/b%ERRORLEVEL%;
:noJavaScriptFilesSpecifiedecho No JavaScript files were specified as parameters. Exiting.exit/b0:rhinoHomeNotSetecho Variable RHINO_HOME is not set! Aborting.exit/b1:javaHomeNotSetecho Variable JAVA_HOME is not set! Aborting.exit/b1