こんにちは、中川です。
今回は「Google JavaScript Style Guide」を気軽にチェックできるClosure Linterをご紹介したいと思います。
http://google-styleguide.googlecode.com/svn/trunk/javascriptguide.xml
Google JavaScript Style Guideのいいところですが、
規約だけでなく、しっかりしたツールも揃っているところが素晴らしいと思います。
コーディング規約はあったとしても、それに従っているか定期的に簡単にチェックする方法がないと、
いつしか守られないまま開発されてしまうことになりますね。
※今回の内容はMacにて動作確認を行なっています。
https://developers.google.com/closure/utilities/
「Google JavaScript Style Guide」に則ってチェック・修正を行えるツールとなります。
$ sudo easy_install http://closure-linter.googlecode.com/files/closure_linter-latest.tar.gz
これで、gjslint、fixjsstyleのコマンドが利用できるようになります。
以下のような以下のサンプルで動作確認したいと思います。
// sample.js
var params = {
name: "Asial",
message: 'Hello',
};
hello(params.name,params.message);
function hello(name, message) {
alert(name + ' : ' + message)
}
gjslintでコーディングスタイル違反を確認してみましょう。
$ gjslint sample.js
FILE : /path/to/sample.js -----
Line 3, E:0131: Single-quoted string preferred over double-quoted string.
Line 4, E:0121: Illegal comma at end of object literal
Line 7, E:0002: Missing space after ","
Line 10, E:0010: (New error) Missing semicolon at end of line
Found 4 errors, including 1 new errors, in 1 files (0 files OK).
Some of the errors reported by GJsLint may be auto-fixable using the script
fixjsstyle. Please double check any changes it makes and report any bugs. The
script can be run by executing:
fixjsstyle sample.js
このように、ズラズラと警告を表示してくれます。
これらをまとめて修正してくれるのが、fixjsstyleコマンドになります。
では、実行してみましょう。
$ fixjsstyle sample.js
Fixed 4 errors in /path/to/sample.js