LoginSignup
6
6

More than 5 years have passed since last update.

javascriptの自動テストに向けて、JSHintと呼ばれるJavascript用構文チェッカーを入れてみた。

Last updated at Posted at 2013-04-19
sudo su

nvmを導入

git clone https://github.com/creationix/nvm.git  /usr/local/nvm
. /usr/local/nvm/nvm.sh
/etc/profile.d/nvm.sh
source /usr/local/nvm/nvm.sh
nvm use v0.11.0
nvm install v0.11.0

 バージョン確認

$ node -v
v0.11.0
$ npm -v
1.2.15
sudo visudo
Defaults    env_keep += "PATH"
#Defaults    secure_path = /sbin:/bin:/usr/sbin:/usr/bin

セキュリティ上は好ましくないらしい

npmのインストール

npm install -g jshint

~/.jshintrc
{
  "indent" : 2,      // インデント
  "camelcase":true,  // キャメルケース
  "maxlen" : 80,     // 一行の最大文字数
  "unused" : true,   // 宣言のみで使用していない変数を検出
  "eqeqeq":true,     // ==、!=の使用禁止
  "undef" : true,    // グローバル変数へのアクセスを禁止
  "devel" : true     // console、alertを許可
}

ではやってみよう

test.js
function x(a,b) {
 return a + b;
}

res = x(10,20);
console.log("res = " + res);

jsとしては間違ってないハズ!!

実行

$ jshint test.js
test.js: line 2, col 2, Expected 'return' to have an indentation at 3 instead at 2.
test.js: line 5, col 1, 'res' is not defined.
test.js: line 6, col 24, 'res' is not defined.

3 errors

3件もエラー。。
res定義されてないよ
もう一つはわかりにくかったけど、インデントがスペース二個分じゃないのでエラー

test.js
function x(a,b) {
  return a + b;
}

var res = x(10,20);
console.log("res = " + res);

最終テスト

$ jshint test.js

問題なしを確認しました!

最後に、一括チェック

find <jsのディレクトリ> -name "*.js" | xargs jshint

その他

参考URL

jshintのオプション一覧
http://blog.craftgear.net/50832ff38cdc8fb415000001/title

6
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
6