Skip to content

Commit

Permalink
[Fix #9160] Fix an incorrect auto-correct for `Style/SoleNestedCondit…
Browse files Browse the repository at this point in the history
…ional`

Fixes #9160.

This PR fixes an incorrect auto-correct for `Style/IfUnlessModifier`
and `Style/SoleNestedConditional` when auto-correction conflicts for
guard condition.
  • Loading branch information
koic authored and bbatsov committed Dec 9, 2020
1 parent 1386f77 commit fe46341
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#9160](https://github.com/rubocop-hq/rubocop/issues/9160): Fix an incorrect auto-correct for `Style/IfUnlessModifier` and `Style/SoleNestedConditional` when auto-correction conflicts for guard condition. ([@koic][])
4 changes: 4 additions & 0 deletions lib/rubocop/cop/style/if_unless_modifier.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ class IfUnlessModifier < Base
MSG_USE_NORMAL =
'Modifier form of `%<keyword>s` makes the line too long.'

def self.autocorrect_incompatible_with
[Style::SoleNestedConditional]
end

def on_if(node)
msg = if single_line_as_modifier?(node) && !named_capture_in_condition?(node)
MSG_USE_MODIFIER
Expand Down
5 changes: 3 additions & 2 deletions lib/rubocop/cop/style/sole_nested_conditional.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,8 @@ def autocorrect(corrector, node, if_branch)
correct_for_guard_condition_style(corrector, node, if_branch, and_operator)
else
correct_for_basic_condition_style(corrector, node, if_branch, and_operator)
correct_for_comment(corrector, node, if_branch)
end

correct_for_comment(corrector, node, if_branch)
end

def correct_for_guard_condition_style(corrector, node, if_branch, and_operator)
Expand All @@ -99,6 +98,8 @@ def correct_for_basic_condition_style(corrector, node, if_branch, and_operator)
end

def correct_for_comment(corrector, node, if_branch)
return if config.for_cop('Style/IfUnlessModifier')['Enabled']

comments = processed_source.comments_before_line(if_branch.source_range.line)
comment_text = comments.map(&:text).join("\n") << "\n"

Expand Down
24 changes: 24 additions & 0 deletions spec/rubocop/cli/cli_autocorrect_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,30 @@ def batch
RUBY
end

it 'corrects `Style/IfUnlessModifier` with `Style/SoleNestedConditional`' do
source = <<~RUBY
def foo
# NOTE: comment
if a? && b?
puts "looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong message" unless c?
end
end
RUBY
create_file('example.rb', source)
expect(cli.run([
'--auto-correct-all',
'--only', 'Style/IfUnlessModifier,Style/SoleNestedConditional'
])).to eq(0)
expect(IO.read('example.rb')).to eq(<<~RUBY)
def foo
# NOTE: comment
if a? && b? && !c?
puts "looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong message"
end
end
RUBY
end

describe 'trailing comma cops' do
let(:source) do
<<~RUBY
Expand Down
39 changes: 31 additions & 8 deletions spec/rubocop/cop/style/sole_nested_conditional_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -198,22 +198,45 @@
RUBY
end

it 'registers an offense and corrects when using nested conditional and branch contains a comment' do
context 'when disabling `Style/IfUnlessModifier`' do
let(:config) do
RuboCop::Config.new('Style/IfUnlessModifier' => { 'Enabled' => false })
end

it 'registers an offense and corrects when using nested conditional and branch contains a comment' do
expect_offense(<<~RUBY)
if foo
# Comment.
if bar
^^ Consider merging nested conditions into outer `if` conditions.
do_something
end
end
RUBY

expect_correction(<<~RUBY)
# Comment.
if foo && bar
do_something
end
RUBY
end
end

it 'registers an offense and corrects when using guard conditional with outer comment' do
expect_offense(<<~RUBY)
# Comment.
if foo
# Comment.
if bar
^^ Consider merging nested conditions into outer `if` conditions.
do_something
end
do_something if bar
^^ Consider merging nested conditions into outer `if` conditions.
end
RUBY

expect_correction(<<~RUBY)
# Comment.
if foo && bar
do_something
end
do_something
end
RUBY
end

Expand Down

0 comments on commit fe46341

Please sign in to comment.