参考にさせていただいたページ
今日の環境
ローカル
リモート
Gemfileの編集
以下の行をdevelopmentグループに追記しました。
group :development do gem 'rubocop', require: false gem "rubocop-performance", require: false gem "rubocop-rails", require: false gem "rubocop-rspec", require: false end
bundle install
bundle installします。
% docker-compose run app bundle install
設定をauto-genしてみる
以下のコマンドを実行してみました。
$ docker-compose run app rubocop --auto-gen-config
上手くいきません...。
[+] Running 1/0 ⠿ Container web_bonsai-db-1 Running 0.0s [+] Running 1/1 ⠿ Container web_bonsai-app-1 Started 0.2s /usr/bin/entrypoint.sh: line 8: exec: rubocop: not found
docker-compose buildしてみる
以下の通り実行しました。
% docker-compose build
もう一度設定をauto-genしてみる
以下の通り実行しました。
% docker-compose run app rubocop --auto-gen-config
今度は上手くいきました。
rails/.rubocop.ymlの編集
RuboCopの機能を拡張する
以下の行を追記しました。
require: - rubocop-performance - rubocop-rails - rubocop-rspec
最新のCopを自動で有効にする
# AllCopsオプションで全てのCopsを対象にできる AllCops: NewCops: enable
ここまでやったrails/.rubocop.ymlの記述内容
以下のようになりました。一行目は生成時に記述されています。
inherit_from: .rubocop_todo.yml require: - rubocop-performance - rubocop-rails - rubocop-rspec # AllCopsオプションで全てのCopsを対象にできる AllCops: NewCops: enable
使い方
通常
以下のコマンドを実行します。
% docker-compose run app rubocop
ファイルを指定して実行したい場合は以下のような感じで実行します。./path/to/file
はご自身の環境に合わせて変更してください。
% docker-compose run app rubocop ./path/to/file
SafeなCopのみ自動修正
% docker-compose run app rubocop -a ./path/to/file
Safe、UnsafeなCopを自動修正
% docker-compose run app rubocop -A ./path/to/file