- Coding style consistency — always use ; at the end of a line
- Syntax error detection — did you forget that ) ?
- Logical error detection — did you forget that var?
There’s a whole bunch of stuff JSLint will pick up for you.
I have talked before about JSLint in the context of SpiderMonkey, but, nowadays, I install node.js for a few things. If I run JSLint through node.js, that means I won’t have to install SpiderMonkey anymore.
Installing Node and NPM
I admit, these pieces of software are moving fast and the instructions (or lack thereof) are limited. But these things will vary with your OS and skill level.
I recommend the simply named “jslint”. You can look it up on GitHub as node-jslint.
Make sure you don’t forget that “-g” flag with NPM. NPM changed a lot in version 1.0.
The end goal is:
The main part of integrating with Vim to “compile” something is to set makeprg and errorformat (aka efm). If you ever need to integrate with something else, be sure to Google for those.
Since we are going to invoke :make all the time, I’m going to bind it to F4. (put it in your .vimrc)
nmap <F4> :w<CR>:make<CR>:cw<CR>
Step by step:
- :w — save the file, doesn’t hurt if it’s already saved
- :make — invoke make
- :cw — open the quickfix window if there are errors. Close it if there are no errors.
setlocal makeprg=jslint\ %
\%*[\ ]%n\ %l\\,%c:\ %m,
\%-G\ \ \ \ %.%#,
\%-GNo\ errors\ found.,
The variable makeprg is just was it invoked when you do :make. The variable errorformat are instructions on how to parse the error messages of the “compiler”. That variable and how to configure it are a whole world of complexity.
If things don’t work out, try this:
- try to run “jslint” from the command-line, if it doesn’t work Vim won’t work either
- if the output of “jslint” changes format, you’ll have to tweak errorformat