Skip to content

Commit

Permalink
[Fix #1066] Fix auto-correct of parenthesized condition in NegatedIf
Browse files Browse the repository at this point in the history
  • Loading branch information
jonas054 committed May 6, 2014
1 parent 893e9b5 commit 83f0bfe
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
* [#1032](https://github.com/bbatsov/rubocop/issues/1032): Avoid duplicate reporting when code moves around due to `--auto-correct`. ([@jonas054][])
* [#1036](https://github.com/bbatsov/rubocop/issues/1036): Handle strings like `__FILE__` in `LineEndConcatenation`. ([@bbatsov][])
* [#1006](https://github.com/bbatsov/rubocop/issues/1006): Fix LineEndConcatenation to handle chained concatenations. ([@barunio][])
* [#1066](https://github.com/bbatsov/rubocop/issues/1066): Fix auto-correct for `NegatedIf` when the condition has parentheses around it. ([@jonas054][])

## 0.21.0 (24/04/2014)

Expand Down
2 changes: 2 additions & 0 deletions lib/rubocop/cop/style/negated_if.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ def message(node)
def autocorrect(node)
@corrections << lambda do |corrector|
condition, _body, _rest = *node
# look inside parentheses around the condition
condition = condition.children.first while condition.type == :begin
# unwrap the negated portion of the condition (a send node)
pos_condition, _method, = *condition
corrector.replace(
Expand Down
6 changes: 5 additions & 1 deletion spec/rubocop/cop/style/negated_if_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,13 @@
expect(corrected).to eq 'something unless x.even?'
end

it 'autocorrects by replacing parenthesized if not with unless' do
corrected = autocorrect_source(cop, 'something if (!x.even?)')
expect(corrected).to eq 'something unless (x.even?)'
end

it 'autocorrects by replacing unless not with if' do
corrected = autocorrect_source(cop, 'something unless !x.even?')
expect(corrected).to eq 'something if x.even?'
end

end

0 comments on commit 83f0bfe

Please sign in to comment.