Ian Bicking: the old part of his blog

JSLint from the Command Line

Javascript can be a finicky language, all the worse that it's often permissive, because different browsers are more permissive than others. Annoyingly for me, Mozilla is more permissive than IE, so my Mozilla testing often misses things.

There aren't many programs out there to check Javascript. The only one I've really found is JSLint. The hard part is that it is written in Javascript. I guess it makes sense, but I want to do automated checking, and a browser context doesn't work for me.

However, you can run Javascript from the command-line with Spidermonkey (Mozilla's Javascript interpreter split out). The command-line interface is hard to decypher.

I eventually figured out I could run jslint like this:


var body = arguments[0];

var result = jslint(body);
if (result) {
  print('All good.');
} else {

I can't figure out any way to access stdin, so I have to stuff the entire Javascript into the first argument, and I run it like:

$ js jslintrun.js "`cat jsfile.js`"

Eh... at least it works. I should also use js -s jsfile.js, as this prints out another set of useful warnings (-s is for strict mode).

I want to turn this into another piece of Paste middleware, checking all the JS as it goes past, and probably putting the report in the session so it can be displayed the next time the user/developer views an HTML page.

Then I just have to interface with the CSS validator (or this one?).

Created 19 Aug '05


To access stdin, you have to turn on JS_HAVE_FILE_OBJECT when building SpiderMonkey. The latest CVS versions of spidermonkey have the best file support.

# Evan Prodromou