CoffeeLintは、JsLintのCoffeeScript版で、構文チェックをしてくれます。
$ npm install -g coffeelint
sample.coffee
class MyClass print:(msg)-> console.log msg
Lintしてみます。
$ coffeelint sample.coffee
✗ sample.coffee
✗ #2: Line contains inconsistent indentation. Expected 2 got 1.
✗ #2: Line contains tab indentation.
✗ #3: Line contains inconsistent indentation. Expected 2 got 1.
✗ #3: Line contains tab indentation.
✗ Lint! » 4 errors and 0 warnings in 1 file
カラーで表示されます。どうもタブを含んでいるらしいです。
タブをスペースに変更してもう一度。
$ coffeelint sample.coffee ✓ sample.coffee ✓ Ok! » 0 errors and 0 warnings in 1 file
グリーンになりました。
設定ファイルをJSONで指定することで、エラーレベルを決められます。
$ coffeelint -f config.json sample.coffee
サンプルの設定はこんな感じになっています。
{
"no_tabs" : {
"level" : "error"
},
"no_trailing_whitespace" : {
"level" : "error"
},
"max_line_length" : {
"value": 80,
"level" : "error"
},
"camel_case_classes" : {
"level" : "error"
},
"indentation" : {
"value" : 2,
"level" : "error"
},
"no_implicit_braces" : {
"level" : "ignore"
},
"no_trailing_semicolons" : {
"level" : "error"
},
"no_plusplus" : {
"level" : "ignore"
},
"no_throwing_strings" : {
"level" : "error"
},
"cyclomatic_complexity" : {
"value" : 11,
"level" : "ignore"
},
"line_endings" : {
"value" : "unix",
"level" : "ignore"
},
"no_implicit_parens" : {
"level" : "ignore"
}
}
コードからも使えるようです。(TypeErrorで未確認)
fs = require 'fs' coffeelint = require 'coffeelint' source = fs.readFile 'sample.coffee' config = fs.readFile 'config.json' errors = coffeelint.lint source, config console.log errors
errorsに以下のJSONが配列で戻ってきます。OKの場合は空。
JSON形式は以下のとおり。
{
rule : 'ルール名',
lineNumber: 'エラーが起こった行番号',
level: 'エラーのレベル',
message: 'エラーメッセージ',
context: 'エラーが起きた原因(オプション)'
}
コード上から使えるのは便利なのですが、僕の場合、TypeErrorでした。今日はこれまで。
$ coffee lint.coffee
TypeError: Cannot call method 'replace' of undefined
at Lexer.tokenize (/Users/inouetomoyuki/.nvm/v0.6.14/lib/node_modules/coffeelint/node_modules/coffee-script/lib/coffee-script/lexer.js:24:19)
at Object.tokens (/Users/inouetomoyuki/.nvm/v0.6.14/lib/node_modules/coffeelint/node_modules/coffee-script/lib/coffee-script/coffee-script.js:58:18)
at new LexicalLinter (/Users/inouetomoyuki/.nvm/v0.6.14/lib/node_modules/coffeelint/lib/coffeelint.js:249:34)
at Object.lint (/Users/inouetomoyuki/.nvm/v0.6.14/lib/node_modules/coffeelint/lib/coffeelint.js:488:21)
at Object.<anonymous> (/Users/inouetomoyuki/Projects/node/coffeelint_sample/lint.coffee:12:23)
at Object.<anonymous> (/Users/inouetomoyuki/Projects/node/coffeelint_sample/lint.coffee:16:4)
at Module._compile (module.js:441:26)
at Object.run (/usr/local/lib/coffee-script/lib/coffee-script/coffee-script.js:73:25)
at /usr/local/lib/coffee-script/lib/coffee-script/command.js:147:29
at /usr/local/lib/coffee-script/lib/coffee-script/command.js:122:18
0 件のコメント:
コメントを投稿