Tags

, , , , ,

I currently ran into an issue using the should.js assertion library with the Vim Syntastic plugin. I was attempting to use the “should.be.ok” function and receiving the following error:

Expected an assignment or function call and instead saw an expression.

I generally follow this advice from Douglas Crockford and most of the rules he laid out in JSLint but on this occasion it is how the library was designed to be used.

To allow the use of should.js in my tests, I first configured Syntastic to use JSHint. JSHint basically allows you to enable and disable the rules in JSLint. Here is how I did it.

  • You will need to have JSHint installed globally via NPM.
npm install -g jshint
  • Once installed you need to tell Syntastic to use JSHint. You can do this in your .vimrc file.
// Create a reusable variable that contains the path of the global NPM directory
let npmDir=escape($NPMGLOBAL_BIN,' \')

// Tell syntastic where the jshint.cmd file is located
let g:syntastic_jshint_exec=npmDir . "/jshint.cmd"

// Tell syntastic to use jshint as the syntax checker
let g:syntastic_javascript_checkers=['jshint']

note: The $NPMGLOBAL_BIN variable is a reference to the windows environment variable of the same name (ie: %NPMGLOBAL_BIN% at the command prompt). The escape function fixes the windows path for use by console vim running under cygwin, you may not need this. At the end of the day, all you need in the npmDir variable is a valid path to the location of the jshint.cmd file.

  • Now when you open up Vim and edit a javascript file Syntastic will be using jshint. To confirm this you can run the following command
:SyntasticInfo

and you should be presented with something that looks like this

Syntastic info for filetype: javascript
Available checkers: jshint
Currently active checker(s): jshint

Okay, great! We now have jshint installed. How does this help us with our original problem. Well, we can disable just the syntax rule that is failing in our test file. To do this add the following to the top of each of your test files

/*jshint expr: true*/

JSHint also allows you to configure these rules globally if you like but I only want to disable this rule in my test files that are actually using should.js. This way I still get this rule checked everywhere else.

Hope this helps!

Advertisements