From 3b7793d4558f457532e6b47b8043e1181905d89d Mon Sep 17 00:00:00 2001 From: Dmytro Savochkin Date: Mon, 24 Aug 2020 16:47:20 +0300 Subject: [PATCH] [Fix #8576] Fix `Style/IfUnlessModifier` to ignore cop disable comment directives when considering conversion to modifier form --- CHANGELOG.md | 1 + lib/rubocop/cop/mixin/statement_modifier.rb | 12 +++++++++--- lib/rubocop/cop/style/if_unless_modifier.rb | 4 ---- spec/rubocop/cli/cli_autocorrect_spec.rb | 21 +++++++++++++++++++++ 4 files changed, 31 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4f11ffba641f..2ce12502fa2d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -43,6 +43,7 @@ * [#8481](https://github.com/rubocop-hq/rubocop/pull/8481): Fix autocorrect for elements with newlines in `Style/SymbolArray` and `Style/WordArray`. ([@biinari][]) * [#8475](https://github.com/rubocop-hq/rubocop/issues/8475): Fix a false positive for `Style/HashAsLastArrayItem` when there are duplicate hashes in the array. ([@wcmonty][]) * [#8497](https://github.com/rubocop-hq/rubocop/issues/8497): Fix `Style/IfUnlessModifier` to add parentheses when converting if-end condition inside a parenthesized method argument list. ([@dsavochkin][]) +* [#8576](https://github.com/rubocop-hq/rubocop/issues/8576): Fix `Style/IfUnlessModifier` to ignore cop disable comment directives when considering conversion to the modifier form. ([@dsavochkin][]) ### Changes diff --git a/lib/rubocop/cop/mixin/statement_modifier.rb b/lib/rubocop/cop/mixin/statement_modifier.rb index 4fe2be136e36..4aae0e9f3554 100644 --- a/lib/rubocop/cop/mixin/statement_modifier.rb +++ b/lib/rubocop/cop/mixin/statement_modifier.rb @@ -57,10 +57,11 @@ def to_modifier_form(node) end def first_line_comment(node) - comment = - processed_source.find_comment { |c| c.loc.line == node.loc.line } + comment = processed_source.find_comment { |c| c.loc.line == node.loc.line } + return unless comment - comment ? comment.loc.expression.source : nil + comment_source = comment.loc.expression.source + comment_source unless comment_disables_cop?(comment_source) end def parenthesize?(node) @@ -80,6 +81,11 @@ def max_line_length config.for_cop('Layout/LineLength')['Max'] end + + def comment_disables_cop?(comment) + regexp_pattern = "# rubocop : (disable|todo) ([^,],)* (all|#{cop_name})" + Regexp.new(regexp_pattern.gsub(' ', '\s*')).match?(comment) + end end end end diff --git a/lib/rubocop/cop/style/if_unless_modifier.rb b/lib/rubocop/cop/style/if_unless_modifier.rb index 53d3a36f3b41..b73663662460 100644 --- a/lib/rubocop/cop/style/if_unless_modifier.rb +++ b/lib/rubocop/cop/style/if_unless_modifier.rb @@ -157,10 +157,6 @@ def to_normal_form(node) #{indentation}end RUBY end - - def first_line_comment(node) - processed_source.comment_at_line(node.loc.line)&.text - end end end end diff --git a/spec/rubocop/cli/cli_autocorrect_spec.rb b/spec/rubocop/cli/cli_autocorrect_spec.rb index 88cca7b65728..15f455db9cb9 100644 --- a/spec/rubocop/cli/cli_autocorrect_spec.rb +++ b/spec/rubocop/cli/cli_autocorrect_spec.rb @@ -1607,4 +1607,25 @@ def self.some_method(foo, bar: 1) RUBY expect(source_file.read).to eq(corrected) end + + it 'does not correct Style/IfUnlessModifier offense disabled by a comment directive and ' \ + 'does not fire Lint/RedundantCopDisableDirective offense even though that directive ' \ + 'would make the modifier form too long' do + create_file('.rubocop.yml', <<~YAML) + Style/FrozenStringLiteralComment: + Enabled: false + YAML + + source_file = Pathname('example.rb') + source = <<~RUBY + if i > 1 # rubocop:disable Style/IfUnlessModifier + raise '_______________________________________________________________________' + end + RUBY + create_file(source_file, source) + + status = cli.run(['--auto-correct-all']) + expect(status).to eq(0) + expect(source_file.read).to eq(source) + end end