アドテクdivのほったです٩( 'ω' )و
最近はscalaで開発をしています٩( 'ω' )و
(´-`).。oO( もっと楽にコードレビューしたい
(´-`).。oO( 目視でのスタイルのチェックが辛い
と思ったので、静的解析ツールを使ってみようと思います٩( 'ω' )و
Scalastyleを試してみる
ビルドツールはsbtを使っているので、今回はsbtを使ったサンプルです٩( 'ω' )و
※公式サイトに手順が記載されているので、
他のビルドツールを使う場合はそちらを参考にしてください(`・ω・´)ゞ
まずsbt pluginを追加します٩( 'ω' )و
project/plugin.sbt
addSbtPlugin("org.scalastyle" %% "scalastyle-sbt-plugin" % "1.0.0")
次に設定ファイルを用意するためにコマンドを叩きます٩( 'ω' )و
sbt scalastyleGenerateConfig
実行するとカレントディレクトリにxmlファイルが吐き出されると思いますが
これはScalastyleがデフォルトでチェックする設定が書かれています!
早速以下のコードをチェックさせてみます(`・ω・´)ゞ
Hello.scala
package com.example object Hello { def main(args: Array[String]): Unit = { println("Hello, world!") } }
コンソールでコマンドを叩くっ٩( 'ω' )و
sbt scalastyle
実行結果が出力されました‹‹(´ω` )/›› ‹‹( ´)/›› ‹‹( ´ω`)/››
すでにwarningが出てますね(´・ω・`)
デフォルトで設定されている内容は公式ページから確認できるので
今回warningになったものを確認してみます(´-`).。oO
[warn] /Users/hotta_naho/workspace/scala/scalastyle-sample/src/main/scala/com/example/Hello.scala:2: Header does not match expected text [warn] /Users/hotta_naho/workspace/scala/scalastyle-sample/src/main/scala/com/example/Hello.scala:20:4: Regular expression matched 'println'
それぞれ
org.scalastyle.file.HeaderMatchesChecker
と
org.scalastyle.file.RegexChecker
のチェックでひっかっているようです
scalastyle-config.xml
の該当箇所の設定を確認します٩( 'ω' )و
… <check level="warning" class="org.scalastyle.file.HeaderMatchesChecker" enabled="true"> <parameters> <parameter name="header"><![CDATA[// Copyright (C) 2011-2012 the original author or authors. // See the LICENCE.txt file distributed with this work for additional // information regarding copyright ownership. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License.]]></parameter> </parameters> </check> … <check level="warning" class="org.scalastyle.file.RegexChecker" enabled="true"> <parameters> <parameter name="regex"><![CDATA[println]]></parameter> </parameters> </check> …
デフォルトの設定だと
HeaderMatchesCheckerはファイルの先頭にLicenseに関する記述が必要そうです(´-`).。oO
また、RegexCheckerは println
を使うと引っかかるようです
ではチェックしないようにHello.scalaを修正してみます٩( 'ω' )و
Hello.scala
// Copyright (C) 2011-2012 the original author or authors. // See the LICENCE.txt file distributed with this work for additional // information regarding copyright ownership. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package com.example object Hello { def main(args: Array[String]): Unit = { //scalastyle:off println("Hello, world!") } }
HeaderMatchesCheckerは単純なファイルの先頭と、
scalastyle-config.xmlで設定しているテキストの比較なので
一言一句同じである必要があります(正規表現も使えます)
また、一部分だけ全てのチェックをさせないようにしたい場合は、
コードの中に scalastyle:off
を記述することで、
そのファイルの記述した行以降をチェックしないこともできます٩( 'ω' )و
では再度確認してみます‹‹(´ω` )/›› ‹‹( ´)/›› ‹‹( ´ω`)/››
全てのチェックが通りました‹‹(´ω` )/›› ‹‹( ´)/›› ‹‹( ´ω`)/››
やってみて
結構導入が簡単だったのと、コーディングルールをまとめなくてよくなったので
個人的にやってよかったなーと思ってます(灬ºωº灬)
今はデフォルトの設定をチームメンバーと相談して適宜修正していますが
今後も随時メンバーと相談しつつ独自チェックを追加して、
Scalastyleに任せていきたいなーと思います(`・ω・´)ゞ