What is JSHint? It is JSLint tamed for humans.
I always wanted to have JSHint properly integrated into my development flow but never took the plunge. Until now. Prerequisites: nodejs installed in your PATH.
Install JSHint CLI
The command line interface to JSHint can be installed via npm:
npm install -g jshint
And you will be able to do this in the shell:
# JSHint can verify recursively every js file in a folder: $ jshint . # … or a single file $ jshint assets/app.js # to ignore others' problems: $ jshint assets/!(vendor|require.js) assets/app.js: line 23, col 1, 'console' is not defined. 1 error
The jshint command searches the current folder and its parents for a
file and falls back to
~/.jshintrc. This enables project-level jshint options.
.jshintignore can be provided to exclude certain files or
folders from syntax check.
Note: to have the
!() syntax you might need to enable the
extglob feature in
bash (see bash manpage).
Add JSHint to VIM
Install syntastic. Done.
Syntastic will automatically pick the appropriate syntax checker for most languages. If you happen to have both JSHint and JSLint installed, syntastic has to be configured to ue the former. Add this to vimrc:
Add JSHint as a compiler to VIM
Install jshint cli (see above) and create
~/.vim/compiler/jshint.vim with the
Then in VIM comand line:
You might need to add this line to
.vimrc to enable advanced patters in bash:
Make collects errors output the quickfix window (
:copen). You might want to
use the location list window instead. In this case use
:lopen. Read more about quickfix/location list window:
Credit goes to Jonathan Jacobs for this trick.
Add JSHint to Rake build pipeline
It might be useful to automate running JSHint on the codebase before deployment,
from CI or as part of the test phase. Using rake it is as simple as adding this
to Rakefile (replace
assets with the folder to be JSHinted):
gem install jshintrb
Or add this line to Gemfile:
gem "jshintrb", "~> 0.2.1"
Add jshint task to Rakefile:
Cons of JshintTask: does not use
.jshintignore, neither the same magic for
config lookup as JSHint cli does.