rubocop&rubocop-railsを導入してみる

rubocop&rubocop-railsを個人練習用リポジトリに導入してみました!

その際の手順について書いておこうと思います!

 

やり方

gemの導入

ローカル&CI上で動かしたいので:development, :testで使えるようにしました。

group :development, :test do

gem 'rubocop'
gem 'rubocop-rails'
end

 

rubocop.yml作成

rubocop.ymlを作成し、以下のように設定を書いていきます!

require:
- rubocop-rails

AllCops:
TargetRubyVersion: 2.6
Exclude:
- app/channels/**/*
- node_modules/**/*
- bin/*
- test/**/*
- spec/**/*
- lib/*
- db/**/*
- lib/tasks/*
- public/*
- tmp/*
- vendor/**/*
- config/spring.rb
- config/application.rb
- config/environments/*
- config/puma.rb


Style/Documentation:
Description: 'Document classes and non-namespace modules.'
Enabled: false

Metrics/LineLength:
Description: 'This cop checks the length of lines in the source code.'
Max: 120

Style/ClassAndModuleChildren:
Descriotion: 'This cop checks the style of children definitions at classes and modules.'
EnforcedStyle: compact

Style/FrozenStringLiteralComment:
Enabled: false

 

 

実行してみる

以下のコマンドでrubocopを実行します。

bundle exec rubocop

 

自分で書いたファイル以外のものに関しても警告が出てくるので、そららはExcludeに追加していきます。

 

 

自動修正する

以下のコマンドを実行すると書き方違反している部分をrubocopが自動修正してくれます。

bundle exec rubocop --auto-correct

 

 

感想その他

rubocop、いろんなオプションがあるようなのでもっと調べてみようと思います!!