rubocop についてざっくり解説
TRANSCRIPT
![Page 1: Rubocop についてざっくり解説](https://reader031.vdocuments.site/reader031/viewer/2022012406/58a59e811a28abaf3e8b6545/html5/thumbnails/1.jpg)
Rubocop
についてざっくり解説
ota42y
2016/10/26 Shinjuku.rb #42
![Page 2: Rubocop についてざっくり解説](https://reader031.vdocuments.site/reader031/viewer/2022012406/58a59e811a28abaf3e8b6545/html5/thumbnails/2.jpg)
自己紹介
ota42y
ヘルスケア系の会社勤務
rubyとかgoとかC++とか
最近dartが凄いというウワサ
![Page 3: Rubocop についてざっくり解説](https://reader031.vdocuments.site/reader031/viewer/2022012406/58a59e811a28abaf3e8b6545/html5/thumbnails/3.jpg)
![Page 4: Rubocop についてざっくり解説](https://reader031.vdocuments.site/reader031/viewer/2022012406/58a59e811a28abaf3e8b6545/html5/thumbnails/4.jpg)
Rubocophttps://github.com/bbatsov/rubocop
コーディング規約にそっているかをチェック
修正可能なら自動修正(オプション)
規約は設定ファイルで細かく変更可能
ベースはRuby style guide (同作者)
![Page 5: Rubocop についてざっくり解説](https://reader031.vdocuments.site/reader031/viewer/2022012406/58a59e811a28abaf3e8b6545/html5/thumbnails/5.jpg)
How to works (demo)% rubocop Inspecting 67 files.C..CCCCCCCCWCCC.W.C..C.C..C.....C.CCCCCWCCC......C.WWCCC
Offenses:
test/utils_test.rb:9:43: C: Prefer single-quoted strings assert_equal true, Prmd::Utils.blank?(" ") ^^^...
67 files inspected, 552 offenses detected
行と警告内容を出してくれる
![Page 6: Rubocop についてざっくり解説](https://reader031.vdocuments.site/reader031/viewer/2022012406/58a59e811a28abaf3e8b6545/html5/thumbnails/6.jpg)
auto-correct (demo)% rubocop --auto-correct
test/utils_test.rb:4:3: C: Assignment Branch Condition siz def test_blank? ^^^test/utils_test.rb:8:43: C: [Corrected] Prefer single-quot assert_equal true, Prmd::Utils.blank?("")
--auto-correctオプションで自動で直してくれる
[Corrected]がつく
直さない物もある
メソッド長すぎ系や例外握り潰すな系
![Page 7: Rubocop についてざっくり解説](https://reader031.vdocuments.site/reader031/viewer/2022012406/58a59e811a28abaf3e8b6545/html5/thumbnails/7.jpg)
対応しているcon�ghttps://github.com/bbatsov/rubocop/blob/master/con�g/default.yml
多すぎて説明無理
スタイル系からバグ検出まで様々
x == nil より x.nil?
{には空白を入れろ
メソッド多重定義の検出
downcase比較よりcasecmp
![Page 8: Rubocop についてざっくり解説](https://reader031.vdocuments.site/reader031/viewer/2022012406/58a59e811a28abaf3e8b6545/html5/thumbnails/8.jpg)
copの区分けcop (チェックルールをこう呼ぶ)
Style
Lint
bad practicesやエラーになりそうなの抽出
Metrics
一定量超えたらダメ系(行長すぎ、関数巨大すぎ)
Performance
Rails
Rails特有のもの(�nd_by_name,where().�rst)
![Page 9: Rubocop についてざっくり解説](https://reader031.vdocuments.site/reader031/viewer/2022012406/58a59e811a28abaf3e8b6545/html5/thumbnails/9.jpg)
既存プロジェクトへの導入
linterで困るのが既存コードへの適応
既に入ってたので途中からの導入は(゚⊿゚)シラネ
http://blog.onk.ninja/2015/10/27/rubocop-getting-started
おすすめの入れ方っぽい
現状で警告が出るものを全て無効にする
設定を一ずつ有効にして消していく
![Page 10: Rubocop についてざっくり解説](https://reader031.vdocuments.site/reader031/viewer/2022012406/58a59e811a28abaf3e8b6545/html5/thumbnails/10.jpg)
便利
簡単
![Page 11: Rubocop についてざっくり解説](https://reader031.vdocuments.site/reader031/viewer/2022012406/58a59e811a28abaf3e8b6545/html5/thumbnails/11.jpg)
その他
内部ではASTにして頑張って解析しているみたい
parser gemが解析やってる
https://github.com/whitequark/parser
rubyソースコードで遊ぶとき便利そう
Rubocopは主に解析ルールを持ってる
300個以上(´・_・`)!
独自ルールを作るのは凄い大変そう…?
![Page 12: Rubocop についてざっくり解説](https://reader031.vdocuments.site/reader031/viewer/2022012406/58a59e811a28abaf3e8b6545/html5/thumbnails/12.jpg)
コード構成色々ありますが重要なのはほぼこれぐらい
Runner
解析実行するところ
auto-correctでは差分が消えるまで実行
Cop
解析のルールを実装するクラス
xxxxCopクラスで解析をする
on_whileとかon_ifとかをパーサーから受け取る
![Page 13: Rubocop についてざっくり解説](https://reader031.vdocuments.site/reader031/viewer/2022012406/58a59e811a28abaf3e8b6545/html5/thumbnails/13.jpg)
実際どんな感じ
line length チェック
本体
https://goo.gl/E9FqBY
共通のものはincludeされている
https://goo.gl/CrlZcI
https://goo.gl/q9B0BU