Archive for August, 2007

Google Test Automation Conference 2007

The titles of the videos look very much like “must watch”. Found via ThinkPHP, thanks.

Comments (2)

Getting JSLint going

Yesterday I had to get JSLint up an running to check some code and improve the code quality. Since setting it up, getting it running and configuring JSLint is not really a piece of cake I thought I might share the steps so for those interested it goes faster than for me :-).

Overview
JSLint is available as an online version, where you can paste your source code on a page and have it analyzed. This analyzing takes place locally, so there is no source code being sent to the server!
JSLint is also available in full source code.
But working locally it only really makes sense to have JSLint installed on your maschine. And therefore you need Rhino to run locally and that is what brings the fun :-).

Setup Rhino
Check out Rhino first from the mozilla cvs (detailed description). The short version is simply:
cvs -d :pserver:anonymous@cvs-mirror.mozilla.org:/cvsroot co mozilla/js/rhino
For building Rhino be sure to have Ant installed and go into the directory and run ant
cd mozilla/js/rhino
ant compile

That gave me a lot of deprecated warnings, but I guess that is only because I have the JRE1.5 running and Rhino usually uses 1.4 afaik. Now your build hopefully finished with “BUILD SUCCESSFUL”.
Now Rhino should be ready to go.

Run JSLint
To make JSLint work locally you need to download jslint.js that we will execute using Rhino.
Ant generates the class files (the compiled java files) into the directory build/classes, so you now need to go there in order to run the Rhino.
It’s easiest when you simply save jslint.js in the same directory too.
Running jslint now is as easy as

cd build/classes
java org.mozilla.javascript.tools.shell.Main jslint.js mypath/and/file.js

This assumes that you have jslint.js in the current directory (mozilla/js/rhino).
If you now get lines like the following:

  Lint at line 55 character 2: Missing semicolon.
  }

then you got JSLint running.
Now it needs tweaking to get most our of it.

Configure JSLint
All the reasoning for using JSLint and some hints how to write good code can be found on the JSLint page. All the options you can use to tweak JSLint are also found on that page, pretty far down in the section “Options”.
You apply those options by simply writing them (at the top) of yourfile.js, I for example used the following options:

/*jslint nomen:false, evil:false, browser:true, laxbreak:true, passfail:false, adsafe:true, undef:true */
/*extern dojo */

There are a couple more options. Those used here are for:

  • nomen:false - does not check the variable names, otherwise it would mourn about variable names like “this._privateProp”, it’s the underscore that JSLint doesn’t like, for whatever reason
  • evil:false - warns about all evals which are evil
  • browser:true - the standard browser globals should be predefined
  • laxbreak:true - does NOT check line breaks, JSLint seems to be pretty picky otherwise and it’s rules don’t always fit what we expect
  • passfail:false - this doesn’t let the scan stop on the first error
  • adsafe:true - I really don’t know :-) may be it checks that your JS will also work with advertisiing embedded in your site …

Tweak jslint.js
There is just one annoying thing, it’s that JSLint stops after 50 messages, which is not what I want. I just run JSLint put the results in a file and parse through the file to find the really important things first (that might need some tool :-)).
Therefore you need to modify the jslint.js. In line 17 you will find

if(warnings===50){quit("Too many errors.",l,ch);}

change this to

if(warnings===100000){quit("Too many errors.",l,ch);}
.
At least that is what I did, now it dumps me all the errors (well at least a 100,000 which I hopefully never reach).

Finally
As you can see it is all pretty simple, the tricky part imho was to find the information one needs to get all out of the tool. And that just consumes a lot of time, so I hope this helps all those interested. Have fun, and let me know if I missed a point …

Comments (2)