Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VIM + JSLint?

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.

like image 604
ben lemasurier Avatar asked Oct 16 '22 15:10

ben lemasurier


People also ask

What is Jslint used for?

JSLint is a static code analysis tool used in software development for checking if JavaScript source code complies with coding rules. It is provided primarily as a browser-based web application accessible through the domain jslint.com, but there are also command-line adaptations.


2 Answers

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.
  • I recommend cloning from the GitHub repo and installing using a plugin manager like Vundle or Pathogen, since it's more frequently updated.
  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
    • See http://npmjs.org for more info.
  3. Install jshint globally: 'npm install jshint -g'
  4. Put your jshint config file in your $HOME dir: '~/.jshintrc'
    • Here's JSHint-Node's example configuration file, they pulled from mine and added more stuff.
    • Here's my original copy, which you can also start from.
  5. Overwrite Syntastic's syntax_checkers/javascript.vim file with this one - EDIT: NO LONGER NECESSARY WITH NEWEST SYNTASTIC VERSION.
    • Script copied from FactorayLab's really helpful Vim GitHub page, which I recommend every Vim user to check out.

Enjoy! :)

like image 200
17 revs, 6 users 87% Avatar answered Nov 02 '22 15:11

17 revs, 6 users 87%


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.

like image 35
f3lix Avatar answered Nov 02 '22 15:11

f3lix