Posts Tagged ‘Jslint JavaScript Vim’

JSLint, Vim, and Windows XP

Just a quick post about integrating JSLint with Vim on a Windows XP machine. I had posted awhile back about integrating JavascriptLint with Vim on XP, but while re-reading The Good Parts I got the urge to revisit JSLint just to revel in the Crockfordness of it all…

First off, get Jesse Hallett’s excellent jslint.vim plugin here. I installed this on my Fedora box without a hitch (well, the only hitch was figuring out the name of the Spidermonkey package for Fedora 12, ‘js-devel’ I believe it was) but my work XP machine would spit this out when attempting a :jslint command:

Error detected while processing function <SNR>17_JSLint:
line  43:
E484: Con't open file C:\...\...\...\...\VIo1F3B.tmp

It’s obvious that some paths (literally, figuratively) were getting crossed somewhere (*Note you may also want to attempt to use cscript from the command line just to make sure it is, in fact, installed and working. I did, and mine was*.)

In my experience the first place to check for errors with Vim plugins is the PATH so I opened up Jesse’s plugin and focused on this section (lines 41-59 on his github repo version):

  let s:plugin_path = '"' . expand("~/") . '"'
  if has("win32")
    let s:cmd = 'cscript /NoLogo '
    let s:plugin_path = s:plugin_path . "vimfiles"
    let s:runjslint_ext = 'wsf'
  else
    if has("gui_macvim") && filereadable('/System/Library/Frameworks/JavaScriptCore.framework/Resources/jsc')
      let s:cmd = '/System/Library/Frameworks/JavaScriptCore.framework/Resources/jsc'
    else
      let s:cmd = 'js'
    endif
    let s:plugin_path = s:plugin_path . ".vim"
    let s:runjslint_ext = 'js'
  endif
  let s:plugin_path = s:plugin_path . "/plugin/jslint/"
  let s:cmd = "cd " . s:plugin_path . " && " . s:cmd . " " . s:plugin_path
               \ . "runjslint." . s:runjslint_ext
  let s:jslintrc_file = expand('~/.jslintrc')

Here’s the version I ended up with after experimenting for awhile:

let s:plugin_path = '"' . expand("C:/Program Files/Vim/vimfiles") . '"'
  let s:cmd = 'cscript /NoLogo '
  let s:runjslint_ext = 'wsf'
let s:plugin_path = s:plugin_path . "/plugin/jslint/"
let s:cmd = "cd " . s:plugin_path . " && " . s:cmd . " " . s:plugin_path
       \ . "runjslint." . s:runjslint_ext
let s:jslintrc_file = expand('C:/Program Files/Vim/_jslintrc')

So, I ended up changing 3 things:

  • Remove tests for which OS is being used
  • Explicitly state the plugin path
  • Changed the name and path of the jslintrc file

First, since we know this is for XP, the tests for Win32, Mac and ‘Nix are not needed. Second, there are more compact and portable ways to specify this path, but posting it this way makes it easy to adapt to your system path. Third, On my XP system my .vimrc is actually _vimrc and I keep it here in the vimfiles directory so it made sense to me to do the same with a jslintrc.

Speaking of the _jslintrc, it looks like the ‘fulljslint.js’ file is updated quite a bit on the jslint.com website, and allows for more options in the jslintrc file. I need to experiment with that next

Integrating JavaScriptLint With Vim On Windows

I use Vim to code JavaScript on my work machine, a PC running WinXp. Looking around for a way to integrate a good lint program to catch typical .js errors I found JavaScriptLint.vim. The instructions there are simple, drop the script in your vim/plugins folder after the jsl.exe program has been installed. That executable, the javascriptlint program itself, has good documentation for Vim integration on Linux (which works well btw) but none for Windows. Here is what I did:

  • First, download the version for windows here
  • Extract that .zip anywhere, doesn’t matter
  • From the extracted folder grab the jsl.exe and jsl.default.conf files and put them in your C:\Program Files\Vim\Vim72 folder (If you’re using Vim 7.2, I would assume other versions have a similar folder structure. It’s the same directory the gvim.exe, vim.exe and others are in).
  • Drop the JavaScriptLint.vim file in C:\Program Files\Vim\vim72\plugin

Now, you’re almost done. Open up the jsl.default.conf file in a text editor and in the ‘Defining Identifiers’ section enable ‘always_use_option_explicit’ like so:

### Defining identifiers
# By default, "option explicit" is enabled on a per-file basis.
# To enable this for all files, use "+always_use_option_explicit"
+always_use_option_explicit

Also, just below the ‘common uses for webpages might be: line I added these, a la Ken Guest:

# Common uses for webpages might be:
+define ActiveXObject
+define addEventListener
+define alert
+define attributes
+define blur
+define childNodes
+define click
+define clearTimeout
+define dispatchEvent
+define document
+define firstChild
+define focus
+define Image
+define item
+define lastChild
+define localName
+define namespaceURI
+define navigator
+define nextSibling
+define nodeName
+define nodeType
+define nodeValue
+define ownerDocument
+define parent
+define parentNode
+define prefix
+define previousSibling
+define removeEventListener
+define screen
+define scrollIntoView
+define setTimeout
+define tagName
+define window
+define XMLHttpRequest

The last thing I did, and this just because it looked like the correct thing to do, was I commented out the ‘files’ section test file since Vim is sending the file for me (and since I didn’t drop that file in from the extracted folder anyway…):

### Files
# Specify which files to lint
# Use "+recurse" to enable recursion (disabled by default).
# To add a set of files, use "+process FileName", "+process Folder\Path\*.js",
# or "+process Folder\Path\*.htm".
#
#+process jsl-test.js

Now, Action! Let’s say I just hacked together this object literal (which, I just did…) and saved it, seems OK, no errors reported:

Not triggering the .js lint error messages

Not triggering the .js lint error messages


Now, what if I had forgot to put a semicolon after the loadMap() function call in the objects init: and then saved?
Error warning triggered

Error warning triggered


The ‘quickfix’ window opens with the error highlighted. The line number here seems to be incorrect as line 23 is where the closing brace is, the actual missing semicolon should be on line 21. This is, however, the first run and may be a matter of configuration. Still a great start I think

Return top