Cory Bennett        

jslint command line tool
[ download ]     Language: Perl/Javascript
If you write javascript you probably know that jslint is a very useful tool, however Douglas Crockford only publishes it as a web tool.

Knowing that spidermonkey existed I knew there must be a away to run Douglas's code from the command line. Then I ran accross this blog by Ian Bicking. This highlights how it could be done, but fell short of being very useful for me.

So I took Ian's javascript and Douglas's javascript and then I hacked up a perl wrapper to find a pretty useful combo. Here is the usage:
Usage: jslint [options] <js file>

options:
    --adsafe   --no-adsafe   : if use of some browser features should be restricted
    --bitwise  --no-bitwise  : if bitwise operators should not be allowed
    --browser  --no-browser  : if the standard browser globals should be predefined
    --cap      --no-cap      : if upper case HTML should be allowed
    --debug    --no-debug    : if debugger statements should be allowed
    --eqeqeq   --no-eqeqeq   : if === should be required
    --evil     --no-evil     : if eval should be allowed
    --forin    --no-forin    : if for in statements must filter
    --fragment --no-fragment : if HTML fragments should be allowed
    --laxbreak --no-laxbreak : if line breaks should not be checked
    --nomen    --no-nomen    : if names should be checked
    --on       --no-on       : if HTML event handlers should be allowed
    --passfail --no-passfail : if the scan should stop on first error
    --plusplus --no-plusplus : if increment/decrement should not be allowed
    --regexp   --no-regexp   : if the . should not be allowed in regexp literals
    --rhino    --no-rhino    : if the Rhino environment globals should be predefined
    --undef    --no-undef    : if variables should be declared before used
    --sidebar  --no-sidebar  : if the System object should be predefined
    --white    --no-white    : if strict whitespace rules apply
    --widget   --no-widget   : if the Yahoo Widgets globals should be predefined

    --indentby <int>         : if used with --white this will will override the 
                               default 4 space indentation level
You can have a ~/.jslint file to control these options as well, so you dont need to specify the on the command line all the time. Mine looks like:
options = {
    bitwise: true,
    browser: true,
    eqeqeq: true,
    forin: true,
    nomen: true,
    plusplus: true,
    undef: true,
    white: true,
    indentby: 2
};
Since I am an emacs users, I next hooked it up to the javascript-mode by adding this snip to my ~/.emacs:
;;; use javascript mode for js files
(require 'javascript-mode)
(setq auto-mode-alist (cons '("\\.js\\'" . javascript-mode)
             auto-mode-alist))

(add-hook 'javascript-mode-hook
  (lambda ()
    ;;; make emacs recognize the error format produced by jslint
    (set (make-local-variable 'compilation-error-regexp-alist)
       '(("^\\([a-zA-Z.0-9_/-]+\\):\\([0-9]+\\):\\([0-9]+\\)" 1 2 3)))
    (set (make-local-variable 'compile-command)
       (let ((file (file-name-nondirectory buffer-file-name)))
          (concat "/path/to/jslint " file)))))

To use this in emacs, open a .js javascript file, then run jslint on it with "M-x compile". Emacs will split-screen and show the js in one half and the jslint output int the other half. Then when you run "M-x next-error" emacs will automatically adjust your source code window to focus on the error from jslint.

Note: You will need spidermonkey (version 1.6 minimum) installed which is usually /usr/bin/js or /usr/bin/smjs. You can get this easily on Fedora systems with:
sudo yum install js
and on Debian/Ubuntu systems with:
sudo apt-get install spidermonkey-bin