2
1

More than 1 year has passed since last update.

【Rubocop】rubocop -Aを実行する前はexcludeを設定しておく

Last updated at Posted at 2022-09-10

Rubocopの自動修正

Rubocopの自動修正、rubocop -a や rubocop -Aは痛快なコマンドで、間違っているところを一括で全部直してくれる。使った後はなんか清々しい気持ちになる。
ただし、初めて利用する際の注意点もある。
それは、実行前にRubocopをよく設定していなければ、Rubocopの対象とすべきでないファイルにも自動修正をかけてしまうことである。
これをやってしまうとgithubでPRした時にchangedのファイル数がえげつないことになる.

rubocop.yml

rubocop.ymlにあらかじめ対象から除外したいファイルを指定しておくのが良い。
以下は設定の一例である。
AllCops:の下にExcludeとある。この下に除外したいファイルを指定していく。ここで* はワイルドカードなのでbin/**/*と指定しておくことでbin以下のファイルを全て対象外とすることができる。

rubocop.yml
inherit_from:
  - .rubocop_airbnb.yml

Rails:
  Enabled: true

AllCops:
  Exclude:
    - 'bin/**/*'
    - 'db/**/*'
    - 'config/**/*'
    - 'vendor/**/*'
    - 'tmp/**/*'

Layout/LineLength:
  Max: 120

Airbnb/ModuleMethodInWrongFile:
  Description: Define a module method in the file that defines the module, not a file that
    happens to use the module as a namespace.
  Enabled: false
  Include:
    - 'app/**/*'
    - 'lib/**/*'
2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1