RuboCop 0.79.0 リリース解説

RuboCop 0.79.0 がリリースされたので、概要をざっくり記しておきます。

github.com

今回は RoboCop 1.0 に向けた新たな Enable オプションへの新たな値 pending が導入されたのと、Ruby 2.7 の初期サポートが含まれたリリースです。

ちなみにレビュー以外での自分の関わりは、11のバグフィックスうち7つを手がけていました。

New features

#7296: Recognize console and binding.console (rails/web-console) calls in Lint/Debuggers. ([@gsamokovarov][])

Lint/Debuggers cop で web-console での consoleデバッグ用のメソッドとして検知するようにしています。ただ、TTY ライブラリで使われるような名前なので、そのあたりの衝突が懸念されていたところリリースされていたものでした。実際のところは Rails で衝突しているようでリバートされるかもしれません。

github.com

#7567: Introduce new pending status for new cops. ([@Darhazer], [@pirj])

RuboCop 1.0 に向けて、.rubocop.yml の Enable オプションに、新たな値 pending が導入されました。 初期リリースされた新たな cop でままある誤検知への対策で、リリース初期の cop は pending というステータスではじまりデフォルトでは実行されない流れになります。

たしか Enable: false だけだと無効化の意味合いが曖昧ということで新たに追加されたステータスです。

Bug fixes

#7193: Prevent Style/PercentLiteralDelimiters from changing %i literals that contain escaped delimiters. ([@buehmann][])

Style/PercentLiteralDelimiters での問題を直しています。これなぜか自分の環境では再現できかったんですよね。

#7590: Fix an error for Layout/SpaceBeforeBlockBraces when using with EnforcedStyle: line_count_based of Style/BlockDelimiters cop. ([@koic][])

Style/BlockDelimiters cop の EnforcedStyleline_count_based の際にエラーになる問題を直しました。これは Layout/SpaceBeforeBlockBraces cop 中で Style/BlockDelimiters cop の設定を参照している部分のコードに対してテストが足りていないことで、見落としていたバグでした。

#7569: Make Style/YodaCondition accept __FILE__ == $0. ([@koic][])

__FILE__ == $0 はいずれも事実上の読み取り専用ということで、Style/YodaCondition cop で検知しなくても良いだろうと受け入れるようにしました。

#7576: Fix an error for Gemspec/OrderedDependencies when using a local variable in an argument of dependent gem. ([@koic][])

gemspec で以下のように add_dependency をループ処理内に書くなどしたときにエラーになるのを防ぎました。

Gem::Specification.new do |s|
  %w[foo bar].each { |dep| s.add_dependency dep }
  s.add_dependency 'baz'
end

そもそもループ処理などではなく、一律でフラットに書くという cop がレビュー中で提案されています。

#7595: Make Style/NumericPredicate aware of ignored methods when specifying ignored methods. ([@koic][])

Style/NumericPredicate cop で IgnoredMethods オプションが効いていなかった問題を直しています。

#7607: Fix Style/FrozenStringLiteralComment infinite loop when magic comments are newline-separated. ([@pirj][])

# frozen_string_literal: true でループするようなバグがあったようです。自分の環境だと再現がうまくできなかったけれど何だったのだろう。

#7602: Ensure proper handling of Ruby 2.7 syntax. ([@drenmi][])

Ruby 2.7 の初期サポートが入りました。まだバグがあるようで、新たな問題があればフィードバックをください。

github.com

#7620: Fix a false positive for Migration/DepartmentName when a disable comment contains a plain comment. ([@koic][])

# rubocop:disable Style/SafeNavigation # support Ruby < 2.3.0 のように、disable コメントに続いてコメントが書かれているようなケースでの偽陽性を直しました。

#7616: Fix an incorrect autocorrect for Style/MultilineWhenThen for when statement with then is an array or a hash. ([@koic][])

Style/MultilineWhenThen cop で以下のような auto-correct による syntax の破壊を直しました。

% cat example.rb
case condition
when foo then {
    key: 'value'
  }
end

% ruby -c example.rb
Syntax OK

% bundle exec rubocop -a --only Style/MultilineWhenThen
(snip)

% cat example.rb
case condition
when foo {
    key: 'value'
  }
end

% ruby -c example.rb
example.rb:3: syntax error, unexpected ':', expecting '}'
    key: 'value'
example.rb:4: syntax error, unexpected '}', expecting end-of-input

#7628: Fix an incorrect autocorrect for Layout/MultilineBlockLayout removing trailing comma with single argument. ([@pawptart][])

以下のようなケースで auto-correct によりブロック引数のコードが破壊される問題が直されています。

# Before
[[1, 2, 3], [3, 4, 5]].each do |a,
  |
  p [a]
end
# After
[[1, 2, 3], [3, 4, 5]].each do |a|
  p [a]
end

#7627: Fix a false negative for Migration/DepartmentName when there is space around : (e.g. # rubocop : disable). ([@koic][])

disable コメントは、rubocop : disable のように : の前後にスペースを入れても有効だったものが検出できなかった問題を直しました。この振る舞い知らなかった。

Changes

#7287: Style/FrozenStringLiteralComment is now considered unsafe. ([@buehmann][])

fstring のマジックコメントを書くとコードとしての互換性がなくなっている可能性があるため、unsafe にマークされました。


多くのユーザーにとってはバグフィックスがメインになると思うので、アップデートしてみてください。