Skip to content

Commit

Permalink
[Fix rubocop#7682] Fix Style/InverseMethods autofix leaving parenth…
Browse files Browse the repository at this point in the history
…esis

Closes rubocop#7682
  • Loading branch information
tejasbubane committed Feb 22, 2020
1 parent bad4b8a commit ee84650
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -6,6 +6,7 @@

* [#7719](https://github.com/rubocop-hq/rubocop/issues/7719): Fix `Style/NestedParenthesizedCalls` cop for newline. ([@tejasbubane][])
* [#7709](https://github.com/rubocop-hq/rubocop/issues/7709): Fix correction of `Style/RedundantCondition` when the else branch contains a range. ([@rrosenblum][])
* [#7682](https://github.com/rubocop-hq/rubocop/issues/7682): Fix `Style/InverseMethods` autofix leaving parenthesis. ([@tejasbubane][])

## 0.80.0 (2020-02-18)

Expand Down
12 changes: 8 additions & 4 deletions lib/rubocop/cop/style/inverse_methods.rb
Expand Up @@ -109,10 +109,7 @@ def correct_inverse_method(node)
corrector.remove(not_to_receiver(node, method_call))
corrector.replace(method_call.loc.selector,
inverse_methods[method].to_s)

if EQUALITY_METHODS.include?(method)
corrector.remove(end_parentheses(node, method_call))
end
remove_end_parenthesis(corrector, node, method, method_call)
end
end

Expand Down Expand Up @@ -187,6 +184,13 @@ def camel_case_constant?(node)
def dot_range(loc)
range_between(loc.dot.begin_pos, loc.expression.end_pos)
end

def remove_end_parenthesis(corrector, node, method, method_call)
return unless EQUALITY_METHODS.include?(method) ||
method_call.parent.begin_type?

corrector.remove(end_parentheses(node, method_call))
end
end
end
end
Expand Down
6 changes: 6 additions & 0 deletions spec/rubocop/cop/style/inverse_methods_spec.rb
Expand Up @@ -76,6 +76,12 @@ def test_method

expect(new_source).to eq('foo.any? { |f| f.even? }')
end

it 'corrects inverse any? inside parens' do
new_source = autocorrect_source('!(foo.any? &:working?)')

expect(new_source).to eq('foo.none? &:working?')
end
end

shared_examples 'all variable types' do |variable|
Expand Down

0 comments on commit ee84650

Please sign in to comment.