Railsのrubocop.ymlを見てみる

Rails本体にrubocopが導入されているの知ってました?
私はrails commit log流し読み(2016/08/07) - なるようになるブログ
を見て知りました。

railsの.rubocop.ymlはこちら 見てみたところ、ほぼ納得の設定です。個人的にもこれに寄せていきたいと思います。
本当、rubocopいいですね。

どんな設定が入ってるの?

Railsが取り入れたということは、これが世界標準になっていくでしょう。
現時点のルールを細かく見てみます。
ヘルプ見ながら手元で試してみました。

Style/AndOr:

# Prefer &&/|| over and/or.
Style/AndOr:
  Enabled: true

and/or と書きたい時がある派でしたが
結合度の問題をクリアにするためという理解はできます。

Style/BracesAroundHashParameters:

# Do not use braces for hash literals when they are the last argument of a
# method call.
Style/BracesAroundHashParameters:
  Enabled: true

これは賛成。

example_method({ key: "value" }) # warn
example_method(key: :value1) # ok

Style/CaseIndentation:

# Align `when` with `case`.
Style/CaseIndentation:
  Enabled: true

whenをcaseに揃える形式です。
こういう設定が入っています。

IndentWhenRelativeTo: case
SupportedStyles:
- case
- end
IndentOneStep: false

https://github.com/bbatsov/ruby-style-guide#indent-when-to-case
例をみればわかりやすいです。

# bad
case
  when song.name == 'Misty'
    puts 'Not again!'
  when song.duration > 120
    puts 'Too long!'
  when Time.now.hour > 21
    puts "It's too late"
  else
    song.play
end

# good
case
when song.name == 'Misty'
  puts 'Not again!'
when song.duration > 120
  puts 'Too long!'
when Time.now.hour > 21
  puts "It's too late"
else
  song.play
end

Style/EmptyLines:

# No extra empty lines.
Style/EmptyLines:
  Enabled: true

空行は1つ。

Style/EmptyLinesAroundClassBody:

# In a regular class definition, no empty lines around the body.
Style/EmptyLinesAroundClassBody:
  Enabled: true

EnforcedStyle: no_empty_lines が指定されているため、class直後、end直前の空行はngとなります。

class Example
  # ng
  def method
  end
  # ng
end

Style/EmptyLinesAroundModuleBody:

# In a regular module definition, no empty lines around the body.
Style/EmptyLinesAroundModuleBody:
  Enabled: true

上のmodule版。

Style/HashSyntax:

# Use Ruby >= 1.9 syntax for hashes. Prefer { a: :b } over { :a => :b }.
Style/HashSyntax:
  Enabled: true

これは言わずもがな。

Style/IndentationConsistency:

# Method definitions after `private` or `protected` isolated calls need one
# extra level of indentation.
Style/IndentationConsistency:
  Enabled: true
  EnforcedStyle: rails

private / protected 後のメソッドは1つインデント下げる。
個人的には、下げない派でした。

Style/IndentationWidth:

# Two spaces, no tabs (for indentation).
Style/IndentationWidth:
  Enabled: true

これも言うまでもなく。

Style/MethodDefParentheses:

# Defining a method with parameters needs parentheses.
Style/MethodDefParentheses:
  Enabled: true

https://github.com/bbatsov/ruby-style-guide#method-parens

# bad
def some_method()

# good
def some_method

# bad
def some_method_with_parameters param1, param2

# good
def some_method_with_parameters(param1, param2)

Style/SpaceBeforeBlockBraces:

# Use `foo {}` not `foo{}`.
Style/SpaceBeforeBlockBraces:
  Enabled: true
(1..10).each{ |i| puts i } # ng
(1..10).each { |i| puts i } # ok

Style/SpaceInsideBlockBraces:

# Use `foo { bar }` not `foo {bar}`.
Style/SpaceInsideBlockBraces:
  Enabled: true
(1..10).each {|i| puts i } # ng
(1..10).each { |i| puts i} # ng
(1..10).each { |i| puts i } # ok

Style/SpaceInsideHashLiteralBraces:

# Use `{ a: 1 }` not `{a:1}`.
Style/SpaceInsideHashLiteralBraces:
  Enabled: true

Style/StringLiterals:

# Check quotes usage according to lint rule below.
Style/StringLiterals:
  Enabled: true
  EnforcedStyle: double_quotes

これが驚きなのですが、double-quote で統一だそうです。
個人的には使い分ける派だったのですが、
まぁこれはこれで揃ってれば良いかなと思います。

Style/Tab:

# Detect hard tabs, no hard tabs.
Style/Tab:
  Enabled: true

Style/TrailingBlankLines:

# Blank lines should not have any spaces.
Style/TrailingBlankLines:
  Enabled: true

エディタで自動削除する派でしたので、これも賛成。

Style/TrailingWhitespace:

# No trailing whitespace.
Style/TrailingWhitespace:
  Enabled: true

エディタで自動削除する派でしたので、これも賛成。

Style/UnneededPercentQ:

# Use quotes for string literals when they are enough.
Style/UnneededPercentQ:
  Enabled: true

single-quote を伴わない %q記法はng。

Lint/EndAlignment:

# Align `end` with the matching keyword or starting expression except for
# assignments, where it should be aligned with the LHS.
Lint/EndAlignment:
  Enabled: true
  AlignWith: variable

endのalignが開始のものと同一かをチェックする。

Lint/RequireParentheses:

# Use my_method(my_arg) not my_method( my_arg ) or my_method my_arg.
Lint/RequireParentheses:
  Enabled: true