views:

2068

answers:

6
+18  Q: 

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?

Edit:

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

+2  A: 

I was feeling lucky: Javascript Syntax Checking From Vim :-)

jetxee
Thanks, I saw that and it looked decent... was hoping for a more solid solution. Although, in the end this might just have to do the trick =].
uidzer0
+15  A: 

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.

f3lix
This is exactly what I was looking for, thanks!
uidzer0
Why are you catting the buffer and passing it as an argument to mylintrun?
jamessan
@jamessan: the mylintrun.js script reads the file from stdin. So that's what the catting is for. For the error output mylintrun.js needs also the file name, so it is given as an argument. You could modify the script to open the file for reading instead of reading from stdin (making the cat unnecessary). But I didn't want to bother with this... and I believe there is a problem reading files if you have a JavaScript engine compiled w/o the FileObject
f3lix
what is 'the path to SpiderMonkey' on your machine - I've got a few on mine
Dr. Frankenstein
Think Rhino will work as well as SpiderMonkey?
superjoe30
+2  A: 

I wrote a vim script similar to f3lix's solution, but instead of invoking the :make command manually, the script will list any lint warnings in the quickfix window each time you save a javascript file.

http://www.vim.org/scripts/script.php?script_id=2578

joestelmach
Hi Joel, I had a question about how to use this script, but I thought more people might have the same question and I asked it as a separate Q here: http://stackoverflow.com/questions/1747091/how-do-you-use-vims-quickfix-featureThink you can help? I'd appreciate it.
hora
A: 

i am using JavascriptLint.vim by joestelmach but i am not seeing any errors in my quick fix error window.

Subba Rao
A: 

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

mde
+5  A: 

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!

Alex Kahn