自己紹介
はじめまして、はる(@lemonade_37)と申します。
駆け出しエンジニアとして働き始めて約11ヶ月が経過しました🐣
概要
既存の Rails アプリ内に RuboCop を導入したかったのですが、厳しすぎるルールだと修正量が膨大になってしまい、ちょうど良い設定内容を考えるのが難しかったです。
参考記事を参考に、揃えるべきところはチェックしつつも、少しゆるめなルールで導入しました。その際の備忘録です。
環境
- Mac OS(Apple Silicon)
- Docker
- Ruby 3.2.3
- Rails 7.1.3
- Importmap
- Tailwindcss、daisyUI
- PostgreSQL
ケースバイケースであり、あくまで私を取り巻く環境において考えたものです🙇
より良い考え方がありましたら、ぜひコメントにて教えていただけますと幸いです。
設定内容
.rubocop.yml
inherit_from: .rubocop_todo.yml
require:
- rubocop-rails
- rubocop-capybara
- rubocop-performance
- rubocop-rubycw
AllCops:
DisabledByDefault: true # デフォルトで全てのRuboCopをオフ、必要なものだけオン。今後必要に応じて項目を追加。
DisplayCopNames: true
NewCops: enable
Exclude:
- "vendor/**/*"
- "db/**/*"
- "bin/**/*"
- "lib/**/*"
- "spec/**/*"
- "config/**/*"
- "node_modules/**/*"
Bundler:
Enabled: true
Lint:
Enabled: true
Lint/EmptyClass:
Enabled: false
Performance:
Enabled: true
Rubycw:
Enabled: true
Security:
Enabled: true
# Rails
Rails/I18nLocaleTexts:
Enabled: true
Rails/LexicallyScopedActionFilter:
Enabled: true
Rails/TimeZone:
Enabled: true
# Style
Style/StringLiterals:
Enabled: true
Style/SymbolArray:
Enabled: true
Style/EmptyMethod:
Enabled: true
Style/CommentedKeyword:
Enabled: true
Style/GuardClause:
Enabled: true
# Layout
Layout/EmptyLines:
Enabled: true
Layout/EmptyLinesAroundBlockBody:
Enabled: true
Layout/EmptyLinesAroundMethodBody:
Enabled: true
Layout/EmptyLineBetweenDefs:
Enabled: true
Layout/SpaceBeforeFirstArg:
Enabled: true
Layout/SpaceBeforeSemicolon:
Enabled: true
Layout/SpaceAfterColon:
Enabled: true
Layout/SpaceAfterComma:
Enabled: true
Layout/SpaceInsideHashLiteralBraces:
EnforcedStyle: space
Layout/SpaceInsideParens:
EnforcedStyle: no_space
Layout/TrailingWhitespace:
Enabled: true
Layout/TrailingEmptyLines:
Enabled: true
Layout/IndentationConsistency:
Enabled: true
Layout/IndentationWidth:
Enabled: true
Width: 2
Layout/DefEndAlignment:
Enabled: true
まとめ
自分なりに、既存プロジェクトに合った設定を考えてみました。
様々なプロジェクトを経験する中で、社内でちょうど良い設定を模索していきたいと思います。
参考・引用記事
- .rubocop.ymlシンプルに、ゆるめにやりたいんじゃ
- 【Rails】RuboCopの導入 ~ おすすめルールの設定方法
- 【Rails】RuboCop 設定編(2020年10月版)
- RuboCop :: RuboCop Docs
関連記事