Skip to content

Commit

Permalink
Merge pull request #9436 from tejasbubane/fix-9429
Browse files Browse the repository at this point in the history
[Fix #9429] Fix `Style/NegatedIfElseCondition` autocorrect to keep comments in correct branch
  • Loading branch information
koic committed Mar 7, 2021
2 parents 538c06d + 63fd2d7 commit 7addc3d
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 2 deletions.
1 change: 1 addition & 0 deletions changelog/fix_comments_negated_if_else_condition_cop.md
@@ -0,0 +1 @@
* [#9429](https://github.com/rubocop-hq/rubocop/issues/9429): Fix `Style/NegatedIfElseCondition` autocorrect to keep comments in correct branch. ([@tejasbubane][])
17 changes: 15 additions & 2 deletions lib/rubocop/cop/style/negated_if_else_condition.rb
Expand Up @@ -29,6 +29,7 @@ module Style
#
class NegatedIfElseCondition < Base
include RangeHelp
include CommentsHelp
extend AutoCorrector

MSG = 'Invert the negated condition and swap the %<type>s branches.'
Expand Down Expand Up @@ -97,10 +98,22 @@ def swap_branches(corrector, node)
if node.if_branch.nil?
corrector.remove(range_by_whole_lines(node.loc.else, include_final_newline: true))
else
corrector.replace(node.if_branch, node.else_branch.source)
corrector.replace(node.else_branch, node.if_branch.source)
if_range = node_with_comments(node.if_branch)
else_range = node_with_comments(node.else_branch)

corrector.replace(if_range, else_range.source)
corrector.replace(else_range, if_range.source)
end
end

def node_with_comments(node)
first_statement = node.begin_type? ? node.children[0] : node
return node if processed_source.ast_with_comments[first_statement].empty?

begin_pos = source_range_with_comment(first_statement).begin_pos
end_pos = node.source_range.end_pos
Parser::Source::Range.new(buffer, begin_pos, end_pos)
end
end
end
end
Expand Down
58 changes: 58 additions & 0 deletions spec/rubocop/cop/style/negated_if_else_condition_spec.rb
Expand Up @@ -129,6 +129,64 @@
RUBY
end

it 'moves comments to correct branches during autocorrect' do
expect_offense(<<~RUBY)
if !condition.nil?
^^^^^^^^^^^^^^^^^^ Invert the negated condition and swap the if-else branches.
# part B
# and foo is 39
foo = 39
else
# part A
# and foo is 42
foo = 42
end
RUBY

expect_correction(<<~RUBY)
if condition.nil?
# part A
# and foo is 42
foo = 42
else
# part B
# and foo is 39
foo = 39
end
RUBY
end

it 'works with comments and multiple statements' do
expect_offense(<<~RUBY)
if !condition.nil?
^^^^^^^^^^^^^^^^^^ Invert the negated condition and swap the if-else branches.
# part A
# and foo is 1 and bar is 2
foo = 1
bar = 2
else
# part B
# and foo is 3 and bar is 4
foo = 3
bar = 4
end
RUBY

expect_correction(<<~RUBY)
if condition.nil?
# part B
# and foo is 3 and bar is 4
foo = 3
bar = 4
else
# part A
# and foo is 1 and bar is 2
foo = 1
bar = 2
end
RUBY
end

it 'does not register an offense when negating condition for `if-elsif`' do
expect_no_offenses(<<~RUBY)
if !x
Expand Down

0 comments on commit 7addc3d

Please sign in to comment.