vim で JavaScript の syntax check をする

vim での JavaScript の構文チェックは jslint.vim を使っていたけど、重かったり、表示が崩れたりしてストレスフルだったので、 最近 Syntastic に乗り換えた。多言語の構文チェック機能があり、JavaScript の構文チェックに限らず大変便利。

Syntastic is a syntax checking plugin that runs files through external syntax checkers and displays any resulting errors to the user. This can be done on demand, or automatically as files are saved. If syntax errors are detected, the user is notified and is happy because they didn't have to compile their code or execute their script to find them.


At the time of this writing, syntax checking plugins exist for applescript, c, coffee, cpp, css, cucumber, cuda, docbk, erlang, eruby, fortran, gentoo_metadata, go, haml, haskell, html, javascript, json, less, lua, matlab, perl, php, puppet, python, rst, ruby, sass/scss, sh, tcl, tex, vala, xhtml, xml, xslt, yaml, zpt

scrooloose/syntasticscrooloose/syntastic

Install Syntastic

.vimrc に下記を追記して :BundleInstall!

Bundle 'scrooloose/syntastic'

Help

:help syntastic.txt

Commands

エラー一覧を表示
:Errors
有効、無効を切り替える
:SyntasticToggleMode
手動で構文チェックを実行する
:SyntasticCheck

Options

構文チェックを行いたいファイルタイプを指定する
let g:syntastic_mode_map = { 'mode': 'passive',
                           \ 'active_filetypes': ['ruby', 'javascript'],
                           \ 'passive_filetypes': [] }

active に指定しなかったファイルタイプでも :SyntasticToggleMode で実行中に active に変更できる。

Install JSLint

JavaScript の構文チェックに JSLint を利用するので、node.js, jslint をインストールする。

  1. node.js を何らかの手段でインストール
  2. $ npm install jslint -g
jslint の設定を自分で設定する
let g:syntastic_javascript_jslint_conf = "--white --undef --nomen --regexp --plusplus --bitwise --newcap --sloppy --vars"
// 上記はデフォルト。上書きしたい場合に設定。

JSHint で構文チェックを行う

JSLint はチェックがキツすぎると思う場合は、JSHint を使う手もある。
javascript - VIM + JSLint? - Stack Overflow