rubocop-rails-omakaseから始めるrubocop

2025/02/24に公開

Rails7.2からrails newで、rubocop-rails-omakaseが導入できるようになっている。
生成されたrubocopは軽量なrubocopとなっているため、あくまでプロジェクトの出発点として考えるべき。(omakaseのみのゼロコンフィギュレーションは現実的ではない)

rubocop-rails-omakaseは、初期設定がすべてEnabled: falseにしており個別に有効化している。有効化されているCopについては比較的少なく、Layoutルールに対する設定が多く見られる。また、minitestの利用が前提になっている。
https://github.com/rails/rubocop-rails-omakase/blob/main/rubocop.yml#L14-L36

例えば、Lintルールは、全体60に対し下記の3ルールのみの有効化となっている。
https://github.com/rails/rubocop-rails-omakase/blob/main/rubocop.yml#L168-L176

Lint/UselessAssignmentなど、実装時の不具合が検出できるLint設定が無効化されているので、積極的にrubocopの設定をしていくべき。

rspec/factory_botを利用を前提にし、下記のrubocop設定から出発する。

inherit_gem: { rubocop-rails-omakase: rubocop.yml }

require:
  - rubocop-rspec
  - rubocop-factory_bot

AllCops:
  NewCops: enable
 
#################### Bundler ###########################
Bundler:
  Enabled: true
#################### Lint ###########################
Lint:
  Enabled: true
#################### Rails ###########################
Rails:
  Enabled: true
Rails/DynamicFindBy: # generate_token_forによる定義される`find_by_token_for`で利用。
  Enabled: false
Rails/I18nLocaleTexts:
  Enabled: false
Rails/NotNullColumn:
  Enabled: false
Rails/InverseOf:
  Enabled: false
#################### Security ###########################
Security:
  Enabled: true

参考資料

https://github.com/rails/rails/issues/50456

https://koic.hatenablog.com/entry/what-is-rubocop-rails-omakase

https://speakerdeck.com/linkers_tech/how-to-build-your-rubocop-dot-yml-to-avoid-omakase

Discussion