10 July 2007

Running jslint against many files in the Windows CLI

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 off
rem Windows batch file to run jslint on a collection of JavaScript files
rem see http://agentzlerich.blogspot.com/2007/07/running-jslint-against-many-files-in.html
rem for original post

rem To use, download Rhino from http://www.mozilla.org/rhino/
rem and uncompress it in a folder.  Set RHINO_HOME environment
rem variable to point to the folder.  Download JavaScript Lint (jslint)
rem from http://www.jslint.com/rhino/index.html and place it into
rem RHINO_HOME.  Ensure JAVA_HOME points to your JRE

if "%RHINO_HOME%"=="" goto rhinoHomeNotSet
if "%JAVA_HOME%"=="" goto javaHomeNotSet
if "%1"=="" goto noJavaScriptFilesSpecified

rem N.B. next line is wrapped!
for %%f in (%*) do echo. & echo Processing %%f & "%JAVA_HOME%\bin\java" -classpath "%RHINO_HOME%\js.jar" org.mozilla.javascript.tools.shell.Main "%RHINO_HOME%\jslint.js" %%f
exit /b %ERRORLEVEL%;

:noJavaScriptFilesSpecified
echo No JavaScript files were specified as parameters. Exiting.
exit /b 0

:rhinoHomeNotSet
echo Variable RHINO_HOME is not set! Aborting.
exit /b 1

:javaHomeNotSet
echo Variable JAVA_HOME is not set! Aborting.
exit /b 1

Subscribe Subscribe to The Return of Agent Zlerich