高木のブログ

【RuboCop】grepとawkを使って楽に新しいCopを有効にする

· 128 words · 1 minutes to read

問題 🔗

RuboCopを実行したら、以下のような警告が出るようになった

$ bundle exec rubocop
The following cops were added to RuboCop, but are not configured. Please set Enabled to either `true` or `false` in your `.rubocop.yml` file:
 - Lint/BinaryOperatorWithIdenticalOperands (0.89)
 - Lint/DuplicateElsifCondition (0.88)
 - Lint/DuplicateRescueException (0.89)

 ...省略...

 - Style/RedundantFileExtensionInRequire (0.88)
 - Style/SingleArgumentDig (0.89)
 - Style/StringConcatenation (0.89)
For more information: https://docs.rubocop.org/rubocop/versioning.html

RuboCopのバージョン 0.89.1

新しく追加されたCopはデフォルトで有効にならないので、rubocop.ymlに設定を追加しないといけないらしい
これをちまちま設定していくのはたまにならいいけど、Rubocopの更新頻度が早いのでかなりめんどくさい

対策 🔗

grepとawkを駆使して、有効にする設定を出力するワンライナーを作った
あとはコピペするだけなので、だいぶ楽になった

$ bundle exec rubocop lib/ |grep ' - .\+\/.\+ (.\+)' |awk '{print $3 ":"} {print "  Enabled: true"}'

実行結果 🔗

$ bundle exec rubocop |grep ' - .\+\/.\+ (.\+)' |awk '{print $3 ":"} {print "  Enabled: true"}'
Lint/BinaryOperatorWithIdenticalOperands:
  Enabled: true
Lint/DuplicateElsifCondition:
  Enabled: true
Lint/DuplicateRescueException:
  Enabled: true

...省略...

Style/RedundantFileExtensionInRequire:
  Enabled: true
Style/SingleArgumentDig:
  Enabled: true
Style/StringConcatenation:
  Enabled: true

一括で有効にする方法 🔗

公式ドキュメントに新しいCopを一括で有効にする方法が書いてあった
まあそりゃあるよね

AllCops:
  NewCops: enable

もちろんdisableにしたらすべて無効にできる

まとめ 🔗

一次情報として公式ドキュメントを読むべきという話

参考 🔗

Versioning :: RuboCop Docs

Categories


Tags