mfks17's blog(Life is Good !!)

趣味や思った事を書いていくと思います

jsl.vimの導入する

Node.jsの本にはjsl.vimいいよ書いてあったので、入れてみようと思います。

参考サイト様

環境


下準備


以下のパッケージをインストールする

$ sudo apt-get install build-essential
$ sudo apt-get install python-dev

JavaScript Lintのインストール

$ svn co https://javascriptlint.svn.sourceforge.net/svnroot/javascriptlint javascriptlint

いろいろ落としてきます。 最新を使いたいので、trunkを使います。

$ cd javascriptlint/trunk

では、ビルドしてインストールしていきます。

$ python setup.py build
$ sudo python setup.py install
...
changing mode of /usr/local/bin/jsl to 775
..

/usr/local/bin/jslが本体みたいですね。
動作確認
tests にテスト用ファイルがあるので、試してみます。

$ jsl jsl tests/errors/syntax_error.js
ちなみに、こんなコードでした。
function syntax_error() {
    &; /*warning:syntax_error*/
}

実行結果

JavaScript Lint
Developed by Matthias Miller (http://www.JavaScriptLint.com)
/home/typosterr/javascriptlint/trunk/tests/errors/syntax_error.js
/home/typosterr/javascriptlint/trunk/tests/errors/syntax_error.js(2): warning: s
yntax_error

0 error(s), 1 warnings(s)

うまくいったみたいです。 ここで、jslのヘルプを見てみましょう。

$ jsl -h
Usage: jsl [options] [files]

Options:
  -h, --help          show this help message and exit
  --conf=CONF         set the conf file
  --profile           turn on hotshot profiling
  --recurse           recursively search directories on the command line
  --enable-wildcards  resolve wildcards in the command line
  --dump              dump this script
  --unittest          run the python unittests
  --quiet             minimal output
  --verbose           verbose output
  --nologo            suppress version information
  --nofilelisting     suppress file names
  --nosummary         suppress lint summary
  --help:conf         display the default configuration file

なので、さくっと見たいときは

$ jsl --nologo --nosummary --nofilelisting tests/errors/syntax_error.js
/home/typosterr/javascriptlint/trunk/tests/errors/syntax_error.js(2): warning: syntax_error

こんな感じになります。

vimの設定

vimで開いているファイルのチェックを可能に。
~/.vim/compiler/jsl.vim に記述。

if exists("current_compiler")
    finish
endif

let current_compiler = "jsl"

if exists(":CompilerSet") != 2
    command -nargs=* CompilerSet setlocal 
endif

let s:cpo_save = &cpo
set cpo-=C

CompilerSet makeprg=jsl\ --nologo\ --nofilelisting\ --nosummary\ %
CompilerSet errorformat=%f(%l):\ %m

let &cpo = s:cpo_save
unlet s:cpo_save

~/.vim/after/ftplugin/jsl.vim に以下を記述。

compiler jsl

if !exists('b:undo_ftplugin')
    let b:undo_ftplugin = ''
endif

let b:undo_ftplugin .= '
\ | setlocal makeprg<
\ | setlocal errorformat<
\'

.vimrcに以下を記述

" JavaScript Lint
if !exist('b:current_compiler')
  compiler jsl
endif
autocmd QuickFixCmdPost make copen

これで、準備が整いました。
サンプルとして以下のようなファイルを作って、実行したいと思います。

[1, 2, 3, 4, 5].forEach(function (n) {
    console.log(n * n)
})

これをSample.jsとかにしてvimで開きます。
其の状態で:makeを打つと

quickfixウィンドウを消すときは:ccloseと打ちます。
とりあえず、できたみたいだけど設定ファイルがわけがわからないよ。
追記

Google Closure Linter gjslint
googleさんのGoogle JavaScript Style Guideにあわせたものもあるみたいです。
最後までお読みいただきありがとうございました。