How to do JSLint in Vim

JavascriptVimLint

Javascript Problem Overview


I spend my days in vim, currently writing a lot of JavaScript. I've been trying to find a way to integrate JSLint or something similar into vim to improve my coding. Has anyone managed to do something like this?

I tried this: Javascript Syntax Checking From Vim, unfortunately the output is very crude.

Javascript Solutions


Solution 1 - Javascript

The best-practice way IMO is:

  1. Install Syntastic Vim plugin - Best syntax-checker around for plenty of languages, plus it integrates with Vim's location-list (==quickfix) window.
  1. Choose one of the two options below:

JSLint

  1. Install jsl (JSLint executable) using your favorite package manager (Ubuntu's apt-get, Mac's home brew, etc.).

Community-driven jshint.com (better than JSLint)

  1. Install node.js using your favorite package manager.
  2. Install Node Package Manager: 'curl https://npmjs.org/install.sh | sh' EDIT: npm IS PART OF node.js NOW
  3. Install jshint globally: 'npm install jshint -g'
  4. Put your jshint config file in your $HOME dir: '~/.jshintrc'
  5. Overwrite Syntastic's syntax_checkers/javascript.vim file with this one - EDIT: NO LONGER NECESSARY WITH NEWEST SYNTASTIC VERSION.

Enjoy! :)

Solution 2 - Javascript

You can follow the intructions from JSLint web-service + VIM integration or do what I did:

Download http://jslint.webvm.net/mylintrun.js and http://www.jslint.com/fulljslint.js and put them in a directory of your choice.

Then add the following line to the beginning of mylintrun.js:

var filename= arguments[0];

and change last line of code in mylintrun.js ("print( ...)") to:

 print ( filename + ":" + (obj["line"] + 1) + ":" + (obj["character"] + 1) + ":" + obj["reason"] );

This makes in mylintrun.js output a error list that can be used with the VIM quickfix window (:copen).

Now set the following in VIM:

set makeprg=cat\ %\ \\\|\ /my/path/to/js\ /my/path/to/mylintrun.js\ %
set errorformat=%f:%l:%c:%m

where you have to change /my/path/to/js to the path to SpiderMonkey and /my/path/to/mylintrun.js to the path where you put the JS files.

Now, you can use :make in VIM and use the quickfix window (:he quickfix-window) to jump from error to error.

Solution 3 - Javascript

Another option is jslint.vim from Jesse Hallet. It's available on GitHub and works with or without Vim's QuickFix window. It's a nice plugin!

Solution 4 - Javascript

I've been very happy using node-lint

sudo npm -g install jslint

Then whack this somewhere in your .vim

set makeprg=jslint\ %
set errorformat=%-P%f,
        \%E%>\ #%n\ %m,%Z%.%#Line\ %l\\,\ Pos\ %c,
        \%-G%f\ is\ OK.,%-Q

Now a :make will run jslint. Errors appear in the quickfix window.

Solution 5 - Javascript

Here are the Mac OS instructions updated for Nov. 2012. Assumes you have https://brew.sh/">Homebrew</a> installed in order to get Node.js, and that you've already installed Syntastic for Vim (I use https://github.com/carlhuda/janus which provides this automatically):

$ brew install node.js
$ npm install -g jshint

Then add '/usr/local/share/npm/bin' to your PATH (probably in ~/.bashrc). For example, add the line: export PATH="$PATH:/usr/local/share/npm/bin"

restart your Terminal and check that

$ jshint

is executable from the command line. Syntastic will discover jsHint automatically. Restart MacVim and enjoy!

Solution 6 - Javascript

Much better is to pipe the results through Lynx to deal with JSLint's unfortunate choice of HTML for output format. I have a blog post on how to do it here:

http://www.fleegix.org/articles/2008-09-06-jslint-in-vim-through-lynx

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionben lemasurierView Question on Stackoverflow
Solution 1 - JavascriptOry BandView Answer on Stackoverflow
Solution 2 - Javascriptf3lixView Answer on Stackoverflow
Solution 3 - JavascriptAlex KahnView Answer on Stackoverflow
Solution 4 - JavascriptbluekeysView Answer on Stackoverflow
Solution 5 - JavascriptShaun DychkoView Answer on Stackoverflow
Solution 6 - JavascriptmdeView Answer on Stackoverflow